/
cloudarmor.go
175 lines (157 loc) · 4.94 KB
/
cloudarmor.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cloudarmor
import (
"context"
"fmt"
"strings"
"github.com/fallard84/cs-cloud-firewall-bouncer/pkg/models"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
)
type Client struct {
svc GoogleComputeServiceIface
project string
policy string
priority int64
maxRules int
}
const (
providerName = "cloudarmor"
defaultMaxRules = 100
)
var log *logrus.Entry
func init() {
log = logrus.WithField("provider", providerName)
}
func (c *Client) MaxSourcesPerRule() int {
return 10
}
func (c *Client) MaxRules() int {
return c.maxRules
}
func (c *Client) Priority() int64 {
return c.priority
}
func getProjectIDFromCredentials(config *models.CloudArmorConfig) (string, error) {
ctx := context.Background()
credentials, error := google.FindDefaultCredentials(ctx, compute.ComputeScope)
if error != nil {
return "", error
}
if credentials.ProjectID == "" {
return "", fmt.Errorf("Default credentials does not have a project ID associated")
}
return credentials.ProjectID, nil
}
func checkCloudArmorConfig(config *models.CloudArmorConfig) error {
if config == nil {
return fmt.Errorf("gcp cloud provider must be specified")
}
if config.ProjectID == "" {
var err error
config.ProjectID, err = getProjectIDFromCredentials(config)
if err != nil || config.ProjectID == "" {
return fmt.Errorf("can't get project id from credentials: %s", err)
}
}
if config.Policy == "" {
return fmt.Errorf("policy must be specified in cloudarmor config")
}
if config.MaxRules == 0 {
config.MaxRules = defaultMaxRules
}
return nil
}
// NewClient creates a new GCP client
func NewClient(config *models.CloudArmorConfig) (*Client, error) {
log.Infof("creating client for %s", providerName)
err := checkCloudArmorConfig(config)
if err != nil {
return nil, fmt.Errorf("error while checking GCP config: %s", err)
}
return &Client{
svc: NewGoogleComputeService(config.Endpoint),
project: config.ProjectID,
policy: config.Policy,
priority: config.Priority,
maxRules: config.MaxRules,
}, nil
}
func (c *Client) GetProviderName() string {
return providerName
}
func (c *Client) GetRules(ruleNamePrefix string) ([]*models.FirewallRule, error) {
res, err := c.svc.GetFirewallPolicy(c.project, c.policy)
if err != nil {
return nil, fmt.Errorf("unable to get firewall policy %s: %s", c.policy, err)
}
var rules []*models.FirewallRule
for _, r := range res.Rules {
if !strings.HasPrefix(r.Description, ruleNamePrefix) {
continue
}
log.Infof("%s (%d sources): %#v", r.Description, len(r.Match.Config.SrcIpRanges), r.Match.Config.SrcIpRanges)
rule := models.FirewallRule{
Name: r.Description,
SourceRanges: models.ConvertSourceRangesSliceToMap(r.Match.Config.SrcIpRanges),
Priority: r.Priority,
}
rules = append(rules, &rule)
}
return rules, nil
}
func (c *Client) CreateRule(rule *models.FirewallRule) error {
log.Infof("creating cloud armor policy rule %s with %#v", rule.Name, rule.SourceRanges)
policyRule := compute.SecurityPolicyRule{
Action: "deny(403)",
Match: &compute.SecurityPolicyRuleMatcher{
Config: &compute.SecurityPolicyRuleMatcherConfig{
SrcIpRanges: models.ConvertSourceRangesMapToSlice(rule.SourceRanges),
},
VersionedExpr: "SRC_IPS_V1",
},
Description: rule.Name,
Priority: rule.Priority,
}
op, err := c.svc.AddRule(c.project, c.policy, &policyRule)
if err != nil {
return fmt.Errorf("unable to create policy rule %s: %s", rule.Name, err)
}
if err = c.svc.WaitOperation(c.project, op.Name); err != nil {
return fmt.Errorf("problem waiting on operation %s: %s", op.Name, err)
}
log.Infof("creation of policy rule %s successful", rule.Name)
return nil
}
func (c *Client) DeleteRule(rule *models.FirewallRule) error {
log.Infof("deleting policy rule %s", rule.Name)
op, err := c.svc.RemoveRule(c.project, c.policy, rule.Priority)
if err != nil {
return fmt.Errorf("unable to delete policy rule %s: %s", rule.Name, err)
}
if err = c.svc.WaitOperation(c.project, op.Name); err != nil {
return fmt.Errorf("problem waiting on operation %s: %s", op.Name, err)
}
log.Infof("deletion of policy rule %s successful", rule.Name)
return nil
}
func (c *Client) PatchRule(rule *models.FirewallRule) error {
log.Infof("patching policy rule %s with %#v", rule.Name, rule.SourceRanges)
rulePatchRequest := compute.SecurityPolicyRule{
Match: &compute.SecurityPolicyRuleMatcher{
Config: &compute.SecurityPolicyRuleMatcherConfig{
SrcIpRanges: models.ConvertSourceRangesMapToSlice(rule.SourceRanges),
},
VersionedExpr: "SRC_IPS_V1",
},
}
op, err := c.svc.PatchRule(c.project, c.policy, &rulePatchRequest, rule.Priority)
if err != nil {
return fmt.Errorf("unable to patch policy rule %s: %s", rule.Name, err)
}
if err = c.svc.WaitOperation(c.project, op.Name); err != nil {
return fmt.Errorf("problem waiting on operation %s: %s", op.Name, err)
}
log.Infof("patching of policy rule %s successful", rule.Name)
return nil
}