Skip to content
This repository has been archived by the owner on Jul 5, 2021. It is now read-only.

Make backends configurable #18

Merged
merged 22 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
196a51c
fix 'operator-sdk generate openapi' issue
riccardomc May 9, 2019
a98768a
introduce ExternalSecretBackend
riccardomc May 9, 2019
620cf13
towards pluggable backend implementation
riccardomc May 16, 2019
60e8744
abstract backends registration, configuration and initialization
riccardomc May 17, 2019
d913150
fix test externalsecret test
riccardomc May 17, 2019
44661a3
remove unused test and debug statement
riccardomc May 17, 2019
b85323a
fix asm and dummy backend tests
riccardomc May 17, 2019
84d6cdf
fix CRDs examples
riccardomc May 20, 2019
c2f4ec6
update operator deployment manifest
riccardomc May 20, 2019
ab6dfb1
drop json annotation for ExternalSecret structs
riccardomc May 20, 2019
6cf98b1
asm backend: fix parameter handling
riccardomc May 20, 2019
78dd73d
asm backend: prevent nil dereference when backend hasn't been initial…
riccardomc May 20, 2019
e16d8e9
asm backend: fix AWS credentials, token is optional
riccardomc May 20, 2019
a668706
asm backend: fix parameter handling
riccardomc May 20, 2019
ac6bad8
format
riccardomc May 20, 2019
218b126
bump version 0.0.1 -> 0.0.2
riccardomc May 20, 2019
73421b6
add examples ExternalSecretBackend ExternalSecret CRs
riccardomc May 20, 2019
e3703aa
update README.md
riccardomc May 23, 2019
4f049bb
drop ExternalSecretBackend CRD
riccardomc May 27, 2019
3b98973
add configuration handling
riccardomc May 27, 2019
ef9f322
add backend initialization from Environment
riccardomc May 27, 2019
ae9c20c
introduce OPERATOR_CONFIG environment variable
riccardomc May 29, 2019
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
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
DOCKER_IMAGE ?= containersol/externalsecret-operator
DOCKER_TAG ?= backend-1password
DOCKER_TAG ?= $(shell grep -Po 'Version = "\K.*?(?=")' version/version.go)

# export these if you want to use AWS secrets manager
AWS_ACCESS_KEY_ID ?= AKIACONFIGUREME
AWS_SECRET_ACCESS_KEY ?= Secretsecretconfigureme
AWS_REGION ?= eu-west-1
AWS_DEFAULT_REGION ?= eu-west-1

NAMESPACE ?= "default"

Expand All @@ -19,13 +19,12 @@ push:
.PHONY: deploy
.EXPORT_ALL_VARIABLES: deploy
deploy:
envsubst < ./deploy/onepassword-namespace.yaml | kubectl apply -f -
envsubst < ./deploy/onepassword-configmap.yaml | kubectl apply -n ${NAMESPACE} -f -
kubectl apply -n $(NAMESPACE) -f ./deploy/service_account.yaml
kubectl apply -n $(NAMESPACE) -f ./deploy/role.yaml
envsubst < ./deploy/role_binding.yaml | kubectl apply -n $(NAMESPACE) -f -
kubectl apply -n $(NAMESPACE) -f ./deploy/crds/externalsecret-operator_v1alpha1_externalsecret_crd.yaml
envsubst < deploy/operator-onepassword.yaml | kubectl apply -n $(NAMESPACE) -f -
envsubst < deploy/operator-config.yaml | kubectl apply -n $(NAMESPACE) -f -
envsubst < deploy/operator.yaml | kubectl apply -n $(NAMESPACE) -f -

.PHONY: test
test:
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ and custom resource definitions:


```shell
export AWS_ACCESS_KEY_ID=AKIACONFIGUREME
export AWS_SECRET_ACCESS_KEY=Secretsecretconfigureme
export AWS_REGION=eu-west-1
make deploy
```

Expand All @@ -46,7 +43,7 @@ Given a secret defined in AWS Secrets Manager:
and an `ExternalSecret` resource definition like this one:

