|
| 1 | +--- |
| 2 | +title: Azure Red Hat OpenShift running OpenShift 4 - Configure Azure Active Directory authentication using the command line |
| 3 | +description: Learn how to configure Azure Active Directory authentication for an Azure Red Hat OpenShift cluster running OpenShift 4 using the command line |
| 4 | +ms.service: container-service |
| 5 | +ms.topic: article |
| 6 | +ms.date: 03/12/2020 |
| 7 | +author: sabbour |
| 8 | +ms.author: asabbour |
| 9 | +keywords: aro, openshift, az aro, red hat, cli |
| 10 | +ms.custom: mvc |
| 11 | +#Customer intent: As an operator, I need to configure Azure Active Directory authentication for an Azure Red Hat OpenShift cluster running OpenShift 4 |
| 12 | +--- |
| 13 | + |
| 14 | +# Configure Azure Active Directory authentication for an Azure Red Hat OpenShift 4 cluster (CLI) |
| 15 | + |
| 16 | +If you choose to install and use the CLI locally, this article requires that you are running the Azure CLI version 2.0.75 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest). |
| 17 | + |
| 18 | +Retrieve your cluster-specific URLs that are going to be used to configure the Azure Active Directory application. |
| 19 | + |
| 20 | +Construct the cluster's OAuth callback URL and store it in a variable **oauthCallbackURL**. Make sure to replace **aro-rg** with your resource group's name and **aro-cluster** with your cluster's name. |
| 21 | + |
| 22 | +> [!NOTE] |
| 23 | +> The `AAD` section in the OAuth callback URL should match the OAuth identity provider name you'll setup later. |
| 24 | +
|
| 25 | +```azurecli-interactive |
| 26 | +domain=$(az aro show -g aro-rg -n aro-cluster --query clusterProfile.domain -o tsv) |
| 27 | +location=$(az aro show -g aro-rg -n aro-cluster --query location -o tsv) |
| 28 | +apiServer=$(az aro show -g aro-rg -n aro-cluster --query apiserverProfile.url -o tsv) |
| 29 | +webConsole=$(az aro show -g aro-rg -n aro-cluster --query consoleProfile.url -o tsv) |
| 30 | +oauthCallbackURL=https://oauth-openshift.apps.$domain.$location.aroapp.io/oauth2callback/AAD |
| 31 | +``` |
| 32 | + |
| 33 | +## Create an Azure Active Directory application for authentication |
| 34 | + |
| 35 | +Create an Azure Active Directory application and retrieve the created application identifier. Replace **\<ClientSecret>** with a secure password. |
| 36 | + |
| 37 | +```azurecli-interactive |
| 38 | +az ad app create \ |
| 39 | + --query appId -o tsv \ |
| 40 | + --display-name aro-auth \ |
| 41 | + --reply-urls $oauthCallbackURL \ |
| 42 | + --password '<ClientSecret>' |
| 43 | +``` |
| 44 | + |
| 45 | +You should get back something like this. Make note of it as this is the **AppId** you'll need in later steps. |
| 46 | + |
| 47 | +```output |
| 48 | +6a4cb4b2-f102-4125-b5f5-9ad6689f7224 |
| 49 | +``` |
| 50 | + |
| 51 | +Retrieve the tenant ID of the subscription that owns the application. |
| 52 | + |
| 53 | +```azure |
| 54 | +az account show --query tenantId -o tsv |
| 55 | +``` |
| 56 | + |
| 57 | +You should get back something like this. Make note of it as this is the **TenantId** you'll need in later steps. |
| 58 | + |
| 59 | +```output |
| 60 | +72f999sx-8sk1-8snc-js82-2d7cj902db47 |
| 61 | +``` |
| 62 | + |
| 63 | +## Create a manifest file to define the optional claims to include in the ID Token |
| 64 | + |
| 65 | +Application developers can use [optional claims](https://docs.microsoft.com/azure/active-directory/develop/active-directory-optional-claims) in their Azure AD applications to specify which claims they want in tokens sent to their application. |
| 66 | + |
| 67 | +You can use optional claims to: |
| 68 | + |
| 69 | +- Select additional claims to include in tokens for your application. |
| 70 | +- Change the behavior of certain claims that Azure AD returns in tokens. |
| 71 | +- Add and access custom claims for your application. |
| 72 | + |
| 73 | +We'll configure OpenShift to use the `email` claim and fall back to `upn` to set the Preferred Username by adding the `upn` as part of the ID token returned by Azure Active Directory. |
| 74 | + |
| 75 | +Create a **manifest.json** file to configure the Azure Active Directory application. |
| 76 | + |
| 77 | +```bash |
| 78 | +cat > manifest.json<< EOF |
| 79 | +[{ |
| 80 | + "name": "upn", |
| 81 | + "source": null, |
| 82 | + "essential": false, |
| 83 | + "additionalProperties": [] |
| 84 | +}, |
| 85 | +{ |
| 86 | +"name": "email", |
| 87 | + "source": null, |
| 88 | + "essential": false, |
| 89 | + "additionalProperties": [] |
| 90 | +}] |
| 91 | +EOF |
| 92 | +``` |
| 93 | + |
| 94 | +## Update the Azure Active Directory application's optionalClaims with a manifest |
| 95 | + |
| 96 | +Replace **\<AppID>** with the ID you got earlier. |
| 97 | + |
| 98 | +```azurecli-interactive |
| 99 | +az ad app update \ |
| 100 | + |
| 101 | + --id <AppId> |
| 102 | +``` |
| 103 | + |
| 104 | +## Update the Azure Active Directory application scope permissions |
| 105 | + |
| 106 | +To be able to read the user information from Azure Active Directory, we need to define the proper scopes. |
| 107 | + |
| 108 | +Replace **\<AppID>** with the ID you got earlier. |
| 109 | + |
| 110 | +Add permission for the **Azure Active Directory Graph.User.Read** scope to enable sign in and read user profile. |
| 111 | + |
| 112 | +```azurecli-interactive |
| 113 | +az ad app permission add \ |
| 114 | + --api 00000002-0000-0000-c000-000000000000 \ |
| 115 | + --api-permissions 311a71cc-e848-46a1-bdf8-97ff7156d8e6=Scope \ |
| 116 | + --id <AppId> |
| 117 | +``` |
| 118 | + |
| 119 | +> [!NOTE] |
| 120 | +> Unless you are authenticated as a Global Administrator for this Azure Active Directory, you can ignore the message to grant the consent, since you'll be asked to do this once you login on your own account. |
| 121 | +
|
| 122 | +## Assign users and groups to the cluster (optional) |
| 123 | + |
| 124 | +Applications registered in an Azure Active Directory (Azure AD) tenant are, by default, available to all users of the tenant who authenticate successfully. Azure AD allows tenant administrators and developers to restrict an app to a specific set of users or security groups in the tenant. |
| 125 | + |
| 126 | +Follow the instructions on the Azure Active Directory documentation to [assign users and groups to the app](https://docs.microsoft.com/azure/active-directory/develop/howto-restrict-your-app-to-a-set-of-users#app-registration). |
| 127 | + |
| 128 | +## Configure OpenShift OpenID authentication |
| 129 | + |
| 130 | +Retrieve the `kubeadmin` credentials. Run the following command to find the password for the `kubeadmin` user. |
| 131 | + |
| 132 | +```azurecli-interactive |
| 133 | +az aro list-credentials \ |
| 134 | + --name aro-cluster \ |
| 135 | + --resource-group aro-rg |
| 136 | +``` |
| 137 | + |
| 138 | +The following example output shows the password will be in `kubeadminPassword`. |
| 139 | + |
| 140 | +```json |
| 141 | +{ |
| 142 | + "kubeadminPassword": "<generated password>", |
| 143 | + "kubeadminUsername": "kubeadmin" |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +Log in to the OpenShift cluster's API server using the following command. The `$apiServer` variable was set [earlier](). Replace **\<kubeadmin password>** with the password you retrieved. |
| 148 | + |
| 149 | +```azurecli-interactive |
| 150 | +oc login $apiServer -u kubeadmin -p <kubeadmin password> |
| 151 | +``` |
| 152 | + |
| 153 | +Create an OpenShift secret to store the Azure Active Directory application secret, replacing **\<ClientSecret>** with the secret you retrieved earlier. |
| 154 | + |
| 155 | +```azurecli-interactive |
| 156 | +oc create secret generic openid-client-secret-azuread \ |
| 157 | + --namespace openshift-config \ |
| 158 | + --from-literal=clientSecret=<ClientSecret> |
| 159 | +``` |
| 160 | + |
| 161 | +Create a **oidc.yaml** file to configure OpenShift OpenID authentication against Azure Active Directory. Replace **\<AppID>** and **\<TenantId>** with the values you retrieved earlier. |
| 162 | + |
| 163 | +```bash |
| 164 | +cat > oidc.yaml<< EOF |
| 165 | +apiVersion: config.openshift.io/v1 |
| 166 | +kind: OAuth |
| 167 | +metadata: |
| 168 | + name: cluster |
| 169 | +spec: |
| 170 | + identityProviders: |
| 171 | + - name: AAD |
| 172 | + mappingMethod: claim |
| 173 | + type: OpenID |
| 174 | + openID: |
| 175 | + clientID: <AppId> |
| 176 | + clientSecret: |
| 177 | + name: openid-client-secret-azuread |
| 178 | + extraScopes: |
| 179 | + - email |
| 180 | + - profile |
| 181 | + extraAuthorizeParameters: |
| 182 | + include_granted_scopes: "true" |
| 183 | + claims: |
| 184 | + preferredUsername: |
| 185 | + - email |
| 186 | + - upn |
| 187 | + name: |
| 188 | + - name |
| 189 | + email: |
| 190 | + - email |
| 191 | + issuer: https://login.microsoftonline.com/<TenantId> |
| 192 | +EOF |
| 193 | +``` |
| 194 | + |
| 195 | +Apply the configuration to the cluster. |
| 196 | + |
| 197 | +```azurecli-interactive |
| 198 | +oc apply -f oidc.yaml |
| 199 | +``` |
| 200 | + |
| 201 | +You will get back a response similar to the following. |
| 202 | + |
| 203 | +```output |
| 204 | +oauth.config.openshift.io/cluster configured |
| 205 | +``` |
| 206 | + |
| 207 | +## Verify login through Azure Active Directory |
| 208 | + |
| 209 | +If you now logout of the OpenShift Web Console and try to log in again, you'll be presented with a new option to log in with **AAD**. You may need to wait for a few minutes. |
| 210 | + |
| 211 | + |
0 commit comments