Skip to content

Commit 0490a21

Browse files
Merge pull request #10624 from patrickdillon/gcp-creds-json
CORS-4423: GCP: Use WithCredentialsJSON when Possible
2 parents 7adbf7c + 40e300c commit 0490a21

4 files changed

Lines changed: 43 additions & 7 deletions

File tree

pkg/asset/installconfig/gcp/client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,11 @@ func (c *Client) GetNamespacedTagValue(ctx context.Context, tagNamespacedName st
720720
}
721721

722722
func (c *Client) getKeyManagementClient(ctx context.Context) (*kms.KeyManagementClient, error) {
723-
kmsClient, err := kms.NewKeyManagementClient(ctx, option.WithCredentials(c.ssn.Credentials))
723+
opts, err := CredentialOptions(c.ssn)
724+
if err != nil {
725+
return nil, fmt.Errorf("failed to get credential options: %w", err)
726+
}
727+
kmsClient, err := kms.NewKeyManagementClient(ctx, opts...)
724728
if err != nil {
725729
return nil, fmt.Errorf("failed to create kms key management client: %w", err)
726730
}

pkg/asset/installconfig/gcp/services.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,34 @@ func CreateEndpointOption(endpointName string, service ServiceNameGCP) option.Cl
5959
return option.WithEndpoint(endpoint)
6060
}
6161

62+
// CredentialOptions returns the client options for authenticating with GCP,
63+
// including universe domain support for Google Cloud Dedicated.
64+
// When credential JSON is available, it is passed via WithCredentialsJSON so
65+
// the client library can use self-signed JWTs for non-default universe
66+
// domains (where the OAuth2 token endpoint is unavailable). Falls back to
67+
// WithCredentials for metadata-based credentials that have no JSON.
68+
func CredentialOptions(ssn *Session) ([]option.ClientOption, error) {
69+
ud, err := ssn.Credentials.GetUniverseDomain()
70+
if err != nil {
71+
return nil, fmt.Errorf("failed to get universe domain: %w", err)
72+
}
73+
var opts []option.ClientOption
74+
if len(ssn.Credentials.JSON) > 0 {
75+
opts = append(opts, option.WithCredentialsJSON(ssn.Credentials.JSON))
76+
} else {
77+
opts = append(opts, option.WithCredentials(ssn.Credentials))
78+
}
79+
opts = append(opts, option.WithUniverseDomain(ud))
80+
return opts, nil
81+
}
82+
6283
// getOptions creates the options for use during service creation.
6384
func getOptions(ctx context.Context) ([]option.ClientOption, error) {
6485
ssn, err := GetSession(ctx)
6586
if err != nil {
6687
return nil, fmt.Errorf("failed to get session: %w", err)
6788
}
68-
69-
options := []option.ClientOption{
70-
option.WithCredentials(ssn.Credentials),
71-
}
72-
return options, nil
89+
return CredentialOptions(ssn)
7390
}
7491

7592
// GetComputeService creates the compute service. The service is created with credentials and any service

pkg/clusterapi/system.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,17 @@ func (c *system) Run(ctx context.Context) error { //nolint:gocyclo
311311
logrus.Infof("setting %q to %s for capg infrastructure controller", gAppCredEnvVar, v)
312312
}
313313

314+
// Google Cloud Dedicated support: detect universe domain from
315+
// credentials and pass it to the CAPG controller via env var.
316+
ud, err := session.Credentials.GetUniverseDomain()
317+
if err != nil {
318+
return fmt.Errorf("failed to get universe domain from gcp credentials: %w", err)
319+
}
320+
if ud != "googleapis.com" {
321+
capgEnvVars["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = ud
322+
logrus.Infof("setting GOOGLE_CLOUD_UNIVERSE_DOMAIN to %q for capg infrastructure controller", ud)
323+
}
324+
314325
controllers = append(controllers,
315326
c.getInfrastructureController(
316327
&GCP,

pkg/quota/gcp/gcp.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gcp
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"sort"
78
"strings"
@@ -35,7 +36,10 @@ func Load(ctx context.Context, project string, endpoint *gcptypes.PSCEndpoint, s
3536
if err != nil {
3637
return nil, errors.Wrap(err, "failed to create services svc")
3738
}
38-
metricsOptions := []option.ClientOption{option.WithCredentials(ssn.Credentials)}
39+
metricsOptions, err := gcpconfig.CredentialOptions(ssn)
40+
if err != nil {
41+
return nil, fmt.Errorf("failed to get credential options: %w", err)
42+
}
3943
metricsSvc, err := monitoring.NewMetricClient(ctx, metricsOptions...)
4044
if err != nil {
4145
return nil, errors.Wrap(err, "failed to create metrics svc")

0 commit comments

Comments
 (0)