Skip to content

Commit aad329a

Browse files
authored
Merge pull request #28 from PaloAltoNetworks/FWAAS-12453
FWAAS-12453: Account onboarding support with AWS profile
2 parents 0723c2a + 714b7a4 commit aad329a

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/aws/aws-sdk-go-v2/service/sts v1.28.12
88
github.com/hashicorp/terraform-plugin-log v0.2.1
99
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
10-
github.com/paloaltonetworks/cloud-ngfw-aws-go v1.0.6
10+
github.com/paloaltonetworks/cloud-ngfw-aws-go v1.0.7
1111
go.uber.org/zap v1.25.0
1212
gopkg.in/natefinch/lumberjack.v2 v2.2.1
1313
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k
318318
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
319319
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
320320
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
321-
github.com/paloaltonetworks/cloud-ngfw-aws-go v1.0.6 h1:YwhSlFbXHwDC0fxqSMiG0gG032PJH5FqDhgQTVanAfo=
322-
github.com/paloaltonetworks/cloud-ngfw-aws-go v1.0.6/go.mod h1:K7l99F0euqi1IXc20j0AXKBVElI77RGD5U5+LU8FYVQ=
321+
github.com/paloaltonetworks/cloud-ngfw-aws-go v1.0.7 h1:f91TS1cjyec7SKVsFOPSOr1eZHC4ZyuE+xAIoMhogPY=
322+
github.com/paloaltonetworks/cloud-ngfw-aws-go v1.0.7/go.mod h1:K7l99F0euqi1IXc20j0AXKBVElI77RGD5U5+LU8FYVQ=
323323
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
324324
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
325325
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

internal/provider/account_onboarding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/hashicorp/terraform-plugin-log/tflog"
89
"github.com/paloaltonetworks/cloud-ngfw-aws-go/api"
910
"github.com/paloaltonetworks/cloud-ngfw-aws-go/api/account"
10-
"github.com/hashicorp/terraform-plugin-log/tflog"
1111

1212
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

internal/provider/account_onboarding_stack.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,19 @@ type accountOnboardingStackInput struct {
4040
onboardingCft string
4141
stackId string
4242
region string
43+
profile string
4344
}
4445

