Skip to content

Commit

Permalink
feat(gcp): Add same project secret imports for GCP. (#602)
Browse files Browse the repository at this point in the history
Co-authored-by: Jye Cusch <[email protected]>
  • Loading branch information
tjholm and jyecusch authored Apr 10, 2024
1 parent 8c042fe commit 3bc8819
Show file tree
Hide file tree
Showing 6 changed files with 664 additions and 47 deletions.
6 changes: 6 additions & 0 deletions cloud/gcp/deploy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ type GcpConfigItem struct {
Telemetry int
}

type GcpImports struct {
// A map of nitric names to GCP Secret Manager names
Secrets map[string]string
}

type GcpCloudRunConfig struct {
Memory int
Timeout int
Expand All @@ -37,6 +42,7 @@ type GcpCloudRunConfig struct {

type GcpConfig struct {
config.AbstractConfig[*GcpConfigItem] `mapstructure:"config,squash"`
Import GcpImports
ScheduleTimezone string `mapstructure:"schedule-timezone"`
ProjectId string `mapstructure:"gcp-project-id"`
Refresh bool
Expand Down
8 changes: 8 additions & 0 deletions cloud/gcp/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

apiv1 "cloud.google.com/go/firestore/apiv1/admin"
"cloud.google.com/go/firestore/apiv1/admin/adminpb"
gcpsecretmanager "cloud.google.com/go/secretmanager/apiv1"
"github.com/nitrictech/nitric/cloud/common/deploy"
"github.com/nitrictech/nitric/cloud/common/deploy/provider"
"github.com/nitrictech/nitric/cloud/common/deploy/pulumix"
Expand Down Expand Up @@ -60,6 +61,8 @@ type NitricGcpPulumiProvider struct {
AuthToken *oauth2.Token
BaseComputeRole *projects.IAMCustomRole

SecretManagerClient *gcpsecretmanager.Client

Project *Project
ApiGateways map[string]*apigateway.Gateway
HttpProxies map[string]*apigateway.Gateway
Expand Down Expand Up @@ -107,6 +110,11 @@ func (a *NitricGcpPulumiProvider) Init(attributes map[string]interface{}) error
return status.Errorf(codes.InvalidArgument, "Bad stack configuration: %s", err)
}

a.SecretManagerClient, err = gcpsecretmanager.NewClient(context.Background())
if err != nil {
return err
}

return nil
}

Expand Down
79 changes: 72 additions & 7 deletions cloud/gcp/deploy/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package deploy

import (
gcpsecretmanager "cloud.google.com/go/secretmanager/apiv1"
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
"github.com/nitrictech/nitric/cloud/common/deploy/resources"
common "github.com/nitrictech/nitric/cloud/common/deploy/tags"
deploymentspb "github.com/nitrictech/nitric/core/pkg/proto/deployments/v1"
"github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/secretmanager"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)

type SecretManagerSecret struct {
Expand All @@ -31,22 +34,84 @@ type SecretManagerSecret struct {
Secret *secretmanager.Secret
}

func (p *NitricGcpPulumiProvider) Secret(ctx *pulumi.Context, parent pulumi.Resource, name string, config *deploymentspb.Secret) error {
var err error
opts := append([]pulumi.ResourceOption{}, pulumi.Parent(parent))
// tagSecret - tags an existing secret in GCP and adds it to the stack.
func tagSecret(ctx *pulumi.Context, name string, projectId string, secretId string, tags map[string]string, client *gcpsecretmanager.Client, opts []pulumi.ResourceOption) (*secretmanager.Secret, error) {
secretLookup, err := secretmanager.LookupSecret(ctx, &secretmanager.LookupSecretArgs{
Project: &projectId,
SecretId: secretId,
})
if err != nil {
return nil, err
}

secId := pulumi.Sprintf("%s-%s", p.StackName, name)
_, err = client.UpdateSecret(ctx.Context(), &secretmanagerpb.UpdateSecretRequest{
Secret: &secretmanagerpb.Secret{
Name: secretLookup.Name,
Labels: tags,
},
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{"labels"},
},
})
if err != nil {
return nil, err
}

p.Secrets[name], err = secretmanager.NewSecret(ctx, name, &secretmanager.SecretArgs{
sec, err := secretmanager.GetSecret(
ctx,
name,
pulumi.ID(secretLookup.Id),
nil,
// nitric didn't create this resource, so it shouldn't delete it either.
append(opts, pulumi.RetainOnDelete(true))...,
)
if err != nil {
return nil, err
}
return sec, nil
}

// createSecret - creates a new secret in GCP Secret Manager, using the provided name and tags.
func createSecret(ctx *pulumi.Context, name string, stackName string, tags map[string]string, opts []pulumi.ResourceOption) (*secretmanager.Secret, error) {
secId := pulumi.Sprintf("%s-%s", stackName, name)
sec, err := secretmanager.NewSecret(ctx, name, &secretmanager.SecretArgs{
Replication: secretmanager.SecretReplicationArgs{
Automatic: pulumi.Bool(true),
},
SecretId: secId,
Labels: pulumi.ToStringMap(common.Tags(p.StackId, name, resources.Secret)),
}, p.WithDefaultResourceOptions(opts...)...)
Labels: pulumi.ToStringMap(tags),
}, opts...)
if err != nil {
return nil, err
}

return sec, nil
}

func (p *NitricGcpPulumiProvider) Secret(ctx *pulumi.Context, parent pulumi.Resource, name string, config *deploymentspb.Secret) error {
var err error
opts := append([]pulumi.ResourceOption{}, pulumi.Parent(parent))

secretLabels := common.Tags(p.StackId, name, resources.Secret)

var secret *secretmanager.Secret

importId := ""
if p.GcpConfig.Import.Secrets != nil {
importId = p.GcpConfig.Import.Secrets[name]
}

if importId != "" {
secret, err = tagSecret(ctx, name, p.GcpConfig.ProjectId, importId, secretLabels, p.SecretManagerClient, p.WithDefaultResourceOptions(opts...))
} else {
secret, err = createSecret(ctx, name, p.StackName, secretLabels, p.WithDefaultResourceOptions(opts...))
}

if err != nil {
return err
}

p.Secrets[name] = secret

return nil
}
69 changes: 36 additions & 33 deletions cloud/gcp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module github.com/nitrictech/nitric/cloud/gcp
go 1.21

require (
cloud.google.com/go/apigateway v1.6.4
cloud.google.com/go/cloudtasks v1.12.4
cloud.google.com/go/firestore v1.14.0
cloud.google.com/go/pubsub v1.33.0
cloud.google.com/go/secretmanager v1.11.4
cloud.google.com/go/storage v1.35.1
cloud.google.com/go/apigateway v1.6.6
cloud.google.com/go/cloudtasks v1.12.7
cloud.google.com/go/firestore v1.15.0
cloud.google.com/go/pubsub v1.37.0
cloud.google.com/go/secretmanager v1.12.0
cloud.google.com/go/storage v1.39.1
github.com/GoogleCloudPlatform/opentelemetry-operations-go/propagator v0.34.2
github.com/charmbracelet/log v0.2.4
github.com/fasthttp/router v1.4.18
github.com/golang/mock v1.6.0
github.com/golangci/golangci-lint v1.56.1
github.com/google/addlicense v1.1.1
github.com/googleapis/gax-go/v2 v2.12.0
github.com/googleapis/gax-go/v2 v2.12.3
github.com/mitchellh/mapstructure v1.5.0
github.com/nitrictech/nitric/cloud/common v0.0.0-20230430232207-a0e427e2d646
github.com/nitrictech/nitric/core v0.0.0-20230616021604-4036d005db63
Expand All @@ -26,11 +26,11 @@ require (
github.com/samber/lo v1.38.1
github.com/uw-labs/lichen v0.1.7
github.com/valyala/fasthttp v1.45.0
go.opentelemetry.io/otel v1.11.2
golang.org/x/oauth2 v0.15.0
google.golang.org/api v0.152.0
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
go.opentelemetry.io/otel v1.24.0
golang.org/x/oauth2 v0.18.0
google.golang.org/api v0.170.0
google.golang.org/grpc v1.62.1
google.golang.org/protobuf v1.33.0
)

require (
Expand All @@ -48,7 +48,7 @@ require (
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.2 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect
github.com/imdario/mergo v0.3.15
github.com/invopop/yaml v0.1.0 // indirect
Expand All @@ -72,20 +72,20 @@ require (
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/term v0.18.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
lukechampine.com/frand v1.4.2 // indirect
)

require (
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
4d63.com/gochecknoglobals v0.2.1 // indirect
cloud.google.com/go v0.110.10 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go v0.112.2 // indirect
cloud.google.com/go/compute v1.25.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/longrunning v0.5.4 // indirect
cloud.google.com/go/iam v1.1.7 // indirect
cloud.google.com/go/longrunning v0.5.6 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/4meepo/tagalign v1.3.3 // indirect
github.com/Abirdcfly/dupword v0.0.13 // indirect
Expand Down Expand Up @@ -138,18 +138,19 @@ require (
github.com/docker/docker v20.10.24+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
github.com/esimonov/ifshort v1.0.4 // indirect
github.com/ettle/strcase v0.2.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/firefart/nonamedreturns v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/ghostiam/protogetter v0.3.4 // indirect
github.com/go-critic/go-critic v0.11.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-toolsmith/astcast v1.1.0 // indirect
Expand All @@ -164,7 +165,7 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe // indirect
Expand All @@ -177,7 +178,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/licenseclassifier v0.0.0-20201113175434-78a70215ca36 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/gordonklaus/ineffassign v0.1.0 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
Expand Down Expand Up @@ -303,26 +304,28 @@ require (
go-simpler.org/musttag v0.8.0 // indirect
go-simpler.org/sloglint v0.4.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.36.4 // indirect
go.opentelemetry.io/otel/sdk v1.11.2 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/exp/typeparams v0.0.0-20231219180239-dc181d75b848 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 3bc8819

Please sign in to comment.