Skip to content

Commit

Permalink
Oppdater til bruk av google_oauth_access_token
Browse files Browse the repository at this point in the history
  • Loading branch information
marensofier committed Aug 28, 2024
1 parent bff0d52 commit 5ef4c8e
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 182 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

PROJECT := github.com/getsops/sops/v3
PROJECT := github.com/bekk/sops
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
BIN_DIR := $(PROJECT_DIR)/bin

GO := GOPROXY=https://proxy.golang.org go
GO_TEST_FLAGS ?= -race -coverprofile=profile.out -covermode=atomic

GITHUB_REPOSITORY ?= github.com/getsops/sops
GITHUB_REPOSITORY ?= github.com/bekk/sops

STATICCHECK := $(BIN_DIR)/staticcheck
STATICCHECK_VERSION := latest
Expand All @@ -33,7 +33,7 @@ origin-build: test vet generate install functional-tests-all

.PHONY: install
install:
$(GO) install github.com/getsops/sops/v3/cmd/sops
$(GO) install github.com/bekk/sops/cmd/sops

.PHONY: staticcheck
staticcheck: install-staticcheck
Expand Down Expand Up @@ -82,12 +82,12 @@ generate: keyservice/keyservice.pb.go

.PHONY: functional-tests
functional-tests:
$(GO) build -o functional-tests/sops github.com/getsops/sops/v3/cmd/sops
$(GO) build -o functional-tests/sops github.com/bekk/sops/cmd/sops
cd functional-tests && cargo test

.PHONY: functional-tests-all
functional-tests-all:
$(GO) build -o functional-tests/sops github.com/getsops/sops/v3/cmd/sops
$(GO) build -o functional-tests/sops github.com/bekk/sops/cmd/sops
# Ignored tests are ones that require external services (e.g. AWS KMS)
# TODO: Once `--include-ignored` lands in rust stable, switch to that.
cd functional-tests && cargo test && cargo test -- --ignored
Expand Down
18 changes: 9 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ Encrypting with SSH keys via age is not yet supported by SOPS.
Encrypting using GCP KMS
~~~~~~~~~~~~~~~~~~~~~~~~
GCP KMS has support for authorization with the use of `Application Default Credentials
<https://developers.google.com/identity/protocols/application-default-credentials>`_ and access tokens.
<https://developers.google.com/identity/protocols/application-default-credentials>`_ and using an oauth2 token.
Application default credentials precedes the use of access token.


Using Application Default Credentials you can authorize by doing this:

If you already logged in using
Expand All @@ -240,19 +239,15 @@ you can enable application default credentials using the sdk:
$ gcloud auth application-default login
Using oauth tokens you can authorize by doing this:

.. code:: sh
$ export GOOGLE_OAUTH_ACCESS_TOKEN=<your access token>
$ export CLOUDSDK_AUTH_ACCESS_TOKEN=<your access token>
If you are already logged in
Or if you are logged in you can authorize by generating an access token:

