Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions cmd/fleet-registration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"os"
"slices"
"sort"
"strings"
"time"
Expand All @@ -24,24 +25,27 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"go.datum.net/fleet/internal/capacity"
"go.datum.net/fleet/internal/custody/creatorwebhook"
)

func main() {
var configFile, providers, cert, key, clientCA string
var configFile, providers, cert, key, clientCA, clients, projectors string
flag.StringVar(&configFile, "config", "", "Fleet manager discovery configuration")
flag.StringVar(&providers, "provider-projects", "", "complete comma-separated provider scope searched for duplicate hardware identity")
flag.StringVar(&cert, "tls-cert", "/etc/fleet/registration/tls.crt", "serving certificate")
flag.StringVar(&key, "tls-key", "/etc/fleet/registration/tls.key", "serving key")
flag.StringVar(&clientCA, "client-ca", "/etc/fleet/registration/client-ca.crt", "CA dedicated to trusted API-server webhook clients")
flag.StringVar(&clients, "client-identities", "", "required comma-separated subjects of the API-server webhook client certificates; any other certificate is refused")
flag.StringVar(&projectors, "capacity-projectors", "", "comma-separated usernames of the capacity projector, the only writer of registered capacity status")
flag.Parse()
if err := run(ctrl.SetupSignalHandler(), configFile, providers, cert, key, clientCA); err != nil {
if err := run(ctrl.SetupSignalHandler(), configFile, providers, cert, key, clientCA, list(clients), list(projectors)); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func run(ctx context.Context, configFile, providers, cert, key, clientCA string) error {
func run(ctx context.Context, configFile, providers, cert, key, clientCA string, clients, projectors []string) error {
config, err := projectdiscovery.LoadConfig(configFile)
if err != nil {
return err
Expand Down Expand Up @@ -72,7 +76,11 @@ func run(ctx context.Context, configFile, providers, cert, key, clientCA string)
}
}
handler := registration.NewHandler(config.AuthorityID, projects, root, template, scheme)
tlsConfig, err := webhookTLS(clientCA)
handler.Projectors = projectors
// No cleaning policy resolver exists yet: the cleaning policy owner
// provides it. Until then no registered capacity can be enabled.
handler.Cleaning = capacity.Unresolved
tlsConfig, err := webhookTLS(clientCA, clients)
if err != nil {
return err
}
Expand Down Expand Up @@ -101,7 +109,24 @@ func providerScope(providers string) ([]string, error) {
return projects, nil
}

func webhookTLS(clientCA string) (*tls.Config, error) {
func list(value string) []string {
var out []string
for _, item := range strings.Split(value, ",") {
if item = strings.TrimSpace(item); item != "" {
out = append(out, item)
}
}
return out
}

// webhookTLS accepts only clients whose certificate chains to the dedicated
// client CA and whose subject common name is one of the configured API
// server identities. Any other certificate is refused during the handshake,
// before a request is read.
func webhookTLS(clientCA string, identities []string) (*tls.Config, error) {
if len(identities) == 0 {
return nil, errors.New("--client-identities must name the API-server webhook clients")
}
caBytes, err := os.ReadFile(clientCA)
if err != nil {
return nil, err
Expand All @@ -110,5 +135,11 @@ func webhookTLS(clientCA string) (*tls.Config, error) {
if !pool.AppendCertsFromPEM(caBytes) {
return nil, errors.New("API-server client CA is invalid")
}
return &tls.Config{MinVersion: tls.VersionTLS13, ClientAuth: tls.RequireAndVerifyClientCert, ClientCAs: pool}, nil
return &tls.Config{MinVersion: tls.VersionTLS13, ClientAuth: tls.RequireAndVerifyClientCert, ClientCAs: pool,
VerifyConnection: func(state tls.ConnectionState) error {
if len(state.PeerCertificates) == 0 || !slices.Contains(identities, state.PeerCertificates[0].Subject.CommonName) {
return errors.New("the client certificate is not a configured API-server webhook client")
}
return nil
}}, nil
}
20 changes: 18 additions & 2 deletions cmd/fleet-registration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestWebhookRequiresTrustedAPIServerClient(t *testing.T) {
if err := os.WriteFile(path, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca}), 0600); err != nil {
t.Fatal(err)
}
config, err := webhookTLS(path)
config, err := webhookTLS(path, []string{"synthetic-api-server"})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -68,7 +68,23 @@ func TestWebhookRequiresTrustedAPIServerClient(t *testing.T) {
response.Body.Close()
t.Fatal("untrusted client certificate was accepted")
}
if _, err := webhookTLS(filepath.Join(t.TempDir(), "absent")); err == nil {
if _, err := webhookTLS(filepath.Join(t.TempDir(), "absent"), []string{"synthetic-api-server"}); err == nil {
t.Fatal("missing CA accepted")
}
if _, err := webhookTLS(path, nil); err == nil {
t.Fatal("a webhook with no configured client identity started")
}
// A certificate from the same CA with another subject is refused during
// the handshake.
other := &x509.Certificate{SerialNumber: big.NewInt(3), Subject: pkix.Name{CommonName: "synthetic-other-client"}, NotBefore: issuer.NotBefore, NotAfter: issuer.NotAfter, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, KeyUsage: x509.KeyUsageDigitalSignature}
otherCertificate, err := x509.CreateCertificate(rand.Reader, other, issuer, publicKey, privateKey)
if err != nil {
t.Fatal(err)
}
otherTransport := server.Client().Transport.(*http.Transport).Clone()
otherTransport.TLSClientConfig.Certificates = []tls.Certificate{{Certificate: [][]byte{otherCertificate}, PrivateKey: privateKey}}
if response, err := (&http.Client{Transport: otherTransport}).Get(server.URL); err == nil {
response.Body.Close()
t.Fatal("a client certificate with an unlisted subject was accepted")
}
}
5 changes: 5 additions & 0 deletions config/base/registration/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ spec:
args:
- --config=/etc/fleet/manager/config.yaml
- --provider-projects=datum-cloud
# Environment-owned: the subjects of the API-server webhook
# client certificates, and the capacity projector's username.
# The placeholders accept no real caller.
- --client-identities=replace-with-api-server-webhook-client
- --capacity-projectors=replace-with-capacity-projector
ports:
- name: https
containerPort: 9443
Expand Down
18 changes: 10 additions & 8 deletions config/components/registration-admission/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ metadata:
name: fleet-capacity-registration
webhooks:
# Only inventory-backed capacity is routed here, and it is fail-closed.
# Creates are checked against live Devices; updates and deletes are always
# denied. Capacity is inventory-backed when it carries a registration
# annotation (fleet.datumapis.com/registration-*) on the new or old object,
# or when a create uses the derived device- name. Adding, changing or
# removing a registration annotation is therefore always routed. Capacity
# without the marker, such as synthetic or hand-made capacity, and every
# status write are not routed and are governed by RBAC alone.
# Creates are checked against live Devices. Updates may change only the
# allocation policy, cleaning policy and pool label, under the stage-2
# rules; deletes only remove Disabled capacity whose Device is gone or
# Retired; status writes come only from the capacity projector. Capacity is
# inventory-backed when it carries a registration annotation
# (fleet.datumapis.com/registration-*) on the new or old object, or when a
# create uses the derived device- name. Capacity without the marker, such
# as synthetic or hand-made capacity, is not routed and is governed by RBAC
# alone.
- name: registration.fleet.datumapis.com
admissionReviewVersions: [v1]
failurePolicy: Fail
Expand All @@ -23,7 +25,7 @@ webhooks:
- apiGroups: [metal.datumapis.com]
apiVersions: [v1alpha1]
operations: [CREATE, UPDATE, DELETE]
resources: [machinecapacities]
resources: [machinecapacities, machinecapacities/status]
scope: Namespaced
matchConditions:
- name: inventory-backed-capacity
Expand Down
Loading
Loading