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 authentication support #14

Merged
merged 12 commits into from
Feb 27, 2018
Prev Previous commit
Next Next commit
Ensuring secrets are created
  • Loading branch information
ewoutp committed Feb 26, 2018
commit e68fd5d130aa09b1954c81b129daa1e4a291d1e3
6 changes: 6 additions & 0 deletions pkg/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ func (d *Deployment) send(ev *deploymentEvent) {
func (d *Deployment) run() {
log := d.deps.Log

// Create secrets
if err := d.createSecrets(d.apiObject); err != nil {
d.failOnError(err, "Failed to create secrets")
return
}

// Create services
if err := d.createServices(d.apiObject); err != nil {
d.failOnError(err, "Failed to create services")
Expand Down
51 changes: 51 additions & 0 deletions pkg/deployment/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,65 @@
package deployment

import (
"crypto/rand"
"encoding/hex"
"fmt"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

api "github.com/arangodb/k8s-operator/pkg/apis/arangodb/v1alpha"
"github.com/arangodb/k8s-operator/pkg/util/k8sutil"
)

// createSecrets creates all secrets needed to run the given deployment
func (d *Deployment) createSecrets(apiObject *api.ArangoDeployment) error {
if apiObject.Spec.IsAuthenticated() {
if err := d.ensureJWTSecret(apiObject.Spec.Authentication.JWTSecretName); err != nil {
return maskAny(err)
}
}
return nil
}

// ensureJWTSecret checks if a secret with given name exists in the namespace
// of the deployment. If not, it will add such a secret with a random
// JWT token.
func (d *Deployment) ensureJWTSecret(secretName string) error {
kubecli := d.deps.KubeCli
ns := d.apiObject.GetNamespace()
if _, err := kubecli.CoreV1().Secrets(ns).Get(secretName, metav1.GetOptions{}); k8sutil.IsNotFound(err) {
// Secret not found, create it
// Create token
tokenData := make([]byte, 32)
rand.Read(tokenData)
token := hex.EncodeToString(tokenData)

// Create secret
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
},
Data: map[string][]byte{
"token": []byte(token),
},
}
// Attach secret to deployment
secret.SetOwnerReferences(append(secret.GetOwnerReferences(), d.apiObject.AsOwner()))
if _, err := kubecli.CoreV1().Secrets(ns).Create(secret); k8sutil.IsAlreadyExists(err) {
// Secret added while we tried it also
return nil
} else if err != nil {
// Failed to create secret
return maskAny(err)
}
} else if err != nil {
// Failed to get secret for other reasons
return maskAny(err)
}
return nil
}

// getJWTSecret loads the JWT secret from a Secret configured in apiObject.Spec.Authentication.JWTSecretName.
func (d *Deployment) getJWTSecret(apiObject *api.ArangoDeployment) (string, error) {
if !apiObject.Spec.IsAuthenticated() {
Expand Down