/
security_domain.go
286 lines (240 loc) · 9.2 KB
/
security_domain.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package goaviatrix
import (
"context"
)
// AwsTGW simple struct to hold aws_tgw details
type SecurityDomain struct {
Action string `form:"action, omitempty"`
CID string `form:"CID, omitempty"`
Name string `form:"route_domain_name, omitempty"`
AccountName string `form:"account_name, omitempty"`
Region string `form:"region, omitempty"`
AwsTgwName string `form:"tgw_name, omitempty"`
AviatrixFirewallDomain bool `form:"firewall_domain, omitempty"`
NativeEgressDomain bool `form:"native_egress_domain, omitempty"`
NativeFirewallDomain bool `form:"native_firewall_domain, omitempty"`
ForceDelete bool `form:"force,omitempty"`
Async bool `form:"async,omitempty"`
}
type SecurityDomainAPIResp struct {
Return bool `json:"return"`
Results []string `json:"results"`
Reason string `json:"reason"`
}
type SecurityDomainList struct {
Return bool `json:"return"`
Results []string `json:"results"`
Reason string `json:"reason"`
}
type SecurityDomainRule struct {
Name string `json:"security_domain_name,omitempty"`
ConnectedDomain []string `json:"connected_domains,omitempty"`
AttachedVPCs []VPCSolo `json:"attached_vpc,omitempty"`
AviatrixFirewallDomain bool `json:"firewall_domain,omitempty"`
NativeEgressDomain bool `json:"egress_domain,omitempty"`
NativeFirewallDomain bool `json:"native_firewall_domain,omitempty"`
}
type SecurityDomainDetails struct {
Name string `json:"name"`
ConnectedDomain []string `json:"connected_route_domain,omitempty"`
AttachedVPCs []VPCSolo `json:"attached_vpc,omitempty"`
AviatrixFirewallDomain bool `json:"firewall_domain,omitempty"`
NativeEgressDomain bool `json:"egress_domain,omitempty"`
NativeFirewallDomain bool `json:"native_firewall_domain,omitempty"`
}
type VPCSolo struct {
Region string `json:"vpc_region,omitempty"`
AccountName string `json:"vpc_account_name,omitempty"`
VpcID string `json:"vpc_id,omitempty"`
Subnets string
RouteTables string
CustomizedRoutes string `json:",omitempty"`
CustomizedRouteAdvertisement string
DisableLocalRoutePropagation bool `json:",omitempty"`
}
type IntraDomainInspection struct {
TgwName string `form:"tgw_name,omitempty" json:"tgw_name"`
RouteDomainName string `form:"route_domain_name,omitempty" json:"name"`
FirewallDomainName string `form:"firewall_domain_name,omitempty" json:"intra_domain_inspection_name"`
IntraDomainInspectionEnabled bool `json:"intra_domain_inspection"`
}
type NetworkDomainDetails struct {
Name string `json:"name"`
TgwName string `json:"tgw_name"`
RouteTableId string `json:"route_table_id"`
Account string `json:"account"`
CouldType string `json:"cloud_type"`
Region string `json:"region"`
IntraDomainInspectionEnabled bool `json:"intra_domain_inspection"`
EgressInspection bool `json:"egress_inspection"`
InspectionPolicy string `json:"inspection_policy"`
IntraDomainInspectionName string `json:"intra_domain_inspection_name"`
EgressInspectionName string `json:"egress_inspection_name"`
Type string `json:"type"`
}
func (c *Client) CreateSecurityDomain(securityDomain *SecurityDomain) error {
securityDomain.CID = c.CID
securityDomain.Action = "add_route_domain"
securityDomain.Async = true
return c.PostAsyncAPI(securityDomain.Action, securityDomain, BasicCheck)
}
func (c *Client) GetSecurityDomain(securityDomain *SecurityDomain) (string, error) {
form := map[string]string{
"CID": c.CID,
"action": "list_route_domain_names",
"tgw_name": securityDomain.AwsTgwName,
}
data := SecurityDomainAPIResp{
Return: false,
Results: make([]string, 0),
Reason: "",
}
err := c.GetAPI(&data, form["action"], form, BasicCheck)
if err != nil {
return "", err
}
securityDomainList := data.Results
for i := range securityDomainList {
if securityDomainList[i] == securityDomain.Name {
return securityDomainList[i], nil
}
}
return "", ErrNotFound
}
func (c *Client) UpdateSecurityDomain(securityDomain *SecurityDomain) error {
return nil
}
func (c *Client) DeleteSecurityDomain(securityDomain *SecurityDomain) error {
securityDomain.CID = c.CID
securityDomain.Action = "delete_route_domain"
return c.PostAPI(securityDomain.Action, securityDomain, BasicCheck)
}
func (c *Client) CreateDomainConnection(awsTgw *AWSTgw, sourceDomain string, destinationDomain string) error {
form := map[string]string{
"CID": c.CID,
"action": "add_connection_between_route_domains",
"account_name": awsTgw.AccountName,
"region": awsTgw.Region,
"tgw_name": awsTgw.Name,
"source_route_domain_name": sourceDomain,
"destination_route_domain_name": destinationDomain,
}
return c.PostAPI(form["action"], form, BasicCheck)
}
func (c *Client) DeleteDomainConnection(awsTgw *AWSTgw, sourceDomain string, destinationDomain string) error {
form := map[string]string{
"CID": c.CID,
"action": "delete_connection_between_route_domains",
"tgw_name": awsTgw.Name,
"source_route_domain_name": sourceDomain,
"destination_route_domain_name": destinationDomain,
}
return c.PostAPI(form["action"], form, BasicCheck)
}
func (c *Client) SecurityDomainRuleValidation(securityDomainRule *SecurityDomainRule) bool {
num := 0
if securityDomainRule.AviatrixFirewallDomain {
num += 1
}
if securityDomainRule.NativeEgressDomain {
num += 1
}
if securityDomainRule.NativeFirewallDomain {
num += 1
}
if num > 1 {
return false
}
return true
}
func (c *Client) GetSecurityDomainDetails(ctx context.Context, domain *SecurityDomain) (*SecurityDomainDetails, error) {
params := map[string]string{
"action": "list_tgw_security_domain_details",
"CID": c.CID,
"tgw_name": domain.AwsTgwName,
"route_domain_name": domain.Name,
}
type Resp struct {
Return bool `json:"return"`
Results []SecurityDomainDetails `json:"results"`
Reason string `json:"reason"`
}
var data Resp
err := c.GetAPIContext(ctx, &data, params["action"], params, BasicCheck)
if err != nil {
return nil, err
}
if len(data.Results) == 0 {
return nil, ErrNotFound
}
return &data.Results[0], nil
}
func (c *Client) EnableIntraDomainInspection(ctx context.Context, intraDomainInspection *IntraDomainInspection) error {
params := map[string]string{
"action": "enable_tgw_intra_domain_inspection",
"CID": c.CID,
"tgw_name": intraDomainInspection.TgwName,
"route_domain_name": intraDomainInspection.RouteDomainName,
"firewall_domain_name": intraDomainInspection.FirewallDomainName,
}
return c.PostAPIContext(ctx, params["action"], params, BasicCheck)
}
func (c *Client) DisableIntraDomainInspection(ctx context.Context, intraDomainInspection *IntraDomainInspection) error {
params := map[string]string{
"action": "disable_tgw_intra_domain_inspection",
"CID": c.CID,
"tgw_name": intraDomainInspection.TgwName,
"route_domain_name": intraDomainInspection.RouteDomainName,
}
return c.PostAPIContext(ctx, params["action"], params, BasicCheck)
}
func (c *Client) GetAllNetworkDomains(ctx context.Context) ([]NetworkDomainDetails, error) {
params := map[string]string{
"action": "list_all_tgw_security_domains",
"CID": c.CID,
}
type DomainDetail struct {
Domains []NetworkDomainDetails `json:"domains"`
}
type Resp struct {
Return bool `json:"return"`
Results DomainDetail `json:"results"`
Reason string `json:"reason"`
}
var data Resp
err := c.GetAPIContext(ctx, &data, params["action"], params, BasicCheck)
if err != nil {
return nil, err
}
return data.Results.Domains, nil
}
func (c *Client) GetIntraDomainInspectionStatus(ctx context.Context, intraDomainInspection *IntraDomainInspection) error {
params := map[string]string{
"action": "list_all_tgw_security_domains",
"CID": c.CID,
}
type DomainDetails struct {
Domains []IntraDomainInspection `json:"domains"`
}
type Resp struct {
Return bool `json:"return"`
Results DomainDetails `json:"results"`
Reason string `json:"reason"`
}
var data Resp
err := c.GetAPIContext(ctx, &data, params["action"], params, BasicCheck)
if err != nil {
return err
}
for _, domain := range data.Results.Domains {
if domain.TgwName == intraDomainInspection.TgwName && domain.RouteDomainName == intraDomainInspection.RouteDomainName {
if !domain.IntraDomainInspectionEnabled {
return ErrNotFound
}
intraDomainInspection.IntraDomainInspectionEnabled = domain.IntraDomainInspectionEnabled
intraDomainInspection.FirewallDomainName = domain.FirewallDomainName
return nil
}
}
return ErrNotFound
}