Last active
April 21, 2020 09:07
-
-
Save rahulrumalla/64b398cdaa9b8124b1126ae99875df85 to your computer and use it in GitHub Desktop.
Patterns to secure your GCP credentials file with your Go app
This file contains 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
package demo | |
import ( | |
"context" | |
"google.golang.org/api/option" | |
"cloud.google.com/go/storage" | |
) | |
// NewClient returns a new Storage client for Google Cloud Storage on GCP | |
func NewClient(ctx context.Context) (*Storage, error) { | |
client, err := storage.NewClient(ctx, option.WithCredentialsFile(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"))) | |
if err != nil { | |
return nil, err | |
} | |
return client, err | |
} |
This file contains 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
package demo | |
import ( | |
"context" | |
"google.golang.org/api/option" | |
"cloud.google.com/go/storage" | |
) | |
// NewClient returns a new Storage client for Google Cloud Storage on GCP | |
func NewClient(ctx context.Context) (*Storage, error) { | |
d, _ := base64.StdEncoding.DecodeString(os.Getenv("GCP_CREDS_JSON_BASE64")) | |
client, err := storage.NewClient(ctx, option.WithCredentialsFileJSON(d)) | |
if err != nil { | |
return nil, err | |
} | |
return client, err | |
} |
This file contains 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
{ | |
"type": "service_account", | |
"project_id": "mygcpproject-123456", | |
"private_key_id": "private_key_id", | |
"private_key": "-----BEGIN PRIVATE KEY-----\nprivate_key\n-----END PRIVATE KEY-----\n", | |
"client_email": "[email protected]", | |
"client_id": "404567770391234433658", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://oauth2.googleapis.com/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment