-
Notifications
You must be signed in to change notification settings - Fork 199
feat(bigquery): support GCP workload identity #4607
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
Draft
jkaflik
wants to merge
3
commits into
main
Choose a base branch
from
kk/clp-962-bigquery-workload-identity
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,269 @@ | ||
| package connbigquery | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/url" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "cloud.google.com/go/auth/credentials" | ||
| "cloud.google.com/go/bigquery" | ||
| "cloud.google.com/go/compute/metadata" | ||
|
|
||
| "github.com/PeerDB-io/peerdb/flow/generated/protos" | ||
| ) | ||
|
|
||
| const ( | ||
| // BigQueryAuthTypeServiceAccount selects legacy service-account-key credentials. | ||
| BigQueryAuthTypeServiceAccount = "service_account" | ||
| // BigQueryAuthTypeServiceAccountWorkloadIdentity selects deployment-scoped workload identity credentials. | ||
| BigQueryAuthTypeServiceAccountWorkloadIdentity = "service_account_workload_identity" | ||
|
|
||
| workloadIdentityServiceAccountEnv = "PEERDB_GCP_WORKLOAD_IDENTITY_TARGET_SERVICE_ACCOUNT" | ||
| //nolint:gosec // Environment variable name, not a credential. | ||
| workloadIdentityTokenFileEnv = "PEERDB_GCP_WORKLOAD_IDENTITY_TOKEN_FILE" | ||
| workloadIdentityProjectIDEnv = "PEERDB_GCP_PROJECT_ID" | ||
| workloadIdentityClusterLocationEnv = "PEERDB_GCP_CLUSTER_LOCATION" | ||
| workloadIdentityClusterNameEnv = "PEERDB_GCP_CLUSTER_NAME" | ||
|
|
||
| //nolint:gosec // Fixed public Google authentication endpoints, not credentials. | ||
| googleSTSTokenURL = "https://sts.googleapis.com/v1/token" | ||
| //nolint:gosec // Fixed public Google authentication endpoints, not credentials. | ||
| googleIAMCredentialsURL = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/" | ||
| //nolint:gosec // OAuth subject-token type identifier, not a credential. | ||
| jwtSubjectTokenType = "urn:ietf:params:oauth:token-type:jwt" | ||
| ) | ||
|
|
||
| type bigQueryCredentialConfig struct { | ||
| credentialType credentials.CredType | ||
| clientProjectID string | ||
| credentialsJSON []byte | ||
| } | ||
|
|
||
| type workloadIdentityDeploymentConfig struct { | ||
| targetServiceAccount string | ||
| tokenFile string | ||
| projectID string | ||
| clusterLocation string | ||
| clusterName string | ||
| } | ||
|
|
||
| type workloadIdentityConfigSource struct { | ||
| lookupEnv func(string) (string, bool) | ||
| projectID func(context.Context) (string, error) | ||
| instanceAttributeValue func(context.Context, string) (string, error) | ||
| } | ||
|
|
||
| type externalAccountCredentials struct { | ||
| Type string `json:"type"` | ||
| Audience string `json:"audience"` | ||
| SubjectTokenType string `json:"subject_token_type"` | ||
| TokenURL string `json:"token_url"` | ||
| ServiceAccountImpersonationURL string `json:"service_account_impersonation_url"` | ||
| CredentialSource externalAccountCredentialSource `json:"credential_source"` | ||
| } | ||
|
|
||
| type externalAccountCredentialSource struct { | ||
| File string `json:"file"` | ||
| Format externalAccountCredentialSourceFormat `json:"format"` | ||
| } | ||
|
|
||
| type externalAccountCredentialSourceFormat struct { | ||
| Type string `json:"type"` | ||
| } | ||
|
|
||
| func resolveBigQueryResource(config *protos.BigqueryConfig) (string, string, error) { | ||
| datasetID := config.GetDatasetId() | ||
| projectID := config.GetProjectId() | ||
| projectPart, datasetPart, found := strings.Cut(datasetID, ".") | ||
| if found && strings.Contains(datasetPart, ".") { | ||
| return "", "", fmt.Errorf( | ||
| "invalid dataset ID: %s. Ensure that it is just a single string or string1.string2", | ||
| datasetID, | ||
| ) | ||
| } | ||
| if projectPart != "" && datasetPart != "" { | ||
| datasetID = datasetPart | ||
| projectID = projectPart | ||
| } | ||
| return projectID, datasetID, nil | ||
| } | ||
|
|
||
| func newBigQueryCredentialConfig( | ||
| ctx context.Context, | ||
| config *protos.BigqueryConfig, | ||
| resourceProjectID string, | ||
| ) (*bigQueryCredentialConfig, error) { | ||
| authType := config.GetAuthType() | ||
| if authType != BigQueryAuthTypeServiceAccount && authType != BigQueryAuthTypeServiceAccountWorkloadIdentity { | ||
| return nil, fmt.Errorf( | ||
| "unsupported BigQuery auth_type %q: expected %q or %q", | ||
| authType, | ||
| BigQueryAuthTypeServiceAccount, | ||
| BigQueryAuthTypeServiceAccountWorkloadIdentity, | ||
| ) | ||
| } | ||
|
jkaflik marked this conversation as resolved.
|
||
|
|
||
| if authType == BigQueryAuthTypeServiceAccount { | ||
| serviceAccount, err := NewBigQueryServiceAccount(config) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create BigQueryServiceAccount: %w", err) | ||
| } | ||
| serviceAccountJSON, err := json.Marshal(serviceAccount) //nolint:gosec // G117: credential struct marshaled for inline use | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal service account: %v", err) | ||
| } | ||
| return &bigQueryCredentialConfig{ | ||
| credentialsJSON: serviceAccountJSON, | ||
| credentialType: credentials.ServiceAccount, | ||
| clientProjectID: bigquery.DetectProjectID, | ||
| }, nil | ||
| } | ||
|
|
||
| if resourceProjectID == "" { | ||
| return nil, fmt.Errorf("BigQuery project ID must be set in the peer when workload identity is selected") | ||
| } | ||
|
|
||
| deploymentConfig, err := resolveWorkloadIdentityDeploymentConfig(ctx, workloadIdentityConfigSource{ | ||
| lookupEnv: os.LookupEnv, | ||
| projectID: metadata.ProjectIDWithContext, | ||
| instanceAttributeValue: metadata.InstanceAttributeValueWithContext, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| credentialsJSON, err := deploymentConfig.credentialsJSON() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &bigQueryCredentialConfig{ | ||
| credentialsJSON: credentialsJSON, | ||
| credentialType: credentials.ExternalAccount, | ||
| clientProjectID: resourceProjectID, | ||
| }, nil | ||
| } | ||
|
|
||
| func resolveWorkloadIdentityDeploymentConfig( | ||
| ctx context.Context, | ||
| source workloadIdentityConfigSource, | ||
| ) (*workloadIdentityDeploymentConfig, error) { | ||
| targetServiceAccount := envValue(source.lookupEnv, workloadIdentityServiceAccountEnv) | ||
| if targetServiceAccount == "" { | ||
| return nil, fmt.Errorf( | ||
| "BigQuery workload identity requires deployment environment variable %s", | ||
| workloadIdentityServiceAccountEnv, | ||
| ) | ||
| } | ||
| tokenFile := envValue(source.lookupEnv, workloadIdentityTokenFileEnv) | ||
| if tokenFile == "" { | ||
| return nil, fmt.Errorf( | ||
| "BigQuery workload identity requires deployment environment variable %s", | ||
| workloadIdentityTokenFileEnv, | ||
| ) | ||
| } | ||
|
|
||
| projectID, err := envOrMetadata( | ||
| ctx, | ||
| source.lookupEnv, | ||
| workloadIdentityProjectIDEnv, | ||
| source.projectID, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| clusterLocation, err := envOrMetadata( | ||
| ctx, | ||
| source.lookupEnv, | ||
| workloadIdentityClusterLocationEnv, | ||
| func(ctx context.Context) (string, error) { | ||
| return source.instanceAttributeValue(ctx, "cluster-location") | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| clusterName, err := envOrMetadata( | ||
| ctx, | ||
| source.lookupEnv, | ||
| workloadIdentityClusterNameEnv, | ||
| func(ctx context.Context) (string, error) { | ||
| return source.instanceAttributeValue(ctx, "cluster-name") | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &workloadIdentityDeploymentConfig{ | ||
| targetServiceAccount: targetServiceAccount, | ||
| tokenFile: tokenFile, | ||
| projectID: projectID, | ||
| clusterLocation: clusterLocation, | ||
| clusterName: clusterName, | ||
| }, nil | ||
| } | ||
|
|
||
| func envValue(lookupEnv func(string) (string, bool), name string) string { | ||
| value, _ := lookupEnv(name) | ||
| return strings.TrimSpace(value) | ||
| } | ||
|
|
||
| func envOrMetadata( | ||
| ctx context.Context, | ||
| lookupEnv func(string) (string, bool), | ||
| envName string, | ||
| metadataValue func(context.Context) (string, error), | ||
| ) (string, error) { | ||
| if value := envValue(lookupEnv, envName); value != "" { | ||
| return value, nil | ||
| } | ||
| value, err := metadataValue(ctx) | ||
| if err != nil { | ||
| return "", fmt.Errorf( | ||
| "BigQuery workload identity requires %s or the corresponding GKE metadata: %w", | ||
| envName, | ||
| err, | ||
| ) | ||
| } | ||
| if strings.TrimSpace(value) == "" { | ||
| return "", fmt.Errorf( | ||
| "BigQuery workload identity requires %s or non-empty corresponding GKE metadata", | ||
| envName, | ||
| ) | ||
| } | ||
| return strings.TrimSpace(value), nil | ||
| } | ||
|
|
||
| func (config *workloadIdentityDeploymentConfig) credentialsJSON() ([]byte, error) { | ||
| pool := config.projectID + ".svc.id.goog" | ||
| audience := fmt.Sprintf( | ||
| "identitynamespace:%s:https://container.googleapis.com/v1/projects/%s/locations/%s/clusters/%s", | ||
| pool, | ||
| config.projectID, | ||
| config.clusterLocation, | ||
| config.clusterName, | ||
| ) | ||
| credentialConfig := externalAccountCredentials{ | ||
| Type: "external_account", | ||
| Audience: audience, | ||
| SubjectTokenType: jwtSubjectTokenType, | ||
| TokenURL: googleSTSTokenURL, | ||
| ServiceAccountImpersonationURL: googleIAMCredentialsURL + | ||
| url.PathEscape(config.targetServiceAccount) + ":generateAccessToken", | ||
| CredentialSource: externalAccountCredentialSource{ | ||
| // The cloud auth file provider reopens this path for every token exchange, | ||
| // allowing Kubernetes to rotate the projected token in place. | ||
| File: config.tokenFile, | ||
| Format: externalAccountCredentialSourceFormat{ | ||
| Type: "text", | ||
| }, | ||
| }, | ||
| } | ||
| credentialsJSON, err := json.Marshal(credentialConfig) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal BigQuery workload identity credentials: %w", err) | ||
| } | ||
| return credentialsJSON, nil | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you explored GCE_METADATA_HOST? Callbacks are a bit hard on the brain