|
| 1 | +--- |
| 2 | +title: Use Azure Active Directory and native PostgreSQL roles for authentication with Azure Cosmos DB for PostgreSQL |
| 3 | +description: Learn how to set up Azure Active Directory (Azure AD) and add native PostgreSQL roles for authentication with Azure Cosmos DB for PostgreSQL. |
| 4 | +author: niklarin |
| 5 | +ms.author: nlarin |
| 6 | +ms.service: cosmos-db |
| 7 | +ms.subservice: postgresql |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 08/01/2023 |
| 10 | +--- |
| 11 | + |
| 12 | +# Use Azure Active Directory and native PostgreSQL roles for authentication with Azure Cosmos DB for PostgreSQL |
| 13 | + |
| 14 | +[!INCLUDE [PostgreSQL](../includes/appliesto-postgresql.md)] |
| 15 | + |
| 16 | +> [!IMPORTANT] |
| 17 | +> Azure Active Directory authentication in Azure Cosmos DB for PostgreSQL is currently in preview. |
| 18 | +> This preview version is provided without a service level agreement, and it's not recommended |
| 19 | +> for production workloads. Certain features might not be supported or might have constrained |
| 20 | +> capabilities. |
| 21 | +> |
| 22 | +> [Contact us](mailto:[email protected]) if you're interested in participating in Azure Active Directory authentication |
| 23 | +> for Azure Cosmos DB for PostgreSQL preview. |
| 24 | +> |
| 25 | +> You can see a complete list of other new features in [preview features](product-updates.md#features-in-preview). |
| 26 | +
|
| 27 | +In this article, you configure authentication methods for Azure Cosmos DB for PostgreSQL. You manage Azure Active Directory (Azure AD) admin users and native PostgreSQL roles for authentication with Azure Cosmos DB for PostgreSQL. You also learn how to use an Azure AD token with Azure Cosmos DB for PostgreSQL. |
| 28 | + |
| 29 | +An Azure Cosmos DB for PostgreSQL cluster is created with one built-in native PostgreSQL role named 'citus'. You can add more native PostgreSQL roles after cluster provisioning is completed. |
| 30 | + |
| 31 | +You can also configure Azure AD authentication for Azure Cosmos DB for PostgreSQL. You can enable Azure AD authentication in addition or instead of the native PostgreSQL authentication on your cluster. You can change authentication methods enabled on cluster at any point after the cluster is provisioned. When Azure Active Directory authentication is enabled, you can add multiple Azure AD users to an Azure Cosmos DB for PostgreSQL cluster and make any of them administrators. Azure AD user can be a user or a service principal. |
| 32 | + |
| 33 | +## Choose authentication method |
| 34 | +You need to use Azure portal to configure authentication methods on an Azure Cosmos DB for PostgreSQL cluster. |
| 35 | + |
| 36 | +Complete the following items on your Azure Cosmos DB for PostgreSQL cluster to enable or disable Azure Active Directory authentication and native PostgreSQL authentication. |
| 37 | + |
| 38 | +1. On the cluster page, under the **Cluster management** heading, choose **Authentication** to open authentication management options. |
| 39 | +1. In **Authentication methods** section, choose **PostgreSQL authentication only**, **Azure Active Directory authentication (preview)**, or **PostgreSQL and Azure Active Directory authentication (preview)** as the authentication method based on your requirements. |
| 40 | + |
| 41 | +Once done proceed with [configuring Azure Active Directory authentication](#configure-azure-active-directory-authentication) or [adding native PostgreSQL roles](#configure-native-postgresql-authentication) on **Authentication** page. |
| 42 | + |
| 43 | +## Configure Azure Active Directory authentication |
| 44 | + |
| 45 | +To add or remove Azure AD roles on cluster, follow these steps on **Authentication** page: |
| 46 | + |
| 47 | +1. In **Azure Active Directory (Azure AD) authentication (preview)** section, select **Add Azure AD admins**. |
| 48 | +1. In **Select Azure AD Admins** panel, select one or more valid Azure AD user or enterprise application in the current AD tenant to be an Azure AD administrator on your Azure Cosmos DB for PostgreSQL cluster. |
| 49 | +1. Use **Select** to confirm your choice. |
| 50 | +1. In the **Authentication** page, select **Save** in the toolbar to save changes or proceed with adding native PostgreSQL roles. |
| 51 | + |
| 52 | +## Configure native PostgreSQL authentication |
| 53 | + |
| 54 | +To add Postgres roles on cluster, follow these steps on **Authentication** page: |
| 55 | + |
| 56 | +1. In **PostgreSQL authentication** section, select **Add PostgreSQL role**. |
| 57 | +1. Enter the role name and password. Select **Save**. |
| 58 | +1. In the **Authentication** page, select **Save** in the toolbar to save changes or proceed with adding Azure Active Directory admin users. |
| 59 | + |
| 60 | +The native PostgreSQL user is created on the coordinator node of the cluster, and propagated to all the worker nodes. Roles created through the Azure portal have the LOGIN attribute, which means they’re true users who can sign in to the database. |
| 61 | + |
| 62 | +## Connect to Azure Cosmos for PostgreSQL by using Azure AD authentication |
| 63 | + |
| 64 | +Azure AD integration works with standard PostgreSQL client tools like **psql**, which aren't Azure AD aware and support only specifying the username and password when you're connecting to PostgreSQL. In such cases, the Azure AD token is passed as the password. |
| 65 | + |
| 66 | +We've tested the following clients: |
| 67 | + |
| 68 | +- **psql command line**: Use the `PGPASSWORD` variable to pass the token. |
| 69 | +- **Other libpq-based clients**: Examples include common application frameworks and object-relational mappers (ORMs). |
| 70 | +- **pgAdmin**: Clear **Connect now** at server creation. |
| 71 | + |
| 72 | +Use the following procedures to authenticate with Azure AD as an Azure Cosmos DB for PostgreSQL user. You can follow along in [Azure Cloud Shell](./../../cloud-shell/quickstart.md), on an Azure virtual machine, or on your local machine. |
| 73 | + |
| 74 | +### Sign in to the user's Azure subscription |
| 75 | + |
| 76 | +Start by authenticating with Azure AD by using the Azure CLI. This step isn't required in Azure Cloud Shell. |
| 77 | + |
| 78 | +```azurecli |
| 79 | +az login |
| 80 | +``` |
| 81 | + |
| 82 | +The command opens a browser window to the Azure AD authentication page. It requires you to give your Azure AD user ID and password. |
| 83 | + |
| 84 | +### Retrieve the Azure AD access token |
| 85 | + |
| 86 | +Use the Azure CLI to acquire an access token for the Azure AD authenticated user to access Azure Cosmos for PostgreSQL. Here's an example: |
| 87 | + |
| 88 | +```azurecli-interactive |
| 89 | +az account get-access-token --resource https://postgres.cosmos.azure.com |
| 90 | +``` |
| 91 | + |
| 92 | +After authentication is successful, Azure AD returns an access token for current Azure subscription: |
| 93 | + |
| 94 | +```json |
| 95 | +{ |
| 96 | + "accessToken": "[TOKEN]", |
| 97 | + "expiresOn": "[expiration_date_and_time]", |
| 98 | + "subscription": "[subscription_id]", |
| 99 | + "tenant": "[tenant_id]", |
| 100 | + "tokenType": "Bearer" |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +The TOKEN is a Base64 string. It encodes all the information about the authenticated user and is targeted to the Azure Cosmos DB for PostgreSQL service. The token is valid for at least 5 minutes with the maximum of 90 minutes. The expiresOn defines actual token expiration time. |
| 105 | + |
| 106 | +### Use a token as a password for signing in with client psql |
| 107 | + |
| 108 | +When connecting, it's best to use the access token as the PostgreSQL user password. |
| 109 | + |
| 110 | +While using the psql command-line client, the access token needs to be passed through the `PGPASSWORD` environment variable. The reason is that the access token exceeds the password length that psql can accept directly. |
| 111 | + |
| 112 | +Here's a Windows example: |
| 113 | + |
| 114 | +```cmd |
| 115 | +set PGPASSWORD=<TOKEN value from the previous step> |
| 116 | +``` |
| 117 | + |
| 118 | +```powerShell |
| 119 | +$env:PGPASSWORD='<TOKEN value from from the previous step>' |
| 120 | +``` |
| 121 | + |
| 122 | +Here's a Linux/macOS example: |
| 123 | + |
| 124 | +```bash |
| 125 | +export PGPASSWORD=<TOKEN value from the previous step> |
| 126 | +``` |
| 127 | + |
| 128 | +You can also combine the previous two steps together using command substitution. The token retrieval can be encapsulated into a variable and passed directly as a value for `PGPASSWORD` environment variable: |
| 129 | + |
| 130 | +```bash |
| 131 | +export PGPASSWORD=$(az account get-access-token --resource-type oss-rdbms --query "[accessToken]" -o tsv) |
| 132 | +``` |
| 133 | + |
| 134 | + |
| 135 | +> [!NOTE] |
| 136 | +> Make sure PGPASSWORD variable is set to the Azure AD access token for your |
| 137 | +> subscription for Azure AD authentication. If you need to do Postgres role authentication |
| 138 | +> from the same session you can set PGPASSWORD to the Postgres role password |
| 139 | +> or clear the PGPASSWORD variable value to enter the password interactively. |
| 140 | +> Authentication would fail with the wrong value in PGPASSWORD. |
| 141 | +
|
| 142 | +Now you can initiate a connection with Azure Cosmos DB for PostgreSQL as you usually would (without 'password' parameter in the command line): |
| 143 | + |
| 144 | +```sql |
| 145 | +psql "host=mycluster.[uniqueID].postgres.cosmos.azure.com [email protected] dbname=[db_name] sslmode=require" |
| 146 | +``` |
| 147 | + |
| 148 | +### Use a token as a password for signing in with PgAdmin |
| 149 | + |
| 150 | +To connect by using an Azure AD token with PgAdmin, follow these steps: |
| 151 | + |
| 152 | +1. Clear the **Connect now** option at server creation. |
| 153 | +1. Enter your server details on the **Connection** tab and save. |
| 154 | + 1. Make sure a valid Azure AD user is specified in **Username**. |
| 155 | +1. From the pgAdmin **Object** menu, select **Connect Server**. |
| 156 | +1. Enter the Active Directory token password when you're prompted. |
| 157 | + |
| 158 | +Here are some essential considerations when you're connecting: |
| 159 | + |
| 160 | +- `[email protected]` is the name of the Azure AD user. |
| 161 | +- Be sure to use the exact way the Azure user is spelled. Azure AD user and group names are case-sensitive. |
| 162 | +- If the name contains spaces, use a backslash (`\`) before each space to escape it. |
| 163 | +- The access token's validity is 5 minutes to 90 minutes. You should get the access token before initiating the sign-in to Azure Cosmos for PostgreSQL. |
| 164 | + |
| 165 | +You're now authenticated to your Azure Cosmos for PostgreSQL server through Azure AD authentication. |
| 166 | + |
| 167 | +## Manage native PostgreSQL roles |
| 168 | + |
| 169 | +When native PostgreSQL authentication is enabled on cluster, you can add and delete Postgres roles in addition to built-in 'citus' role. You can also reset password and modify Postgres privileges for native roles. |
| 170 | + |
| 171 | +### How to delete a native PostgreSQL user role or change their password |
| 172 | + |
| 173 | +To update a user, visit the **Authentication** page for your cluster, |
| 174 | +and select the ellipses **...** next to the user. The ellipses open a menu |
| 175 | +to delete the user or reset their password. |
| 176 | + |
| 177 | +The `citus` role is privileged and can't be deleted. However, `citus` role would be disabled, if 'Azure Active Directory authentication only' authentication method is selected for the cluster. |
| 178 | + |
| 179 | +## How to modify privileges for user roles |
| 180 | + |
| 181 | +New user roles are commonly used to provide database access with restricted |
| 182 | +privileges. To modify user privileges, use standard PostgreSQL commands, using |
| 183 | +a tool such as PgAdmin or psql. For more information, see [Connect to a cluster](quickstart-connect-psql.md). |
| 184 | + |
| 185 | +For example, to allow PostgreSQL `db_user` to read `mytable`, grant the permission: |
| 186 | + |
| 187 | +```sql |
| 188 | +GRANT SELECT ON mytable TO db_user; |
| 189 | +``` |
| 190 | + |
| 191 | +To grant the same permissions to Azure AD role `[email protected]` use the following command: |
| 192 | + |
| 193 | +```sql |
| 194 | +GRANT SELECT ON mytable TO "[email protected]"; |
| 195 | +``` |
| 196 | + |
| 197 | +Azure Cosmos DB for PostgreSQL propagates single-table GRANT statements through the entire |
| 198 | +cluster, applying them on all worker nodes. It also propagates GRANTs that are |
| 199 | +system-wide (for example, for all tables in a schema): |
| 200 | + |
| 201 | +```sql |
| 202 | +-- applies to the coordinator node and propagates to worker nodes for Postgres role db_user |
| 203 | +GRANT SELECT ON ALL TABLES IN SCHEMA public TO db_user; |
| 204 | +``` |
| 205 | + |
| 206 | +Or for Azure AD role |
| 207 | + |
| 208 | +```sql |
| 209 | +-- applies to the coordinator node and propagates to worker nodes for Azure AD role [email protected] |
| 210 | +GRANT SELECT ON ALL TABLES IN SCHEMA public TO "[email protected]"; |
| 211 | +``` |
| 212 | + |
| 213 | + |
| 214 | +## Next steps |
| 215 | + |
| 216 | +- Learn about [authentication in Azure Cosmos DB for PostgreSQL](./concepts-authentication.md). |
| 217 | +- Review [Azure Active Directory fundamentals](./../../active-directory/fundamentals/active-directory-whatis.md). |
| 218 | +- [Learn more about SQL GRANT in PostgreSQL](https://www.postgresql.org/docs/current/sql-grant.html). |
0 commit comments