mirror of
https://github.com/external-secrets/external-secrets.git
synced 2024-12-14 11:57:59 +00:00
67fedc840e
* build(deps): bump sigs.k8s.io/controller-runtime from 0.11.2 to 0.12.3 Bumps [sigs.k8s.io/controller-runtime](https://github.com/kubernetes-sigs/controller-runtime) from 0.11.2 to 0.12.3. - [Release notes](https://github.com/kubernetes-sigs/controller-runtime/releases) - [Changelog](https://github.com/kubernetes-sigs/controller-runtime/blob/master/RELEASE.md) - [Commits](https://github.com/kubernetes-sigs/controller-runtime/compare/v0.11.2...v0.12.3) --- updated-dependencies: - dependency-name: sigs.k8s.io/controller-runtime dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * feat: bump kubernetes 1.24 Signed-off-by: Moritz Johner <beller.moritz@googlemail.com> * fix: backwards-compatible vault implementation Signed-off-by: Moritz Johner <beller.moritz@googlemail.com> * feat: add audiences field to serviceAccountRef This will be used by aws, azure, gcp, kubernetes & vault providers in combination with TokenRequest API: it will _append_ audience claims to provider-specific audiences. Signed-off-by: Moritz Johner <beller.moritz@googlemail.com> * feat: refactor kubernetes client to match provider/client interfaces the kubernetes provider mixed up provider and client interfaces which made it really hard to reason about. This commit separates into two structs, each implements one interface. The client struct fields have been renamed and annotated so their use and scope is clear. Signed-off-by: Moritz Johner <beller.moritz@googlemail.com> * fix: deprecate expirationSeconds expirationSeconds is not needed because we generate a service account token on the fly for a single use. There will be no replacement for this. Signed-off-by: Moritz Johner <beller.moritz@googlemail.com> * fix: rename token fetch audiences field Signed-off-by: Moritz Johner <beller.moritz@googlemail.com> * fix: generate CRDs Signed-off-by: Moritz Johner <beller.moritz@googlemail.com> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Moritz Johner <beller.moritz@googlemail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Moritz Johner <beller.moritz@googlemail.com>
183 lines
5.7 KiB
Go
183 lines
5.7 KiB
Go
/*
|
|
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.
|
|
*/
|
|
|
|
package kubernetes
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
authenticationv1 "k8s.io/api/authentication/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
|
|
esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
|
|
)
|
|
|
|
const (
|
|
errInvalidClusterStoreMissingNamespace = "missing namespace"
|
|
errFetchCredentials = "could not fetch credentials: %w"
|
|
errMissingCredentials = "missing credentials: \"%s\""
|
|
errEmptyKey = "key %s found but empty"
|
|
errUnableCreateToken = "cannot create service account token: %q"
|
|
)
|
|
|
|
func (c *Client) setAuth(ctx context.Context) error {
|
|
err := c.setCA(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if c.store.Auth.Token != nil {
|
|
c.BearerToken, err = c.fetchSecretKey(ctx, c.store.Auth.Token.BearerToken)
|
|
if err != nil {
|
|
return fmt.Errorf("could not fetch Auth.Token.BearerToken: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
if c.store.Auth.ServiceAccount != nil {
|
|
c.BearerToken, err = c.serviceAccountToken(ctx, c.store.Auth.ServiceAccount)
|
|
if err != nil {
|
|
return fmt.Errorf("could not fetch Auth.ServiceAccount: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
if c.store.Auth.Cert != nil {
|
|
return c.setClientCert(ctx)
|
|
}
|
|
return fmt.Errorf("no credentials provided")
|
|
}
|
|
|
|
func (c *Client) setCA(ctx context.Context) error {
|
|
if c.store.Server.CABundle != nil {
|
|
c.CA = c.store.Server.CABundle
|
|
return nil
|
|
}
|
|
if c.store.Server.CAProvider != nil {
|
|
var ca []byte
|
|
var err error
|
|
switch c.store.Server.CAProvider.Type {
|
|
case esv1beta1.CAProviderTypeConfigMap:
|
|
keySelector := esmeta.SecretKeySelector{
|
|
Name: c.store.Server.CAProvider.Name,
|
|
Namespace: c.store.Server.CAProvider.Namespace,
|
|
Key: c.store.Server.CAProvider.Key,
|
|
}
|
|
ca, err = c.fetchConfigMapKey(ctx, keySelector)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to fetch Server.CAProvider ConfigMap: %w", err)
|
|
}
|
|
case esv1beta1.CAProviderTypeSecret:
|
|
keySelector := esmeta.SecretKeySelector{
|
|
Name: c.store.Server.CAProvider.Name,
|
|
Namespace: c.store.Server.CAProvider.Namespace,
|
|
Key: c.store.Server.CAProvider.Key,
|
|
}
|
|
ca, err = c.fetchSecretKey(ctx, keySelector)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to fetch Server.CAProvider Secret: %w", err)
|
|
}
|
|
}
|
|
c.CA = ca
|
|
return nil
|
|
}
|
|
return fmt.Errorf("no Certificate Authority provided")
|
|
}
|
|
|
|
func (c *Client) setClientCert(ctx context.Context) error {
|
|
var err error
|
|
c.Certificate, err = c.fetchSecretKey(ctx, c.store.Auth.Cert.ClientCert)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to fetch client certificate: %w", err)
|
|
}
|
|
c.Key, err = c.fetchSecretKey(ctx, c.store.Auth.Cert.ClientKey)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to fetch client key: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) serviceAccountToken(ctx context.Context, serviceAccountRef *esmeta.ServiceAccountSelector) ([]byte, error) {
|
|
namespace := c.namespace
|
|
if (c.storeKind == esv1beta1.ClusterSecretStoreKind) &&
|
|
(serviceAccountRef.Namespace != nil) {
|
|
namespace = *serviceAccountRef.Namespace
|
|
}
|
|
expirationSeconds := int64(3600)
|
|
tr, err := c.ctrlClientset.ServiceAccounts(namespace).CreateToken(ctx, serviceAccountRef.Name, &authenticationv1.TokenRequest{
|
|
Spec: authenticationv1.TokenRequestSpec{
|
|
Audiences: serviceAccountRef.Audiences,
|
|
ExpirationSeconds: &expirationSeconds,
|
|
},
|
|
}, metav1.CreateOptions{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf(errUnableCreateToken, err)
|
|
}
|
|
return []byte(tr.Status.Token), nil
|
|
}
|
|
|
|
func (c *Client) fetchSecretKey(ctx context.Context, key esmeta.SecretKeySelector) ([]byte, error) {
|
|
keySecret := &corev1.Secret{}
|
|
objectKey := types.NamespacedName{
|
|
Name: key.Name,
|
|
Namespace: c.namespace,
|
|
}
|
|
// only ClusterStore is allowed to set namespace (and then it's required)
|
|
if c.storeKind == esv1beta1.ClusterSecretStoreKind {
|
|
if key.Namespace == nil {
|
|
return nil, fmt.Errorf(errInvalidClusterStoreMissingNamespace)
|
|
}
|
|
objectKey.Namespace = *key.Namespace
|
|
}
|
|
err := c.ctrlClient.Get(ctx, objectKey, keySecret)
|
|
if err != nil {
|
|
return nil, fmt.Errorf(errFetchCredentials, err)
|
|
}
|
|
val, ok := keySecret.Data[key.Key]
|
|
if !ok {
|
|
return nil, fmt.Errorf(errMissingCredentials, key.Key)
|
|
}
|
|
if len(val) == 0 {
|
|
return nil, fmt.Errorf(errEmptyKey, key.Key)
|
|
}
|
|
return val, nil
|
|
}
|
|
|
|
func (c *Client) fetchConfigMapKey(ctx context.Context, key esmeta.SecretKeySelector) ([]byte, error) {
|
|
configMap := &corev1.ConfigMap{}
|
|
objectKey := types.NamespacedName{
|
|
Name: key.Name,
|
|
Namespace: c.namespace,
|
|
}
|
|
// only ClusterStore is allowed to set namespace (and then it's required)
|
|
if c.storeKind == esv1beta1.ClusterSecretStoreKind {
|
|
if key.Namespace == nil {
|
|
return nil, fmt.Errorf(errInvalidClusterStoreMissingNamespace)
|
|
}
|
|
objectKey.Namespace = *key.Namespace
|
|
}
|
|
err := c.ctrlClient.Get(ctx, objectKey, configMap)
|
|
if err != nil {
|
|
return nil, fmt.Errorf(errFetchCredentials, err)
|
|
}
|
|
val, ok := configMap.Data[key.Key]
|
|
if !ok {
|
|
return nil, fmt.Errorf(errMissingCredentials, key.Key)
|
|
}
|
|
if val == "" {
|
|
return nil, fmt.Errorf(errEmptyKey, key.Key)
|
|
}
|
|
return []byte(val), nil
|
|
}
|