2021-01-19 15:52:12 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-09-16 14:19:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-14 15:50:14 +00:00
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
// Package concierge contains functionality to load/store Config's from/to
|
2020-07-14 15:50:14 +00:00
|
|
|
// some source.
|
2020-10-15 19:40:56 +00:00
|
|
|
package concierge
|
2020-07-14 15:50:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-05-12 20:20:00 +00:00
|
|
|
"k8s.io/utils/pointer"
|
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"
|
2021-01-13 01:27:41 +00:00
|
|
|
"go.pinniped.dev/internal/groupsuffix"
|
2020-11-10 14:57:29 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
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
|
2021-11-17 00:43:51 +00:00
|
|
|
|
|
|
|
// Use 10250 because it happens to be the same port on which the Kubelet listens, so some cluster types
|
|
|
|
// are more permissive with servers that run on this port. For example, GKE private clusters do not
|
|
|
|
// allow traffic from the control plane to most ports, but do allow traffic to port 10250. This allows
|
|
|
|
// the Concierge to work without additional configuration on these types of clusters.
|
|
|
|
aggregatedAPIServerPortDefault = 10250
|
2021-11-17 21:27:59 +00:00
|
|
|
|
|
|
|
// Use port 8444 because that is the port that was selected for the first released version of the
|
|
|
|
// impersonation proxy, and has been the value since. It was originally selected because the
|
|
|
|
// aggregated API server used to run on 8443 (has since changed), so 8444 was the next available port.
|
|
|
|
impersonationProxyPortDefault = 8444
|
2020-08-20 19:17:18 +00:00
|
|
|
)
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
// FromPath loads an Config from a provided local file path, inserts any
|
|
|
|
// defaults (from the Config documentation), and verifies that the config is
|
|
|
|
// valid (per the Config documentation).
|
2020-07-14 15:50:14 +00:00
|
|
|
//
|
2020-10-15 19:40:56 +00:00
|
|
|
// Note! The Config file should contain base64-encoded WebhookCABundle data.
|
2020-07-14 15:50:14 +00:00
|
|
|
// This function will decode that base64-encoded data to PEM bytes to be stored
|
2020-10-15 19:40:56 +00:00
|
|
|
// in the Config.
|
|
|
|
func FromPath(path string) (*Config, error) {
|
2020-07-14 15:50:14 +00:00
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read file: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
var config Config
|
2020-07-14 15:50:14 +00:00
|
|
|
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)
|
2021-11-17 00:43:51 +00:00
|
|
|
maybeSetAggregatedAPIServerPortDefaults(&config.AggregatedAPIServerPort)
|
2021-11-17 21:27:59 +00:00
|
|
|
maybeSetImpersonationProxyServerPortDefaults(&config.ImpersonationProxyServerPort)
|
2021-01-19 15:52:12 +00:00
|
|
|
maybeSetAPIGroupSuffixDefault(&config.APIGroupSuffix)
|
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)
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:27:41 +00:00
|
|
|
if err := validateAPIGroupSuffix(*config.APIGroupSuffix); err != nil {
|
|
|
|
return nil, fmt.Errorf("validate apiGroupSuffix: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:27:59 +00:00
|
|
|
if err := validateServerPort(config.AggregatedAPIServerPort); err != nil {
|
2021-11-17 00:43:51 +00:00
|
|
|
return nil, fmt.Errorf("validate aggregatedAPIServerPort: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:27:59 +00:00
|
|
|
if err := validateServerPort(config.ImpersonationProxyServerPort); err != nil {
|
|
|
|
return nil, fmt.Errorf("validate impersonationProxyServerPort: %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-11-10 14:57:29 +00:00
|
|
|
if err := plog.ValidateAndSetLogLevelGlobally(config.LogLevel); err != nil {
|
|
|
|
return nil, fmt.Errorf("validate log level: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:14:23 +00:00
|
|
|
if config.Labels == nil {
|
|
|
|
config.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2020-07-14 15:50:14 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|
2020-08-20 19:17:18 +00:00
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
func maybeSetAPIDefaults(apiConfig *APIConfigSpec) {
|
2020-08-20 19:17:18 +00:00
|
|
|
if apiConfig.ServingCertificateConfig.DurationSeconds == nil {
|
2021-05-12 20:20:00 +00:00
|
|
|
apiConfig.ServingCertificateConfig.DurationSeconds = pointer.Int64Ptr(aboutAYear)
|
2020-08-20 19:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if apiConfig.ServingCertificateConfig.RenewBeforeSeconds == nil {
|
2021-05-12 20:20:00 +00:00
|
|
|
apiConfig.ServingCertificateConfig.RenewBeforeSeconds = pointer.Int64Ptr(about9Months)
|
2020-08-20 19:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:52:12 +00:00
|
|
|
func maybeSetAPIGroupSuffixDefault(apiGroupSuffix **string) {
|
|
|
|
if *apiGroupSuffix == nil {
|
2021-05-12 20:20:00 +00:00
|
|
|
*apiGroupSuffix = pointer.StringPtr(groupsuffix.PinnipedDefaultSuffix)
|
2021-01-19 15:52:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:27:59 +00:00
|
|
|
func maybeSetAggregatedAPIServerPortDefaults(port **int64) {
|
|
|
|
if *port == nil {
|
|
|
|
*port = pointer.Int64Ptr(aggregatedAPIServerPortDefault)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func maybeSetImpersonationProxyServerPortDefaults(port **int64) {
|
|
|
|
if *port == nil {
|
|
|
|
*port = pointer.Int64Ptr(impersonationProxyPortDefault)
|
2021-11-17 00:43:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
func maybeSetKubeCertAgentDefaults(cfg *KubeCertAgentSpec) {
|
2020-09-21 18:16:14 +00:00
|
|
|
if cfg.NamePrefix == nil {
|
2021-05-12 20:20:00 +00:00
|
|
|
cfg.NamePrefix = pointer.StringPtr("pinniped-kube-cert-agent-")
|
2020-09-21 18:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Image == nil {
|
2021-05-12 20:20:00 +00:00
|
|
|
cfg.Image = pointer.StringPtr("debian:latest")
|
2020-09-21 18:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
func validateNames(names *NamesConfigSpec) error {
|
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
|
|
|
missingNames := []string{}
|
|
|
|
if names == nil {
|
2021-03-02 17:31:24 +00:00
|
|
|
names = &NamesConfigSpec{}
|
|
|
|
}
|
|
|
|
if names.ServingCertificateSecret == "" {
|
|
|
|
missingNames = append(missingNames, "servingCertificateSecret")
|
|
|
|
}
|
|
|
|
if names.CredentialIssuer == "" {
|
|
|
|
missingNames = append(missingNames, "credentialIssuer")
|
|
|
|
}
|
|
|
|
if names.APIService == "" {
|
|
|
|
missingNames = append(missingNames, "apiService")
|
|
|
|
}
|
|
|
|
if names.ImpersonationLoadBalancerService == "" {
|
|
|
|
missingNames = append(missingNames, "impersonationLoadBalancerService")
|
|
|
|
}
|
2021-05-20 21:11:35 +00:00
|
|
|
if names.ImpersonationClusterIPService == "" {
|
|
|
|
missingNames = append(missingNames, "impersonationClusterIPService")
|
|
|
|
}
|
2021-03-02 17:31:24 +00:00
|
|
|
if names.ImpersonationTLSCertificateSecret == "" {
|
|
|
|
missingNames = append(missingNames, "impersonationTLSCertificateSecret")
|
|
|
|
}
|
|
|
|
if names.ImpersonationCACertificateSecret == "" {
|
|
|
|
missingNames = append(missingNames, "impersonationCACertificateSecret")
|
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
|
|
|
}
|
2021-03-10 18:30:06 +00:00
|
|
|
if names.ImpersonationSignerSecret == "" {
|
|
|
|
missingNames = append(missingNames, "impersonationSignerSecret")
|
|
|
|
}
|
2021-05-03 21:31:48 +00:00
|
|
|
if names.AgentServiceAccount == "" {
|
|
|
|
missingNames = append(missingNames, "agentServiceAccount")
|
2021-05-03 21:20:13 +00:00
|
|
|
}
|
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 len(missingNames) > 0 {
|
|
|
|
return constable.Error("missing required names: " + strings.Join(missingNames, ", "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:40:56 +00:00
|
|
|
func validateAPI(apiConfig *APIConfigSpec) error {
|
2020-08-20 19:17:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:27:41 +00:00
|
|
|
func validateAPIGroupSuffix(apiGroupSuffix string) error {
|
|
|
|
return groupsuffix.Validate(apiGroupSuffix)
|
|
|
|
}
|
2021-11-17 00:43:51 +00:00
|
|
|
|
2021-11-17 21:27:59 +00:00
|
|
|
func validateServerPort(port *int64) error {
|
2021-11-17 00:43:51 +00:00
|
|
|
// It cannot be below 1024 because the container is not running as root.
|
2021-11-17 21:27:59 +00:00
|
|
|
if *port < 1024 || *port > 65535 {
|
2021-11-17 00:43:51 +00:00
|
|
|
return constable.Error("must be within range 1024 to 65535")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|