Andrey Smirnov 31e56e63db fix: update in-cluster kubeconfig validity to match other certs
Talos generates in-cluster kubeconfig for the kube-scheduler and
kube-controller-manager to authenticate to kube-apiserver. Bug was that
validity of that kubeconfig was set to 24h by mistake. Fix that by
bumping validity to default for other Kubernetes certs (1 year).

Add a certificate refresh at 50% of the validity.

Fix bugs with copying secret resources which was leading to updates not
being propagated correctly.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2021-03-01 11:16:04 -08:00

126 lines
3.0 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package secrets
import (
"fmt"
"net"
"net/url"
"github.com/talos-systems/crypto/x509"
"github.com/talos-systems/os-runtime/pkg/resource"
"github.com/talos-systems/os-runtime/pkg/resource/core"
)
// RootType is type of Root secret resource.
const RootType = resource.Type("secrets/root")
// IDs of various resources of RootType.
const (
RootEtcdID = resource.ID("etcd-root")
RootKubernetesID = resource.ID("k8s-root")
)
// Root contains root (not generated) secrets.
type Root struct {
md resource.Metadata
spec interface{}
}
// RootEtcdSpec describes etcd CA secrets.
type RootEtcdSpec struct {
EtcdCA *x509.PEMEncodedCertificateAndKey `yaml:"etcdCA"`
}
// RootKubernetesSpec describes root Kubernetes secrets.
type RootKubernetesSpec struct {
Name string `yaml:"name"`
Endpoint *url.URL `yaml:"endpoint"`
CertSANs []string `yaml:"certSANs"`
APIServerIPs []net.IP `yaml:"apiServerIPs"`
DNSDomain string `yaml:"dnsDomain"`
CA *x509.PEMEncodedCertificateAndKey `yaml:"ca"`
ServiceAccount *x509.PEMEncodedKey `yaml:"serviceAccount"`
AggregatorCA *x509.PEMEncodedCertificateAndKey `yaml:"aggregatorCA"`
AESCBCEncryptionSecret string `yaml:"aesCBCEncryptionSecret"`
BootstrapTokenID string `yaml:"bootstrapTokenID"`
BootstrapTokenSecret string `yaml:"bootstrapTokenSecret"`
}
// NewRoot initializes a Root resource.
func NewRoot(id resource.ID) *Root {
r := &Root{
md: resource.NewMetadata(NamespaceName, RootType, id, resource.VersionUndefined),
}
switch id {
case RootEtcdID:
r.spec = &RootEtcdSpec{}
case RootKubernetesID:
r.spec = &RootKubernetesSpec{}
}
r.md.BumpVersion()
return r
}
// Metadata implements resource.Resource.
func (r *Root) Metadata() *resource.Metadata {
return &r.md
}
// Spec implements resource.Resource.
func (r *Root) Spec() interface{} {
return r.spec
}
func (r *Root) String() string {
return fmt.Sprintf("secrets.Root(%q)", r.md.ID())
}
// DeepCopy implements resource.Resource.
func (r *Root) DeepCopy() resource.Resource {
var specCopy interface{}
switch v := r.spec.(type) {
case *RootEtcdSpec:
vv := *v
specCopy = &vv
case *RootKubernetesSpec:
vv := *v
specCopy = &vv
default:
panic("unexpected spec type")
}
return &Root{
md: r.md,
spec: specCopy,
}
}
// ResourceDefinition implements core.ResourceDefinitionProvider interface.
func (r *Root) ResourceDefinition() core.ResourceDefinitionSpec {
return core.ResourceDefinitionSpec{
Type: RootType,
Aliases: []resource.Type{"rootSecret", "rootSecrets"},
DefaultNamespace: NamespaceName,
}
}
// EtcdSpec returns .spec.
func (r *Root) EtcdSpec() *RootEtcdSpec {
return r.spec.(*RootEtcdSpec)
}
// KubernetesSpec returns .spec.
func (r *Root) KubernetesSpec() *RootKubernetesSpec {
return r.spec.(*RootKubernetesSpec)
}