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

adding support for comma separate list of keys in generated annotatio… #9

Merged
merged 1 commit into from
Oct 25, 2018
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
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ $ kubectl apply -f https://raw.githubusercontent.com/mittwald/kubernetes-secret-
## Usage

Add the annotation `secret-generator.v1.mittwald.de/autogenerate` to any Kubernetes
secret object. The value of the annotation can be a field name within the secret; the
SecretGeneratorController will pick up this annotation and add a field (`password` in
the example below) to the secret with a randomly generated string value.
secret object. The value of the annotation can be a field name
(or comma separated list of field names) within the secret; the
SecretGeneratorController will pick up this annotation and add a field [or fields]
(`password` in the example below) to the secret with a randomly generated string value.

```yaml
apiVersion: v1
Expand All @@ -39,4 +40,8 @@ data:
- Regenerate all automatically generated passwords:

```
$ kubectl annotate secrets --all secret-generator.v1.mittwald.de/regenerate=true
$ kubectl annotate secrets --all secret-generator.v1.mittwald.de/regenerate=true

- Regenerate only certain fields
```
$ kubectl annotate secrets --all secret-generator.v1.mittwald.de/regenerate=password1,password2
42 changes: 31 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"crypto/rand"
"math/big"
"strings"
"time"
)

Expand Down Expand Up @@ -131,7 +132,9 @@ func (c *GeneratorController) SecretAdded(obj interface{}) {
regenerateNeeded = true
}

if _, ok := secret.Annotations[SecretRegenerateAnnotation]; ok {
regenerate, ok := secret.Annotations[SecretRegenerateAnnotation]

if ok {
glog.Infof("regenerating of secret %s requested", secret.Name)
regenerateNeeded = true
}
Expand All @@ -152,12 +155,6 @@ func (c *GeneratorController) SecretAdded(obj interface{}) {
return
}

newPassword, err := generateSecret(secretLength)
if err != nil {
glog.Errorf("could not generate new secret: %s", err)
return
}

if _, ok := secretCopy.Annotations[SecretRegenerateAnnotation]; ok {
glog.Infof("removing annotation %s from secret %s", SecretRegenerateAnnotation, secret.Name)
delete(secretCopy.Annotations, SecretRegenerateAnnotation)
Expand All @@ -167,11 +164,25 @@ func (c *GeneratorController) SecretAdded(obj interface{}) {
secretCopy.Data = make(map[string][]byte)
}

secretCopy.Annotations[SecretGeneratedAtAnnotation] = time.Now().String()
secretCopy.Annotations[SecretSecureAnnotation] = "yes"
secretCopy.Data[val] = []byte(newPassword)
regenKeys := strings.Split(regenerate, ",")

for _, key := range strings.Split(val, ",") {
if regenerate == "" || regenerate == "true" || contains(regenKeys, key) {

secretCopy.Annotations[SecretGeneratedAtAnnotation] = time.Now().String()
secretCopy.Annotations[SecretSecureAnnotation] = "yes"

glog.Infof("set value %s of secret %s to new randomly generated secret of %d bytes length", val, secret.Name, secretLength)
newPassword, err := generateSecret(secretLength)
if err != nil {
glog.Errorf("could not generate new secret: %s", err)
return
}

secretCopy.Data[key] = []byte(newPassword)

glog.Infof("set value %s of secret %s to new randomly generated secret of %d bytes length", key, secret.Name, secretLength)
}
}

if _, err := c.client.Core().Secrets(secret.Namespace).Update(secretCopy); err != nil {
glog.Errorf("could not add %s annotation to secret %s: %s", SecretGeneratedAtAnnotation, secret.Name, err)
Expand All @@ -190,3 +201,12 @@ func generateSecret(length int) (string, error) {
}
return string(b), nil
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}