Skip to content

Commit

Permalink
feat: Add support for GCP secret manager
Browse files Browse the repository at this point in the history
- Users can store github private keys in GCP secret manager
- Tested in GKE with workload identity
  • Loading branch information
samirtahir91 committed Jul 11, 2024
1 parent cfe8abf commit bc355ed
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 17 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ Key features:
- Uses a custom resource `GithubApp` in your destination namespace.
- Reads `appId`, `installId` and either and `privateKeySecret` or `vaultPrivateKey` defined in a `GithubApp` resource and requests an access token from Github for the Github App.
- It stores the access token in a secret as per `accessTokenSecret`
- For pulling a GitHub Apps private key, there are 2 options built-in:
- For pulling a GitHub Apps private key, there are 3 options built-in:
- Using a Kubernetes secret:
- Use `privateKeySecret` - refers to an existing secret in the namespace which holds the base64 encoded PEM of the Github App's private key.
- It expects the field `data.privateKey` in the secret to pull the private key from.
- Hashicorp Vault (this takes priority over `privateKeySecret` if both are specified):
- Using GCP Secret Manager (this takes priority over `privateKeySecret` if it is specified):
- **You must base64 encode your private key before saving in Secret Manager**
- The operator logic will only accept base64 encoded secrets else it will fail.
- Configure with the `googlePrivateKeySecret` - the full secret path in Secret Manager for your GithubApp secret
- i.e. "projects/xxxxxxxxxx/secrets/my-gh-app/versions/latest"
- Configure [Workload Identity](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity) to bind secret access permissions to the operators Kubernetes Service Account
- Tested with role `roles/secretmanager.secretAccessor`
- Hashicorp Vault (this takes priority over `privateKeySecret` and `googlePrivateKeySecret` if they are specified):
- **You must base64 encode your private key before saving in Vault**
- The operator logic will only accept base64 encoded secrets else it will fail.
- This will create a short-lived JWT (10mins TTL) via Kubernetes Token Request API, with an audience you define.
Expand Down Expand Up @@ -144,6 +151,24 @@ spec:
EOF
```

## Example GithubApp object using GCP Secret Manager to pull the private key during run-time
- Below example will fetch the private key from GCP Secret Manager when the github access token expires
- It requires that the Kubernetes Service Account has permissions on the secret in SEcret Manager, i.e. via [Workload Identity](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity)
```sh
kubectl apply -f - <<EOF
apiVersion: githubapp.samir.io/v1
kind: GithubApp
metadata:
name: GithubApp-sample
namespace: team-1
spec:
appId: 123123
installId: 12312312
accessTokenSecret: github-app-access-token-123123
googlePrivateKeySecret: "projects/123123123123/secrets/gh-app-123123/versions/latest"
EOF
```

## Getting Started

### Prerequisites
Expand Down
13 changes: 7 additions & 6 deletions api/v1/githubapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (

// GithubAppSpec defines the desired state of GithubApp
type GithubAppSpec struct {
AppId int `json:"appId"`
InstallId int `json:"installId"`
PrivateKeySecret string `json:"privateKeySecret,omitempty"`
RolloutDeployment *RolloutDeploymentSpec `json:"rolloutDeployment,omitempty"`
VaultPrivateKey *VaultPrivateKeySpec `json:"vaultPrivateKey,omitempty"`
AccessTokenSecret string `json:"accessTokenSecret"`
AppId int `json:"appId"`
InstallId int `json:"installId"`
PrivateKeySecret string `json:"privateKeySecret,omitempty"`
RolloutDeployment *RolloutDeploymentSpec `json:"rolloutDeployment,omitempty"`
VaultPrivateKey *VaultPrivateKeySpec `json:"vaultPrivateKey,omitempty"`
AccessTokenSecret string `json:"accessTokenSecret"`
GcpPrivateKeySecret string `json:"googlePrivateKeySecret,omitempty"`
}

// GithubAppStatus defines the observed state of GithubApp
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/githubapp.samir.io_githubapps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ spec:
type: string
appId:
type: integer
googlePrivateKeySecret:
type: string
installId:
type: integer
privateKeySecret:
Expand Down
38 changes: 30 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ require (
)

require (
cloud.google.com/go/auth v0.7.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
cloud.google.com/go/compute/metadata v0.4.0 // indirect
cloud.google.com/go/iam v1.1.10 // indirect
cloud.google.com/go/secretmanager v1.13.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-jose/go-jose/v4 v4.0.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -38,7 +45,10 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.5 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down Expand Up @@ -68,20 +78,32 @@ require (
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
go.opencensus.io v0.24.0 // 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 v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.18.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/api v0.188.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/genproto v0.0.0-20240708141625-4ad9e859172b // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit bc355ed

Please sign in to comment.