waf.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 azure
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. "yunion.io/x/cloudmux/pkg/multicloud"
  25. )
  26. type SMatchvariable struct {
  27. Variablename string `json:"variableName"`
  28. Selector string `json:"selector"`
  29. }
  30. type SMatchcondition struct {
  31. Matchvariables []SMatchvariable `json:"matchVariables"`
  32. Operator string `json:"operator"`
  33. Negationconditon bool `json:"negationConditon"`
  34. Matchvalues []string `json:"matchValues"`
  35. Transforms []string `json:"transforms"`
  36. }
  37. type CustomRule struct {
  38. waf *SAppGatewayWaf
  39. Name string `json:"name"`
  40. Priority int `json:"priority"`
  41. Ruletype string `json:"ruleType"`
  42. //RateLimitThreshold *int `json:"rateLimitThreshold"`
  43. Matchconditions []SMatchcondition `json:"matchConditions"`
  44. Action string `json:"action"`
  45. }
  46. func (self *CustomRule) GetName() string {
  47. return self.Name
  48. }
  49. func (self *CustomRule) GetType() string {
  50. return api.WAF_RULE_TYPE_CUSTOM
  51. }
  52. func (self *CustomRule) GetGlobalId() string {
  53. return fmt.Sprintf("%s-%s", self.waf.GetGlobalId(), self.GetName())
  54. }
  55. func (self *CustomRule) GetDesc() string {
  56. return ""
  57. }
  58. func (self *CustomRule) GetPriority() int {
  59. return self.Priority
  60. }
  61. func (self *CustomRule) Delete() error {
  62. rules := []CustomRule{}
  63. for _, rule := range self.waf.Properties.Customrules {
  64. if rule.Name != self.Name {
  65. rules = append(rules, rule)
  66. }
  67. }
  68. self.waf.Properties.Customrules = rules
  69. return self.waf.region.update(jsonutils.Marshal(self.waf), nil)
  70. }
  71. func wafMatchFieldAndKeyLocal2Cloud(opts cloudprovider.SWafStatement) ([]SMatchvariable, error) {
  72. ret := []SMatchvariable{}
  73. switch opts.MatchField {
  74. case cloudprovider.WafMatchFieldQuery:
  75. ret = append(ret, SMatchvariable{
  76. Variablename: "QueryString",
  77. })
  78. case cloudprovider.WafMatchFieldMethod:
  79. ret = append(ret, SMatchvariable{
  80. Variablename: "RequestMethod",
  81. })
  82. case cloudprovider.WafMatchFiledUriPath:
  83. ret = append(ret, SMatchvariable{
  84. Variablename: "RequestUri",
  85. })
  86. case cloudprovider.WafMatchFiledHeader:
  87. ret = append(ret, SMatchvariable{
  88. Variablename: "RequestHeaders",
  89. Selector: opts.MatchFieldKey,
  90. })
  91. case cloudprovider.WafMatchFiledPostArgs:
  92. ret = append(ret, SMatchvariable{
  93. Variablename: "PostArgs",
  94. Selector: opts.MatchFieldKey,
  95. })
  96. case cloudprovider.WafMatchFieldBody:
  97. ret = append(ret, SMatchvariable{
  98. Variablename: "RequestBody",
  99. })
  100. case cloudprovider.WafMatchFiledCookie:
  101. ret = append(ret, SMatchvariable{
  102. Variablename: "RequestCookies",
  103. Selector: opts.MatchFieldKey,
  104. })
  105. default:
  106. return ret, fmt.Errorf("unsupported match filed %s", opts.MatchField)
  107. }
  108. return ret, nil
  109. }
  110. func wafMatchFieldAndKeyCloud2Local(v SMatchvariable) (cloudprovider.TWafMatchField, string, error) {
  111. switch v.Variablename {
  112. case "QueryString":
  113. return cloudprovider.WafMatchFieldQuery, v.Selector, nil
  114. case "RequestMethod":
  115. return cloudprovider.WafMatchFieldMethod, "", nil
  116. case "RequestUri":
  117. return cloudprovider.WafMatchFiledUriPath, "", nil
  118. case "RequestHeaders":
  119. return cloudprovider.WafMatchFiledHeader, v.Selector, nil
  120. case "PostArgs":
  121. return cloudprovider.WafMatchFiledPostArgs, v.Selector, nil
  122. case "RequestBody":
  123. return cloudprovider.WafMatchFieldBody, "", nil
  124. case "RequestCookies":
  125. return cloudprovider.WafMatchFiledCookie, v.Selector, nil
  126. case "RemoteAddr":
  127. return cloudprovider.WafMatchFiledHeader, v.Selector, nil
  128. default:
  129. return "", "", fmt.Errorf("invalid variablename %s", v.Variablename)
  130. }
  131. }
  132. func wafStatementLocal2Cloud(opts cloudprovider.SWafStatement) (SMatchcondition, error) {
  133. ret := SMatchcondition{}
  134. if opts.Transformations != nil {
  135. for _, tran := range *opts.Transformations {
  136. ret.Transforms = append(ret.Transforms, string(tran))
  137. }
  138. }
  139. if opts.MatchFieldValues != nil {
  140. ret.Matchvalues = *opts.MatchFieldValues
  141. }
  142. ret.Negationconditon = opts.Negation
  143. ret.Operator = string(opts.Operator)
  144. var err error
  145. switch opts.Type {
  146. case cloudprovider.WafStatementTypeIPSet:
  147. ret.Operator = "IPMatch"
  148. ret.Matchvariables = []SMatchvariable{
  149. SMatchvariable{
  150. Variablename: "RemoteAddr",
  151. },
  152. }
  153. case cloudprovider.WafStatementTypeGeoMatch:
  154. ret.Operator = "GeoMatch"
  155. if len(opts.ForwardedIPHeader) == 0 {
  156. ret.Matchvariables = []SMatchvariable{
  157. SMatchvariable{
  158. Variablename: "RemoteAddr",
  159. },
  160. }
  161. } else {
  162. ret.Matchvariables = []SMatchvariable{
  163. SMatchvariable{
  164. Variablename: "RequestHeaders",
  165. Selector: opts.ForwardedIPHeader,
  166. },
  167. }
  168. }
  169. case cloudprovider.WafStatementTypeSize:
  170. switch opts.Operator {
  171. case "LT":
  172. ret.Operator = "LessThan"
  173. case "LE":
  174. ret.Operator = "LessThanOrEqual"
  175. case "GT":
  176. ret.Operator = "GreaterThan"
  177. default:
  178. return ret, fmt.Errorf("invalid operator %s for %s", opts.Operator, opts.Type)
  179. }
  180. ret.Matchvariables, err = wafMatchFieldAndKeyLocal2Cloud(opts)
  181. if err != nil {
  182. return ret, errors.Wrapf(err, "wafMatchFieldAndKeyLocal2Cloud")
  183. }
  184. case cloudprovider.WafStatementTypeByteMatch:
  185. switch opts.Operator {
  186. case "Contains", "EndsWith", "Regex":
  187. case "StartsWith":
  188. ret.Operator = "BeginsWith"
  189. case "Exactly":
  190. ret.Operator = "Equal"
  191. default:
  192. return ret, fmt.Errorf("invalid operator %s for %s", opts.Operator, opts.Type)
  193. }
  194. ret.Matchvariables, err = wafMatchFieldAndKeyLocal2Cloud(opts)
  195. if err != nil {
  196. return ret, errors.Wrapf(err, "wafMatchFieldAndKeyLocal2Cloud")
  197. }
  198. }
  199. return ret, nil
  200. }
  201. func wafRuleLocal2Cloud(opts *cloudprovider.SWafRule) (*CustomRule, error) {
  202. ret := &CustomRule{}
  203. ret.Name = opts.Name
  204. ret.Priority = opts.Priority
  205. ret.Ruletype = "MatchRule"
  206. ret.Matchconditions = []SMatchcondition{}
  207. for _, s := range opts.Statements {
  208. cds, err := wafStatementLocal2Cloud(s)
  209. if err != nil {
  210. return nil, errors.Wrapf(err, "wafStatementLocal2Cloud")
  211. }
  212. ret.Matchconditions = append(ret.Matchconditions, cds)
  213. }
  214. ret.Action = "Block"
  215. if opts.Action != nil {
  216. ret.Action = string(opts.Action.Action)
  217. }
  218. return ret, nil
  219. }
  220. func (self *CustomRule) Update(opts *cloudprovider.SWafRule) error {
  221. rules := []CustomRule{}
  222. for _, rule := range self.waf.Properties.Customrules {
  223. if rule.Name != self.Name {
  224. rules = append(rules, rule)
  225. } else {
  226. rule, err := wafRuleLocal2Cloud(opts)
  227. if err != nil {
  228. return errors.Wrapf(err, "wafRuleLocal2Cloud")
  229. }
  230. rules = append(rules, *rule)
  231. }
  232. }
  233. self.waf.Properties.Customrules = rules
  234. return self.waf.region.update(jsonutils.Marshal(self.waf), nil)
  235. }
  236. func (self *CustomRule) GetAction() *cloudprovider.DefaultAction {
  237. return &cloudprovider.DefaultAction{
  238. Action: cloudprovider.TWafAction(self.Action),
  239. }
  240. }
  241. func (self *CustomRule) GetStatementCondition() cloudprovider.TWafStatementCondition {
  242. return cloudprovider.WafStatementConditionAnd
  243. }
  244. func (self *CustomRule) GetExpression() string {
  245. return ""
  246. }
  247. func (self *CustomRule) GetEnabled() bool {
  248. return true
  249. }
  250. func (self *CustomRule) Enable() error {
  251. return cloudprovider.ErrNotImplemented
  252. }
  253. func (self *CustomRule) Disable() error {
  254. return cloudprovider.ErrNotImplemented
  255. }
  256. func (self *CustomRule) GetConfig() (jsonutils.JSONObject, error) {
  257. return jsonutils.NewDict(), nil
  258. }
  259. func (self *CustomRule) GetStatements() ([]cloudprovider.SWafStatement, error) {
  260. ret := []cloudprovider.SWafStatement{}
  261. for _, condition := range self.Matchconditions {
  262. trans := cloudprovider.TextTransformations{}
  263. for _, tran := range condition.Transforms {
  264. trans = append(trans, cloudprovider.TWafTextTransformation(tran))
  265. }
  266. values := cloudprovider.TWafMatchFieldValues(condition.Matchvalues)
  267. statement := cloudprovider.SWafStatement{
  268. Negation: condition.Negationconditon,
  269. Transformations: &trans,
  270. MatchFieldValues: &values,
  271. }
  272. switch condition.Operator {
  273. case "IPMatch":
  274. statement.Type = cloudprovider.WafStatementTypeIPSet
  275. case "GeoMatch":
  276. statement.Type = cloudprovider.WafStatementTypeGeoMatch
  277. case "LessThan":
  278. statement.Type = cloudprovider.WafStatementTypeSize
  279. statement.Operator = cloudprovider.WafOperatorLT
  280. case "LessThanOrEqual":
  281. statement.Type = cloudprovider.WafStatementTypeSize
  282. statement.Operator = cloudprovider.WafOperatorLE
  283. case "GreaterThan":
  284. statement.Type = cloudprovider.WafStatementTypeSize
  285. statement.Operator = cloudprovider.WafOperatorGT
  286. case "BeginsWith":
  287. statement.Type = cloudprovider.WafStatementTypeByteMatch
  288. statement.Operator = cloudprovider.WafOperatorStartsWith
  289. case "Contains", "EndsWith", "Regex":
  290. statement.Type = cloudprovider.WafStatementTypeByteMatch
  291. statement.Operator = cloudprovider.TWafOperator(condition.Operator)
  292. case "Equal":
  293. statement.Type = cloudprovider.WafStatementTypeByteMatch
  294. statement.Operator = cloudprovider.WafOperatorExactly
  295. default:
  296. statement.Type = cloudprovider.WafStatementTypeByteMatch
  297. }
  298. var err error
  299. for _, v := range condition.Matchvariables {
  300. statement.MatchField, statement.MatchFieldKey, err = wafMatchFieldAndKeyCloud2Local(v)
  301. if err != nil {
  302. log.Errorf("wafMatchFieldAndKeyCloud2Local %s error: %v", v, err)
  303. continue
  304. }
  305. ret = append(ret, statement)
  306. }
  307. }
  308. return ret, nil
  309. }
  310. type ManagedRule struct {
  311. Rulesettype string `json:"ruleSetType"`
  312. Rulesetversion string `json:"ruleSetVersion"`
  313. }
  314. type ManagedRules struct {
  315. waf *SAppGatewayWaf
  316. Managedrulesets []ManagedRule `json:"managedRuleSets"`
  317. }
  318. func (self *ManagedRules) GetType() string {
  319. return api.WAF_RULE_TYPE_TEMPLATE
  320. }
  321. func (self *ManagedRules) GetName() string {
  322. return fmt.Sprintf("%s Managed rules", self.waf.GetName())
  323. }
  324. func (self *ManagedRules) GetGlobalId() string {
  325. return self.waf.GetGlobalId()
  326. }
  327. func (self *ManagedRules) GetDesc() string {
  328. return ""
  329. }
  330. func (self *ManagedRules) GetPriority() int {
  331. return 0
  332. }
  333. func (self *ManagedRules) GetAction() *cloudprovider.DefaultAction {
  334. return nil
  335. }
  336. func (self *ManagedRules) Delete() error {
  337. return cloudprovider.ErrNotSupported
  338. }
  339. func (self *ManagedRules) Update(opts *cloudprovider.SWafRule) error {
  340. rules := []ManagedRule{}
  341. for _, s := range opts.Statements {
  342. if len(s.ManagedRuleGroupName) == 0 {
  343. return fmt.Errorf("missing managed rule group name")
  344. }
  345. names := strings.Split(s.ManagedRuleGroupName, "_")
  346. if len(names) != 2 {
  347. return fmt.Errorf("invalid managed rule group name %s", s.ManagedRuleGroupName)
  348. }
  349. rules = append(rules, ManagedRule{
  350. Rulesettype: names[0],
  351. Rulesetversion: names[1],
  352. })
  353. }
  354. if len(rules) == 0 {
  355. return fmt.Errorf("missing statements")
  356. }
  357. self.waf.Properties.Managedrules = ManagedRules{
  358. Managedrulesets: rules,
  359. }
  360. return self.waf.region.update(jsonutils.Marshal(self.waf), nil)
  361. }
  362. func (self *ManagedRules) GetStatementCondition() cloudprovider.TWafStatementCondition {
  363. return cloudprovider.WafStatementConditionAnd
  364. }
  365. func (self *ManagedRules) GetExpression() string {
  366. return ""
  367. }
  368. func (self *ManagedRules) GetEnabled() bool {
  369. return true
  370. }
  371. func (self *ManagedRules) Enable() error {
  372. return cloudprovider.ErrNotImplemented
  373. }
  374. func (self *ManagedRules) Disable() error {
  375. return cloudprovider.ErrNotImplemented
  376. }
  377. func (self *ManagedRules) GetConfig() (jsonutils.JSONObject, error) {
  378. return jsonutils.NewDict(), nil
  379. }
  380. func (self *ManagedRules) GetStatements() ([]cloudprovider.SWafStatement, error) {
  381. ret := []cloudprovider.SWafStatement{}
  382. for i := range self.Managedrulesets {
  383. ruleGroupName := fmt.Sprintf("%s_%s", self.Managedrulesets[i].Rulesettype, self.Managedrulesets[i].Rulesetversion)
  384. ret = append(ret, cloudprovider.SWafStatement{
  385. ManagedRuleGroupName: ruleGroupName,
  386. Type: cloudprovider.WafStatementTypeManagedRuleGroup,
  387. RuleGroupId: ruleGroupName,
  388. })
  389. }
  390. return ret, nil
  391. }
  392. type SAppGatewayWaf struct {
  393. multicloud.SResourceBase
  394. AzureTags
  395. region *SRegion
  396. Name string `json:"name"`
  397. ID string `json:"id"`
  398. Type string `json:"type"`
  399. Location string `json:"location"`
  400. Properties struct {
  401. ApplicationGateways []SApplicationGateway
  402. HttpListeners []struct {
  403. Id string
  404. }
  405. PathBasedRules []struct {
  406. Id string
  407. }
  408. Resourcestate string `json:"resourceState"`
  409. Provisioningstate string `json:"provisioningState"`
  410. Policysettings struct {
  411. State string `json:"state"`
  412. Mode string `json:"mode"`
  413. Maxrequestbodysizeinkb int `json:"maxRequestBodySizeInKb"`
  414. Fileuploadlimitinmb int `json:"fileUploadLimitInMb"`
  415. Requestbodycheck bool `json:"requestBodyCheck"`
  416. } `json:"policySettings"`
  417. Customrules []CustomRule `json:"customRules"`
  418. Managedrules ManagedRules `json:"managedRules"`
  419. } `json:"properties"`
  420. }
  421. func (self *SAppGatewayWaf) GetEnabled() bool {
  422. return self.Properties.Policysettings.State == "Enabled"
  423. }
  424. func (self *SAppGatewayWaf) GetName() string {
  425. return self.Name
  426. }
  427. func (self *SAppGatewayWaf) GetId() string {
  428. return self.ID
  429. }
  430. func (self *SAppGatewayWaf) GetGlobalId() string {
  431. return strings.ToLower(self.ID)
  432. }
  433. func (self *SAppGatewayWaf) Delete() error {
  434. return self.region.del(self.ID)
  435. }
  436. func (self *SAppGatewayWaf) GetWafType() cloudprovider.TWafType {
  437. return cloudprovider.WafTypeAppGateway
  438. }
  439. func (self *SAppGatewayWaf) GetIsAccessProduct() bool {
  440. return true
  441. }
  442. func (self *SAppGatewayWaf) GetAccessHeaders() []string {
  443. return []string{}
  444. }
  445. func (self *SAppGatewayWaf) GetHttpPorts() []int {
  446. return []int{}
  447. }
  448. func (self *SAppGatewayWaf) GetHttpsPorts() []int {
  449. return []int{}
  450. }
  451. func (self *SAppGatewayWaf) GetCname() string {
  452. return ""
  453. }
  454. func (self *SAppGatewayWaf) GetUpstreamScheme() string {
  455. return ""
  456. }
  457. func (self *SAppGatewayWaf) GetCertId() string {
  458. return ""
  459. }
  460. func (self *SAppGatewayWaf) GetCertName() string {
  461. return ""
  462. }
  463. func (self *SAppGatewayWaf) GetUpstreamPort() int {
  464. return 0
  465. }
  466. func (self *SAppGatewayWaf) GetSourceIps() []string {
  467. return []string{}
  468. }
  469. func (self *SAppGatewayWaf) GetCcList() []string {
  470. return []string{}
  471. }
  472. func (self *SAppGatewayWaf) AddRule(opts *cloudprovider.SWafRule) (cloudprovider.ICloudWafRule, error) {
  473. rule, err := wafRuleLocal2Cloud(opts)
  474. if err != nil {
  475. return nil, errors.Wrapf(err, "wafRuleLocal2Cloud")
  476. }
  477. rule.waf = self
  478. self.Properties.Customrules = append(self.Properties.Customrules, *rule)
  479. err = self.region.update(jsonutils.Marshal(self), nil)
  480. if err != nil {
  481. return nil, errors.Wrapf(err, "update")
  482. }
  483. return rule, nil
  484. }
  485. func (self *SAppGatewayWaf) GetStatus() string {
  486. switch self.Properties.Provisioningstate {
  487. case "Deleting":
  488. return api.WAF_STATUS_DELETING
  489. case "Failed":
  490. return api.WAF_STATUS_CREATE_FAILED
  491. case "Succeeded":
  492. return api.WAF_STATUS_AVAILABLE
  493. case "Updating":
  494. return api.WAF_STATUS_UPDATING
  495. default:
  496. return self.Properties.Provisioningstate
  497. }
  498. }
  499. func (self *SAppGatewayWaf) GetRules() ([]cloudprovider.ICloudWafRule, error) {
  500. ret := []cloudprovider.ICloudWafRule{}
  501. for i := range self.Properties.Customrules {
  502. self.Properties.Customrules[i].waf = self
  503. ret = append(ret, &self.Properties.Customrules[i])
  504. }
  505. self.Properties.Managedrules.waf = self
  506. ret = append(ret, &self.Properties.Managedrules)
  507. return ret, nil
  508. }
  509. func (self *SAppGatewayWaf) Refresh() error {
  510. waf, err := self.region.GetAppGatewayWaf(self.ID)
  511. if err != nil {
  512. return errors.Wrapf(err, "GetAppGatewayWa")
  513. }
  514. return jsonutils.Update(self, waf)
  515. }
  516. func (self *SAppGatewayWaf) GetDefaultAction() *cloudprovider.DefaultAction {
  517. return &cloudprovider.DefaultAction{
  518. Action: cloudprovider.TWafAction(self.Properties.Policysettings.Mode),
  519. }
  520. }
  521. func (self *SRegion) ListAppWafs() ([]SAppGatewayWaf, error) {
  522. ret := []SAppGatewayWaf{}
  523. err := self.list("Microsoft.Network/ApplicationGatewayWebApplicationFirewallPolicies", url.Values{}, &ret)
  524. if err != nil {
  525. return nil, errors.Wrapf(err, "list")
  526. }
  527. return ret, nil
  528. }
  529. type SAppWafRuleGroup struct {
  530. Name string `json:"name"`
  531. ID string `json:"id"`
  532. Type string `json:"type"`
  533. Properties struct {
  534. Provisioningstate string `json:"provisioningState"`
  535. Rulesettype string `json:"ruleSetType"`
  536. Rulesetversion string `json:"ruleSetVersion"`
  537. Rulegroups []struct {
  538. Rulegroupname string `json:"ruleGroupName"`
  539. Description string `json:"description"`
  540. Rules []struct {
  541. Ruleid int `json:"ruleId"`
  542. Description string `json:"description"`
  543. } `json:"rules"`
  544. } `json:"ruleGroups"`
  545. } `json:"properties"`
  546. }
  547. func (self *SRegion) CreateICloudWafInstance(opts *cloudprovider.WafCreateOptions) (cloudprovider.ICloudWafInstance, error) {
  548. switch opts.Type {
  549. case cloudprovider.WafTypeAppGateway:
  550. return self.CreateAppWafInstance(opts.Name, opts.DefaultAction)
  551. default:
  552. return nil, errors.Wrapf(cloudprovider.ErrNoSuchProvder, "invalid waf type %s", opts.Type)
  553. }
  554. }
  555. func (self *SRegion) GetICloudWafInstanceById(id string) (cloudprovider.ICloudWafInstance, error) {
  556. if strings.Contains(id, "microsoft.network/applicationgatewaywebapplicationfirewallpolicies") {
  557. return self.GetAppGatewayWaf(id)
  558. }
  559. return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "%s", id)
  560. }
  561. func (self *SRegion) CreateAppWafInstance(name string, action *cloudprovider.DefaultAction) (*SAppGatewayWaf, error) {
  562. mode := cloudprovider.WafActionDetection
  563. if action != nil {
  564. switch action.Action {
  565. case cloudprovider.WafActionDetection, cloudprovider.WafActionPrevention:
  566. mode = action.Action
  567. default:
  568. return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "invalid action %s", action.Action)
  569. }
  570. }
  571. params := map[string]interface{}{
  572. "Type": "Microsoft.Network/applicationGatewayWebApplicationFirewallPolicies",
  573. "Name": name,
  574. "Location": self.Name,
  575. "properties": map[string]interface{}{
  576. "customRules": []string{},
  577. "policySettings": map[string]interface{}{
  578. "fileUploadLimitInMb": 100,
  579. "maxRequestBodySizeInKb": 128,
  580. "mode": mode,
  581. "requestBodyCheck": true,
  582. "state": "Enabled",
  583. },
  584. "managedRules": map[string]interface{}{
  585. "exclusions": []string{},
  586. "managedRuleSets": []map[string]interface{}{
  587. map[string]interface{}{
  588. "ruleSetType": "OWASP",
  589. "ruleSetVersion": "3.1",
  590. "ruleGroupOverrides": []string{},
  591. },
  592. },
  593. },
  594. },
  595. }
  596. ret := &SAppGatewayWaf{region: self}
  597. err := self.create("", jsonutils.Marshal(params), ret)
  598. if err != nil {
  599. return nil, err
  600. }
  601. return ret, nil
  602. }
  603. func (self *SRegion) GetAppGatewayWaf(id string) (*SAppGatewayWaf, error) {
  604. res := &SAppGatewayWaf{region: self}
  605. return res, self.get(id, nil, &res)
  606. }
  607. func (self *SRegion) ListAppWafManagedRuleGroup() ([]SAppWafRuleGroup, error) {
  608. ret := []SAppWafRuleGroup{}
  609. err := self.list("Microsoft.Network/applicationGatewayAvailableWafRuleSets", url.Values{}, &ret)
  610. if err != nil {
  611. return nil, errors.Wrapf(err, "list")
  612. }
  613. return ret, nil
  614. }
  615. func (self *SRegion) GetICloudWafInstances() ([]cloudprovider.ICloudWafInstance, error) {
  616. wafs, err := self.ListAppWafs()
  617. if err != nil {
  618. return nil, errors.Wrapf(err, "ListAppWafs")
  619. }
  620. ret := []cloudprovider.ICloudWafInstance{}
  621. for i := range wafs {
  622. wafs[i].region = self
  623. ret = append(ret, &wafs[i])
  624. }
  625. return ret, nil
  626. }
  627. func (self *SAppGatewayWaf) GetCloudResources() ([]cloudprovider.SCloudResource, error) {
  628. ret := []cloudprovider.SCloudResource{}
  629. for _, ag := range self.Properties.ApplicationGateways {
  630. ret = append(ret, cloudprovider.SCloudResource{
  631. Id: ag.Id,
  632. Name: ag.Id[strings.LastIndex(ag.Id, "/")+1:],
  633. Type: "Application Gateway",
  634. CanDissociate: true,
  635. })
  636. }
  637. for _, lis := range self.Properties.HttpListeners {
  638. ret = append(ret, cloudprovider.SCloudResource{
  639. Id: lis.Id,
  640. Name: lis.Id[strings.LastIndex(lis.Id, "/")+1:],
  641. Type: "HTTP Listener",
  642. CanDissociate: true,
  643. })
  644. }
  645. for _, route := range self.Properties.PathBasedRules {
  646. ret = append(ret, cloudprovider.SCloudResource{
  647. Id: route.Id,
  648. Name: route.Id[strings.LastIndex(route.Id, "/")+1:],
  649. Type: "Route Path",
  650. CanDissociate: true,
  651. })
  652. }
  653. return ret, nil
  654. }
  655. func (self *SAppGatewayWaf) SetTags(tags map[string]string, replace bool) error {
  656. if !replace {
  657. for k, v := range self.Tags {
  658. if _, ok := tags[k]; !ok {
  659. tags[k] = v
  660. }
  661. }
  662. }
  663. _, err := self.region.client.SetTags(self.ID, tags)
  664. if err != nil {
  665. return errors.Wrapf(err, "SetTags")
  666. }
  667. return nil
  668. }