2020-12-14 22:24:13 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
|
|
|
|
configv1alpha1 "go.pinniped.dev/generated/1.19/apis/supervisor/config/v1alpha1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-16 22:27:09 +00:00
|
|
|
opKind = "FederationDomain"
|
2020-12-14 22:24:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func generateSymmetricKey() ([]byte, error) {
|
|
|
|
b := make([]byte, symmetricKeySize)
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:55:14 +00:00
|
|
|
func isValid(secret *corev1.Secret, labels map[string]string) bool {
|
2020-12-15 14:13:01 +00:00
|
|
|
if secret.Type != symmetricSecretType {
|
2020-12-14 22:24:13 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-15 14:13:01 +00:00
|
|
|
data, ok := secret.Data[symmetricSecretDataKey]
|
2020-12-14 22:24:13 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(data) != symmetricKeySize {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:55:14 +00:00
|
|
|
for key, value := range labels {
|
|
|
|
if secret.Labels[key] != value {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 22:24:13 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func secretDataFunc() (map[string][]byte, error) {
|
|
|
|
symmetricKey, err := generateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string][]byte{
|
2020-12-15 14:13:01 +00:00
|
|
|
symmetricSecretDataKey: symmetricKey,
|
2020-12-14 22:24:13 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSecret(namespace, name string, labels map[string]string, secretDataFunc func() (map[string][]byte, error), owner metav1.Object) (*corev1.Secret, error) {
|
|
|
|
secretData, err := secretDataFunc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
deploymentGVK := schema.GroupVersionKind{
|
|
|
|
Group: appsv1.SchemeGroupVersion.Group,
|
|
|
|
Version: appsv1.SchemeGroupVersion.Version,
|
|
|
|
Kind: "Deployment",
|
|
|
|
}
|
2020-12-15 20:05:06 +00:00
|
|
|
|
|
|
|
blockOwnerDeletion := true
|
|
|
|
isController := false
|
|
|
|
|
2020-12-14 22:24:13 +00:00
|
|
|
return &corev1.Secret{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Namespace: namespace,
|
|
|
|
OwnerReferences: []metav1.OwnerReference{
|
2020-12-15 20:05:06 +00:00
|
|
|
{
|
|
|
|
APIVersion: deploymentGVK.GroupVersion().String(),
|
|
|
|
Kind: deploymentGVK.Kind,
|
|
|
|
Name: owner.GetName(),
|
|
|
|
UID: owner.GetUID(),
|
|
|
|
BlockOwnerDeletion: &blockOwnerDeletion,
|
|
|
|
Controller: &isController,
|
|
|
|
},
|
2020-12-14 22:24:13 +00:00
|
|
|
},
|
|
|
|
Labels: labels,
|
|
|
|
},
|
2020-12-15 14:13:01 +00:00
|
|
|
Type: symmetricSecretType,
|
2020-12-14 22:24:13 +00:00
|
|
|
Data: secretData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-17 19:34:49 +00:00
|
|
|
// isFederationDomainControllee returns whether the provided obj is controlled by an FederationDomain.
|
|
|
|
func isFederationDomainControllee(obj metav1.Object) bool {
|
2020-12-14 22:24:13 +00:00
|
|
|
controller := metav1.GetControllerOf(obj)
|
|
|
|
return controller != nil &&
|
|
|
|
controller.APIVersion == configv1alpha1.SchemeGroupVersion.String() &&
|
|
|
|
controller.Kind == opKind
|
|
|
|
}
|