1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-15 17:51:03 +00:00
kube-arangodb/pkg/deployment/access_package.go

238 lines
7.7 KiB
Go
Raw Normal View History

2018-05-31 11:20:49 +00:00
//
// DISCLAIMER
//
2022-01-10 11:35:49 +00:00
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
2018-05-31 11:20:49 +00:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package deployment
import (
2021-03-23 15:47:28 +00:00
"context"
2018-05-31 11:20:49 +00:00
"strings"
"time"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
2021-01-08 14:35:38 +00:00
"github.com/arangodb/kube-arangodb/pkg/util/errors"
2018-05-31 11:20:49 +00:00
certificates "github.com/arangodb-helper/go-certificates"
"github.com/ghodss/yaml"
2020-03-10 09:26:38 +00:00
v1 "k8s.io/api/core/v1"
2018-05-31 11:20:49 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
)
const (
clientAuthValidFor = time.Hour * 24 * 365 // 1yr
clientAuthCurve = "P256"
labelKeyOriginalDeployment = "original-deployment-name"
2018-05-31 11:20:49 +00:00
)
// createAccessPackages creates a arangosync access packages specified
// in spec.sync.externalAccess.accessPackageSecretNames.
2021-04-26 08:30:06 +00:00
func (d *Deployment) createAccessPackages(ctx context.Context) error {
log := d.deps.Log
2018-05-31 11:20:49 +00:00
spec := d.apiObject.Spec
secrets := d.deps.Client.Kubernetes().CoreV1().Secrets(d.GetNamespace())
2018-05-31 11:20:49 +00:00
if !spec.Sync.IsEnabled() {
// We're only relevant when sync is enabled
return nil
}
// Create all access packages that we're asked to build
apNameMap := make(map[string]struct{})
2018-05-31 11:20:49 +00:00
for _, apSecretName := range spec.Sync.ExternalAccess.AccessPackageSecretNames {
apNameMap[apSecretName] = struct{}{}
2021-04-26 08:30:06 +00:00
if err := d.ensureAccessPackage(ctx, apSecretName); err != nil {
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
}
// Remove all access packages that we did build, but are no longer needed
ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx)
2021-04-26 08:30:06 +00:00
defer cancel()
secretList, err := secrets.List(ctxChild, metav1.ListOptions{})
if err != nil {
log.Debug().Err(err).Msg("Failed to list secrets")
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
}
for _, secret := range secretList.Items {
if d.isOwnerOf(&secret) {
if _, found := secret.Data[constants.SecretAccessPackageYaml]; found {
// Secret is an access package
if _, wanted := apNameMap[secret.GetName()]; !wanted {
// We found an obsolete access package secret. Remove it.
err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
2021-04-26 08:30:06 +00:00
return secrets.Delete(ctxChild, secret.GetName(), metav1.DeleteOptions{
Preconditions: &metav1.Preconditions{UID: &secret.UID},
})
})
if err != nil && !k8sutil.IsNotFound(err) {
// Not serious enough to stop everything now, just log and create an event
log.Warn().Err(err).Msg("Failed to remove obsolete access package secret")
d.CreateEvent(k8sutil.NewErrorEvent("Access Package cleanup failed", err, d.apiObject))
} else {
// Access package removed, notify user
log.Info().Str("secret-name", secret.GetName()).Msg("Removed access package Secret")
d.CreateEvent(k8sutil.NewAccessPackageDeletedEvent(d.apiObject, secret.GetName()))
}
}
}
}
}
2018-05-31 11:20:49 +00:00
return nil
}
// ensureAccessPackage creates an arangosync access package with given name
// it is does not already exist.
2021-04-26 08:30:06 +00:00
func (d *Deployment) ensureAccessPackage(ctx context.Context, apSecretName string) error {
2018-05-31 11:20:49 +00:00
log := d.deps.Log
ns := d.GetNamespace()
secrets := d.deps.Client.Kubernetes().CoreV1().Secrets(ns)
2018-05-31 11:20:49 +00:00
spec := d.apiObject.Spec
err := globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
2021-04-26 08:30:06 +00:00
_, err := secrets.Get(ctxChild, apSecretName, metav1.GetOptions{})
return err
})
if err == nil {
2018-05-31 11:20:49 +00:00
// Secret already exists
return nil
2021-04-26 08:30:06 +00:00
} else if !k8sutil.IsNotFound(err) {
log.Debug().Err(err).Str("name", apSecretName).Msg("Failed to get arangosync access package secret")
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
// Fetch client authentication CA
clientAuthSecretName := spec.Sync.Authentication.GetClientCASecretName()
ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx)
2021-04-26 08:30:06 +00:00
defer cancel()
clientAuthCert, clientAuthKey, _, err := k8sutil.GetCASecret(ctxChild, secrets, clientAuthSecretName, nil)
2018-05-31 11:20:49 +00:00
if err != nil {
log.Debug().Err(err).Msg("Failed to get client-auth CA secret")
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
// Fetch TLS CA public key
tlsCASecretName := spec.Sync.TLS.GetCASecretName()
2021-04-26 08:30:06 +00:00
tlsCACert, err := k8sutil.GetCACertficateSecret(ctx, secrets, tlsCASecretName)
2018-05-31 11:20:49 +00:00
if err != nil {
log.Debug().Err(err).Msg("Failed to get TLS CA secret")
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
// Create keyfile
ca, err := certificates.LoadCAFromPEM(clientAuthCert, clientAuthKey)
if err != nil {
log.Debug().Err(err).Msg("Failed to parse client-auth CA")
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
// Create certificate
options := certificates.CreateCertificateOptions{
ValidFor: clientAuthValidFor,
ECDSACurve: clientAuthCurve,
IsClientAuth: true,
}
cert, key, err := certificates.CreateCertificate(options, &ca)
if err != nil {
log.Debug().Err(err).Msg("Failed to create client-auth keyfile")
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
keyfile := strings.TrimSpace(cert) + "\n" + strings.TrimSpace(key)
// Create secrets (in memory)
keyfileSecret := v1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: apSecretName + "-auth",
Labels: map[string]string{
labelKeyOriginalDeployment: d.apiObject.GetName(),
2018-05-31 11:20:49 +00:00
},
},
Data: map[string][]byte{
constants.SecretTLSKeyfile: []byte(keyfile),
},
Type: "Opaque",
}
tlsCASecret := v1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: apSecretName + "-ca",
Labels: map[string]string{
labelKeyOriginalDeployment: d.apiObject.GetName(),
2018-05-31 11:20:49 +00:00
},
},
Data: map[string][]byte{
constants.SecretCACertificate: []byte(tlsCACert),
},
Type: "Opaque",
}
// Serialize secrets
keyfileYaml, err := yaml.Marshal(keyfileSecret)
if err != nil {
log.Debug().Err(err).Msg("Failed to encode client-auth keyfile Secret")
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
tlsCAYaml, err := yaml.Marshal(tlsCASecret)
if err != nil {
log.Debug().Err(err).Msg("Failed to encode TLS CA Secret")
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
allYaml := strings.TrimSpace(string(keyfileYaml)) + "\n---\n" + strings.TrimSpace(string(tlsCAYaml))
// Create secret containing access package
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: apSecretName,
},
Data: map[string][]byte{
constants.SecretAccessPackageYaml: []byte(allYaml),
constants.SecretCACertificate: []byte(tlsCACert),
constants.SecretTLSKeyfile: []byte(keyfile),
2018-05-31 11:20:49 +00:00
},
}
// Attach secret to owner
secret.SetOwnerReferences(append(secret.GetOwnerReferences(), d.apiObject.AsOwner()))
err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
2021-04-26 08:30:06 +00:00
_, err := secrets.Create(ctxChild, secret, metav1.CreateOptions{})
return err
})
if err != nil {
2018-05-31 11:20:49 +00:00
// Failed to create secret
log.Debug().Err(err).Str("secret-name", apSecretName).Msg("Failed to create access package Secret")
2021-01-08 14:35:38 +00:00
return errors.WithStack(err)
2018-05-31 11:20:49 +00:00
}
// Write log entry & create event
log.Info().Str("secret-name", apSecretName).Msg("Created access package Secret")
d.CreateEvent(k8sutil.NewAccessPackageCreatedEvent(d.apiObject, apSecretName))
return nil
}