Files
nvke/auth/authn/setup.go
Abhay Krishna 51dc0c4dae feat!: make apiAuth the only API authentication source (#1938)
apiAuth in nvke-controller-config becomes the only place the
controller reads its API authentication configuration from. This is
the breaking second step of phase 2.

- The controller refuses to start when AUTH_CONFIG_PATH is set, naming
  apiAuth as the replacement, and passes authn.Setup only the apiAuth
  section. authn.Load and Options.ConfigPath stay for
  management-engine's --auth-config.
- The chart no longer renders or mounts nvke-auth-config. It refuses
  apiAuth.configMap and an AUTH_CONFIG_PATH env entry by name, and a
  caBundlePath into the old mount, pointing at jwks.caBundle.
- apiAuth.permissive requires apiAuth.enabled, in the chart and at
  startup. Tilt passes it only with authentication on.
- controllerconfig, which decodes the apiAuth section, gets the
  decoder-strictness tests.
- nvkectl and the token scripts keep their fallback to the
  authentication ConfigMap for controllers from before this change.
- The e2e suite reads the enforcement mode from apiAuth alone.
- The pod template carries a checksum of nvke-controller-config, so an
  upgrade that changes apiAuth rolls the controller without Reloader,
  which the NVKE stack does not install.
- API authorization (#2125) counts authentication as on exactly when
  apiAuth.enabled is.

BREAKING CHANGE: AUTH_CONFIG_PATH and apiAuth.configMap are removed.
A site that managed its own authentication ConfigMap moves audiences
and issuers into apiAuth.config, CA bundles into jwks.caBundle, and
sets apiAuth.enabled. apiAuth.permissive without apiAuth.enabled is
refused. nvkectl older than the previous step gets 401.

Signed-off-by: Abhay Krishna Arunachalam <abhaykrishna@nvidia.com>
2026-11-17 13:25:25 -08:00

118 lines
3.8 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package authn
import (
"context"
"fmt"
"log/slog"
)
// Options is what a deployment configures. Config or ConfigPath decides whether
// authentication runs at all; the other two apply only when it does.
type Options struct {
// ConfigPath is a YAML file holding the issuer registry, for a server that
// takes it as a file, such as management-engine's public API. Empty means no
// file. The NVKE controller reads only Config.
ConfigPath string
// Permissive evaluates and logs every decision without acting on it, so no
// request is rejected.
Permissive bool
// Recorder counts decisions. Optional: nil records nothing.
Recorder Recorder
// Config is the issuer registry read from apiAuth in
// nvke-controller-config. Nil means none. At most one of Config and
// ConfigPath may be set.
Config *Config
}
// Where Setup read the issuer registry from, as its startup log names it.
const (
sourceControllerConfig = "nvke-controller-config"
sourceConfigPath = "file"
)
// Setup builds the Authenticator for a deployment, or reports that
// authentication is disabled.
//
// With neither Config nor ConfigPath it returns (nil, nil), meaning disabled:
// the caller leaves the middleware out of the chain. A nil Authenticator is not
// a permissive one.
//
// A ConfigPath that is set but cannot be read, parsed or validated is an error,
// never "disabled", and so is a Config that fails ValidateStartup.
func Setup(ctx context.Context, opts Options, logger *slog.Logger) (*Authenticator, error) {
cfg, source, err := opts.load()
if err != nil {
return nil, err
}
if cfg == nil {
logger.Info("API authentication is disabled", "reason", "apiAuth is not enabled in nvke-controller-config and no authentication config path is set")
return nil, nil //nolint:nilnil // a nil Authenticator and a nil error is exactly "disabled"
}
mode := ModeEnforcing
if opts.Permissive {
mode = ModePermissive
}
authLogger := logger.With("logger", "auth")
// Enforcing refuses to start without every issuer's keys; permissive logs and
// carries on, since it rejects nobody anyway.
sources, err := NewKeySources(ctx, cfg, mode, authLogger)
if err != nil {
return nil, fmt.Errorf("building issuer key sources: %w", err)
}
verifier, err := NewVerifier(cfg, sources)
if err != nil {
return nil, fmt.Errorf("building token verifier: %w", err)
}
authenticator, err := NewAuthenticator(verifier, mode, authLogger, opts.Recorder)
if err != nil {
return nil, fmt.Errorf("building the authentication middleware: %w", err)
}
issuers := make([]string, 0, len(cfg.Issuers))
for _, issuer := range cfg.Issuers {
issuers = append(issuers, issuer.Name)
}
logger.Info("API authentication is enabled",
"mode", string(mode),
"source", source,
"config_path", opts.ConfigPath,
"audiences", cfg.Audiences,
"issuers", issuers,
)
return authenticator, nil
}
// load returns the issuer registry and the name of its source, or a nil
// registry when authentication is disabled.
func (o Options) load() (*Config, string, error) {
switch {
case o.Config != nil && o.ConfigPath != "":
return nil, "", fmt.Errorf("API authentication is configured twice, by apiAuth in nvke-controller-config and by the file at %q; remove one", o.ConfigPath)
case o.Config != nil:
if err := o.Config.ValidateStartup(); err != nil {
return nil, "", fmt.Errorf("invalid apiAuth in nvke-controller-config: %w", err)
}
return o.Config, sourceControllerConfig, nil
case o.ConfigPath != "":
cfg, err := Load(o.ConfigPath)
if err != nil {
return nil, "", fmt.Errorf("loading authentication config %q: %w", o.ConfigPath, err)
}
return cfg, sourceConfigPath, nil
default:
return nil, "", nil
}
}