-
Notifications
You must be signed in to change notification settings - Fork 25
/
azure.go
499 lines (413 loc) · 15.2 KB
/
azure.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// A package that generates Lacework deployment code for Azure cloud.
package azure
import (
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/lacework/go-sdk/lwgenerate"
"github.com/pkg/errors"
)
type GenerateAzureTfConfigurationArgs struct {
// Should we configure Activity Log integration in LW?
ActivityLog bool
// Should we add Config integration in LW?
Config bool
// Should we create an Active Directory integration
CreateAdIntegration bool
// If Config is true, give the user the opportunity to name their integration. Defaults to "TF Config"
ConfigIntegrationName string
// If ActivityLog is true, give the user the opportunity to name their integration. Defaults to "TF activity log"
ActivityLogIntegrationName string
// Active Directory application Id
AdApplicationId string
// Active Directory password
AdApplicationPassword string
// Active Directory Enterprise app object id
AdServicePrincipalId string
// Should we use the management group, rather than subscription
ManagementGroup bool
// Management Group ID to set
ManagementGroupId string
// List of subscription Ids
SubscriptionIds []string
// Subscription ID configured in azurerm provider block
SubscriptionID string
// Grant read access to ALL subscriptions
AllSubscriptions bool
// Storage Account name
StorageAccountName string
// Storage Account Resource Group
StorageAccountResourceGroup string
// Should we use existing storage account
ExistingStorageAccount bool
// Azure region where the storage account for logging resides
StorageLocation string
LaceworkProfile string
}
// Ensure all combinations of inputs are valid for supported spec
func (args *GenerateAzureTfConfigurationArgs) validate() error {
// Validate one of config or activity log was enabled; otherwise error out
if !args.ActivityLog && !args.Config {
return errors.New("audit log or config integration must be enabled")
}
// Validate that active directory settings are correct
if !args.CreateAdIntegration && (args.AdApplicationId == "" ||
args.AdServicePrincipalId == "" || args.AdApplicationPassword == "") {
return errors.New("Active directory details must be set")
}
// Validate the Mangement Group
if args.ManagementGroup && args.ManagementGroupId == "" {
return errors.New("When Group Management is enabled, then Group Id must be configured")
}
// Validate Storage Account
if args.ExistingStorageAccount && (args.StorageAccountName == "" || args.StorageAccountResourceGroup == "") {
return errors.New("When using existing storage account, storage account details must be configured")
}
return nil
}
type AzureTerraformModifier func(c *GenerateAzureTfConfigurationArgs)
// NewTerraform returns an instance of the GenerateAzureTfConfigurationArgs struct with the provided enabled
// settings (config/activity log).
//
// Note: Additional configuration details may be set using modifiers of the AzureTerraformModifier type
func NewTerraform(
enableConfig bool, enableActivityLog bool, createAdIntegration bool, mods ...AzureTerraformModifier,
) *GenerateAzureTfConfigurationArgs {
config := &GenerateAzureTfConfigurationArgs{
ActivityLog: enableActivityLog,
Config: enableConfig,
CreateAdIntegration: createAdIntegration,
}
for _, m := range mods {
m(config)
}
return config
}
// WithConfigIntegrationName Set the Config Integration name to be displayed on the Lacework UI
func WithConfigIntegrationName(name string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.ConfigIntegrationName = name
}
}
// WithActivityLogIntegrationName Set the Activity Log Integration name to be displayed on the Lacework UI
func WithActivityLogIntegrationName(name string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.ActivityLogIntegrationName = name
}
}
// WithAdApplicationId Set Active Directory application id
func WithAdApplicationId(AdApplicationId string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.AdApplicationId = AdApplicationId
}
}
// WithAdApplicationPassword Set the Active Directory password
func WithAdApplicationPassword(AdApplicationPassword string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.AdApplicationPassword = AdApplicationPassword
}
}
// WithAdServicePrincipalId Set Active Directory principal id
func WithAdServicePrincipalId(AdServicePrincipalId string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.AdServicePrincipalId = AdServicePrincipalId
}
}
// WithManagementGroup Enable the Management Group to allow AD to be reader on management group
// rather then subscription
func WithManagementGroup(enableManagentGroup bool) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.ManagementGroup = enableManagentGroup
}
}
// WithManagementGroupId The Group Id to add reader permissions
func WithManagementGroupId(managementGroupId string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.ManagementGroupId = managementGroupId
}
}
// WithSubscriptionIds List of subscriptions to to enable logging
func WithSubscriptionIds(subscriptionIds []string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.SubscriptionIds = subscriptionIds
}
}
// WithAllSubscriptions Grant read access to ALL subscriptions within
// the selected Tenant (overrides 'subscription_ids')
func WithAllSubscriptions(allSubscriptions bool) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.AllSubscriptions = allSubscriptions
}
}
// WithExistingStorageAccount Use an existing Storage Account
func WithExistingStorageAccount(existingStorageAccount bool) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.ExistingStorageAccount = existingStorageAccount
}
}
// WithStorageAccountName The name of the Storage Account
func WithStorageAccountName(storageAccountName string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.StorageAccountName = storageAccountName
}
}
// WithStorageAccountResourceGroup The Resource Group for the existing Storage Account
func WithStorageAccountResourceGroup(storageAccountResourceGroup string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.StorageAccountResourceGroup = storageAccountResourceGroup
}
}
// WithStorageLocation The Azure region where storage account for logging is
func WithStorageLocation(location string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.StorageLocation = location
}
}
func WithLaceworkProfile(name string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.LaceworkProfile = name
}
}
func WithSubscriptionID(subcriptionID string) AzureTerraformModifier {
return func(c *GenerateAzureTfConfigurationArgs) {
c.SubscriptionID = subcriptionID
}
}
// Generate new Terraform code based on the supplied args.
func (args *GenerateAzureTfConfigurationArgs) Generate() (string, error) {
// Validate inputs
if err := args.validate(); err != nil {
return "", errors.Wrap(err, "invalid inputs")
}
// Create blocks
requiredProviders, err := createRequiredProviders()
if err != nil {
return "", errors.Wrap(err, "failed to generate required providers")
}
laceworkProvider, err := createLaceworkProvider(args)
if err != nil {
return "", errors.Wrap(err, "failed to generate lacework provider")
}
azureADProvider, err := createAzureADProvider()
if err != nil {
return "", errors.Wrap(err, "failed to generate AD provider")
}
azureRMProvider, err := createAzureRMProvider(args.SubscriptionID)
if err != nil {
return "", errors.Wrap(err, "failed to generate AM provider")
}
laceworkADProvider, err := createLaceworkAzureADModule(args)
if err != nil {
return "", errors.Wrap(err, "failed to generate lacework Azure AD provider")
}
configModule, err := createConfig(args)
if err != nil {
return "", errors.Wrap(err, "failed to generate azure config module")
}
activityLogModule, err := createActivityLog(args)
if err != nil {
return "", errors.Wrap(err, "failed to generate azure activity log module")
}
// Render
hclBlocks := lwgenerate.CreateHclStringOutput(
lwgenerate.CombineHclBlocks(
requiredProviders,
laceworkProvider,
azureADProvider,
azureRMProvider,
laceworkADProvider,
configModule,
activityLogModule),
)
return hclBlocks, nil
}
func createRequiredProviders() (*hclwrite.Block, error) {
return lwgenerate.CreateRequiredProviders(
lwgenerate.NewRequiredProvider(
"lacework",
lwgenerate.HclRequiredProviderWithSource(lwgenerate.LaceworkProviderSource),
lwgenerate.HclRequiredProviderWithVersion(lwgenerate.LaceworkProviderVersion),
),
)
}
func createLaceworkProvider(args *GenerateAzureTfConfigurationArgs) (*hclwrite.Block, error) {
if args.LaceworkProfile != "" {
return lwgenerate.NewProvider(
"lacework",
lwgenerate.HclProviderWithAttributes(map[string]interface{}{"profile": args.LaceworkProfile}),
).ToBlock()
}
return nil, nil
}
func createAzureADProvider() ([]*hclwrite.Block, error) {
blocks := []*hclwrite.Block{}
//attrs := map[string]interface{}{}
provider, err := lwgenerate.NewProvider(
"azuread",
).ToBlock()
if err != nil {
return nil, err
}
blocks = append(blocks, provider)
return blocks, nil
}
// In this we need to create a provider block with a features
// configuration but with nothing set, this is as per the
// Azure examples and is of the format
//
// provider "azurerm" {
// features = {}
// }
func createAzureRMProvider(subscriptionID string) ([]*hclwrite.Block, error) {
blocks := []*hclwrite.Block{}
attrs := map[string]interface{}{}
featureAttrs := map[string]interface{}{}
if subscriptionID != "" {
attrs["subscription_id"] = subscriptionID
}
provider, err := lwgenerate.NewProvider(
"azurerm",
lwgenerate.HclProviderWithAttributes(attrs),
).ToBlock()
if err != nil {
return nil, err
}
// Create the features block
featuresBlock, err := lwgenerate.HclCreateGenericBlock("features", []string{}, featureAttrs)
provider.Body().AppendBlock(featuresBlock)
if err != nil {
return nil, err
}
blocks = append(blocks, provider)
return blocks, nil
}
func createLaceworkAzureADModule(args *GenerateAzureTfConfigurationArgs) ([]*hclwrite.Block, error) {
blocks := []*hclwrite.Block{}
if args.CreateAdIntegration {
provider, err := lwgenerate.NewModule(
"az_ad_application",
lwgenerate.LWAzureADSource,
lwgenerate.HclModuleWithVersion(lwgenerate.LWAzureADVersion),
).ToBlock()
if err != nil {
return nil, err
}
blocks = append(blocks, provider)
}
return blocks, nil
}
func createConfig(args *GenerateAzureTfConfigurationArgs) ([]*hclwrite.Block, error) {
blocks := []*hclwrite.Block{}
if args.Config {
attributes := map[string]interface{}{}
moduleDetails := []lwgenerate.HclModuleModifier{}
if args.ConfigIntegrationName != "" {
attributes["lacework_integration_name"] = args.ConfigIntegrationName
}
// Check if we have created an Active Directory app
if args.CreateAdIntegration {
attributes["use_existing_ad_application"] = true
attributes["application_id"] = lwgenerate.CreateSimpleTraversal(
[]string{"module", "az_ad_application", "application_id"})
attributes["application_password"] = lwgenerate.CreateSimpleTraversal(
[]string{"module", "az_ad_application", "application_password"})
attributes["service_principal_id"] = lwgenerate.CreateSimpleTraversal(
[]string{"module", "az_ad_application", "service_principal_id"})
} else {
attributes["use_existing_ad_application"] = true
attributes["application_id"] = args.AdApplicationId
attributes["application_password"] = args.AdApplicationPassword
attributes["service_principal_id"] = args.AdServicePrincipalId
}
// Only set subscription ids if all subscriptions flag is not set
if !args.AllSubscriptions {
if len(args.SubscriptionIds) > 0 {
attributes["subscription_ids"] = args.SubscriptionIds
}
} else {
// Set Subscription information
attributes["all_subscriptions"] = args.AllSubscriptions
}
// Set Management Group details
if args.ManagementGroup {
attributes["use_management_group"] = args.ManagementGroup
attributes["management_group_id"] = args.ManagementGroupId
}
moduleDetails = append(moduleDetails,
lwgenerate.HclModuleWithAttributes(attributes),
)
moduleBlock, err := lwgenerate.NewModule(
"az_config",
lwgenerate.LWAzureConfigSource,
append(moduleDetails, lwgenerate.HclModuleWithVersion(lwgenerate.LWAzureConfigVersion))...,
).ToBlock()
if err != nil {
return nil, err
}
blocks = append(blocks, moduleBlock)
}
return blocks, nil
}
func createActivityLog(args *GenerateAzureTfConfigurationArgs) ([]*hclwrite.Block, error) {
blocks := []*hclwrite.Block{}
if args.ActivityLog {
attributes := map[string]interface{}{}
moduleDetails := []lwgenerate.HclModuleModifier{}
if args.ActivityLogIntegrationName != "" {
attributes["lacework_integration_name"] = args.ActivityLogIntegrationName
}
// Check if we have created an Active Directory integration
if args.CreateAdIntegration {
attributes["use_existing_ad_application"] = true
attributes["application_id"] = lwgenerate.CreateSimpleTraversal(
[]string{"module", "az_ad_application", "application_id"})
attributes["application_password"] = lwgenerate.CreateSimpleTraversal(
[]string{"module", "az_ad_application", "application_password"})
attributes["service_principal_id"] = lwgenerate.CreateSimpleTraversal(
[]string{"module", "az_ad_application", "service_principal_id"})
} else {
attributes["use_existing_ad_application"] = true
attributes["application_id"] = args.AdApplicationId
attributes["application_password"] = args.AdApplicationPassword
attributes["service_principal_id"] = args.AdServicePrincipalId
}
// Only set subscription ids if all subscriptions flag is not set
if !args.AllSubscriptions {
if len(args.SubscriptionIds) > 0 {
attributes["subscription_ids"] = args.SubscriptionIds
}
} else {
// Set Subscription information
attributes["all_subscriptions"] = args.AllSubscriptions
}
// Set storage account name, if set
if args.StorageAccountName != "" {
attributes["storage_account_name"] = args.StorageAccountName
}
// Set storage info if existing storage flag is set
if args.ExistingStorageAccount {
attributes["use_existing_storage_account"] = args.ExistingStorageAccount
attributes["storage_account_resource_group"] = args.StorageAccountResourceGroup
}
// if a new storage account is being created (i.e., ExistingStorageAccount is false), enable infrastructure
// encryption
if !args.ExistingStorageAccount {
attributes["infrastructure_encryption_enabled"] = true
}
// Set the location if needed
if args.StorageLocation != "" {
attributes["location"] = args.StorageLocation
}
moduleDetails = append(moduleDetails,
lwgenerate.HclModuleWithAttributes(attributes),
)
moduleBlock, err := lwgenerate.NewModule(
"az_activity_log",
lwgenerate.LWAzureActivityLogSource,
append(moduleDetails, lwgenerate.HclModuleWithVersion(lwgenerate.LWAzureActivityLogVersion))...,
).ToBlock()
if err != nil {
return nil, err
}
blocks = append(blocks, moduleBlock)
}
return blocks, nil
}