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"
|
|
|
|
|
2022-07-11 11:49:47 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-06-30 18:39:07 +00:00
|
|
|
core "k8s.io/api/core/v1"
|
|
|
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2018-05-31 11:20:49 +00:00
|
|
|
|
2022-07-11 11:49:47 +00:00
|
|
|
certificates "github.com/arangodb-helper/go-certificates"
|
|
|
|
|
2018-05-31 11:20:49 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/constants"
|
2022-07-11 11:49:47 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/errors"
|
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/globals"
|
2018-05-31 11:20:49 +00:00
|
|
|
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-05-31 11:50:22 +00:00
|
|
|
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 {
|
2022-06-14 07:26:07 +00:00
|
|
|
log := d.sectionLogger("access-package")
|
2018-05-31 11:20:49 +00:00
|
|
|
spec := d.apiObject.Spec
|
|
|
|
|
|
|
|
if !spec.Sync.IsEnabled() {
|
|
|
|
// We're only relevant when sync is enabled
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-31 11:50:22 +00:00
|
|
|
// 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 {
|
2018-05-31 11:50:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 11:50:22 +00:00
|
|
|
// Remove all access packages that we did build, but are no longer needed
|
2022-05-31 11:31:00 +00:00
|
|
|
secretList := d.acs.CurrentClusterCache().Secret().V1().ListSimple()
|
2022-03-23 22:19:36 +00:00
|
|
|
for _, secret := range secretList {
|
|
|
|
if d.isOwnerOf(secret) {
|
2018-05-31 11:50:22 +00:00
|
|
|
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.
|
2022-03-23 22:19:36 +00:00
|
|
|
err := globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
|
2022-06-30 18:39:07 +00:00
|
|
|
return d.SecretsModInterface().Delete(ctxChild, secret.GetName(), meta.DeleteOptions{
|
|
|
|
Preconditions: &meta.Preconditions{UID: &secret.UID},
|
2021-04-26 08:30:06 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil && !k8sutil.IsNotFound(err) {
|
2022-06-14 07:26:07 +00:00
|
|
|
// Not serious enough to stop everything now, just sectionLogger and create an event
|
|
|
|
log.Err(err).Warn("Failed to remove obsolete access package secret")
|
2018-05-31 11:50:22 +00:00
|
|
|
d.CreateEvent(k8sutil.NewErrorEvent("Access Package cleanup failed", err, d.apiObject))
|
|
|
|
} else {
|
|
|
|
// Access package removed, notify user
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Str("secret-name", secret.GetName()).Info("Removed access package Secret")
|
2018-05-31 11:50:22 +00:00
|
|
|
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 {
|
2022-06-14 07:26:07 +00:00
|
|
|
log := d.sectionLogger("access-package")
|
2018-05-31 11:20:49 +00:00
|
|
|
spec := d.apiObject.Spec
|
|
|
|
|
2022-06-30 18:39:07 +00:00
|
|
|
_, err := d.acs.CurrentClusterCache().Secret().V1().Read().Get(ctx, apSecretName, meta.GetOptions{})
|
2021-04-26 08:30:06 +00:00
|
|
|
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) {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Str("name", apSecretName).Debug("Failed to get arangosync access package secret")
|
2021-04-26 08:30:06 +00:00
|
|
|
return errors.WithStack(err)
|
2018-05-31 11:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch client authentication CA
|
|
|
|
clientAuthSecretName := spec.Sync.Authentication.GetClientCASecretName()
|
2022-05-31 11:31:00 +00:00
|
|
|
clientAuthCert, clientAuthKey, _, err := k8sutil.GetCASecret(ctx, d.acs.CurrentClusterCache().Secret().V1().Read(), clientAuthSecretName, nil)
|
2018-05-31 11:20:49 +00:00
|
|
|
if err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Debug("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()
|
2022-05-31 11:31:00 +00:00
|
|
|
tlsCACert, err := k8sutil.GetCACertficateSecret(ctx, d.acs.CurrentClusterCache().Secret().V1().Read(), tlsCASecretName)
|
2018-05-31 11:20:49 +00:00
|
|
|
if err != nil {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Debug("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 {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Debug("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 {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Debug("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)
|
2022-06-30 18:39:07 +00:00
|
|
|
keyfileSecret := core.Secret{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
2018-05-31 11:20:49 +00:00
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "Secret",
|
|
|
|
},
|
2022-06-30 18:39:07 +00:00
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2018-05-31 11:20:49 +00:00
|
|
|
Name: apSecretName + "-auth",
|
|
|
|
Labels: map[string]string{
|
2018-05-31 11:50:22 +00:00
|
|
|
labelKeyOriginalDeployment: d.apiObject.GetName(),
|
2018-05-31 11:20:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
|
|
|
constants.SecretTLSKeyfile: []byte(keyfile),
|
|
|
|
},
|
|
|
|
Type: "Opaque",
|
|
|
|
}
|
2022-06-30 18:39:07 +00:00
|
|
|
tlsCASecret := core.Secret{
|
|
|
|
TypeMeta: meta.TypeMeta{
|
2018-05-31 11:20:49 +00:00
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "Secret",
|
|
|
|
},
|
2022-06-30 18:39:07 +00:00
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2018-05-31 11:20:49 +00:00
|
|
|
Name: apSecretName + "-ca",
|
|
|
|
Labels: map[string]string{
|
2018-05-31 11:50:22 +00:00
|
|
|
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 {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Debug("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 {
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Debug("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
|
2022-06-30 18:39:07 +00:00
|
|
|
secret := &core.Secret{
|
|
|
|
ObjectMeta: meta.ObjectMeta{
|
2018-05-31 11:20:49 +00:00
|
|
|
Name: apSecretName,
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
|
|
|
constants.SecretAccessPackageYaml: []byte(allYaml),
|
2019-03-06 13:21:36 +00:00
|
|
|
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()))
|
2021-12-10 12:04:48 +00:00
|
|
|
err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
|
2022-06-30 18:39:07 +00:00
|
|
|
_, err := d.SecretsModInterface().Create(ctxChild, secret, meta.CreateOptions{})
|
2021-04-26 08:30:06 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-05-31 11:20:49 +00:00
|
|
|
// Failed to create secret
|
2022-06-14 07:26:07 +00:00
|
|
|
log.Err(err).Str("secret-name", apSecretName).Debug("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
|
|
|
}
|
|
|
|
|
2022-06-14 07:26:07 +00:00
|
|
|
// Write sectionLogger entry & create event
|
|
|
|
log.Str("secret-name", apSecretName).Info("Created access package Secret")
|
2018-05-31 11:20:49 +00:00
|
|
|
d.CreateEvent(k8sutil.NewAccessPackageCreatedEvent(d.apiObject, apSecretName))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|