Authorize API callers by role
This page describes how to limit what each caller of the NVIDIA Kubernetes Engine (NVKE) API can do, by giving it a role. Do this after Secure the NVKE API: authorization decides what an admitted caller may do, so it needs authentication on.
The NVKE controller checks every API operation before it serves it, by the operation’s operationId, such as listClusters or deleteCluster. The API spec lists every operationId: it is controller/api/openapi.yaml in the checkout, and NVKE serves it without a token at <API_URL>/openapi.yaml. The paths that need no token, such as /healthz and /api/version, are not operations and are not checked.
The chart ships two roles. A role applies to every workload cluster: NVKE has no per-cluster permissions.
admin: may call every operation.viewer: may call thegetandlistoperations, such asgetClusterandlistNodePools.
A grant gives a role to the callers whose token matches it. A caller holds every role that any of its grants gives, and a caller with no role is refused every operation. You normally write only grants.
A token that authentication rejects still gets 401, or 403 with Not admitted: ..., before authorization runs.
Before you begin
- You have completed Secure the NVKE API, and its Helm values are in
auth-values.yaml, in the directory above thenvke/checkout. Authentication may still be permissive. - You run the commands on this page from the root of the
nvke/checkout, withKUBECONFIGset as in Before you begin on that page. - You know which group, or which service account, identifies each caller of the API.
Grant roles
Grants live under apiAuthz.authorizer.localBundle.config.issuers, keyed by the name of an issuer in apiAuth.config.issuers. Each issuer takes two kinds of grant:
claims: a token whose claim namednameequalsvalue, or is an array containing it, gets the roles ingrants. This is the same match asadmit.claimsin authentication. Grants have nogroupsshorthand: to grant by group, useclaimswithname: groups. Use a claim the identity provider (IdP) sets, not one users can edit.serviceAccounts: a Kubernetes service account token fromnamespacegets the roles ingrants. Setnameto match one service account; omitnameto match every service account in the namespace.
For the IdP from Secure the NVKE API, grant admin and viewer by group:
<ADMITTED_GROUP> is the group that authentication admits. <VIEWER_GROUP> is a group whose members get read-only access.
A grant applies only to callers that authentication admits, and authentication admits only <ADMITTED_GROUP>. Add <VIEWER_GROUP> to admit.groups of the existing example-idp issuer:
Edit the issuer in place. Helm replaces a list as a whole, so a second issuers list drops every field you leave out of it.
NVKE checks the grants at startup and on every reload. It refuses an issuer that apiAuth does not configure, a grant of a role the bundle does not declare, and a value for the tenancy keys operatorTenant, tenant and operators, which this API does not use.
Grant a role to a service account
Once authorization enforces, every caller that authentication admits needs a role, including automation. One such caller is management-engine, a controller in the management-engine-system namespace that manages workload clusters through the NVKE API. The install on these pages does not deploy it. A service account’s token has the subject system:serviceaccount:<NAMESPACE>:<SERVICE_ACCOUNT>. If that subject appears in the permissive-mode log in Find callers that enforcing mode would refuse, the service account calls the API and needs a grant.
Grant it under the issuer that admits it. The issuer key, local-kubernetes here, must equal the name of an apiAuth issuer that admits Kubernetes service account tokens. Secure the NVKE API does not set one up; Configuring NVKE API Authentication describes it.
<NAMESPACE> and <SERVICE_ACCOUNT> are the namespace and name of the caller’s service account.
Turn on authorization in permissive mode
-
Add the
apiAuthzandapiAuthvalues from Grant roles toauth-values.yaml. Merge them into the existing file rather than writing a second one. -
Run the install command again, with the same flags as in Secure the NVKE API, step 3.
The chart refuses to render if
apiAuthz.enabledis true whileapiAuth.enabledis false, or ifapiAuthz.permissiveis true whileapiAuthz.enabledis false. -
Wait for the rollout.
-
Check the startup log.
The output contains an
API authorization is enabledline with"mode":"permissive". If the pod does not become ready, its log containsFailed to set up API authorizationwith the problems it found in the bundle.
Find callers that enforcing mode would refuse
-
Read the authorization counter.
Every series with an
outcomeother thanallowcounts requests that enforcing mode would refuse, byaction. -
Find the caller behind each one in the log.
Each line names the
outcome, theactionand the request path asresource_id. NVKE never logs the token or its claims. Act on each line by itsoutcome: -
In
auth-values.yaml, grant eachdenycaller the role it needs. For a user, look thesubjectup in the IdP and grant a group the user belongs to. For a service account, thesubjectissystem:serviceaccount:<NAMESPACE>:<SERVICE_ACCOUNT>; grant it underserviceAccounts, as in Grant a role to a service account. -
Run the install again with the same flags.
NVKE picks up grants without a restart, as described in Change grants.
Continue when only outcome="allow" series increase.
Switch to enforcing mode
Authentication must enforce before authorization does. With authentication permissive, a request without a valid token reaches authorization with no caller, and enforcing authorization refuses it with 403 rather than 401, so the chart refuses to render apiAuthz.permissive: false while apiAuth.permissive is true. You can switch both in one install.
-
In
auth-values.yaml, setapiAuthz.permissive: false, andapiAuth.permissive: falseif it is not already. Run the install again with the same flags. -
Make sure NVKE restarted. NVKE reads the mode from the
nvke-controller-configConfigMap only at startup. The chart rolls the pod when that ConfigMap changes; without the chart, restart it: -
Wait for the rollout.
In enforcing mode, NVKE logs each refused request as
refused request, with the same fields as the permissive-mode message, or asfailed to authorize requestwhen NVKE could not decide.
If callers get an unexpected 403 with {"message":"Not authorized"}, set apiAuthz.permissive: true, run the install again and restart NVKE, as in steps 1 to 3.
Verify
-
Check the mode NVKE reports.
The output is
nvke_authz_enabled{mode="enforcing"} 1. If the mode ispermissive, NVKE has not restarted since the change: restart it as in step 2 of Switch to enforcing mode. -
Run
kubectl oidc-login cleanto drop the cached token. Sign in to the IdP as a member of<VIEWER_GROUP>who is not in<ADMITTED_GROUP>, and get a token intoTOKENas in Secure the NVKE API, step 1 of “With curl”.<API_URL>is the address described in step 2 of the same section. -
List the workload clusters.
The response is
200. -
Try to delete a cluster that does not exist.
The response is
403with{"message":"Not authorized"}, because authorization runs before NVKE looks the cluster up. With a token from a member of<ADMITTED_GROUP>, the same delete returns404.
Change grants
NVKE applies grant changes without a restart: the kubelet updates the mounted bundle within a minute or two, and NVKE reloads it every 10 seconds.
-
Change the grants under
apiAuthz.authorizer.localBundle.configinauth-values.yaml. -
Run the install again with the same flags.
-
Wait two minutes for the kubelet to update the bundle, then check that NVKE loaded the change.
The output is
Reloaded the authorization bundle, and requests from the callers you changed get the new decisions. A bundle that fails its checks does not replace the live one: NVKE keeps the last bundle that loaded, logsFailed to reload the authorization bundle; keeping the live one, and countsnvke_authz_bundle_reload_failures_total.
A change to apiAuth restarts NVKE instead: the chart rolls the pod, and NVKE loads the new bundle at startup.
Customize roles
To change what a role allows, or to add a role, set apiAuthz.authorizer.localBundle.policies (Cedar policies) and apiAuthz.authorizer.localBundle.roles (the role list) in auth-values.yaml. The defaults are in the chart’s values.yaml. A policy names actions as NVKE::Action::"<operationId>", and NVKE refuses a policy that names an action the API does not have. NVKE reloads policies and roles the same way as grants, as in Change grants.
Use Teleport application access
This section applies only if callers reach the API through Teleport application access. Teleport signs a JSON Web Token (JWT) for each request to an application, with the user’s Teleport roles in its roles claim, and has no groups claim. Admit those tokens by role in apiAuth, and grant NVKE roles by the same claim:
<TELEPORT_CLUSTER_NAME>is the Teleport cluster name, not a URL. Teleport signs it into the token as the issuer.<APP_URI>is the application’s URI in Teleport, and the token’s audience. Give the application for each management cluster its own URI: two applications that share one accept each other’s tokens.<TELEPORT_PROXY_ADDRESS>is the address of the Teleport proxy.
Add the teleport issuer to the existing apiAuth.config.issuers list, and its grants beside the existing ones, rather than replacing them.
In the Teleport application, leave rewrite.jwt_claims at its default, roles-and-traits, or set it to roles, so the token carries roles. Teleport sends the token in its Teleport-Jwt-Assertion header, so also rewrite the Authorization header. In teleport.yaml:
In a dynamic app resource: