Security
Intermediate 17 min read Security

Authentication & Authorization

Authentication proves identity, authorization grants access. Master passwords, MFA, tokens, JWT, OAuth 2.0 / OIDC and RBAC so you can secure both humans and machines.

authenticationauthorizationMFAJWTOAuth 2.0OIDCRBACservice accounts
Introduction

What is this?

Authentication answers who are you; authorization answers what may you do. In DevOps you must secure humans (SSO, MFA) and machines (service accounts, tokens, workload identity). OAuth 2.0 / OIDC provide the modern token ecosystem, and JWT is the token format almost everything uses.

Motivation

Why do you need it?

  • Weak or stolen credentials are the number one way attackers enter systems — identity is the new perimeter.
  • Authorizing by identity instead of shared secrets lets you revoke one person without breaking everyone.
  • OIDC plus short-lived tokens shrink the window in which a leaked token is useful.
  • Machine identities (service accounts, workload identity) need the same rigor as human identities.
  • RBAC makes who-can-do-what reviewable, auditable and revocable.
Think about it

Mental model

Imagine

An airport security line

Think of

Authentication and authorization

First you prove who you are with your boarding pass (authentication). Then your ticket determines which gate, seat and lounge you may enter (authorization). One step proves identity; the other grants scope.

Mechanics

How it works

  • Authentication verifies identity via something you know (password), have (MFA device, key) or are (biometrics).
  • MFA combines factors so a stolen password alone is not enough.
  • The system then issues an assertion — a session cookie or token — that carries the authenticated identity.
  • Authorization systems (RBAC, ABAC, policies) evaluate that identity against the permissions of the resource.
  • Tokens are typically short-lived JWTs signed by the identity provider, verified by the resource without a round trip.

The OIDC authorization code flow.

User clicks log in
User authenticates
Authorization code
App exchanges code
App calls APIs
Reference

Key commands

kubectl auth can-i get pods

Ask the cluster whether your current identity may list pods.

kubectl auth can-i create secrets --as=system:serviceaccount:default:builder

Check permissions as if you were a given service account.

kubectl get roles,rolebindings,clusterroles,clusterrolebindings -A | head -20

Dump the RBAC surface so you can audit who can do what.

id && whoami

Inspect local account identity and group membership.

curl -sI https://auth.example.com/oauth2/authorize | head -5

Confirm an OAuth authorization endpoint is reachable and enforcing TLS.

Hands-on

Examples

Inspect a token and test authorization

Inspect a token and test authorization
kubectl auth can-i get pods --as=system:serviceaccount:default:app
kubectl auth can-i delete pods --as=system:serviceaccount:default:app
kubectl auth can-i --list | head -15

kubectl auth can-i answers authorization questions directly against the API server's RBAC engine, both for your own identity and impersonated identities like a service account. The list variant prints the full permission matrix.

Real world

In production

  • SSO via OIDC (Okta, Entra ID, Keycloak) so humans authenticate once with MFA enforced.
  • Kubernetes RBAC scopes ServiceAccounts to the namespace and workload they serve.
  • CI/CD platforms federate through OIDC so pipelines get short-lived cloud tokens without long-lived static keys.
  • Fine-grained authorization (OPA/Kyverno, IAM conditions, ABAC) evaluates policies on every request.
  • Session and token revocation is wired to the identity provider so a fired employee loses access instantly.
Guidelines

Best practices

  • Enforce MFA for every human account, especially administrators.
  • Prefer short-lived tokens and refresh them; never issue tokens that never expire.
  • Give service accounts and roles the smallest scope that works (least privilege).
  • Store password hashes with a slow, salted algorithm (bcrypt, argon2) — never plaintext.
  • Log authentication and authorization decisions to an immutable audit trail.
Watch out

Common mistakes

  • Using the same long-lived API key for every environment.
  • Putting tokens in the client where anyone with the bundle can steal them.
  • Authorizing by is-the-token-valid without checking scope or audience.
  • Granting admin to whole teams because individual roles are too much work.
  • Trusting a JWT without verifying its signature and expiry.
Root cause analysis

Common issues

Error
401 Unauthorized on an API call
Root cause

Missing, expired or malformed bearer token

Fix

Re-authenticate, check the token expiry claim, and confirm it is sent in the Authorization header.

Error
403 Forbidden despite a valid token
Root cause

Token is valid but the identity lacks the permission

Fix

Review the subject's roles/grants and the resource's policy; test with can-i.

Error
Token valid in dev but rejected in prod
Root cause

Different signing keys or audience between environments

Fix

Align the OIDC issuer, audience and JWKS across environments and rotation.

When things break

Debugging tips

  • Decode the JWT payload to confirm the expected issuer, subject, expiry and scopes.
  • Test authorization with kubectl auth can-i to isolate policy from network issues.
  • Check the identity provider logs: a failed login has an explicit reason (bad password, MFA declined, disabled user).
  • Verify the audience claim — a token minted for API A must be rejected by API B.
Interviews

Interview checkpoint

Practice

Hands-on lab

Linux Permissions & Access Control
25 min · intermediate
In progress
Objectives
  • Read account identity and group membership
  • Apply file permissions with chmod
  • Change ownership with chown
  • Audit a directory tree for exposure

Linux Permissions & Access Control

25 min · intermediate

0/6 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/3
1

Which step happens first in the AAA model?

2

A valid, unexpired JWT from the right issuer still returns 403. What is the most likely cause?

3

JWT claims like iss and exp must be validated by the receiving service before trusting the token.

Quick reference

Cheat sheet

Token handling
header.payload.signatureJWT structure (base64url segments)
iss / sub / exp / audIssuer, subject, expiry, audience claims
Authorization: Bearer <token>Standard way to send access tokens
kubectl auth can-iTest RBAC authorization instantly
Design choices
MFASomething you know + something you have
OIDCOAuth 2.0 + identity layer for SSO
Service accountMachine identity with scoped permissions
Refresh tokenShort-lived access, revocable refresh
Go deeper

Official documentation