Skip to content
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

Add support for masking secret values in a GHA context #299

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions tools/env-loader/cmd/env-loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/alecthomas/kingpin/v2"
envloader "github.com/gravitational/shared-workflows/tools/env-loader/pkg"
"github.com/gravitational/shared-workflows/tools/env-loader/pkg/values"
"github.com/gravitational/shared-workflows/tools/env-loader/pkg/writers"
"github.com/gravitational/trace"
)
Expand Down Expand Up @@ -77,9 +78,9 @@ func parseCLI(args []string) *config {
return c
}

func getRequestedEnvValues(c *config) (map[string]string, error) {
func getRequestedEnvValues(c *config) (map[string]values.Value, error) {
// Load in values
var envValues map[string]string
var envValues map[string]values.Value
var err error
if c.EnvironmentsDirectory != "" {
envValues, err = envloader.LoadEnvironmentValuesInDirectory(c.EnvironmentsDirectory, c.Environment, c.ValueSets)
Expand All @@ -93,7 +94,7 @@ func getRequestedEnvValues(c *config) (map[string]string, error) {

// Filter out values not requested
if len(c.Values) > 0 {
maps.DeleteFunc(envValues, func(key, _ string) bool {
maps.DeleteFunc(envValues, func(key string, _ values.Value) bool {
return !slices.Contains(c.Values, key)
})
}
Expand Down
64 changes: 43 additions & 21 deletions tools/env-loader/cmd/env-loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"bytes"
"os"
"path/filepath"
"slices"
"testing"

"github.com/alecthomas/kingpin/v2"
"github.com/gravitational/shared-workflows/tools/env-loader/pkg/values"
"github.com/stretchr/testify/require"
)

Expand All @@ -30,7 +32,7 @@ func TestGetRequestedEnvValues(t *testing.T) {
tests := []struct {
desc string
c *config
expectedValues map[string]string
expectedValues map[string]values.Value
}{
{
desc: "specific values",
Expand All @@ -45,9 +47,9 @@ func TestGetRequestedEnvValues(t *testing.T) {
"envLevelCommon1",
},
},
expectedValues: map[string]string{
"setLevel": "set level",
"envLevelCommon1": "env level",
expectedValues: map[string]values.Value{
"setLevel": {UnderlyingValue: "set level"},
"envLevelCommon1": {UnderlyingValue: "env level"},
},
},
{
Expand All @@ -59,13 +61,13 @@ func TestGetRequestedEnvValues(t *testing.T) {
"testing1",
},
},
expectedValues: map[string]string{
"setLevel": "set level",
"setLevelCommon": "testing1 level",
"envLevelCommon1": "env level",
"envLevelCommon2": "set level",
"topLevelCommon1": "top level",
"topLevelCommon2": "env level",
expectedValues: map[string]values.Value{
"setLevel": {UnderlyingValue: "set level"},
"setLevelCommon": {UnderlyingValue: "testing1 level"},
"envLevelCommon1": {UnderlyingValue: "env level"},
"envLevelCommon2": {UnderlyingValue: "set level"},
"topLevelCommon1": {UnderlyingValue: "top level"},
"topLevelCommon2": {UnderlyingValue: "env level"},
},
},
{
Expand All @@ -74,11 +76,11 @@ func TestGetRequestedEnvValues(t *testing.T) {
EnvironmentsDirectory: filepath.Join("..", "pkg", "testdata", "repos", "basic repo", ".environments"),
Environment: "env1",
},
expectedValues: map[string]string{
"envLevelCommon1": "env level",
"envLevelCommon2": "env level",
"topLevelCommon1": "top level",
"topLevelCommon2": "env level",
expectedValues: map[string]values.Value{
"envLevelCommon1": {UnderlyingValue: "env level"},
"envLevelCommon2": {UnderlyingValue: "env level"},
"topLevelCommon1": {UnderlyingValue: "top level"},
"topLevelCommon2": {UnderlyingValue: "env level"},
},
},
}
Expand All @@ -91,10 +93,12 @@ func TestGetRequestedEnvValues(t *testing.T) {
}

func TestRun(t *testing.T) {
os.Setenv("SOPS_AGE_KEY_FILE", filepath.Join("..", "pkg", "loaders", "testdata", "key1.age"))

tests := []struct {
desc string
c *config
expectedOutput string
desc string
c *config
expectedOutputs []string // Must support multiple options due to map iteration randomness
}{
{
desc: "specific values",
Expand All @@ -110,7 +114,25 @@ func TestRun(t *testing.T) {
},
Writer: "dotenv",
},
expectedOutput: "envLevelCommon1=env level\nsetLevel=set level\n",
expectedOutputs: []string{
"envLevelCommon1=env level\nsetLevel=set level\n",
"setLevel=set level\nenvLevelCommon1=env level\n",
},
},
{
desc: "secret masked values",
c: &config{
EnvironmentsDirectory: filepath.Join("..", "pkg", "testdata", "repos", "basic repo", ".environments"),
Environment: "env1",
Writer: "gha-mask",
ValueSets: []string{
"secrets",
},
},
expectedOutputs: []string{
"::add-mask::value1\n::add-mask::value2_unencrypted\n",
"::add-mask::value2_unencrypted\n::add-mask::value1\n",
},
},
}

Expand All @@ -124,6 +146,6 @@ func TestRun(t *testing.T) {
output := outputBytes.String()

require.NoError(t, err)
require.Equal(t, test.expectedOutput, output)
require.Contains(t, test.expectedOutputs, output)
}
}
129 changes: 66 additions & 63 deletions tools/env-loader/go.mod
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
module github.com/gravitational/shared-workflows/tools/env-loader

go 1.23.2
go 1.23.4

require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/getsops/sops/v3 v3.9.1
github.com/getsops/sops/v3 v3.9.2
github.com/gravitational/trace v1.4.0
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
golang.org/x/crypto v0.31.0
gopkg.in/yaml.v3 v3.0.1
)

require (
cel.dev/expr v0.18.0 // indirect
cloud.google.com/go v0.116.0 // indirect
cloud.google.com/go/auth v0.10.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.5 // indirect
cloud.google.com/go/compute/metadata v0.5.2 // indirect
cloud.google.com/go/iam v1.2.2 // indirect
cloud.google.com/go/kms v1.20.1 // indirect
cloud.google.com/go/longrunning v0.6.2 // indirect
cloud.google.com/go/monitoring v1.21.2 // indirect
cloud.google.com/go/storage v1.46.0 // indirect
cel.dev/expr v0.19.1 // indirect
cloud.google.com/go v0.117.0 // indirect
cloud.google.com/go/auth v0.13.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.6 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/iam v1.3.0 // indirect
cloud.google.com/go/kms v1.20.2 // indirect
cloud.google.com/go/longrunning v0.6.3 // indirect
cloud.google.com/go/monitoring v1.22.0 // indirect
cloud.google.com/go/storage v1.48.0 // indirect
filippo.io/age v1.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.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/azkeys v1.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.1.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.3 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 // indirect
github.com/ProtonMail/go-crypto v1.1.0-beta.0-proton // indirect
github.com/ProtonMail/go-crypto v1.1.3 // indirect
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
github.com/aws/aws-sdk-go-v2 v1.32.3 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.6 // indirect
github.com/aws/aws-sdk-go-v2/config v1.28.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.42 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.18 // indirect
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.35 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.22 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.22 // indirect
github.com/aws/aws-sdk-go-v2 v1.32.6 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7 // indirect
github.com/aws/aws-sdk-go-v2/config v1.28.6 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.47 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21 // indirect
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.43 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.25 // 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.22 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.4.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.3 // indirect
github.com/aws/aws-sdk-go-v2/service/kms v1.37.3 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.66.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.3 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.32.3 // indirect
github.com/aws/smithy-go v1.22.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.25 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.4.6 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.6 // indirect
github.com/aws/aws-sdk-go-v2/service/kms v1.37.7 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.71.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2 // indirect
github.com/aws/smithy-go v1.22.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudflare/circl v1.5.0 // indirect
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/cncf/xds/go v0.0.0-20241213214725-57cfbe6fad57 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/envoyproxy/go-control-plane v0.13.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
Expand All @@ -70,13 +71,12 @@ require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/googleapis/gax-go/v2 v2.14.0 // indirect
github.com/goware/prefixer v0.0.0-20160118172347-395022866408 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down Expand Up @@ -104,29 +104,32 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/urfave/cli v1.22.16 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.31.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.31.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.7.0 // indirect
google.golang.org/api v0.204.0 // indirect
google.golang.org/genproto v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 // indirect
google.golang.org/grpc v1.67.1 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.33.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0 // indirect
go.opentelemetry.io/otel v1.33.0 // indirect
go.opentelemetry.io/otel/metric v1.33.0 // indirect
go.opentelemetry.io/otel/sdk v1.33.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.33.0 // indirect
go.opentelemetry.io/otel/trace v1.33.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect
google.golang.org/api v0.212.0 // indirect
google.golang.org/genproto v0.0.0-20241216192217-9240e9c98484 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241216192217-9240e9c98484 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect
google.golang.org/grpc v1.68.1 // indirect
google.golang.org/grpc/stats/opentelemetry v0.0.0-20241028142157-ada6787961b3 // indirect
google.golang.org/protobuf v1.35.1 // indirect
google.golang.org/protobuf v1.36.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

// This is required due to because v1.69.0 has a compilation issue, see
// https://github.com/googleapis/google-cloud-go/issues/11283
exclude google.golang.org/grpc v1.69.0
Loading
Loading