Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello committed Mar 21, 2024
1 parent dea3716 commit bdd692f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/k8s/api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type BootstrapConfig struct {
EnableRBAC *bool `yaml:"enable-rbac"`
K8sDqlitePort int `yaml:"k8s-dqlite-port"`
Datastore string `yaml:"datastore"`
ExtraSANs string `yaml:"extrasans"`
DatastoreURL string `yaml:"datastore-url,omitempty"`
DatastoreCACert string `yaml:"datastore-ca-crt,omitempty"`
DatastoreClientCert string `yaml:"datastore-client-crt,omitempty"`
Expand All @@ -33,6 +34,7 @@ func (b *BootstrapConfig) SetDefaults() {
b.EnableRBAC = vals.Pointer(true)
b.K8sDqlitePort = 9000
b.Datastore = "k8s-dqlite"
b.ExtraSANs = ""
}

// ToMap marshals the BootstrapConfig into yaml and map it to "bootstrapConfig".
Expand Down
4 changes: 4 additions & 0 deletions src/k8s/cmd/k8s/k8s_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func getConfigInteractively(stdin io.Reader, stdout io.Writer, stderr io.Writer)
config.ServiceCIDR = askQuestion(stdin, stdout, stderr, "Please set the Service CIDR:", nil, config.ServiceCIDR, nil)
rbac := askBool(stdin, stdout, stderr, "Enable Role Based Access Control (RBAC)?", []string{"yes", "no"}, "yes")
*config.EnableRBAC = rbac
extraSANs := askBool(stdin, stdout, stderr, "Set extra SANs for the certificates?", []string{"yes", "no"}, "no")
if extraSANs {
config.ExtraSANs = askQuestion(stdin, stdout, stderr, "Please set the Extra SANs:", nil, config.ExtraSANs, nil)
}
return config
}

Expand Down
1 change: 1 addition & 0 deletions src/k8s/pkg/k8sd/app/hooks_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func onBootstrapControlPlane(s *state.State, initConfig map[string]string) error
certificates := pki.NewControlPlanePKI(pki.ControlPlanePKIOpts{
Hostname: s.Name(),
IPSANs: append([]net.IP{nodeIP}, serviceIPs...),
ExtraSANs: cfg.Certificates.ExtraSANs,
Years: 20,
AllowSelfSignedCA: true,
IncludeMachineAddressSANs: true,
Expand Down
13 changes: 10 additions & 3 deletions src/k8s/pkg/k8sd/pki/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"crypto/x509/pkix"
"fmt"
"net"

"github.com/canonical/k8s/pkg/utils"
)

// ControlPlanePKI is a list of all certificates we require for a control plane node.
Expand All @@ -13,7 +15,8 @@ type ControlPlanePKI struct {
hostname string // node name
ipSANs []net.IP // IP SANs for generated certificates
dnsSANs []string // DNS SANs for the certificates below
years int // how many years the generated certificates will be valid for
extraSANs []string
years int // how many years the generated certificates will be valid for

CACert, CAKey string // CN=kubernetes-ca (self-signed)
FrontProxyCACert, FrontProxyCAKey string // CN=kubernetes-front-proxy-ca (self-signed)
Expand All @@ -34,6 +37,7 @@ type ControlPlanePKIOpts struct {
Hostname string
DNSSANs []string
IPSANs []net.IP
ExtraSANs string
Years int
AllowSelfSignedCA bool
IncludeMachineAddressSANs bool
Expand All @@ -44,11 +48,14 @@ func NewControlPlanePKI(opts ControlPlanePKIOpts) *ControlPlanePKI {
opts.Years = 1
}

userDefinedSANs := utils.GetExtraSANsFromString(opts.ExtraSANs)
userDefinedIpSANs, userDefinedDnsSANs := utils.SeparateSANs(userDefinedSANs)

return &ControlPlanePKI{
hostname: opts.Hostname,
years: opts.Years,
ipSANs: opts.IPSANs,
dnsSANs: opts.DNSSANs,
ipSANs: append(opts.IPSANs, userDefinedIpSANs...),
dnsSANs: append(opts.DNSSANs, userDefinedDnsSANs...),
allowSelfSignedCA: opts.AllowSelfSignedCA,
includeMachineAddressSANs: opts.IncludeMachineAddressSANs,
}
Expand Down
1 change: 1 addition & 0 deletions src/k8s/pkg/k8sd/types/cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Certificates struct {
APIServerKubeletClientKey string `yaml:"apiserver-kubelet-client-key,omitempty"`
K8sDqliteCert string `yaml:"k8s-dqlite-crt,omitempty"`
K8sDqliteKey string `yaml:"k8s-dqlite-key,omitempty"`
ExtraSANs string `yaml:"extrasans,omitempty"`

DatastoreCACert string `yaml:"datastore-ca-crt,omitempty"`
DatastoreClientCert string `yaml:"datastore-client-crt,omitempty"`
Expand Down
26 changes: 26 additions & 0 deletions src/k8s/pkg/utils/certificate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utils

import (
"net"
"strings"
)

func GetExtraSANsFromString(extraSANs string) []string {
// TODO: Add validation for the extraSANs
return strings.Split(extraSANs, ",")
}

func SeparateSANs(extraSANs []string) ([]net.IP, []string) {
var ipSANs []net.IP
var dnsSANs []string

for _, san := range extraSANs {
if ip := net.ParseIP(san); ip != nil {
ipSANs = append(ipSANs, ip)
} else {
dnsSANs = append(dnsSANs, san)
}
}

return ipSANs, dnsSANs
}

0 comments on commit bdd692f

Please sign in to comment.