"""OpenID Provider: the lab's Vault signs the user alice in through the server's provider, as a relying party (auth/oidc) with the authorization code flow and PKCE, the sign-in driven in a client as a browser would.""" import secrets import urllib.parse from lm_dev import TESTS, VaultError, enrol_otp, otp_code, store_secret, vault_call #: The FQDN of the server, the host of its issuer. FQDN = 'lm-oidc.test' #: The server's OpenID Provider, the HTTPS site of its web service. ISSUER = f'https://{FQDN}:7444' #: The redirect URI of Vault's CLI convention, registered on both sides; the driver reads it, nothing follows it. REDIRECT = 'http://localhost:8250/oidc/callback' #: The server's root CA, which the server trusts and publishes. ROOT_PEM = '/usr/local/share/ca-certificates/linux-manager-ca.crt' def vault(lab, method, path, body=None, token=None): """A call to the lab's Vault with the root token, or the token given; the answer, or None with the problem printed.""" try: return vault_call(lab.vault['url'], method, path, body, token or lab.vault['root_token']) except VaultError as error: print(f'oidc: {error}') return None def sign_in(lab, url, code): """Hand the client the authorization URL, alice's password and the code, and run the driver; its answer or {}.""" ok = (lab.push_text(url, 'lm-oidc-client', '/run/lm-dev/oidc-url') and lab.push_text('alice', 'lm-oidc-client', '/run/lm-dev/oidc-name') and lab.hand_over('lm-oidc-client', 'oidc-password', lab.admin_password, '/run/lm-dev/oidc-password') and lab.hand_over('lm-oidc-client', 'oidc-code', code, '/run/lm-dev/oidc-code')) return (lab.json_of('lm-oidc-client', 'python3', '/root/oidc_login.py') or {}) if ok else {} def run(lab): lab.network('lmt-oidc-ext', '172.31.12.1/24') lab.instance('lm-oidc', 'vm', 'server.yaml', ('lmt-oidc-ext', '00:16:3e:4c:12:10', '172.31.12.10/24', '172.31.12.1')) lab.instance('lm-oidc-client', 'container', 'client.yaml', ('lmt-oidc-ext', '00:16:3e:4c:12:30', '172.31.12.30/24', '172.31.12.1')) if not lab.ready('lm-oidc', 'lm-oidc-client'): lab.result('oidc: instances ready', False) lab.result('oidc: package installed and configured', lab.server('lm-oidc', 'oidc.json')) server = str(lab.address('lm-oidc').ip) lab.result('oidc: user alice added', lab.vpn_user('lm-oidc')) lab.result('oidc: TOTP token of alice enrolled', enrol_otp(lab.remote, lab.project, 'lm-oidc', 'alice') is None) # The client: a service account with the redirect URI, its secret handed over as a file and kept in the Vault. secret = secrets.token_urlsafe(24) store_secret(lab.vault, 'lm-oidc/vault-client', {'secret': secret}) file = '/run/lm-dev/vault-client.secret' registered = lab.hand_over('lm-oidc', 'vault-client', f'{secret}\n', file) and lab.on( 'lm-oidc', 'sh', '-c', f'linux-manager-user --json add vault --service --display-name Vault --redirect-uri ' f'{REDIRECT} < {file}; status=$?; rm -f {file}; exit $status').returncode == 0 lab.result('oidc: client vault registered', registered) root = lab.on('lm-oidc', 'cat', ROOT_PEM).stdout hosts = f'echo "{server} {FQDN}" >>/etc/hosts' lab.on('lm-oidc-client', 'sh', '-c', hosts) lab.push_text(root, 'lm-oidc-client', '/root/oidc-root.pem') lab.push(TESTS / 'oidc_login.py', 'lm-oidc-client', '/root/oidc_login.py') lab.expect('oidc: discovery answers over HTTPS with the server CA', rf'"issuer": ?"{ISSUER}"', 'lm-oidc-client', 'curl', '-s', '--cacert', '/root/oidc-root.pem', f'{ISSUER}/.well-known/openid-configuration') # Vault, a relying party of the server, configured with the root token. lab.on('lm-vault', 'sh', '-c', hosts) vault(lab, 'POST', '/v1/sys/auth/oidc', {'type': 'oidc'}) configured = vault(lab, 'POST', '/v1/auth/oidc/config', { 'oidc_discovery_url': ISSUER, 'oidc_discovery_ca_pem': root, 'oidc_client_id': 'vault', 'oidc_client_secret': secret, 'default_role': 'lab'}) lab.result('oidc: Vault reads the discovery document', configured is not None) role = vault(lab, 'POST', '/v1/auth/oidc/role/lab', { 'role_type': 'oidc', 'user_claim': 'preferred_username', 'groups_claim': 'groups', 'oidc_scopes': ['profile', 'email', 'groups'], 'allowed_redirect_uris': [REDIRECT], 'claim_mappings': {'email': 'email'}, 'token_policies': ['default']}) lab.result('oidc: Vault role lab written', role is not None) # The authorization code flow: Vault's URL, the sign-in in the client, the code back to Vault. url = ((vault(lab, 'POST', '/v1/auth/oidc/oidc/auth_url', {'role': 'lab', 'redirect_uri': REDIRECT}) or {}) .get('data') or {}).get('auth_url') or '' query = urllib.parse.parse_qs(urllib.parse.urlsplit(url).query) lab.result('oidc: authorization URL asks for PKCE S256', query.get('code_challenge_method') == ['S256']) answer = sign_in(lab, url, otp_code(lab.project, 'lm-oidc', 'alice')) if url else {} lab.result('oidc: sign-in page shown', answer.get('page') is True) location = urllib.parse.parse_qs(urllib.parse.urlsplit(answer.get('location') or '').query) lab.result('oidc: sign-in redirects with a code and the state', answer.get('status') == 302 and bool(location.get('code')) and location.get('state') == query.get('state')) login = vault(lab, 'GET', '/v1/auth/oidc/oidc/callback?' + urllib.parse.urlencode({ 'state': (location.get('state') or [''])[0], 'code': (location.get('code') or [''])[0], 'nonce': (query.get('nonce') or [''])[0]})) if location.get('code') else None token = ((login or {}).get('auth') or {}).get('client_token') lab.result('oidc: Vault issues a token for alice', bool(token)) looked = vault(lab, 'GET', '/v1/auth/token/lookup-self', token=token) if token else None entity = (looked or {}).get('data', {}).get('entity_id') aliases = ((vault(lab, 'GET', f'/v1/identity/entity/id/{entity}') or {}).get('data') or {}).get('aliases', []) \ if entity else [] alias = next((item for item in aliases if item.get('name') == 'alice'), {}) lab.result('oidc: the token is of the entity alias alice', bool(alias)) lab.result('oidc: the email claim is mapped', (alias.get('metadata') or {}).get('email') == 'alice@example.org') # A wrong code: the sign-in page again, no redirect. url = ((vault(lab, 'POST', '/v1/auth/oidc/oidc/auth_url', {'role': 'lab', 'redirect_uri': REDIRECT}) or {}) .get('data') or {}).get('auth_url') or '' wrong = f'{(int(otp_code(lab.project, "lm-oidc", "alice")) + 1) % 1000000:06d}' answer = sign_in(lab, url, wrong) if url else {} lab.result('oidc: a wrong code is refused without a redirect', answer.get('status') == 401 and not answer.get('location'))