4546
// CloudFormationClient returns a AWS cloudformation client by assuming the CFT role in the specified account.
46-
func CloudFormationClient(ctx context.Context, accountId string, cftRoleName string, region string) (*cloudformation.Client, error) {
47+
func CloudFormationClient(ctx context.Context, accountId, cftRoleName, region, profile string) (*cloudformation.Client, error) {
4748
cftRoleArn := fmt.Sprintf("arn:aws:iam::%s:role/%s", accountId, cftRoleName)
48-
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
49+
options := []func(*config.LoadOptions) error{
50+
config.WithRegion(region),
51+
}
52+
if profile != "" {
53+
options = append(options, config.WithSharedConfigProfile(profile))
54+
}
55+
cfg, err := config.LoadDefaultConfig(ctx, options...)
4956
if err != nil {
5057
tflog.Info(ctx, "error: %s", err)
5158
return nil, err
@@ -95,7 +102,7 @@ func FindStackByName(ctx context.Context, name string, nextToken *string,
95102
}
96103

97104
func CreateAccountOnboardingStack(ctx context.Context, input accountOnboardingStackInput) (string, error) {
98-
cfrClient, err := CloudFormationClient(ctx, input.accountId, input.cftRoleName, input.region)
105+
cfrClient, err := CloudFormationClient(ctx, input.accountId, input.cftRoleName, input.region, input.profile)
99106
if err != nil {
100107
tflog.Info(ctx, "error: %s", err)
101108
return "", err
@@ -232,7 +239,7 @@ func WaitForStackDeletion(ctx context.Context, svc *cloudformation.Client, stack
232239
}
233240

234241
func DeleteStack(ctx context.Context, input accountOnboardingStackInput) error {
235-
cfrClient, err := CloudFormationClient(ctx, input.accountId, input.cftRoleName, input.region)
242+
cfrClient, err := CloudFormationClient(ctx, input.accountId, input.cftRoleName, input.region, input.profile)
236243
if err != nil {
237244
tflog.Info(ctx, "error: %s", err)
238245
return err
@@ -254,7 +261,7 @@ func DeleteStack(ctx context.Context, input accountOnboardingStackInput) error {
254261
}
255262

256263
func ReadStack(ctx context.Context, input accountOnboardingStackInput) (string, error) {
257-
cfrClient, err := CloudFormationClient(ctx, input.accountId, input.cftRoleName, input.region)
264+
cfrClient, err := CloudFormationClient(ctx, input.accountId, input.cftRoleName, input.region, input.profile)
258265
if err != nil {
259266
tflog.Info(ctx, "error: %s", err)
260267
return "", err
@@ -293,6 +300,7 @@ func resourceAccountOnboardingStack() *schema.Resource {
293300
func createAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
294301
svc := meta.(*api.ApiClient)
295302
mpRegion := svc.GetMPRegion(ctx)
303+
profile := svc.GetProfile(ctx)
296304
accountId := d.Get("account_id").(string)
297305
stackInput := accountOnboardingStackInput{
298306
auditLogGroup: d.Get("auditlog_group").(string),
@@ -309,6 +317,7 @@ func createAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, m
309317
snsTopicArn: d.Get("sns_topic_arn").(string),
310318
accountId: accountId,
311319
region: mpRegion,
320+
profile: profile,
312321
}
313322
stackId, err := CreateAccountOnboardingStack(ctx, stackInput)
314323
if err != nil {
@@ -323,12 +332,14 @@ func createAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, m
323332
func deleteAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
324333
svc := meta.(*api.ApiClient)
325334
mpRegion := svc.GetMPRegion(ctx)
335+
profile := svc.GetProfile(ctx)
326336
accountId := d.Get("account_id").(string)
327337
stackInput := accountOnboardingStackInput{
328338
cftRoleName: d.Get("cft_role_name").(string),
329339
stackId: d.Get("stack_id").(string),
330340
accountId: accountId,
331341
region: mpRegion,
342+
profile: profile,
332343
}
333344
err := DeleteStack(ctx, stackInput)
334345
if err != nil {
@@ -341,13 +352,15 @@ func deleteAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, m
341352
func readAccountOnboardingStack(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
342353
svc := meta.(*api.ApiClient)
343354
mpRegion := svc.GetMPRegion(ctx)
355+
profile := svc.GetProfile(ctx)
344356
accountId := d.Get("account_id").(string)
345357
stackId := d.Get("stack_id").(string)
346358
stackInput := accountOnboardingStackInput{
347359
cftRoleName: d.Get("cft_role_name").(string),
348360
stackId: stackId,
349361
accountId: accountId,
350362
region: mpRegion,
363+
profile: profile,
351364
}
352365
stackStatus, err := ReadStack(ctx, stackInput)
353366
if err != nil {

internal/provider/provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"strings"
77
"time"
88

9-
ngfw "github.com/paloaltonetworks/cloud-ngfw-aws-go"
10-
"github.com/paloaltonetworks/cloud-ngfw-aws-go/api"
11-
"github.com/paloaltonetworks/cloud-ngfw-aws-go/ngfw/aws"
129
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1310
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
1411
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1512
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+
ngfw "github.com/paloaltonetworks/cloud-ngfw-aws-go"
14+
"github.com/paloaltonetworks/cloud-ngfw-aws-go/api"
15+
"github.com/paloaltonetworks/cloud-ngfw-aws-go/ngfw/aws"
1616
)
1717

1818
var resourceTimeout = 120 * time.Minute

internal/provider/security_rule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"strconv"
77
"strings"
88

9-
"github.com/paloaltonetworks/cloud-ngfw-aws-go/api"
10-
"github.com/paloaltonetworks/cloud-ngfw-aws-go/api/security"
119
"github.com/hashicorp/terraform-plugin-log/tflog"
1210
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1311
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1412
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+
"github.com/paloaltonetworks/cloud-ngfw-aws-go/api"
14+
"github.com/paloaltonetworks/cloud-ngfw-aws-go/api/security"
1515
)
1616

1717
// Data source.

0 commit comments

Comments
 (0)