Configure P4 Server for built-in SSO
To let users log in through Keycloak, give P4 Server the details of the Keycloak client, and then start the P4 Web Server with the idp service. The P4 Web Server handles the browser part of the login.
super access to P4 Server, and access to the environment of the p4d service account.Before you begin
Ensure that you have the following:
-
A configured Keycloak realm and client. To learn how, see Configure Keycloak for built-in SSO.
-
P4 Server that uses SSL, so that the P4 Web Server uses HTTPS.
-
The issuer URL, the client ID, and the client secret from Keycloak.
Provide the client secret
Supply the client secret as the P4 variable P4IDPCLIENTSECRET, in the environment of the p4d service account. You cannot store it with p4 configure set. Alternatively:
-
Set
P4IDPCLIENTSECRETFILEto the path of a file that contains the secret. A path is not a secret, so you can set this one withp4 configure set. When you use a file, you can rotate the secret by updating the file, without restarting P4 Server. -
Set
P4IDPCLIENTSECRETNAMEto the path of the secret in a remote vault.
The client secret must be at least 32 bytes.
P4 Server can also retrieve the client secret from a remote vault. To learn how, see Manage server secrets.
On Linux, to set the variable for a systemd service, add the following to the service unit. Replace the placeholder with your own value.
[Service] Environment=P4IDPCLIENTSECRET=<client-secret>
To keep the secret out of the unit file, point to a file that is readable by the service account:
[Service] EnvironmentFile=/etc/p4d/idp.env
The file contains a line of the form P4IDPCLIENTSECRET=<client-secret>.
On Windows, create a system environment variable named P4IDPCLIENTSECRET with the client secret as its value, and then restart the p4d service. For example, from an elevated command prompt:
setx P4IDPCLIENTSECRET "<client-secret>" /M
Provide the signing key
If you set auth.idp.auth_method to private_key_jwt, the P4 Web Server signs its requests to Keycloak with a private key. Supply the key in one of the following forms, as P4 variables:
-
Set
P4IDPSIGNINGKEYto the key. -
Set
P4IDPSIGNINGKEYFILEto the path of a file that contains the key. -
Set
P4IDPSIGNINGKEYNAMEto the path of the key in a HashiCorp Vault KV version 2 secrets engine. The Vault policy must allow read access to the key and to the metadata path of the key. To learn how to connect P4 Server to Vault, see Manage server secrets.
If you set more than one of the variables, P4 Server uses P4IDPSIGNINGKEY first, then P4IDPSIGNINGKEYFILE, and then P4IDPSIGNINGKEYNAME.
The key must be a private JSON Web Key (JWK) that is Base64url-encoded. The JWK must include the kid and alg parameters, and alg must be ES256 or RS256. The P4 Web Server publishes the public part of the key at /.well-known/jwks.json, where Keycloak reads the key to check requests.
To rotate the key, write a new key with a new kid value to the file that P4IDPSIGNINGKEYFILE names, or to the Vault path that P4IDPSIGNINGKEYNAME names. P4 Server reads the new key within about 5 seconds, without a restart. In the Keycloak administration console, click Realm settings, click the Keys tab, and clear the keys cache, so that Keycloak reads the new key.
If you set none of the variables, P4 Server reports that no signing key secret is configured. To learn more, see Supply secrets to P4 Server.
Set the built-in SSO configurables
To point P4 Server at Keycloak, run the following commands. Replace the example values with your own.
p4 configure set auth.idp.issuer_url=https://keycloak.example.com/realms/perforce p4 configure set auth.idp.client_id=p4server p4 configure set auth.idp.auth_method=client_secret_post p4 configure set auth.idp.web_uri=https://p4.example.com:8443 p4 configure set auth.idp.audience=account p4 configure set auth.idp.subject_claim=p4_username p4 configure set auth.idp.id_token_signing_alg=ES256
The configurables have the following purposes:
| Configurable | Purpose |
|---|---|
|
The Keycloak realm URL. The URL must use |
|
|
The ID of the Keycloak client. |
|
|
How the P4 Web Server authenticates to Keycloak. The values are |
|
|
The URL at which users reach the P4 Web Server. Keycloak redirects users back to this URL. |
|
|
The audience that the access token must contain. The value must match the |
|
|
The claim that identifies the P4 Server user on first login. The values are |
|
|
The algorithm that Keycloak must use to sign the ID token. The default is |
For the full details of each configurable, see Built-in SSO configurables in the P4 CLI Reference.
After you change auth.idp.web_uri, restart the P4 Web Server. P4 Server picks up changes to the other auth.idp.* configurables within about 10 seconds, without a restart. A login that is already in progress uses the previous values.
Trust the Keycloak certificate
The P4 Web Server connects to Keycloak, and to Vault if you use Vault, over HTTPS. The web server trusts the certificate authorities that P4 Server trusts for its own outbound SSL connections. If a private certificate authority signed the Keycloak certificate, set the ssl.client.ca.path configurable to a PEM file that contains the certificate of the certificate authority:
-
To trust the certificate authorities in the file in addition to the certificate store of the operating system, add a
+before the path. For example,p4 configure set ssl.client.ca.path=+/etc/p4d/private-ca.pem. -
To trust only the certificate authorities in the file, set the path without a
+.
The value of ssl.client.ca.path must be a PEM file. If the value is a directory, the web server doesn't trust the certificates in the directory.
The + prefix also applies to the outbound SSL connections of P4 Server, for example the connection from a replica server to the commit server.
If ssl.client.ca.path is unset, the web server trusts the certificate store of the operating system.
P4 Server passes the trusted certificates to the web server when P4 Server starts and when you run p4 admin restart. After you change ssl.client.ca.path or the certificate store, run p4 admin restart or restart P4 Server. A restart of the web server alone doesn't apply the change.
auth.idp.allow_invalid_certs configurable turns off certificate checks for connections to Keycloak. Set auth.idp.allow_invalid_certs to 1 only for a test or development identity provider. In production, trust the certificate authority instead.Start the P4 Web Server
To run the P4 Web Server with the idp service, run the following command:
p4 webserver start -p 8443 --services=idp
To run the REST API as well, list both services:
p4 webserver start -p 8443 --services=restapi,idp
To start the P4 Web Server automatically with p4d, set a startup command. Omit p4 at the start of the startup command. For example:
p4 configure set "<serverid>#startup.1=webserver start -p 8443 --services=idp"
To learn about the command and its options, see p4 webserver in the P4 CLI Reference.
Next steps
To prepare users to log in through Keycloak, see Set up users for built-in SSO.