waf.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 cloudprovider
  15. import (
  16. "fmt"
  17. "reflect"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/gotypes"
  20. )
  21. type TWafStatementType string
  22. type TWafStatementCondition string
  23. type TWafAction string
  24. type TWafMatchField string
  25. type TWafType string
  26. type TWafOperator string
  27. type TWafTextTransformation string
  28. var (
  29. WafTypeCloudFront = TWafType("CloudFront")
  30. WafTypeRegional = TWafType("Regional")
  31. WafTypeDefault = TWafType("Default")
  32. WafTypeAppGateway = TWafType("AppGateway")
  33. WafTypeSaaS = TWafType("SaaS")
  34. WafTypeLoadbalancer = TWafType("Loadbalancer")
  35. WafStatementTypeByteMatch = TWafStatementType("ByteMatch")
  36. WafStatementTypeGeoMatch = TWafStatementType("GeoMatch")
  37. WafStatementTypeIPSet = TWafStatementType("IPSet")
  38. WafStatementTypeLabelMatch = TWafStatementType("LabelMatch")
  39. WafStatementTypeManagedRuleGroup = TWafStatementType("ManagedRuleGroup")
  40. WafStatementTypeRate = TWafStatementType("Rate")
  41. WafStatementTypeRegexSet = TWafStatementType("RegexSet")
  42. WafStatementTypeRuleGroup = TWafStatementType("RuleGroup")
  43. WafStatementTypeSize = TWafStatementType("Size")
  44. WafStatementTypeSqliMatch = TWafStatementType("SqliMatch")
  45. WafStatementTypeXssMatch = TWafStatementType("XssMatch")
  46. WafStatementConditionAnd = TWafStatementCondition("And")
  47. WafStatementConditionOr = TWafStatementCondition("Or")
  48. WafStatementConditionNot = TWafStatementCondition("Not")
  49. WafStatementConditionNone = TWafStatementCondition("")
  50. WafActionAllow = TWafAction("Allow")
  51. WafActionBlock = TWafAction("Block")
  52. WafActionLog = TWafAction("Log")
  53. WafActionCount = TWafAction("Count")
  54. WafActionAlert = TWafAction("Alert")
  55. WafActionDetection = TWafAction("Detection")
  56. WafActionPrevention = TWafAction("Prevention")
  57. WafActionNone = TWafAction("")
  58. WafMatchFieldBody = TWafMatchField("Body")
  59. WafMatchFieldJsonBody = TWafMatchField("JsonBody")
  60. WafMatchFieldQuery = TWafMatchField("Query")
  61. WafMatchFieldMethod = TWafMatchField("Method")
  62. WafMatchFiledHeader = TWafMatchField("Header")
  63. WafMatchFiledUriPath = TWafMatchField("UriPath")
  64. WafMatchFiledPostArgs = TWafMatchField("PostArgs")
  65. WafMatchFiledCookie = TWafMatchField("Cookie")
  66. // size
  67. WafOperatorEQ = TWafOperator("EQ")
  68. WafOperatorNE = TWafOperator("NE")
  69. WafOperatorLE = TWafOperator("LE")
  70. WafOperatorLT = TWafOperator("LT")
  71. WafOperatorGE = TWafOperator("GE")
  72. WafOperatorGT = TWafOperator("GT")
  73. // string
  74. WafOperatorExactly = TWafOperator("Exactly")
  75. WafOperatorStartsWith = TWafOperator("StartsWith")
  76. WafOperatorEndsWith = TWafOperator("EndsWith")
  77. WafOperatorContains = TWafOperator("Contains")
  78. WafOperatorContainsWord = TWafOperator("ContainsWord")
  79. WafOperatorRegex = TWafOperator("Regex")
  80. WafTextTransformationNone = TWafTextTransformation("")
  81. WafTextTransformationCompressWithSpace = TWafTextTransformation("CompressWithSpace")
  82. WafTextTransformationHtmlEntityDecode = TWafTextTransformation("HtmlEntityDecode")
  83. WafTextTransformationLowercase = TWafTextTransformation("Lowercase")
  84. WafTextTransformationCmdLine = TWafTextTransformation("CmdLine")
  85. WafTextTransformationUrlDecode = TWafTextTransformation("UrlDecode")
  86. // azure
  87. WafTextTransformationTrim = TWafTextTransformation("Trim")
  88. WafTextTransformationUrlEncode = TWafTextTransformation("UrlEncode")
  89. WafTextTransformationRemoveNulls = TWafTextTransformation("RemoveNulls")
  90. )
  91. type TWafMatchFieldValues []string
  92. func (values TWafMatchFieldValues) IsZero() bool {
  93. return len(values) == 0
  94. }
  95. func (values TWafMatchFieldValues) String() string {
  96. return jsonutils.Marshal(values).String()
  97. }
  98. type TextTransformations []TWafTextTransformation
  99. func (transformations TextTransformations) IsZero() bool {
  100. return len(transformations) == 0
  101. }
  102. func (transformations TextTransformations) String() string {
  103. return jsonutils.Marshal(transformations).String()
  104. }
  105. type SExcludeRule struct {
  106. Name string
  107. }
  108. type SExcludeRules []SExcludeRule
  109. func (rules SExcludeRules) IsZero() bool {
  110. return len(rules) == 0
  111. }
  112. func (rules SExcludeRules) String() string {
  113. return jsonutils.Marshal(rules).String()
  114. }
  115. type SWafRule struct {
  116. Name string
  117. Desc string
  118. Action *DefaultAction
  119. Type string
  120. StatementCondition TWafStatementCondition
  121. Expression string
  122. Config jsonutils.JSONObject
  123. Priority int
  124. Enable bool
  125. Statements []SWafStatement
  126. }
  127. // +onecloud:model-api-gen
  128. type SWafStatement struct {
  129. // 管理规则组名称
  130. ManagedRuleGroupName string `width:"64" charset:"utf8" nullable:"false" list:"user"`
  131. // 不包含的规则列表
  132. ExcludeRules *SExcludeRules `width:"200" charset:"utf8" nullable:"false" list:"user"`
  133. // 表达式类别
  134. // enmu: ByteMatch, GeoMatch, IPSet, LabelMatch, ManagedRuleGroup, Rate, RegexSet, RuleGroup, Size, SqliMatch, XssMatch
  135. Type TWafStatementType `width:"20" charset:"ascii" nullable:"false" list:"user"`
  136. // 是否取反操作, 仅对Azure生效
  137. Negation bool `nullable:"false" list:"user"`
  138. // 操作类型
  139. // enum: ["EQ", "NE", "LE", "LT", "GE", "GT"]
  140. Operator TWafOperator `width:"20" charset:"ascii" nullable:"false" list:"user"`
  141. // 匹配字段
  142. // enmu: Body, JsonBody, Query, Method, Header, UriPath, PostArgs, Cookie
  143. MatchField TWafMatchField `width:"20" charset:"utf8" nullable:"false" list:"user"`
  144. // 匹配字段的key
  145. MatchFieldKey string `width:"20" charset:"utf8" nullable:"false" list:"user"`
  146. // 匹配字段的值列表
  147. MatchFieldValues *TWafMatchFieldValues `width:"250" charset:"utf8" nullable:"false" list:"user"`
  148. // 进行转换操作
  149. // enmu: CompressWithSpace, HtmlEntityDecode, Lowercase, CmdLine, UrlDecode, Trim, UrlEncode, RemoveNulls
  150. Transformations *TextTransformations `width:"250" charset:"ascii" nullable:"false" list:"user"`
  151. ForwardedIPHeader string `width:"20" charset:"ascii" nullable:"false" list:"user"`
  152. // 搜索字段, 仅Aws有用
  153. SearchString string `width:"64" charset:"utf8" nullable:"false" list:"user"`
  154. IPSetId string `width:"36" charset:"ascii" nullable:"false" list:"user"`
  155. // 正则表达式Id, 目前只读
  156. RegexSetId string `width:"36" charset:"ascii" nullable:"false" list:"user"`
  157. // 自定义规则组Id, 目前只读
  158. RuleGroupId string `width:"36" charset:"ascii" nullable:"false" list:"user"`
  159. }
  160. func (statement SWafStatement) GetGlobalId() string {
  161. id := fmt.Sprintf("%s-%s-%s-%s-%s",
  162. statement.Type,
  163. statement.MatchField,
  164. statement.MatchFieldKey,
  165. statement.ManagedRuleGroupName,
  166. statement.SearchString,
  167. )
  168. if statement.Type == WafStatementTypeGeoMatch || statement.Type == WafStatementTypeRate || statement.Type == WafStatementTypeLabelMatch {
  169. id = fmt.Sprintf("%s-%s", id, statement.MatchFieldValues)
  170. }
  171. return id
  172. }
  173. func (statement SWafStatement) GetExternalId() string {
  174. return statement.GetGlobalId()
  175. }
  176. // +onecloud:model-api-gen
  177. type DefaultAction struct {
  178. // Allow, Block, Log, Count, Alert, Detection, Prevention
  179. Action TWafAction
  180. // 仅Action为Allow时生效
  181. InsertHeaders map[string]string
  182. // 仅Action为Block时生效
  183. Response string
  184. // 仅Action为Block时生效
  185. ResponseCode *int
  186. // 仅Action为Block时生效
  187. ResponseHeaders map[string]string
  188. }
  189. type WafSourceIps []string
  190. // +onecloud:model-api-gen
  191. type WafRegexPatterns []string
  192. func (patterns WafRegexPatterns) IsZero() bool {
  193. return len(patterns) == 0
  194. }
  195. func (patterns WafRegexPatterns) String() string {
  196. return jsonutils.Marshal(patterns).String()
  197. }
  198. // +onecloud:model-api-gen
  199. type WafAddresses []string
  200. func (addresses WafAddresses) IsZero() bool {
  201. return len(addresses) == 0
  202. }
  203. func (addresses WafAddresses) String() string {
  204. return jsonutils.Marshal(addresses).String()
  205. }
  206. func (action DefaultAction) IsZero() bool {
  207. return false
  208. }
  209. func (action DefaultAction) String() string {
  210. return jsonutils.Marshal(action).String()
  211. }
  212. type SCloudResource struct {
  213. // 资源Id
  214. Id string
  215. // 资源名称
  216. Name string
  217. // 资源类型
  218. Type string
  219. // 资源映射端口
  220. Port int
  221. // 是否可以解除关联
  222. CanDissociate bool
  223. }
  224. type SCloudResources struct {
  225. Data []SCloudResource `json:"data,allowempty"`
  226. Total int
  227. }
  228. type WafCreateOptions struct {
  229. Name string
  230. Desc string
  231. CloudResources []SCloudResource
  232. SourceIps WafSourceIps
  233. Type TWafType
  234. DefaultAction *DefaultAction
  235. }
  236. func init() {
  237. gotypes.RegisterSerializable(reflect.TypeOf(&DefaultAction{}), func() gotypes.ISerializable {
  238. return &DefaultAction{}
  239. })
  240. gotypes.RegisterSerializable(reflect.TypeOf(&WafAddresses{}), func() gotypes.ISerializable {
  241. return &WafAddresses{}
  242. })
  243. gotypes.RegisterSerializable(reflect.TypeOf(&TextTransformations{}), func() gotypes.ISerializable {
  244. return &TextTransformations{}
  245. })
  246. gotypes.RegisterSerializable(reflect.TypeOf(&TWafMatchFieldValues{}), func() gotypes.ISerializable {
  247. return &TWafMatchFieldValues{}
  248. })
  249. gotypes.RegisterSerializable(reflect.TypeOf(&SExcludeRules{}), func() gotypes.ISerializable {
  250. return &SExcludeRules{}
  251. })
  252. gotypes.RegisterSerializable(reflect.TypeOf(&WafRegexPatterns{}), func() gotypes.ISerializable {
  253. return &WafRegexPatterns{}
  254. })
  255. }