/
table_azure_firewall.go
286 lines (262 loc) · 9.88 KB
/
table_azure_firewall.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 azure
import (
"context"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-07-01/network"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)
//// TABLE DEFINITION ////
func tableAzureFirewall(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_firewall",
Description: "Azure Firewall",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "resource_group"}),
Hydrate: getFirewall,
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: isNotFoundError([]string{"ResourceNotFound", "ResourceGroupNotFound", "404"}),
},
},
List: &plugin.ListConfig{
Hydrate: listFirewalls,
},
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Description: "The friendly name that identifies the firewall",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "Contains ID to identify a firewall uniquely",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "etag",
Description: "An unique read-only string that changes whenever the resource is updated",
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "The resource type of the firewall",
Type: proto.ColumnType_STRING,
},
{
Name: "provisioning_state",
Description: "The provisioning state of the firewall resource",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AzureFirewallPropertiesFormat.ProvisioningState").Transform(transform.ToString),
},
{
Name: "firewall_policy_id",
Description: "The firewallPolicy associated with this azure firewall",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AzureFirewallPropertiesFormat.FirewallPolicy.ID"),
},
{
Name: "hub_private_ip_address",
Description: "Private IP Address associated with azure firewall",
Type: proto.ColumnType_IPADDR,
Transform: transform.FromField("AzureFirewallPropertiesFormat.HubIPAddresses.PrivateIPAddress"),
},
{
Name: "hub_public_ip_address_count",
Description: "The number of Public IP addresses associated with azure firewall",
Type: proto.ColumnType_INT,
Transform: transform.FromField("AzureFirewallPropertiesFormat.HubIPAddresses.PublicIPs.Count"),
},
{
Name: "sku_name",
Description: "Name of an Azure Firewall SKU",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AzureFirewallPropertiesFormat.Sku.Name").Transform(transform.ToString),
},
{
Name: "sku_tier",
Description: "Tier of an Azure Firewall",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AzureFirewallPropertiesFormat.Sku.Tier").Transform(transform.ToString),
},
{
Name: "threat_intel_mode",
Description: "The operation mode for Threat Intelligence",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AzureFirewallPropertiesFormat.ThreatIntelMode").Transform(transform.ToString),
},
{
Name: "virtual_hub_id",
Description: "The virtualHub to which the firewall belongs",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AzureFirewallPropertiesFormat.VirtualHub.ID"),
},
{
Name: "additional_properties",
Description: "A collection of additional properties used to further config this azure firewall",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("AzureFirewallPropertiesFormat.AdditionalProperties"),
},
{
Name: "application_rule_collections",
Description: "A collection of application rule collections used by Azure Firewall",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("AzureFirewallPropertiesFormat.ApplicationRuleCollections"),
},
{
Name: "availability_zones",
Description: "A collection of availability zones denoting where the resource needs to come from",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Zones"),
},
{
Name: "hub_public_ip_addresses",
Description: "A collection of Public IP addresses associated with azure firewall or IP addresses to be retained",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("AzureFirewallPropertiesFormat.HubIPAddresses.PublicIPs.Addresses"),
},
{
Name: "ip_configurations",
Description: "A collection of IP configuration of the Azure Firewall resource",
Type: proto.ColumnType_JSON,
Transform: transform.From(ipConfigurationData),
},
{
Name: "ip_groups",
Description: "A collection of IpGroups associated with AzureFirewall",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("AzureFirewallPropertiesFormat.IPGroups"),
},
{
Name: "nat_rule_collections",
Description: "A collection of NAT rule collections used by Azure Firewall",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("AzureFirewallPropertiesFormat.NatRuleCollections"),
},
{
Name: "network_rule_collections",
Description: "A collection of network rule collections used by Azure Firewall",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("AzureFirewallPropertiesFormat.NetworkRuleCollections"),
},
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ID").Transform(idToAkas),
},
// Azure standard columns
{
Name: "region",
Description: ColumnDescriptionRegion,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Location").Transform(toLower),
},
{
Name: "resource_group",
Description: ColumnDescriptionResourceGroup,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
},
}),
}
}
//// FETCH FUNCTIONS ////
func listFirewalls(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
subscriptionID := session.SubscriptionID
networkClient := network.NewAzureFirewallsClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
networkClient.Authorizer = session.Authorizer
result, err := networkClient.ListAll(ctx)
if err != nil {
return nil, err
}
for _, firewall := range result.Values() {
d.StreamListItem(ctx, firewall)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
for result.NotDone() {
err := result.NextWithContext(ctx)
if err != nil {
return nil, err
}
for _, firewall := range result.Values() {
d.StreamListItem(ctx, firewall)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, err
}
//// HYDRATE FUNCTIONS ////
func getFirewall(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getFirewall")
name := d.EqualsQuals["name"].GetStringValue()
resourceGroup := d.EqualsQuals["resource_group"].GetStringValue()
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
subscriptionID := session.SubscriptionID
networkClient := network.NewAzureFirewallsClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
networkClient.Authorizer = session.Authorizer
op, err := networkClient.Get(ctx, resourceGroup, name)
if err != nil {
return nil, err
}
// In some cases resource does not give any notFound error
// instead of notFound error, it returns empty data
if op.ID != nil {
return op, nil
}
return nil, nil
}
//// Transform Functions
func ipConfigurationData(ctx context.Context, d *transform.TransformData) (interface{}, error) {
data := d.HydrateItem.(network.AzureFirewall)
var output []map[string]interface{}
// Add a check for AzureFirewallPropertiesFormat.IPConfigurations data to ensure that
// it is not null to avoid panic errors
if data.AzureFirewallPropertiesFormat.IPConfigurations != nil {
for _, firewall := range *data.AzureFirewallPropertiesFormat.IPConfigurations {
objectMap := make(map[string]interface{})
if firewall.AzureFirewallIPConfigurationPropertiesFormat.PrivateIPAddress != nil {
objectMap["privateIPAddress"] = firewall.AzureFirewallIPConfigurationPropertiesFormat.PrivateIPAddress
}
if firewall.AzureFirewallIPConfigurationPropertiesFormat.PublicIPAddress != nil {
objectMap["publicIPAddress"] = firewall.AzureFirewallIPConfigurationPropertiesFormat.PublicIPAddress
}
if firewall.AzureFirewallIPConfigurationPropertiesFormat.Subnet != nil {
objectMap["subnet"] = firewall.AzureFirewallIPConfigurationPropertiesFormat.Subnet
}
if firewall.AzureFirewallIPConfigurationPropertiesFormat.ProvisioningState != "" {
objectMap["provisioningState"] = firewall.AzureFirewallIPConfigurationPropertiesFormat.ProvisioningState
}
output = append(output, objectMap)
}
return output, nil
}
return nil, nil
}