Configuring a basic authentication identity provider

    By default, only a kubeadmin user exists on your cluster. To specify an identity provider, you must create a custom resource (CR) that describes that identity provider and add it to the cluster.

    About basic authentication

    Basic authentication is a generic back-end integration mechanism that allows users to log in to OKD with credentials validated against a remote identity provider.

    Because basic authentication is generic, you can use this identity provider for advanced authentication configurations.

    Basic authentication must use an HTTPS connection to the remote server to prevent potential snooping of the user ID and password and man-in-the-middle attacks.

    With basic authentication configured, users send their user name and password to OKD, which then validates those credentials against a remote server by making a server-to-server request, passing the credentials as a basic authentication header. This requires users to send their credentials to OKD during login.

    User names and passwords are validated against a remote URL that is protected by basic authentication and returns JSON.

    A 401 response indicates failed authentication.

    A non-200 status, or the presence of a non-empty “error” key, indicates an error:

    A 200 status with a sub (subject) key indicates success:

    1. {"sub":"userid"} (1)
    1The subject must be unique to the authenticated user and must not be able to be modified.

    A successful response can optionally provide additional data, such as:

    • A display name using the name key. For example:

      1. {"sub":"userid", "name": "User Name", ...}
    • An email address using the email key. For example:

      1. {"sub":"userid", "email":"user@example.com", ...}
    • A preferred user name using the preferred_username key. This is useful when the unique, unchangeable subject is a database key or UID, and a more human-readable name exists. This is used as a hint when provisioning the OKD user for the authenticated identity. For example:

      1. {"sub":"014fbff9a07c", "preferred_username":"bob", ...}

    Identity providers use OKD Secret objects in the openshift-config namespace to contain the client secret, client certificates, and keys.

    Procedure

    • Create a Secret object that contains the key and certificate by using the following command:

      1. $ oc create secret tls <secret_name> --key=key.pem --cert=cert.pem -n openshift-config

    Creating a config map

    Identity providers use OKD ConfigMap objects in the openshift-config namespace to contain the certificate authority bundle. These are primarily used to contain certificate bundles needed by the identity provider.

    Procedure

    • Define an OKD ConfigMap object containing the certificate authority by using the following command. The certificate authority must be stored in the ca.crt key of the ConfigMap object.

      You can alternatively apply the following YAML to create the config map:

      1. apiVersion: v1
      2. kind: ConfigMap
      3. metadata:
      4. name: ca-config-map
      5. namespace: openshift-config
      6. type: Opaque
      7. data:
      8. <CA_certificate_PEM>

    The following custom resource (CR) shows the parameters and acceptable values for a basic authentication identity provider.

    Basic authentication CR

    1. apiVersion: config.openshift.io/v1
    2. metadata:
    3. name: cluster
    4. spec:
    5. identityProviders:
    6. - name: basicidp (1)
    7. mappingMethod: claim (2)
    8. type: BasicAuth
    9. basicAuth:
    10. url: https://www.example.com/remote-idp (3)
    11. ca: (4)
    12. name: ca-config-map
    13. tlsClientCert: (5)
    14. name: client-cert-secret
    15. tlsClientKey: (6)
    16. name: client-key-secret

    Additional resources

    Adding an identity provider to your clusters

    After you install your cluster, add an identity provider to it so your users can authenticate.

    Prerequisites

    • Create an OKD cluster.

    • Create the custom resource (CR) for your identity providers.

    • You must be logged in as an administrator.

    Procedure

    1. Apply the defined CR:

      1. $ oc apply -f </path/to/CR>

      If a CR does not exist, oc apply creates a new CR and might trigger the following warning: Warning: oc apply should be used on resources created by either oc create —save-config or oc apply. In this case you can safely ignore this warning.

    2. Log in to the cluster as a user from your identity provider, entering the password when prompted.

      1. $ oc login -u <username>
    3. Confirm that the user logged in successfully, and display the user name.

      1. $ oc whoami

    The basic identify provider (IDP) configuration in OKD 4 requires that the IDP server respond with JSON for success and failures. You can use CGI scripting in Apache HTTPD to accomplish this. This section provides examples.

    1. <VirtualHost *:443>
    2. # CGI Scripts in here
    3. DocumentRoot /var/www/cgi-bin
    4. # SSL Directives
    5. SSLEngine on
    6. SSLCipherSuite PROFILE=SYSTEM
    7. SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    8. SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    9. # Configure HTTPD to execute scripts
    10. ScriptAlias /basic /var/www/cgi-bin
    11. # Handles a failed login attempt
    12. ErrorDocument 401 /basic/fail.cgi
    13. # Handles authentication
    14. <Location /basic/login.cgi>
    15. AuthType Basic
    16. AuthName "Please Log In"
    17. AuthBasicProvider file
    18. AuthUserFile /etc/httpd/conf/passwords
    19. Require valid-user
    20. </Location>
    21. </VirtualHost>

    Example /var/www/cgi-bin/login.cgi

    Example /var/www/cgi-bin/fail.cgi

    1. #!/bin/bash
    2. echo "Content-Type: application/json"
    3. echo ""
    4. echo '{"error": "Login failure"}'
    5. exit 0

    These are the requirements for the files you create on an Apache HTTPD web server:

    • login.cgi and fail.cgi must be executable (chmod +x).

    • login.cgi and fail.cgi must have proper SELinux contexts if SELinux is enabled: restorecon -RFv /var/www/cgi-bin, or ensure that the context is httpd_sys_script_exec_t using ls -laZ.

    • login.cgi is only executed if your user successfully logs in per Require and Auth directives.

    • fail.cgi is executed if the user fails to log in, resulting in an HTTP 401 response.

    Basic authentication troubleshooting

    The most common issue relates to network connectivity to the backend server. For simple debugging, run curl commands on the master. To test for a successful login, replace the <user> and <password> in the following example command with valid credentials. To test an invalid login, replace them with false credentials.

    1. $ curl --cacert /path/to/ca.crt --cert /path/to/client.crt --key /path/to/client.key -u <user>:<password> -v https://www.example.com/remote-idp

    Successful responses

    A 200 status with a sub (subject) key indicates success:

    1. {"sub":"userid"}

    The subject must be unique to the authenticated user, and must not be able to be modified.

    A successful response can optionally provide additional data, such as:

    • A display name using the name key:

      1. {"sub":"userid", "name": "User Name", ...}
    • An email address using the email key:

      1. {"sub":"userid", "email":"user@example.com", ...}
    • A preferred user name using the preferred_username key:

      1. {"sub":"014fbff9a07c", "preferred_username":"bob", ...}

      The preferred_username key is useful when the unique, unchangeable subject is a database key or UID, and a more human-readable name exists. This is used as a hint when provisioning the OKD user for the authenticated identity.

    Failed responses