2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-14 15:50:14 +00:00
|
|
|
|
|
|
|
// Package config contains functionality to load/store api.Config's from/to
|
|
|
|
// some source.
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
"strings"
|
2020-07-14 15:50:14 +00:00
|
|
|
|
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/internal/constable"
|
|
|
|
"go.pinniped.dev/pkg/config/api"
|
2020-07-14 15:50:14 +00:00
|
|
|
)
|
|
|
|
|
2020-08-20 19:17:18 +00:00
|
|
|
const (
|
|
|
|
aboutAYear = 60 * 60 * 24 * 365
|
|
|
|
about9Months = 60 * 60 * 24 * 30 * 9
|
|
|
|
)
|
|
|
|
|
|
|
|
// FromPath loads an api.Config from a provided local file path, inserts any
|
|
|
|
// defaults (from the api.Config documentation), and verifies that the config is
|
|
|
|
// valid (per the api.Config documentation).
|
2020-07-14 15:50:14 +00:00
|
|
|
//
|
|
|
|
// Note! The api.Config file should contain base64-encoded WebhookCABundle data.
|
|
|
|
// This function will decode that base64-encoded data to PEM bytes to be stored
|
|
|
|
// in the api.Config.
|
|
|
|
func FromPath(path string) (*api.Config, error) {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var config api.Config
|
|
|
|
if err := yaml.Unmarshal(data, &config); err != nil {
|
|
|
|
return nil, fmt.Errorf("decode yaml: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-20 19:17:18 +00:00
|
|
|
maybeSetAPIDefaults(&config.APIConfig)
|
2020-09-21 18:16:14 +00:00
|
|
|
maybeSetKubeCertAgentDefaults(&config.KubeCertAgentConfig)
|
2020-08-20 19:17:18 +00:00
|
|
|
|
|
|
|
if err := validateAPI(&config.APIConfig); err != nil {
|
|
|
|
return nil, fmt.Errorf("validate api: %w", err)
|
|
|
|
}
|
|
|
|
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
if err := validateNames(&config.NamesConfig); err != nil {
|
|
|
|
return nil, fmt.Errorf("validate names: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-14 15:50:14 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|
2020-08-20 19:17:18 +00:00
|
|
|
|
|
|
|
func maybeSetAPIDefaults(apiConfig *api.APIConfigSpec) {
|
|
|
|
if apiConfig.ServingCertificateConfig.DurationSeconds == nil {
|
|
|
|
apiConfig.ServingCertificateConfig.DurationSeconds = int64Ptr(aboutAYear)
|
|
|
|
}
|
|
|
|
|
|
|
|
if apiConfig.ServingCertificateConfig.RenewBeforeSeconds == nil {
|
|
|
|
apiConfig.ServingCertificateConfig.RenewBeforeSeconds = int64Ptr(about9Months)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 18:16:14 +00:00
|
|
|
func maybeSetKubeCertAgentDefaults(cfg *api.KubeCertAgentSpec) {
|
|
|
|
if cfg.NamePrefix == nil {
|
|
|
|
cfg.NamePrefix = stringPtr("pinniped-kube-cert-agent-")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Image == nil {
|
|
|
|
cfg.Image = stringPtr("debian:latest")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
func validateNames(names *api.NamesConfigSpec) error {
|
|
|
|
missingNames := []string{}
|
|
|
|
if names == nil {
|
|
|
|
missingNames = append(missingNames, "servingCertificateSecret", "credentialIssuerConfig", "apiService")
|
|
|
|
} else {
|
|
|
|
if names.ServingCertificateSecret == "" {
|
|
|
|
missingNames = append(missingNames, "servingCertificateSecret")
|
|
|
|
}
|
|
|
|
if names.CredentialIssuerConfig == "" {
|
|
|
|
missingNames = append(missingNames, "credentialIssuerConfig")
|
|
|
|
}
|
|
|
|
if names.APIService == "" {
|
|
|
|
missingNames = append(missingNames, "apiService")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(missingNames) > 0 {
|
|
|
|
return constable.Error("missing required names: " + strings.Join(missingNames, ", "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 19:17:18 +00:00
|
|
|
func validateAPI(apiConfig *api.APIConfigSpec) error {
|
|
|
|
if *apiConfig.ServingCertificateConfig.DurationSeconds < *apiConfig.ServingCertificateConfig.RenewBeforeSeconds {
|
|
|
|
return constable.Error("durationSeconds cannot be smaller than renewBeforeSeconds")
|
|
|
|
}
|
|
|
|
|
2020-08-20 22:14:07 +00:00
|
|
|
if *apiConfig.ServingCertificateConfig.RenewBeforeSeconds <= 0 {
|
|
|
|
return constable.Error("renewBefore must be positive")
|
|
|
|
}
|
|
|
|
|
2020-08-20 19:17:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func int64Ptr(i int64) *int64 {
|
|
|
|
return &i
|
|
|
|
}
|
2020-09-21 18:16:14 +00:00
|
|
|
|
|
|
|
func stringPtr(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|