.. code:: sh
$ export CLOUDSDK_AUTH_ACCESS_TOKEN=$(gcloud auth print-access-token)
$ export GOOGLE_OAUTH_ACCESS_TOKEN="$(gcloud auth print-access-token)"
Encrypting/decrypting with GCP KMS requires a KMS ResourceID. You can use the
Expand Down Expand Up @@ -690,6 +685,11 @@ of all new files. If your secrets are stored under a specific directory, like a
``git`` repository, you can create a ``.sops.yaml`` configuration file at the root
directory to define which keys are used for which filename.
.. note::
The file needs to be named ``.sops.yaml``. Other names (i.e. ``.sops.yml``) won't be automatically
discovered by SOPS. You'll need to pass the ``--config .sops.yml`` option for it to be picked up.
Let's take an example:
* file named **something.dev.yaml** should use one set of KMS A, PGP and age
Expand Down
43 changes: 23 additions & 20 deletions gcpkms/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ const (
// SopsGoogleCredentialsEnv can be set as an environment variable as either
// a path to a credentials file, or directly as the variable's value in JSON
// format.
SopsGoogleCredentialsEnv = "GOOGLE_CREDENTIALS"
SopsGoogleCredentialsAccessToken = "CLOUDSDK_AUTH_ACCESS_TOKEN"
SopsGoogleCredentialsEnv = "GOOGLE_CREDENTIALS"
// SopsGoogleCredentialsOAuthToken can be set as an environment variable as either
// a path to a file, or directly as the varialbe's value
SopsGoogleCredentialsOAuthToken = "GOOGLE_OAUTH_ACCESS_TOKEN"
// KeyTypeIdentifier is the string used to identify a GCP KMS MasterKey.
KeyTypeIdentifier = "gcp_kms"
)
Expand Down Expand Up @@ -221,28 +223,30 @@ func (key *MasterKey) newKMSClient() (*kms.KeyManagementClient, error) {
case key.credentialJSON != nil:
opts = append(opts, option.WithCredentialsJSON(key.credentialJSON))
default:
credentials, err := getGoogleCredentials()
credentials, err_credentials_file := getGoogleCredentials()
if credentials != nil {
opts = append(opts, option.WithCredentialsJSON(credentials))
break
}

at_credentials, at_err := getGoogleAccessToken()
at_credentials, err_credentials_token := getGoogleOAuthToken()
if at_credentials != nil {
opts = append(opts, option.WithTokenSource(at_credentials))
}

if err != nil && at_err != nil {
return nil, err
if err_credentials_file != nil && err_credentials_token != nil {
return nil, fmt.Errorf("credentials: failed to get credentials for gcp kms, add default credentials or oauth access token")
}
}

if key.grpcConn != nil {
opts = append(opts, option.WithGRPCConn(key.grpcConn))
}

ctx := context.Background()
client, err := kms.NewKeyManagementClient(ctx, opts...)
if err != nil {
return nil, err
client, err_credentials := kms.NewKeyManagementClient(ctx, opts...)
if err_credentials != nil {
return nil, err_credentials
}

return client, nil
Expand All @@ -257,24 +261,23 @@ func getGoogleCredentials() ([]byte, error) {
if _, err := os.Stat(defaultCredentials); err == nil {
return os.ReadFile(defaultCredentials)
}

return []byte(defaultCredentials), nil
}
return nil, nil
return nil, fmt.Errorf("could not find Google credential file")
}

func getGoogleAccessToken() (oauth2.TokenSource, error) {
if envToken, isSet := os.LookupEnv(SopsGoogleCredentialsAccessToken); isSet {
token := []byte(envToken)
if _, err := os.Stat(envToken); err == nil {
if token, err = os.ReadFile(envToken); err != nil {
return nil, err
}
}
// getGoogleOAuthToken returns the SopsGoogleCredentialsOauthToken variable,
// as the oauth token.
// It returns an error and nil if the envrionment variable is not set.
func getGoogleOAuthToken() (oauth2.TokenSource, error) {
if token, isSet := os.LookupEnv(SopsGoogleCredentialsOAuthToken); isSet {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: string(token)},
&oauth2.Token{AccessToken: token},
)

return tokenSource, nil
}

return nil, nil
return nil, fmt.Errorf("could not find Google OAuth token")
}
43 changes: 37 additions & 6 deletions gcpkms/keysource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func TestMasterKey_Encrypt(t *testing.T) {
})

key := MasterKey{
grpcConn: newGRPCServer("0"),
ResourceID: testResourceID,
grpcConn: newGRPCServer("0"),
ResourceID: testResourceID,
credentialJSON: []byte("arbitrary credentials"),
}
err := key.Encrypt([]byte("encrypt"))
assert.NoError(t, err)
Expand All @@ -80,9 +81,10 @@ func TestMasterKey_Decrypt(t *testing.T) {
Plaintext: []byte(decryptedData),
})
key := MasterKey{
grpcConn: newGRPCServer("0"),
ResourceID: testResourceID,
EncryptedKey: "encryptedKey",
grpcConn: newGRPCServer("0"),
ResourceID: testResourceID,
EncryptedKey: "encryptedKey",
credentialJSON: []byte("arbitrary credentials"),
}
data, err := key.Decrypt()
assert.NoError(t, err)
Expand Down Expand Up @@ -116,7 +118,7 @@ func TestMasterKey_ToMap(t *testing.T) {
}, key.ToMap())
}

