yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/waf_ipsets.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 SWafIPSet struct {
    27  	region      *SRegion
    28  	scope       string
    29  	Addresses   []string
    30  	ARN         string
    31  	Description string
    32  	Id          string
    33  	LockToken   string
    34  	Name        string
    35  }
    36  
    37  func (self *SWafIPSet) GetName() string {
    38  	return self.Name
    39  }
    40  
    41  func (self *SWafIPSet) GetDesc() string {
    42  	return self.Description
    43  }
    44  
    45  func (self *SWafIPSet) GetGlobalId() string {
    46  	return self.ARN
    47  }
    48  
    49  func (self *SWafIPSet) GetType() cloudprovider.TWafType {
    50  	switch self.scope {
    51  	case SCOPE_CLOUDFRONT:
    52  		return cloudprovider.WafTypeCloudFront
    53  	case SCOPE_REGIONAL:
    54  		return cloudprovider.WafTypeRegional
    55  	}
    56  	return cloudprovider.TWafType(self.scope)
    57  }
    58  
    59  func (self *SWafIPSet) GetAddresses() cloudprovider.WafAddresses {
    60  	if len(self.Addresses) == 0 {
    61  		ipSet, err := self.region.GetIPSet(self.Id, self.Name, self.scope)
    62  		if err != nil {
    63  			return cloudprovider.WafAddresses{}
    64  		}
    65  		return ipSet.Addresses
    66  	}
    67  	return self.Addresses
    68  }
    69  
    70  func (self *SWafIPSet) Delete() error {
    71  	return self.region.DeleteIPSet(self.Id, self.Name, self.scope, self.LockToken)
    72  }
    73  
    74  func (self *SRegion) ListIPSets(scope string) ([]SWafIPSet, error) {
    75  	if scope == SCOPE_CLOUDFRONT && self.RegionId != "us-east-1" {
    76  		return []SWafIPSet{}, nil
    77  	}
    78  	client, err := self.getWafClient()
    79  	if err != nil {
    80  		return nil, errors.Wrapf(err, "getWafClient")
    81  	}
    82  	ret := []SWafIPSet{}
    83  	input := wafv2.ListIPSetsInput{}
    84  	input.SetScope(scope)
    85  	for {
    86  		resp, err := client.ListIPSets(&input)
    87  		if err != nil {
    88  			return nil, errors.Wrapf(err, "ListIPSets")
    89  		}
    90  		part := []SWafIPSet{}
    91  		jsonutils.Update(&part, resp.IPSets)
    92  		ret = append(ret, part...)
    93  		if resp.NextMarker == nil || len(*resp.NextMarker) == 0 {
    94  			break
    95  		}
    96  		input.SetNextMarker(*resp.NextMarker)
    97  	}
    98  	return ret, nil
    99  }
   100  
   101  func (self *SRegion) GetIPSet(id, name, scope string) (*SWafIPSet, error) {
   102  	client, err := self.getWafClient()
   103  	if err != nil {
   104  		return nil, errors.Wrapf(err, "getWafClient")
   105  	}
   106  	input := wafv2.GetIPSetInput{}
   107  	input.SetId(id)
   108  	input.SetName(name)
   109  	input.SetScope(scope)
   110  	resp, err := client.GetIPSet(&input)
   111  	if err != nil {
   112  		return nil, errors.Wrapf(err, "GetIPSet")
   113  	}
   114  	ret := &SWafIPSet{LockToken: *resp.LockToken}
   115  	return ret, jsonutils.Update(ret, resp.IPSet)
   116  }
   117  
   118  func (self *SRegion) DeleteIPSet(id, name, scope, lockToken string) error {
   119  	client, err := self.getWafClient()
   120  	if err != nil {
   121  		return errors.Wrapf(err, "getWafClient")
   122  	}
   123  	input := wafv2.DeleteIPSetInput{}
   124  	input.SetId(id)
   125  	input.SetName(name)
   126  	input.SetScope(scope)
   127  	input.SetLockToken(lockToken)
   128  	_, err = client.DeleteIPSet(&input)
   129  	return errors.Wrapf(err, "DeleteIPSet")
   130  }
   131  
   132  func (self *SRegion) GetICloudWafIPSets() ([]cloudprovider.ICloudWafIPSet, error) {
   133  	ret := []cloudprovider.ICloudWafIPSet{}
   134  	for _, scope := range WAF_SCOPES {
   135  		part, err := self.ListIPSets(scope)
   136  		if err != nil {
   137  			return nil, errors.Wrapf(err, "ListIPSets(%s)", scope)
   138  		}
   139  		for i := range part {
   140  			part[i].scope = scope
   141  			part[i].region = self
   142  			ret = append(ret, &part[i])
   143  		}
   144  	}
   145  	return ret, nil
   146  }