forked from yunionio/cloudmux
/
waf_regexsets.go
155 lines (139 loc) · 4.16 KB
/
waf_regexsets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aws
import (
"github.com/aws/aws-sdk-go/service/wafv2"
"github.com/nyl1001/pkg/errors"
"github.com/nyl1001/pkg/jsonutils"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
type RegularExpression struct {
RegexString string
}
type SWafRegexSet struct {
region *SRegion
scope string
RegularExpressionList []RegularExpression
ARN string
Description string
Id string
LockToken string
Name string
}
func (self *SWafRegexSet) GetName() string {
return self.Name
}
func (self *SWafRegexSet) GetDesc() string {
return self.Description
}
func (self *SWafRegexSet) GetGlobalId() string {
return self.ARN
}
func (self *SWafRegexSet) GetType() cloudprovider.TWafType {
switch self.scope {
case SCOPE_REGIONAL:
return cloudprovider.WafTypeRegional
case SCOPE_CLOUDFRONT:
return cloudprovider.WafTypeCloudFront
default:
return cloudprovider.TWafType(self.scope)
}
}
func (self *SWafRegexSet) GetRegexPatterns() cloudprovider.WafRegexPatterns {
if len(self.RegularExpressionList) == 0 {
rSet, err := self.region.GetRegexSet(self.Id, self.Name, self.scope)
if err != nil {
return cloudprovider.WafRegexPatterns{}
}
jsonutils.Update(self, rSet)
}
ret := cloudprovider.WafRegexPatterns{}
for _, r := range self.RegularExpressionList {
ret = append(ret, r.RegexString)
}
return ret
}
func (self *SWafRegexSet) Delete() error {
return self.region.DeleteRegexSet(self.Id, self.Name, self.scope, self.LockToken)
}
func (self *SRegion) ListRegexSets(scope string) ([]SWafRegexSet, error) {
if scope == SCOPE_CLOUDFRONT && self.RegionId != "us-east-1" {
return []SWafRegexSet{}, nil
}
client, err := self.getWafClient()
if err != nil {
return nil, errors.Wrapf(err, "getWafClient")
}
ret := []SWafRegexSet{}
input := wafv2.ListRegexPatternSetsInput{}
input.SetScope(scope)
for {
resp, err := client.ListRegexPatternSets(&input)
if err != nil {
return nil, errors.Wrapf(err, "ListRegexPatternSets")
}
part := []SWafRegexSet{}
jsonutils.Update(&part, resp.RegexPatternSets)
ret = append(ret, part...)
if resp.NextMarker == nil || len(*resp.NextMarker) == 0 {
break
}
input.SetNextMarker(*resp.NextMarker)
}
return ret, nil
}
func (self *SRegion) GetRegexSet(id, name, scope string) (*SWafRegexSet, error) {
client, err := self.getWafClient()
if err != nil {
return nil, errors.Wrapf(err, "getWafClient")
}
input := wafv2.GetRegexPatternSetInput{}
input.SetId(id)
input.SetName(name)
input.SetScope(scope)
resp, err := client.GetRegexPatternSet(&input)
if err != nil {
return nil, errors.Wrapf(err, "GetRegexPatternSet")
}
ret := &SWafRegexSet{LockToken: *resp.LockToken}
return ret, jsonutils.Update(ret, resp.RegexPatternSet)
}
func (self *SRegion) DeleteRegexSet(id, name, scope, lockToken string) error {
client, err := self.getWafClient()
if err != nil {
return errors.Wrapf(err, "getWafClient")
}
input := wafv2.DeleteRegexPatternSetInput{}
input.SetId(id)
input.SetName(name)
input.SetScope(scope)
input.SetLockToken(lockToken)
_, err = client.DeleteRegexPatternSet(&input)
return errors.Wrapf(err, "DeleteRegexPatternSet")
}
func (self *SRegion) GetICloudWafRegexSets() ([]cloudprovider.ICloudWafRegexSet, error) {
ret := []cloudprovider.ICloudWafRegexSet{}
for _, scope := range WAF_SCOPES {
part, err := self.ListRegexSets(scope)
if err != nil {
return nil, errors.Wrapf(err, "ListRegexSets(%s)", scope)
}
for i := range part {
part[i].scope = scope
part[i].region = self
ret = append(ret, &part[i])
}
}
return ret, nil
}