-
-
Notifications
You must be signed in to change notification settings - Fork 133
Support !terraform.state
on GCS Backends
#1393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shirkevich
wants to merge
11
commits into
cloudposse:main
Choose a base branch
from
shirkevich:terraform-state-gcs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
83abcfc
feat: implement GCS backend support for YAML function
shirkevich fa0383c
feat: improve GCS backend with performance optimizations and unified …
shirkevich 6fb4da5
fix: address CodeRabbitAI feedback for GCS backend implementation
shirkevich 99028cc
docs: fix misleading GCS service account impersonation claims
shirkevich 691cf54
[autofix.ci] apply automated fixes
autofix-ci[bot] bb95182
Merge branch 'main' into terraform-state-gcs
aknysh 0fca9f7
Merge branch 'main' into terraform-state-gcs
aknysh 24f17e1
Merge branch 'main' into terraform-state-gcs
aknysh 50dcd4a
Merge branch 'main' into terraform-state-gcs
aknysh 69482f2
Merge branch 'main' into terraform-state-gcs
osterman f7ad22f
Use constants instead of strings for backend type
shirkevich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package gcp | ||
|
||
import ( | ||
"strings" | ||
|
||
"google.golang.org/api/option" | ||
) | ||
|
||
// AuthOptions contains configuration for Google Cloud authentication. | ||
type AuthOptions struct { | ||
// Credentials can be either: | ||
// - JSON content (if starts with "{") | ||
// - File path to service account JSON file | ||
// - Empty string to use Application Default Credentials (ADC) | ||
Credentials string | ||
|
||
// TODO: Add support for service account impersonation | ||
// ImpersonateServiceAccount string | ||
} | ||
|
||
// GetClientOptions returns Google Cloud client options based on the provided authentication configuration. | ||
// This function provides unified authentication handling across all Google Cloud services in Atmos. | ||
// | ||
// Authentication precedence: | ||
// 1. Explicit credentials (JSON content or file path) | ||
// 2. Application Default Credentials (ADC) which automatically handles: | ||
// - GOOGLE_APPLICATION_CREDENTIALS environment variable | ||
// - Compute Engine metadata service | ||
// - Cloud Shell credentials | ||
// - gcloud user credentials (from `gcloud auth application-default login`) | ||
// - Workload Identity (in GKE) | ||
func GetClientOptions(opts AuthOptions) []option.ClientOption { | ||
var clientOpts []option.ClientOption | ||
|
||
if opts.Credentials != "" { | ||
// Determine if credentials are JSON content or file path | ||
if strings.HasPrefix(strings.TrimSpace(opts.Credentials), "{") { | ||
// JSON content | ||
clientOpts = append(clientOpts, option.WithCredentialsJSON([]byte(opts.Credentials))) | ||
} else { | ||
// File path | ||
clientOpts = append(clientOpts, option.WithCredentialsFile(opts.Credentials)) | ||
} | ||
} | ||
// If no explicit credentials, Google Cloud client libraries will automatically use ADC | ||
|
||
return clientOpts | ||
} | ||
|
||
// GetCredentialsFromBackend extracts credentials from a Terraform backend configuration. | ||
// This is used by the GCS Terraform backend. | ||
func GetCredentialsFromBackend(backend map[string]any) string { | ||
if credentials, ok := backend["credentials"].(string); ok { | ||
return credentials | ||
} | ||
return "" | ||
} | ||
|
||
// GetCredentialsFromStore extracts credentials from a store configuration. | ||
// This is used by the Google Secret Manager store. | ||
func GetCredentialsFromStore(credentials *string) string { | ||
if credentials != nil { | ||
return *credentials | ||
} | ||
return "" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package gcp | ||
|
||
import ( | ||
"testing" | ||
|
||
"google.golang.org/api/option" | ||
) | ||
|
||
func TestGetClientOptions(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
opts AuthOptions | ||
expected int // number of client options returned | ||
}{ | ||
{ | ||
name: "no credentials - use ADC", | ||
opts: AuthOptions{ | ||
Credentials: "", | ||
}, | ||
expected: 0, // ADC uses no explicit options | ||
}, | ||
{ | ||
name: "JSON credentials", | ||
opts: AuthOptions{ | ||
Credentials: `{"type": "service_account", "project_id": "test"}`, | ||
}, | ||
expected: 1, // WithCredentialsJSON | ||
}, | ||
{ | ||
name: "file path credentials", | ||
opts: AuthOptions{ | ||
Credentials: "/path/to/service-account.json", | ||
}, | ||
expected: 1, // WithCredentialsFile | ||
}, | ||
{ | ||
name: "JSON with whitespace", | ||
opts: AuthOptions{ | ||
Credentials: ` {"type": "service_account"} `, | ||
}, | ||
expected: 1, // WithCredentialsJSON | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
clientOpts := GetClientOptions(tt.opts) | ||
if len(clientOpts) != tt.expected { | ||
t.Errorf("GetClientOptions() returned %d options, expected %d", len(clientOpts), tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetCredentialsFromBackend(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
backend map[string]any | ||
expected string | ||
}{ | ||
{ | ||
name: "credentials present", | ||
backend: map[string]any{ | ||
"credentials": "/path/to/creds.json", | ||
"bucket": "test-bucket", | ||
}, | ||
expected: "/path/to/creds.json", | ||
}, | ||
{ | ||
name: "JSON credentials", | ||
backend: map[string]any{ | ||
"credentials": `{"type": "service_account"}`, | ||
}, | ||
expected: `{"type": "service_account"}`, | ||
}, | ||
{ | ||
name: "no credentials", | ||
backend: map[string]any{ | ||
"bucket": "test-bucket", | ||
}, | ||
expected: "", | ||
}, | ||
{ | ||
name: "empty backend", | ||
backend: map[string]any{}, | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := GetCredentialsFromBackend(tt.backend) | ||
if result != tt.expected { | ||
t.Errorf("GetCredentialsFromBackend() = %v, expected %v", result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetCredentialsFromStore(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
credentials *string | ||
expected string | ||
}{ | ||
{ | ||
name: "credentials present", | ||
credentials: stringPtr("/path/to/creds.json"), | ||
expected: "/path/to/creds.json", | ||
}, | ||
{ | ||
name: "JSON credentials", | ||
credentials: stringPtr(`{"type": "service_account"}`), | ||
expected: `{"type": "service_account"}`, | ||
}, | ||
{ | ||
name: "empty credentials", | ||
credentials: stringPtr(""), | ||
expected: "", | ||
}, | ||
{ | ||
name: "nil credentials", | ||
credentials: nil, | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := GetCredentialsFromStore(tt.credentials) | ||
if result != tt.expected { | ||
t.Errorf("GetCredentialsFromStore() = %v, expected %v", result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestClientOptionsCreation(t *testing.T) { | ||
// Test that we can actually create valid client options without errors | ||
tests := []struct { | ||
name string | ||
credentials string | ||
expectType string | ||
}{ | ||
{ | ||
name: "JSON credentials create WithCredentialsJSON option", | ||
credentials: `{"type": "service_account", "project_id": "test"}`, | ||
expectType: "JSON", | ||
}, | ||
{ | ||
name: "file path creates WithCredentialsFile option", | ||
credentials: "/path/to/service-account.json", | ||
expectType: "File", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
opts := GetClientOptions(AuthOptions{ | ||
Credentials: tt.credentials, | ||
}) | ||
|
||
// We can't easily inspect the actual option type without reflection, | ||
// but we can verify that an option was created | ||
if len(opts) != 1 { | ||
t.Errorf("Expected 1 client option, got %d", len(opts)) | ||
} | ||
|
||
// Verify the option is of type option.ClientOption | ||
var _ option.ClientOption = opts[0] | ||
}) | ||
} | ||
} | ||
|
||
// Helper function to create string pointers for tests | ||
func stringPtr(s string) *string { | ||
return &s | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / golangci-lint
Comment should end in a period Error test