func TestMasterKey_createCloudKMSService(t *testing.T) {
func TestMasterKey_createCloudKMSService_withCredentialsFile(t *testing.T) {
tests := []struct {
key MasterKey
errString string
Expand All @@ -136,6 +138,12 @@ func TestMasterKey_createCloudKMSService(t *testing.T) {
"type": "authorized_user"}`),
},
},
{
key: MasterKey{
ResourceID: testResourceID,
},
errString: "credentials: failed to get credentials",
},
}

for _, tt := range tests {
Expand All @@ -149,6 +157,29 @@ func TestMasterKey_createCloudKMSService(t *testing.T) {
}
}

func TestMasterKey_createCloudKMSService_withOauthToken(t *testing.T) {
t.Setenv(SopsGoogleCredentialsOAuthToken, "token")

masterKey := MasterKey{
ResourceID: testResourceID,
}

_, err := masterKey.newKMSClient()

assert.NoError(t, err)
}

func TestMasterKey_createCloudKMSService_withoutCredentials(t *testing.T) {
masterKey := MasterKey{
ResourceID: testResourceID,
}

_, err := masterKey.newKMSClient()

assert.Error(t, err)
assert.ErrorContains(t, err, "credentials: failed to get credentials")
}

func newGRPCServer(port string) *grpc.ClientConn {
serv := grpc.NewServer()
kmspb.RegisterKeyManagementServiceServer(serv, &mockKeyManagement)
Expand Down
94 changes: 48 additions & 46 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
module github.com/getsops/sops/v3

go 1.21
go 1.22

toolchain go1.23.0

require (
cloud.google.com/go/kms v1.18.4
cloud.google.com/go/kms v1.19.0
cloud.google.com/go/storage v1.43.0
filippo.io/age v1.2.0
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.1.0
github.com/ProtonMail/go-crypto v1.1.0-alpha.5-proton
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.27
github.com/aws/aws-sdk-go-v2/credentials v1.17.27
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.10
github.com/aws/aws-sdk-go-v2/service/kms v1.35.3
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3
github.com/aws/aws-sdk-go-v2 v1.30.4
github.com/aws/aws-sdk-go-v2/config v1.27.30
github.com/aws/aws-sdk-go-v2/credentials v1.17.29
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.14
github.com/aws/aws-sdk-go-v2/service/kms v1.35.5
github.com/aws/aws-sdk-go-v2/service/s3 v1.60.1
github.com/aws/aws-sdk-go-v2/service/sts v1.30.5
github.com/blang/semver v3.5.1+incompatible
github.com/fatih/color v1.17.0
github.com/getsops/gopgagent v0.0.0-20240527072608-0c14999532fe
Expand All @@ -29,58 +31,58 @@ require (
github.com/lib/pq v1.10.9
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-wordwrap v1.0.1
github.com/ory/dockertest/v3 v3.10.0
github.com/ory/dockertest/v3 v3.11.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.15
golang.org/x/net v0.27.0
golang.org/x/oauth2 v0.21.0
golang.org/x/sys v0.23.0
golang.org/x/term v0.22.0
google.golang.org/api v0.190.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf
golang.org/x/net v0.28.0
golang.org/x/oauth2 v0.22.0
golang.org/x/sys v0.24.0
golang.org/x/term v0.23.0
google.golang.org/api v0.193.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
gopkg.in/ini.v1 v1.67.0
gopkg.in/yaml.v3 v3.0.1
)

require (
cloud.google.com/go v0.115.0 // indirect
cloud.google.com/go/auth v0.7.3 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.3 // indirect
cloud.google.com/go v0.115.1 // indirect
cloud.google.com/go/auth v0.9.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/iam v1.1.12 // indirect
cloud.google.com/go/longrunning v0.5.11 // indirect
cloud.google.com/go/iam v1.1.13 // indirect
cloud.google.com/go/longrunning v0.5.12 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.0.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.15 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.15 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.4 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.16 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.18 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.18 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.16 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.5 // indirect
github.com/aws/smithy-go v1.20.4 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/containerd/continuity v0.4.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v27.0.1+incompatible // indirect
github.com/docker/docker v27.1.0+incompatible // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
Expand Down Expand Up @@ -121,15 +123,15 @@ require (
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20240730163845-b1a4ccb954bf // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 5ef4c8e

Please sign in to comment.