From 2209fe8babc56a4448aff7b61ebd5772ec6b6ec2 Mon Sep 17 00:00:00 2001 From: Ashley Prosser Date: Fri, 12 May 2023 12:36:50 +0100 Subject: [PATCH 1/4] Support for Subscription ID in AzureRM URL --- README.md | 1 + tfstate/lookup.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2065eb6..2215a01 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ func main() { state, _ := tfstate.ReadURL(ctx, "s3://mybucket/terraform.tfstate") // state, _ := tfstate.ReadURL("remote://app.terraform.io/myorg/myworkspace") // state, _ := tfstate.ReadURL("azurerm://{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") + // state, _ := tfstate.ReadURL("azurerm://Psubscription_id}/{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") ``` ## LICENSE diff --git a/tfstate/lookup.go b/tfstate/lookup.go index 3b6963e..474c7d6 100644 --- a/tfstate/lookup.go +++ b/tfstate/lookup.go @@ -175,12 +175,20 @@ func ReadURL(ctx context.Context, loc string) (*TFState, error) { key := strings.TrimPrefix(u.Path, "/") src, err = readGCS(ctx, u.Host, key, "", os.Getenv("GOOGLE_ENCRYPTION_KEY")) case "azurerm": - split := strings.SplitN(u.Path, "/", 4) + split := (strings.SplitN(u.Path, "/", 5))[1:] + split = append([]string{u.Host}, split...) + if len(split) < 4 { err = fmt.Errorf("invalid azurerm url: %s", u.String()) break } - src, err = readAzureRM(ctx, u.Host, split[1], split[2], split[3], azureRMOption{}) + + // If the user has not specified the Subscription ID, use an empty string + if len(split) == 4 { + split = append([]string{""}, split...) + } + + src, err = readAzureRM(ctx, split[1], split[2], split[3], split[4], azureRMOption{subscriptionID: split[0]}) case "file": src, err = os.Open(u.Path) case "remote": From 0bd8be91d2b13f9c0c0ab56097866e1050a8025c Mon Sep 17 00:00:00 2001 From: Ashley Prosser Date: Mon, 15 May 2023 13:37:24 +0100 Subject: [PATCH 2/4] Fix a typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2215a01..9f78513 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ func main() { state, _ := tfstate.ReadURL(ctx, "s3://mybucket/terraform.tfstate") // state, _ := tfstate.ReadURL("remote://app.terraform.io/myorg/myworkspace") // state, _ := tfstate.ReadURL("azurerm://{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") - // state, _ := tfstate.ReadURL("azurerm://Psubscription_id}/{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") + // state, _ := tfstate.ReadURL("azurerm://{subscription_id}/{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") ``` ## LICENSE From 262f1f42e849d2aff3add3854ba322d6b9e9c547 Mon Sep 17 00:00:00 2001 From: Ashley Prosser Date: Fri, 19 May 2023 00:07:16 +0100 Subject: [PATCH 3/4] Much cleaner approach using the UserInfo field --- README.md | 2 +- tfstate/lookup.go | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9f78513..e7229c1 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ func main() { state, _ := tfstate.ReadURL(ctx, "s3://mybucket/terraform.tfstate") // state, _ := tfstate.ReadURL("remote://app.terraform.io/myorg/myworkspace") // state, _ := tfstate.ReadURL("azurerm://{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") - // state, _ := tfstate.ReadURL("azurerm://{subscription_id}/{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") + // state, _ := tfstate.ReadURL("azurerm://{subscription_id}@{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") ``` ## LICENSE diff --git a/tfstate/lookup.go b/tfstate/lookup.go index 474c7d6..0ff8836 100644 --- a/tfstate/lookup.go +++ b/tfstate/lookup.go @@ -175,20 +175,14 @@ func ReadURL(ctx context.Context, loc string) (*TFState, error) { key := strings.TrimPrefix(u.Path, "/") src, err = readGCS(ctx, u.Host, key, "", os.Getenv("GOOGLE_ENCRYPTION_KEY")) case "azurerm": - split := (strings.SplitN(u.Path, "/", 5))[1:] - split = append([]string{u.Host}, split...) + split := strings.SplitN(u.Path, "/", 4) if len(split) < 4 { err = fmt.Errorf("invalid azurerm url: %s", u.String()) break } - // If the user has not specified the Subscription ID, use an empty string - if len(split) == 4 { - split = append([]string{""}, split...) - } - - src, err = readAzureRM(ctx, split[1], split[2], split[3], split[4], azureRMOption{subscriptionID: split[0]}) + src, err = readAzureRM(ctx, u.Host, split[1], split[2], split[3], azureRMOption{subscriptionID: u.User.Username()}) case "file": src, err = os.Open(u.Path) case "remote": From 20530f5b6c0909e12a00817479e2c03936fbe3f2 Mon Sep 17 00:00:00 2001 From: ashpr Date: Fri, 12 May 2023 12:36:50 +0100 Subject: [PATCH 4/4] Support for Subscription ID in AzureRM URL --- README.md | 1 + tfstate/lookup.go | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2065eb6..e7229c1 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ func main() { state, _ := tfstate.ReadURL(ctx, "s3://mybucket/terraform.tfstate") // state, _ := tfstate.ReadURL("remote://app.terraform.io/myorg/myworkspace") // state, _ := tfstate.ReadURL("azurerm://{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") + // state, _ := tfstate.ReadURL("azurerm://{subscription_id}@{resource_group_name}/{storage_account_name}/{container_name}/{blob_name}") ``` ## LICENSE diff --git a/tfstate/lookup.go b/tfstate/lookup.go index 3b6963e..0ff8836 100644 --- a/tfstate/lookup.go +++ b/tfstate/lookup.go @@ -176,11 +176,13 @@ func ReadURL(ctx context.Context, loc string) (*TFState, error) { src, err = readGCS(ctx, u.Host, key, "", os.Getenv("GOOGLE_ENCRYPTION_KEY")) case "azurerm": split := strings.SplitN(u.Path, "/", 4) + if len(split) < 4 { err = fmt.Errorf("invalid azurerm url: %s", u.String()) break } - src, err = readAzureRM(ctx, u.Host, split[1], split[2], split[3], azureRMOption{}) + + src, err = readAzureRM(ctx, u.Host, split[1], split[2], split[3], azureRMOption{subscriptionID: u.User.Username()}) case "file": src, err = os.Open(u.Path) case "remote":