Skip to content

Commit

Permalink
cmd/containerboot: fix nil pointer exception (cherry-pick of #14357, #…
Browse files Browse the repository at this point in the history
…14358) (#14359)

* cmd/containerboot: guard kubeClient against nil dereference (#14357)

A method on kc was called unconditionally, even if was not initialized,
leading to a nil pointer dereference when TS_SERVE_CONFIG was set
outside Kubernetes.

Add a guard symmetric with other uses of the kubeClient.

Signed-off-by: Bjorn Neergaard <[email protected]>
(cherry picked from commit 8b1d011)

* cmd/containerboot: don't attempt to write kube Secret in non-kube environments (#14358)

Signed-off-by: Irbe Krumina <[email protected]>
(cherry picked from commit 0cc071f)

* cmd/containerboot: don't attempt to patch a Secret field without permissions (#14365)

Signed-off-by: Irbe Krumina <[email protected]>
(cherry picked from commit 6e552f6)

Updates #14354
  • Loading branch information
irbekrm authored Dec 11, 2024
1 parent 3e3d5d8 commit 6e0f168
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/containerboot/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type kubeClient struct {
kubeclient.Client
stateSecret string
canPatch bool // whether the client has permissions to patch Kubernetes Secrets
}

func newKubeClient(root string, stateSecret string) (*kubeClient, error) {
Expand Down
6 changes: 4 additions & 2 deletions cmd/containerboot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ authLoop:
if err := client.SetServeConfig(ctx, new(ipn.ServeConfig)); err != nil {
log.Fatalf("failed to unset serve config: %v", err)
}
if err := kc.storeHTTPSEndpoint(ctx, ""); err != nil {
log.Fatalf("failed to update HTTPS endpoint in tailscale state: %v", err)
if hasKubeStateStore(cfg) {
if err := kc.storeHTTPSEndpoint(ctx, ""); err != nil {
log.Fatalf("failed to update HTTPS endpoint in tailscale state: %v", err)
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/containerboot/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ func watchServeConfigChanges(ctx context.Context, path string, cdChanged <-chan
if err := updateServeConfig(ctx, sc, certDomain, lc); err != nil {
log.Fatalf("serve proxy: error updating serve config: %v", err)
}
if err := kc.storeHTTPSEndpoint(ctx, certDomain); err != nil {
log.Fatalf("serve proxy: error storing HTTPS endpoint: %v", err)
if kc != nil && kc.canPatch {
if err := kc.storeHTTPSEndpoint(ctx, certDomain); err != nil {
log.Fatalf("serve proxy: error storing HTTPS endpoint: %v", err)
}
}
prevServeConfig = sc
}
Expand Down
1 change: 1 addition & 0 deletions cmd/containerboot/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func (cfg *settings) setupKube(ctx context.Context, kc *kubeClient) error {
return fmt.Errorf("some Kubernetes permissions are missing, please check your RBAC configuration: %v", err)
}
cfg.KubernetesCanPatch = canPatch
kc.canPatch = canPatch

s, err := kc.GetSecret(ctx, cfg.KubeSecret)
if err != nil {
Expand Down

0 comments on commit 6e0f168

Please sign in to comment.