syslog_webservices.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package handler
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "strconv"
  20. "time"
  21. "github.com/google/uuid"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/pkg/util/timeutils"
  24. "yunion.io/x/onecloud/pkg/apigateway/options"
  25. "yunion.io/x/onecloud/pkg/appsrv"
  26. "yunion.io/x/onecloud/pkg/httperrors"
  27. "yunion.io/x/onecloud/pkg/mcclient/auth"
  28. modules "yunion.io/x/onecloud/pkg/mcclient/modules/logger"
  29. )
  30. type SActionLog struct {
  31. User string
  32. Severity string
  33. Service string
  34. Ip string
  35. Notes string
  36. Kind string
  37. OpsTime time.Time
  38. ObjType string
  39. Id uint64
  40. Success *bool
  41. Action string
  42. }
  43. func (a SActionLog) toMsg() Message {
  44. result := "fail"
  45. if a.Success != nil && *a.Success {
  46. result = "success"
  47. }
  48. level := 0
  49. switch a.Severity {
  50. case "EMERGENCY":
  51. level = 0
  52. case "ALERT":
  53. level = 1
  54. case "CRITICAL":
  55. level = 2
  56. case "ERROR":
  57. level = 3
  58. case "WARNING":
  59. level = 4
  60. case "NOTICE":
  61. level = 5
  62. case "INFO":
  63. level = 6
  64. case "DEBUG":
  65. level = 7
  66. }
  67. kind := 0
  68. switch a.Kind {
  69. case "NORMAL":
  70. kind = 0
  71. case "ABNORMAL":
  72. kind = 1
  73. case "ILLEGAL":
  74. kind = 2
  75. }
  76. return Message{
  77. RiskLevel: level,
  78. SendIP: a.Ip,
  79. ManufacturerCode: "0003",
  80. EventId: fmt.Sprintf("%d", a.Id),
  81. Username: a.User,
  82. ModuleType: a.Service,
  83. EventDate: timeutils.MysqlTime(a.OpsTime.Add(8 * time.Hour)),
  84. EventType: a.Action,
  85. EventResult: result,
  86. EventDesc: a.Notes,
  87. BehaviorType: kind,
  88. }
  89. }
  90. type Message struct {
  91. RiskLevel int `json:"riskLevel"`
  92. SendIP string `json:"sendIP"`
  93. ManufacturerCode string `json:"manufacturerCode"`
  94. EventId string `json:"eventId"`
  95. Username string `json:"username"`
  96. ModuleType string `json:"moduleType"`
  97. EventDate string `json:"eventDate"`
  98. EventType string `json:"eventType"`
  99. EventResult string `json:"eventResult"`
  100. EventDesc string `json:"eventDesc"`
  101. BehaviorType int `json:"behaviorType"`
  102. }
  103. type msgResponse struct {
  104. Code int `json:"code"`
  105. Message string `json:"message"`
  106. Data []Message `json:"data"`
  107. Date string `json:"date"`
  108. Count int `json:"count"`
  109. }
  110. func handleSyslogWebServiceMessage(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  111. if !options.Options.EnableSyslogWebservice {
  112. httperrors.ForbiddenError(ctx, w, "syslog webservice not enabled")
  113. return
  114. }
  115. resp := fetchSyslogMessage(ctx, r)
  116. appsrv.SendJSON(w, resp)
  117. }
  118. func fetchSyslogMessage(ctx context.Context, r *http.Request) jsonutils.JSONObject {
  119. token := r.FormValue("token")
  120. date := r.FormValue("date")
  121. eventId := r.FormValue("eventId")
  122. recordSize := r.FormValue("recordSize")
  123. // recordStart := r.FormValue("recordStart")
  124. moduleType := r.FormValue("moduleType")
  125. ret := msgResponse{}
  126. ntoken := genToken(options.Options.SyslogWebserviceUsername, options.Options.SyslogWebservicePassword)
  127. if ntoken != token {
  128. ret.Code = 2
  129. ret.Message = "token无效"
  130. return jsonutils.Marshal(ret)
  131. }
  132. params := jsonutils.NewDict()
  133. params.Add(jsonutils.NewString("desc"), "order")
  134. params.Add(jsonutils.NewString("DESC"), "paging_order")
  135. if len(date) > 0 {
  136. params.Add(jsonutils.NewString(date), "since")
  137. }
  138. if len(eventId) > 0 {
  139. params.Add(jsonutils.NewString(eventId), "paging_marker")
  140. }
  141. limit, _ := strconv.ParseInt(recordSize, 10, 64)
  142. if limit > 0 {
  143. params.Add(jsonutils.NewInt(limit), "limit")
  144. }
  145. if len(moduleType) > 0 {
  146. params.Add(jsonutils.NewString(moduleType), "service")
  147. }
  148. sess := auth.GetAdminSession(ctx, "")
  149. logs, err := modules.Actions.List(sess, params)
  150. if err != nil {
  151. ret.Code = 2
  152. ret.Message = fmt.Sprintf("list fail %s", err)
  153. return jsonutils.Marshal(ret)
  154. }
  155. for i := range logs.Data {
  156. action := SActionLog{}
  157. err := logs.Data[i].Unmarshal(&action)
  158. if err != nil {
  159. continue
  160. }
  161. msg := action.toMsg()
  162. ret.Data = append(ret.Data, msg)
  163. ret.Date = msg.EventDate
  164. }
  165. ret.Code = 1
  166. ret.Count = len(ret.Data)
  167. ret.Message = "成功"
  168. return jsonutils.Marshal(ret)
  169. }
  170. type authResponse struct {
  171. Code string `json:"code"`
  172. Message string `json:"message"`
  173. Token string `json:"token"`
  174. }
  175. func genToken(uname string, passwd string) string {
  176. return uuid.NewSHA1(uuid.NameSpaceOID, []byte(uname+":"+passwd)).String()
  177. }
  178. func handleSyslogWebServiceToken(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  179. if !options.Options.EnableSyslogWebservice {
  180. httperrors.ForbiddenError(ctx, w, "syslog webservice not enabled")
  181. return
  182. }
  183. resp := fetchSyslogToken(r)
  184. appsrv.SendJSON(w, resp)
  185. }
  186. func fetchSyslogToken(r *http.Request) jsonutils.JSONObject {
  187. uname := r.FormValue("username")
  188. passwd := r.FormValue("password")
  189. ret := authResponse{}
  190. if uname == options.Options.SyslogWebserviceUsername && passwd == options.Options.SyslogWebservicePassword {
  191. // succ
  192. token := genToken(uname, passwd)
  193. ret.Code = "1"
  194. ret.Message = "成功"
  195. ret.Token = token
  196. } else {
  197. // fail
  198. ret.Code = "2"
  199. ret.Message = "不匹配的username/password"
  200. }
  201. return jsonutils.Marshal(ret)
  202. }