-
Notifications
You must be signed in to change notification settings - Fork 903
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
Support for GCP Service Account as JSON or Path in Default Application Credentials #953
Changes from 3 commits
ea8b3bb
c0dc484
07aea97
4ffb54c
17fb03f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,17 @@ package gcpkms //import "go.mozilla.org/sops/v3/gcpkms" | |
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"google.golang.org/api/option" | ||
"io/ioutil" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"go.mozilla.org/sops/v3/logging" | ||
|
||
"golang.org/x/net/context" | ||
"golang.org/x/oauth2/google" | ||
|
||
"github.com/sirupsen/logrus" | ||
"golang.org/x/net/context" | ||
cloudkms "google.golang.org/api/cloudkms/v1" | ||
) | ||
|
||
|
@@ -131,12 +132,13 @@ func (key MasterKey) createCloudKMSService() (*cloudkms.Service, error) { | |
} | ||
|
||
ctx := context.Background() | ||
client, err := google.DefaultClient(ctx, cloudkms.CloudPlatformScope) | ||
|
||
creds, err := getDefaultApplicationCredentials() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cloudkmsService, err := cloudkms.New(client) | ||
cloudkmsService, err := cloudkms.NewService(ctx, option.WithCredentialsJSON(creds)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -151,3 +153,19 @@ func (key MasterKey) ToMap() map[string]interface{} { | |
out["created_at"] = key.CreationDate.UTC().Format(time.RFC3339) | ||
return out | ||
} | ||
|
||
// getDefaultApplicationCredentials allows for passing GCP Service Account | ||
// Credentials as either a path to a file, or directly as an environment variable | ||
// in JSON format. | ||
func getDefaultApplicationCredentials() (token []byte, err error) { | ||
var defaultCredentials = os.Getenv("GOOGLE_CREDENTIALS") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (responding on behalf of Josh, because I'm also very interested to have this change released and used by the Terraform sops provider)) One of the goal of this pull request is to be able to read credentials directly out of an environment variable; This is not supported natively by the Google SDK (I can find the related issues in their repository, if needed), but other consumers may provide this, for example the Terraform Google provider. Being able to read the credentials directly out of an environment variable would open up the possibility to use sops in context where it's not possible to read the credentials otherwise. Hence, the deliberate use of this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes agreed, thank you @multani. Apologies, I misread your original comment. I've updated these changes to support using either |
||
|
||
if _, err := os.Stat(defaultCredentials); err == nil { | ||
if token, err = ioutil.ReadFile(defaultCredentials); err != nil { | ||
joshkaplinsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, err | ||
} | ||
} else { | ||
token = []byte(defaultCredentials) | ||
} | ||
return | ||
} |
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.
Why use this when https://pkg.go.dev/golang.org/x/oauth2/google#FindDefaultCredentials exists? Shouldn't we check if it returns an error first and then do this custom implementation?
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.
It would be worth:
GOOGLE_CREDENTIALS
is set, in which caseCredentialsFromJSON
can be called with its contentFindDefaultCredentials
and directly return its resultSo that we have the special use-case when
GOOGLE_CREDENTIALS
is set, otherwise sops fallback to the default behavior of the SDK, what do you think?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.
I agree, though I'd suggest that we reverse the order. It's good to stay as close to the common cloud provider SDK's as possible and this would ensure we don't break any backwards compatibility.
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.
I think @multani's suggestion already is backwards compatible. Given it is not a standard environment variable at the moment, there is some intent for it to be used.
A regular user would not have this set, while by changing it around, it would not be taken into account on GCP instances where the right environment default values are automatically inject at times. Which makes
FindDefaultCredentials
always work, andGOOGLE_CREDENTIALS
unreachable.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.
I agree with @multani's thoughts here. I'll get the the call to
FindDefaultCredentials
integrated. Seems like it would be better to support both casesGOOGLE_CREDENTIALS
andGOOGLE_APPLICATION_CREDENTIALS
to integrate with a wider set of implementations.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.
Ok sounds good.