|
| 1 | +--- |
| 2 | +title: Use managed identities to access Azure SQL Database - Azure Stream Analytics |
| 3 | +description: This article describes how to use managed identities to authenticate your Azure Stream Analytics job to Azure SQL DB output. |
| 4 | +author: mamccrea |
| 5 | +ms.author: mamccrea |
| 6 | +ms.service: stream-analytics |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 05/08/2020 |
| 9 | +--- |
| 10 | + |
| 11 | +# Use managed identities to access Azure SQL Database from an Azure Stream Analytics job (Preview) |
| 12 | + |
| 13 | +Azure Stream Analytics supports [Managed Identity authentication](../active-directory/managed-identities-azure-resources/overview.md) for Azure SQL Database output sinks. Managed identities eliminate the limitations of user-based authentication methods, like the need to reauthenticate due to password changes or user token expirations that occur every 90 days. When you remove the need to manually authenticate, your Stream Analytics deployments can be fully automated. |
| 14 | + |
| 15 | +A managed identity is a managed application registered in Azure Active Directory that represents a given Stream Analytics job. The managed application is used to authenticate to a targeted resource. This article shows you how to enable Managed Identity for an Azure SQL Database output(s) of a Stream Analytics job through the Azure portal. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +The following are required to this feature: |
| 20 | + |
| 21 | +- An Azure Stream Analytics job. |
| 22 | + |
| 23 | +- An Azure SQL Database resource. |
| 24 | + |
| 25 | +## Create a managed identity |
| 26 | + |
| 27 | +First, you create a managed identity for your Azure Stream Analytics job. |
| 28 | + |
| 29 | +1. In the [Azure portal](https://portal.azure.com), open your Azure Stream Analytics job. |
| 30 | + |
| 31 | +1. From the left navigation menu, select **Managed Identity** located under **Configure**. Then, check the box next to **Use System-assigned Managed Identity** and select **Save**. |
| 32 | + |
| 33 | +  |
| 34 | + |
| 35 | + |
| 36 | + A service principal for the Stream Analytics job's identity is created in Azure Active Directory. The life cycle of the newly created identity is managed by Azure. When the Stream Analytics job is deleted, the associated identity (that is, the service principal) is automatically deleted by Azure. |
| 37 | + |
| 38 | +1. When you save the configuration, the Object ID (OID) of the service principal is listed as the Principal ID as shown below: |
| 39 | + |
| 40 | +  |
| 41 | + |
| 42 | + The service principal has the same name as the Stream Analytics job. For example, if the name of your job is *MyASAJob*, the name of the service principal is also *MyASAJob*. |
| 43 | + |
| 44 | +## Select an Active Directory admin |
| 45 | + |
| 46 | +After you've created a managed identity, you select an Active Directory admin. |
| 47 | + |
| 48 | +1. Navigate to your Azure SQL Database resource and select the SQL Server that the database is under. You can find the SQL Server name next to *Server name* on the resource overview page. |
| 49 | + |
| 50 | +1. Select **Active Directory Admin** under **Settings**. Then, select **Set admin**. |
| 51 | + |
| 52 | +  |
| 53 | + |
| 54 | +1. On the Active Directory admin page, search for a user or group to be an administrator for the SQL Server and click **Select**. |
| 55 | + |
| 56 | +  |
| 57 | + |
| 58 | +1. Select **Save** on the **Active Directory admin** page. The process for changing admin takes a few minutes. |
| 59 | + |
| 60 | +## Create a database user |
| 61 | + |
| 62 | +Next, you create a contained database user in your SQL Database that is mapped to the Azure Active Directory identity. The contained database user doesn't have a login for the master database, but it maps to an identity in the directory that is associated with the database. The Azure Active Directory identity can be an individual user account or a group. In this case, you want to create a contained database user for your Stream Analytics job. |
| 63 | + |
| 64 | +1. Connect to the SQL database using SQL Server Management Studio. The **User name** is an Azure Active Directory user with the **ALTER ANY USER** permission. The admin you set on the SQL Server is an example. Use **Azure Active Directory – Universal with MFA** authentication. |
| 65 | + |
| 66 | +  |
| 67 | + |
| 68 | + The server name `<SQL Server name>.database.windows.net` may be different in different regions. For example, the China region should use `<SQL Server name>.database.chinacloudapi.cn`. |
| 69 | + |
| 70 | + You can specify a specific SQL Database by going to **Options > Connection Properties > Connect to Database**. |
| 71 | + |
| 72 | +  |
| 73 | + |
| 74 | +1. When you connect for the first time, you may encounter the following window: |
| 75 | + |
| 76 | +  |
| 77 | + |
| 78 | + 1. If so, go to your SQL Server resource on the Azure portal. Under the **Security** section, open the **Firewalls and virtual network** page. |
| 79 | + 1. Add a new rule with any rule name. |
| 80 | + 1. Use the *From* IP address from the **New Firewall Rule** window for the *Start IP*. |
| 81 | + 1. Use the *To* IP address from the **New Firewall Rule** window for *End IP*. |
| 82 | + 1. Select **Save** and attempt to connect from SQL Server Management Studio again. |
| 83 | + |
| 84 | +1. Once you are connected, create the contained database user. The following SQL command creates a contained database user that has the same name as your Stream Analytics job. Be sure to include the brackets around the *ASA_JOB_NAME*. Use the following T-SQL syntax and run the query. |
| 85 | + |
| 86 | + ```sql |
| 87 | + CREATE USER [ASA_JOB_NAME] FROM EXTERNAL PROVIDER; |
| 88 | + ``` |
| 89 | + |
| 90 | +## Grant Stream Analytics job permissions |
| 91 | + |
| 92 | +The Stream Analytics job has permission from Managed Identity to **CONNECT** to your SQL Database resource. Most likely, it would be efficient to allow the Stream Analytics job to run commands such as **SELECT**. You can grant those permissions to the Stream Analytics job using SQL Server Management Studio. For more information, see the [GRANT (Transact-SQL)](https://docs.microsoft.com/sql/t-sql/statements/grant-transact-sql?view=sql-server-ver15) reference. |
| 93 | + |
| 94 | +Alternatively, you can right-click on your SQL database in SQL Server Management Studio and select **Properties > Permissions**. From the permissions menu, you can see the Stream Analytics job you added previously, and you can manually grant or deny permissions as you see fit. |
| 95 | + |
| 96 | +## Create an Azure SQL Database output |
| 97 | + |
| 98 | +Now that your managed identity is configured, you're ready to add the Azure SQL Database as output to your Stream Analytics job. |
| 99 | + |
| 100 | +1. Go back to your Stream Analytics job, and navigate to the **Outputs** page under **Job Topology**. |
| 101 | + |
| 102 | +1. Select **Add > SQL Database**. In the output properties window of the SQL Database output sink, select **Managed Identity** from the Authentication mode drop-down. |
| 103 | + |
| 104 | +1. Fill out the rest of the properties. To learn more about creating an SQL Database output, see [Create a SQL Database output with Stream Analytics](stream-analytics-define-outputs.md#sql-database). When you are finished, select **Save**. |
| 105 | + |
| 106 | +## Next steps |
| 107 | + |
| 108 | +* [Understand outputs from Azure Stream Analytics](stream-analytics-define-outputs.md) |
| 109 | +* [Azure Stream Analytics output to Azure SQL Database](stream-analytics-sql-output-perf.md) |
0 commit comments