Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve security of Kubernetes control plane components
Fixes #3765 See #3581 There are several changes: * `kube-controller-manager` insecure port is disabled * `kube-controller-manager` and `kube-scheduler` now listen securely only on localhost by default, this can be overridden with `--bind-addr` in extra args * `kube-controller-manager` and `kube-scheduler` now use kubeconfig with limited access role instead of admin one Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
- Loading branch information
Showing
8 changed files
with
223 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 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 kubeconfig | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/talos-systems/crypto/x509" | ||
|
||
"github.com/talos-systems/talos/pkg/machinery/config" | ||
"github.com/talos-systems/talos/pkg/machinery/constants" | ||
) | ||
|
||
const kubeConfigTemplate = `apiVersion: v1 | ||
kind: Config | ||
clusters: | ||
- name: {{ .ClusterName }} | ||
cluster: | ||
server: {{ .Endpoint }} | ||
certificate-authority-data: {{ .CACert | base64Encode }} | ||
users: | ||
- name: {{ .Username }}@{{ .ClusterName }} | ||
user: | ||
client-certificate-data: {{ .ClientCert | base64Encode }} | ||
client-key-data: {{ .ClientKey | base64Encode }} | ||
contexts: | ||
- context: | ||
cluster: {{ .ClusterName }} | ||
namespace: default | ||
user: {{ .Username }}@{{ .ClusterName }} | ||
name: {{ .ContextName }}@{{ .ClusterName }} | ||
current-context: {{ .ContextName }}@{{ .ClusterName }} | ||
` | ||
|
||
// GenerateAdminInput is the interface for the GenerateAdmin function. | ||
// | ||
// This interface is implemented by config.Cluster(). | ||
type GenerateAdminInput interface { | ||
Name() string | ||
Endpoint() *url.URL | ||
CA() *x509.PEMEncodedCertificateAndKey | ||
AdminKubeconfig() config.AdminKubeconfig | ||
} | ||
|
||
// GenerateAdmin generates admin kubeconfig for the cluster. | ||
func GenerateAdmin(config GenerateAdminInput, out io.Writer) error { | ||
return Generate( | ||
&GenerateInput{ | ||
ClusterName: config.Name(), | ||
CA: config.CA(), | ||
CertificateLifetime: config.AdminKubeconfig().CertLifetime(), | ||
|
||
CommonName: constants.KubernetesAdminCertCommonName, | ||
Organization: constants.KubernetesAdminCertOrganization, | ||
|
||
Endpoint: config.Endpoint().String(), | ||
Username: "admin", | ||
ContextName: "admin", | ||
}, | ||
out, | ||
) | ||
} | ||
|
||
// GenerateInput are input parameters for Generate. | ||
type GenerateInput struct { | ||
ClusterName string | ||
|
||
CA *x509.PEMEncodedCertificateAndKey | ||
CertificateLifetime time.Duration | ||
|
||
CommonName string | ||
Organization string | ||
|
||
Endpoint string | ||
Username string | ||
ContextName string | ||
} | ||
|
||
// Generate a kubeconfig for the cluster from the given Input. | ||
func Generate(in *GenerateInput, out io.Writer) error { | ||
tpl, err := template.New("kubeconfig").Funcs(template.FuncMap{ | ||
"base64Encode": base64Encode, | ||
}).Parse(kubeConfigTemplate) | ||
if err != nil { | ||
return fmt.Errorf("error parsing kubeconfig template: %w", err) | ||
} | ||
|
||
k8sCA, err := x509.NewCertificateAuthorityFromCertificateAndKey(in.CA) | ||
if err != nil { | ||
return fmt.Errorf("error getting Kubernetes CA: %w", err) | ||
} | ||
|
||
clientCert, err := x509.NewKeyPair(k8sCA, | ||
x509.CommonName(in.CommonName), | ||
x509.Organization(in.Organization), | ||
x509.NotAfter(time.Now().Add(in.CertificateLifetime))) | ||
if err != nil { | ||
return fmt.Errorf("error generating Kubernetes client certificate: %w", err) | ||
} | ||
|
||
clientCertPEM := x509.NewCertificateAndKeyFromKeyPair(clientCert) | ||
|
||
return tpl.Execute(out, struct { | ||
GenerateInput | ||
|
||
CACert string | ||
ClientCert string | ||
ClientKey string | ||
}{ | ||
GenerateInput: *in, | ||
CACert: string(in.CA.Crt), | ||
ClientCert: string(clientCertPEM.Crt), | ||
ClientKey: string(clientCertPEM.Key), | ||
}) | ||
} | ||
|
||
func base64Encode(content interface{}) (string, error) { | ||
str, ok := content.(string) | ||
if !ok { | ||
return "", fmt.Errorf("argument to base64 encode is not a string: %v", content) | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString([]byte(str)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.