```yaml
% cat deploy/crds/externalsecret-operator_v1alpha1_externalsecret_cr.yaml
% cat ./deploy/crds/examples/externalsecret-asm.yaml
apiVersion: externalsecret-operator.container-solutions.com/v1alpha1
kind: ExternalSecret
metadata:
Expand All @@ -60,7 +57,7 @@ The operator fetches the secret from AWS Secrets Manager and injects it as a
secret:

```shell
% kubectl apply -f deploy/crds/externalsecret-operator_v1alpha1_externalsecret_cr.yaml
% kubectl apply -f ./deploy/crds/examples/externalsecret-asm.yaml
% kubectl get secret example-externalsecret -o jsonpath='{.data.example-externalsecret-key}' | base64 -d
this string is a secret
```
Expand Down
8 changes: 8 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"runtime"

"github.com/ContainerSolutions/externalsecret-operator/pkg/secrets"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
_ "k8s.io/client-go/plugin/pkg/client/auth"

Expand Down Expand Up @@ -61,6 +63,12 @@ func main() {

printVersion()

err := secrets.BackendInitFromEnv()
if err != nil {
log.Error(err, "Failed to initialize backends")
os.Exit(1)
}

namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
log.Error(err, "Failed to get watch namespace")
Expand Down
7 changes: 7 additions & 0 deletions deploy/crds/examples/externalsecret-asm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: externalsecret-operator.container-solutions.com/v1alpha1
kind: ExternalSecret
metadata:
name: example-externalsecret-asm
spec:
Key: example-externalsecret-key
Backend: asm-example
7 changes: 7 additions & 0 deletions deploy/crds/examples/externalsecret-dummy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: externalsecret-operator.container-solutions.com/v1alpha1
kind: ExternalSecret
metadata:
name: example-externalsecret
spec:
Key: example-externalsecret-key
Backend: dummy-example
7 changes: 7 additions & 0 deletions deploy/crds/examples/externalsecret-dummy2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: externalsecret-operator.container-solutions.com/v1alpha1
kind: ExternalSecret
metadata:
name: example-externalsecret-2
spec:
Key: example-externalsecret-key
Backend: dummy-example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ metadata:
name: example-externalsecret
spec:
Key: example-externalsecret
Backend: onepassword
Backend: asm-example
36 changes: 0 additions & 36 deletions deploy/operator-aws.yaml

This file was deleted.

9 changes: 9 additions & 0 deletions deploy/operator-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Name": "asm-example",
"Type": "asm",
"Parameters": {
"accessKeyID": "$AWS_ACCESS_KEY_ID",
"region": "$AWS_REGION",
"secretAccessKey": "$AWS_SECRET_ACCESS_KEY"
}
}
16 changes: 16 additions & 0 deletions deploy/operator-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Secret
metadata:
name: operator-config
type: Opaque
stringData:
operator-config.json: |-
{
"Name": "asm-example",
"Type": "asm",
"Parameters": {
"accessKeyID": "$AWS_ACCESS_KEY_ID",
"region": "$AWS_DEFAULT_REGION",
"secretAccessKey": "$AWS_SECRET_ACCESS_KEY"
}
}
10 changes: 7 additions & 3 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ spec:
serviceAccountName: externalsecret-operator
containers:
- name: externalsecret-operator
# Replace this with the built image name
image: REPLACE_IMAGE
image: containersol/externalsecret-operator:0.0.2
command:
- externalsecret-operator
imagePullPolicy: Always
imagePullPolicy: Never
env:
- name: WATCH_NAMESPACE
value: ""
Expand All @@ -29,3 +28,8 @@ spec:
fieldPath: metadata.name
- name: OPERATOR_NAME
value: "externalsecret-operator"
- name: OPERATOR_CONFIG
valueFrom:
secretKeyRef:
name: operator-config
key: operator-config.json
12 changes: 6 additions & 6 deletions pkg/apis/externalsecretoperator/v1alpha1/externalsecret_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type ExternalSecretSpec struct {
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html

// The ExternalSecretBackend to use to retrieve the secret
Backend string `json:"Backend"`
// The Backend to use to retrieve the secret
Backend string
// The Key of the secret held in the ExternalBackend
Key string `json:"Key"`
Key string
}

// ExternalSecretStatus defines the observed state of ExternalSecret
Expand All @@ -27,10 +27,10 @@ type ExternalSecretStatus struct {
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html

// The ExternalSecretBackend to use to retrieve the secret
Backend string `json:"Backend"`
// The Backend to use to retrieve the secret
Backend string
// The Key of the secret held in the ExternalBackend
Key string `json:"Key"`
Key string
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
16 changes: 0 additions & 16 deletions pkg/apis/externalsecretoperator/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 34 additions & 2 deletions pkg/apis/externalsecretoperator/v1alpha1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions pkg/controller/externalsecret/externalsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return err
}

// Initialize the secret backends
initSecretBackends()

// Watch for changes to primary resource ExternalSecret
err = c.Watch(&source.Kind{Type: &externalsecretoperatorv1alpha1.ExternalSecret{}}, &handler.EnqueueRequestForObject{})
if err != nil {
Expand Down
26 changes: 2 additions & 24 deletions pkg/controller/externalsecret/externalsecret_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,12 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)

func initSecretBackends() {
// TODO: backends should be created on the fly according to CRDs
asm := secrets.NewAWSSecretsManagerBackend()
if err := asm.Init(); err != nil {
log.Error(err, "Failed to initialize AWS Secrets Manager Backend")
}

dummy := secrets.NewDummySecretsBackend()
dummy.Init("-value")
secrets.BackendRegister("dummy", dummy)
log.Info("Initialized Dummy backend")

secrets.BackendRegister("asm", asm)
log.Info("Initialized Amazon Secret Manager backend")

onepasswordClient := secrets.OnePasswordCliClient{}
vault := "Personal"
onepassword := secrets.NewOnePasswordBackend(vault, onepasswordClient)
onepassword.Init()
secrets.BackendRegister("onepassword", onepassword)
log.Info("Initialized 1password backend")
}

func newSecretForCR(cr *externalsecretoperatorv1alpha1.ExternalSecret) (*corev1.Secret, error) {
backend, ok := secrets.Backends[cr.Spec.Backend]
backend, ok := secrets.BackendInstances[cr.Spec.Backend]
if !ok {
return nil, fmt.Errorf("Cannot find backend: %v", cr.Spec.Backend)
}

value, err := backend.Get(cr.Spec.Key)
secret := map[string][]byte{cr.Spec.Key: []byte(value)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestNewSecretForCR(t *testing.T) {
key := "key"
suffix := "-value"

dummy := secrets.NewDummySecretsBackend()
dummy.Init("-value")
secrets.BackendRegister("dummy", dummy)
secrets.BackendRegister("dummy", secrets.NewDummySecretsBackend)
secrets.BackendInstantiate("dummy", "dummy")
secrets.BackendInstances["dummy"].Init(map[string]string{"suffix": "-value"})

Convey("Given an ExternalSecret resource", t, func() {
externalSecret := v1alpha1.ExternalSecret{
Expand Down
Loading