yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/waf_regexsets.go (about)

     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  
    15  package aws
    16  
    17  import (
    18  	"github.com/aws/aws-sdk-go/service/wafv2"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  )
    25  
    26  type RegularExpression struct {
    27  	RegexString string
    28  }
    29  
    30  type SWafRegexSet struct {
    31  	region                *SRegion
    32  	scope                 string
    33  	RegularExpressionList []RegularExpression
    34  	ARN                   string
    35  	Description           string
    36  	Id                    string
    37  	LockToken             string
    38  	Name                  string
    39  }
    40  
    41  func (self *SWafRegexSet) GetName() string {
    42  	return self.Name
    43  }
    44  
    45  func (self *SWafRegexSet) GetDesc() string {
    46  	return self.Description
    47  }
    48  
    49  func (self *SWafRegexSet) GetGlobalId() string {
    50  	return self.ARN
    51  }
    52  
    53  func (self *SWafRegexSet) GetType() cloudprovider.TWafType {
    54  	switch self.scope {
    55  	case SCOPE_REGIONAL:
    56  		return cloudprovider.WafTypeRegional
    57  	case SCOPE_CLOUDFRONT:
    58  		return cloudprovider.WafTypeCloudFront
    59  	default:
    60  		return cloudprovider.TWafType(self.scope)
    61  	}
    62  }
    63  
    64  func (self *SWafRegexSet) GetRegexPatterns() cloudprovider.WafRegexPatterns {
    65  	if len(self.RegularExpressionList) == 0 {
    66  		rSet, err := self.region.GetRegexSet(self.Id, self.Name, self.scope)
    67  		if err != nil {
    68  			return cloudprovider.WafRegexPatterns{}
    69  		}
    70  		jsonutils.Update(self, rSet)
    71  	}
    72  	ret := cloudprovider.WafRegexPatterns{}
    73  	for _, r := range self.RegularExpressionList {
    74  		ret = append(ret, r.RegexString)
    75  	}
    76  	return ret
    77  }
    78  
    79  func (self *SWafRegexSet) Delete() error {
    80  	return self.region.DeleteRegexSet(self.Id, self.Name, self.scope, self.LockToken)
    81  }
    82  
    83  func (self *SRegion) ListRegexSets(scope string) ([]SWafRegexSet, error) {
    84  	if scope == SCOPE_CLOUDFRONT && self.RegionId != "us-east-1" {
    85  		return []SWafRegexSet{}, nil
    86  	}
    87  	client, err := self.getWafClient()
    88  	if err != nil {
    89  		return nil, errors.Wrapf(err, "getWafClient")
    90  	}
    91  	ret := []SWafRegexSet{}
    92  	input := wafv2.ListRegexPatternSetsInput{}
    93  	input.SetScope(scope)
    94  	for {
    95  		resp, err := client.ListRegexPatternSets(&input)
    96  		if err != nil {
    97  			return nil, errors.Wrapf(err, "ListRegexPatternSets")
    98  		}
    99  		part := []SWafRegexSet{}
   100  		jsonutils.Update(&part, resp.RegexPatternSets)
   101  		ret = append(ret, part...)
   102  		if resp.NextMarker == nil || len(*resp.NextMarker) == 0 {
   103  			break
   104  		}
   105  		input.SetNextMarker(*resp.NextMarker)
   106  	}
   107  	return ret, nil
   108  }
   109  
   110  func (self *SRegion) GetRegexSet(id, name, scope string) (*SWafRegexSet, error) {
   111  	client, err := self.getWafClient()
   112  	if err != nil {
   113  		return nil, errors.Wrapf(err, "getWafClient")
   114  	}
   115  	input := wafv2.GetRegexPatternSetInput{}
   116  	input.SetId(id)
   117  	input.SetName(name)
   118  	input.SetScope(scope)
   119  	resp, err := client.GetRegexPatternSet(&input)
   120  	if err != nil {
   121  		return nil, errors.Wrapf(err, "GetRegexPatternSet")
   122  	}
   123  	ret := &SWafRegexSet{LockToken: *resp.LockToken}
   124  	return ret, jsonutils.Update(ret, resp.RegexPatternSet)
   125  }
   126  
   127  func (self *SRegion) DeleteRegexSet(id, name, scope, lockToken string) error {
   128  	client, err := self.getWafClient()
   129  	if err != nil {
   130  		return errors.Wrapf(err, "getWafClient")
   131  	}
   132  	input := wafv2.DeleteRegexPatternSetInput{}
   133  	input.SetId(id)
   134  	input.SetName(name)
   135  	input.SetScope(scope)
   136  	input.SetLockToken(lockToken)
   137  	_, err = client.DeleteRegexPatternSet(&input)
   138  	return errors.Wrapf(err, "DeleteRegexPatternSet")
   139  }
   140  
   141  func (self *SRegion) GetICloudWafRegexSets() ([]cloudprovider.ICloudWafRegexSet, error) {
   142  	ret := []cloudprovider.ICloudWafRegexSet{}
   143  	for _, scope := range WAF_SCOPES {
   144  		part, err := self.ListRegexSets(scope)
   145  		if err != nil {
   146  			return nil, errors.Wrapf(err, "ListRegexSets(%s)", scope)
   147  		}
   148  		for i := range part {
   149  			part[i].scope = scope
   150  			part[i].region = self
   151  			ret = append(ret, &part[i])
   152  		}
   153  	}
   154  	return ret, nil
   155  }