Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.3k stars 1.96k forks source link

[QUERY] Example of creating a database in a SQL Server using the Java SDK #41353

Open Blackbaud-JasonBodnar opened 1 month ago

Blackbaud-JasonBodnar commented 1 month ago

Query/Question I've created a SQL Server in Azure with a Server admin login and password. I'm trying to automate the creation of databases for multiple projects as part of a gradle plugin and task that will be used as part of our build system. I can't find an example of how to do this with the Java SDK. I tried:

UsernamePasswordCredential credential = new UsernamePasswordCredentialBuilder()
            .clientId(clientId)
            .username(username)
            .password(password)
            .build()
SqlServerManager sqlServerManager = SqlServerManager.authenticate(credential, new AzureProfile(AzureEnvironment.AZURE))
SqlServer sqlServer = sqlServerManager.sqlServers().getById(clientId)
if (sqlServer.databases().get(databaseName) != null) {
    sqlServer.databases().define(databaseName).create()
}

Where clientId = the Resource id from the Properties page of my SQL server and username and password are the server admin login password I provided when creating the SQL server.

I get the following error:

[Correlation ID: fa9b5f52-a5bd-4758-a445-0084c8be811a] Execution of class com.microsoft.aad.msal4j.AcquireTokenByAuthorizationGrantSupplier failed: AADSTS90019: No tenant-identifying information found in either the request or implied by any provided credentials. Trace ID: 83e1bebb-1ed0-45bf-a840-f19337665300 Correlation ID: fa9b5f52-a5bd-4758-a445-0084c8be811a Timestamp: 2024-07-30 16:09:14Z
Azure Identity => ERROR in getToken() call for scopes [https://management.core.windows.net//.default]: Failed to acquire token with username and password. To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/java/identity/usernamepasswordcredential/troubleshoot
{"az.sdk.message":"Failed to acquire a new access token.","exception":"Failed to acquire token with username and password. To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/java/identity/usernamepasswordcredential/troubleshoot"}

Why is this not a Bug or a feature Request? I would assume that this is possible but there's no example online that I can find.

Setup (please complete the following information if applicable):

github-actions[bot] commented 1 month ago

Thank you for your feedback. Tagging and routing to the team member best able to assist.

weidongxu-microsoft commented 1 month ago

@Blackbaud-JasonBodnar

the

UsernamePasswordCredential credential = new UsernamePasswordCredentialBuilder()
            .clientId(clientId)
            .username(username)
            .password(password)
            .build()

part is to authorize the client to connect to https://management.azure.com, not about SQL server. Therefore, you should never use SQL name/password on this.

Please see below https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/resourcemanager#authentication and more advanced below https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/resourcemanager/docs/AUTH.md

github-actions[bot] commented 1 month ago

Hi @Blackbaud-JasonBodnar. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

Blackbaud-JasonBodnar commented 1 month ago

... part is to authorize the client to connect to https://management.azure.com, not about SQL server. Therefore, you should never use SQL name/password on this.

Please see below https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/resourcemanager#authentication and more advanced below https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/resourcemanager/docs/AUTH.md

I've looked at those several times but they're not very clear. Are you saying there's no way to call SqlServerManager.authenticate() with SQL name and password? If so, what is the appropriate way to create a database in a sql server in a headless environment? I need to create and then delete a database as part of our build pipeline in order to run tests so there will never be any kind of user interaction, only either a password or some kind of password-like key.

weidongxu-microsoft commented 1 month ago

... part is to authorize the client to connect to https://management.azure.com, not about SQL server. Therefore, you should never use SQL name/password on this. Please see below https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/resourcemanager#authentication and more advanced below https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/resourcemanager/docs/AUTH.md

I've looked at those several times but they're not very clear. Are you saying there's no way to call SqlServerManager.authenticate() with SQL name and password? If so, what is the appropriate way to create a database in a sql server in a headless environment? I need to create and then delete a database as part of our build pipeline in order to run tests so there will never be any kind of user interaction, only either a password or some kind of password-like key.

What I mean is the SqlServerManager.authenticate() is the authentication for the management of all resources relates to the Azure Microsoft SQL server (that is, all the APIs here https://learn.microsoft.com/rest/api/sql/operation-groups?view=rest-sql-2021-11-01), include create/delete an SQL database

You can use "service principal" for the authentication, see https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/identity/azure-identity#authenticate-service-principals


The "SQL name and password" is only for you to connect to that SQL server (probably to SQL database within it). https://learn.microsoft.com/azure/azure-sql/database/logins-create-manage?view=azuresql#existing-logins-and-user-accounts-after-creating-a-new-database, via maybe ODBC