Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing sql-action v2 #122

Merged
merged 18 commits into from
Sep 28, 2022
Merged

Introducing sql-action v2 #122

merged 18 commits into from
Sep 28, 2022

Conversation

zijchen
Copy link
Contributor

@zijchen zijchen commented Aug 29, 2022

Merging v2 changes to master branch.

Main changes in v2:

  • Adds support for ActiveDirectoryPassword, ActiveDirectoryServicePrincipal, and ActiveDirectoryDefault connection strings.
  • Adds support for Script, DeployReport, and DritReport sqlpackage actions (see Add support for Script, DeployReport, and DriftReport #106)
  • Uses go-sqlcmd instead of sqlcmd for script action. This makes script action possible with the different AAD authentication types. go-sqlcmd will be automatically downloaded and cached on the runner VM if it is not found.
  • [Backend changes] Uses mssql for connection string parsing and the initial firewall check, also makes AAD possible.
  • Various bug fixes

Breaking changes from v1:

For complete documentation for v2, visit the README on v2 branch

  • Changes to the yaml inputs:
    • The project-file, dacpac-package, sql-file parameters are consolidated into a single path parameter. sql-action will determine the appropriate action based on the file extension.
    • New action parameter used to specify the sqlpackage action (when .sqlproj or .dacpac files are used)
    • Additional arguments and build-arguments for optional arguments
  • go-sqlcmd has some breaking changes from sqlcmd, view the complete list here: https://docs.microsoft.com/sql/tools/go-sqlcmd-utility#breaking-changes-from-sqlcmd

zijchen added 11 commits June 6, 2022 17:01
* Use tedious mssql library instead of sqlcmd

* Fix mssql connection

* Fix SqlUtils tests

* Use config instead of connection string

* Replace conn string builder with mssql config

* Connect to master db

* Restore connection string validation regex

* PR comments, fix error handling

* Update main.js

* Use try catch for error handling

* Fix typo
* Change script action from sqlcmd to mssql query

* Update action.yml

* Fully qualify Table1 in sql script

* Add more debug logging

* Clone config before changing db to master

* Cleanup

* Set TEST_DB name before cleanup

* Use runner.temp

* Always cleanup

* PR comments
* Debug script contents

* Fix sed command

* Remove debug
…ipal auth (#100)

* Use tedious mssql library instead of sqlcmd

* Fix mssql connection

* Fix SqlUtils tests

* Use config instead of connection string

* Replace conn string builder with mssql config

* Connect to master db

* Restore connection string validation regex

* AAD auth

* Add support for client and tenant IDs

* Add more debug messaging

* Fix connection string find array

* Make client-id and tenant-id action inputs

* Fix error handling

* More fixes

* Use try catch instead

* Add tests to pr-check.yml

* Change script action from sqlcmd to mssql query

* Update action.yml

* Fully qualify Table1 in sql script

* Add more debug logging

* Clone config before changing db to master

* Cleanup

* Set TEST_DB name before cleanup

* Use runner.temp

* Always cleanup

* Add tests for different auth types

* Mask tenant and client IDs

* Add AAD password test to pr-check.yml

* Fix yaml

* Limit max-parallel to 2

* Add test for service principal

* PR comments

* Fix typo
* Retry with DB connection if master fails

* Add tests

* Add ConnectionResult interface

* Add missing doc comment

* PR comments

* PR Comments
* Add setup script to download go-sqlcmd

* Add sqlcmd call to pr-check.yml

* Add bz2 specific extract tar

* Move setup code to main

* Move setup code to main

* Fix casing of Setup.ts
* call go-sqlcmd for script action

* Fix auth options not flowing through

* Add test cases

* Restore sqlcmd variable in cleanup script

* Fix pr-check cleanup

* Undo changes to pr-check.yml

* Undo pr-check.yml changes

* PR comments
* Change inputs

* Add other publish like actions

* Add tests

* Fix test

* PR comments
@zijchen zijchen temporarily deployed to Automation test September 1, 2022 16:04 Inactive
@zijchen zijchen temporarily deployed to Automation test September 1, 2022 16:04 Inactive
@zijchen zijchen temporarily deployed to Automation test September 1, 2022 16:04 Inactive
@zijchen zijchen temporarily deployed to Automation test September 1, 2022 16:04 Inactive
@zijchen zijchen temporarily deployed to Automation test September 1, 2022 16:04 Inactive
@zijchen zijchen temporarily deployed to Automation test September 1, 2022 16:04 Inactive
src/AzureSqlActionHelper.ts Show resolved Hide resolved
@vermegi
Copy link
Member

vermegi commented Sep 26, 2022

@zijchen I am in the middle of testing this out with a Service Principal logging, but v2 keeps on failing with:
Error: Failed to add firewall rule. Unable to detect client IP Address. sqlcmd: error: unexpected argument Directory.

Action config looks like:

database-updates:
runs-on: ubuntu-latest
needs: deploy-infra
steps:
- uses: actions/checkout@v2
- name: Azure SQL Allow MI
uses: Azure/sql-action@v2
with:
# Name of the Azure SQL Server name, like Fabrikam.database.windows.net.
# server-name: ${{ steps.deploy.outputs.sqlServerFullyQualifiedDomainName }}
# The connection string, including authentication information, for the Azure SQL Server database.
connection-string: ${{ format('Server={0};Initial Catalog={1};Authentication=Active Directory Service Principal;User Id={2};Password={3}', needs.deploy-infra.outputs.sqlServerFullyQualifiedDomainName, needs.deploy-infra.outputs.databaseName, secrets.SQL_USERNAME, secrets.SQL_PASSWORD) }}
# Path to SQL script file to deploy
path: ./deploy/mi.sql

Where SQL_USERNAME contains SP clientID and SQL_PASSWORD contains clientSecret of the SP.

I do have a previous job that has an azure login. But the login is not present in the database-updates job, so I would guess this then defaults to not setting temp FW rules and uses current FW rules. SQL Server in Azure has been setup with allow azure services access.
SP has contributor on the RG like indicated in the new README file on the v2 branch.

@vermegi
Copy link
Member

vermegi commented Sep 26, 2022

So the issue in my case is in the connection string:
sqlcmd: error: unexpected argument Directory.

changed connection string to:

connection-string: ${{ format('Server={0};Initial Catalog={1};Authentication="Active Directory Service Principal";User Id={2};Password={3}', needs.deploy-infra.outputs.sqlServerFullyQualifiedDomainName, needs.deploy-infra.outputs.databaseName, secrets.SQL_USERNAME, secrets.SQL_PASSWORD) }}

Might want to change that error message to 'Failed to connect to database' instead of 'failed to add FW rule', since that is misleading.

Also, might want to add the format of the connectionstring in the README file. Currently I had to look for it in the test cases in the code.

Still struggling a bit with the rest of the connection string ... will let you know once I get this working.

@zijchen
Copy link
Contributor Author

zijchen commented Sep 26, 2022

'Server={0};Initial Catalog={1};Authentication=Active Directory Service Principal;User Id={2};Password={3}'

@vermegi Thank you for trying v2. Your connection string should have worked, I have submitted #137 to fix the issue of requiring quotation marks around the authentication option. Could you tell us more about the issue you're still facing after adding the quotation marks? Feel free to share your debug logs with us too.

@dzsquared
Copy link
Collaborator

dzsquared commented Sep 26, 2022

@vermegi @zijchen I'll have a PR into v2 with updates to the readme with connection string info

done - #138

@vermegi
Copy link
Member

vermegi commented Sep 27, 2022

Still getting an error:
mssql: login error: Login failed for user ''.

This is the connection string:
Run Azure/sql-action@v2
with:
connection-string: Server=appsvcnetworkingdemoxprjxhuoxapcy.database.windows.net;Initial Catalog=sampledb;Authentication="Active Directory Service Principal";User Id=;
path: ./deploy/mi.sql
env:
AZURE_RESOURCEGROUP_NAME: appsvcnetworkingdemo

I tried both clientId and clientId@tenant of the service principal as value for the User Id. The service principal has both contributor and SQL Security Manager permissions on the resource group.

Any hints on how to properly make the login work would be appreciated.

@vermegi
Copy link
Member

vermegi commented Sep 27, 2022

Btw, the actual connection string is this:
connection-string: ${{ format('Server={0};Initial Catalog={1};Authentication="Active Directory Service Principal";User Id={2};Password={3}', needs.deploy-infra.outputs.sqlServerFullyQualifiedDomainName , needs.deploy-infra.outputs.sqlDatabaseName, secrets.SQL_USERNAME, secrets.SQL_PASSWORD) }}

Full workflow file: https://github.com/vermegi/app-service-networking-samples/blob/givermei-workflow-update/.github/workflows/infradeploy.yml

It's just in the action workflow output that 'Password' seems to get fully obfuscated.

Error:
image

adding connection string format to docs
@dzsquared dzsquared temporarily deployed to Automation test September 27, 2022 15:53 Inactive
@dzsquared dzsquared temporarily deployed to Automation test September 27, 2022 21:10 Inactive
@dzsquared dzsquared temporarily deployed to Automation test September 27, 2022 21:10 Inactive
@dzsquared dzsquared temporarily deployed to Automation test September 27, 2022 21:10 Inactive
@dzsquared dzsquared temporarily deployed to Automation test September 27, 2022 21:10 Inactive
@dzsquared dzsquared temporarily deployed to Automation test September 27, 2022 21:10 Inactive
@dzsquared dzsquared temporarily deployed to Automation test September 27, 2022 21:10 Inactive
@zijchen
Copy link
Contributor Author

zijchen commented Sep 27, 2022

@vermegi I tried something exactly like your yaml in my own repo and it seemed to work. I have some ideas you can try:

  1. See if you can call go-sqlcmd directly from your machine. If you also get the "Login failed for user <token-identified principal>" error then it's likely a SQL issue.
SET SQLCMDPASSWORD=<service_principal_client_secret>
sqlcmd -S <server_name> -d <database_name> --authentication-method=ActiveDirectoryServicePrincipal -U <client_id> -i <sql_file>
  1. Check to make sure the service principal has a corresponding user in the server and it has the appropriate permissions. You should see a row from the query below with the name of your service principal user.
SELECT * FROM sys.database_principals WHERE type = 'E'
  1. If the service principal used for sql-action the same as the one you're using for the azure/login step? If so, in your database-updates job, could you try adding the azure/login, then changing the connection string to "Active Directory Default" authentication, no need for user ID/password. sql-action should be able to connect using the credentials from the azure/login step.

@vermegi
Copy link
Member

vermegi commented Sep 28, 2022

I probably have a chicken or egg problem there, then, since trying to automate a full setup ... and this is not an issue of this specific action btw, but of SQL Server on Azure in general.

@vermegi
Copy link
Member

vermegi commented Sep 28, 2022

Login succeeds and works like a charm when I first add my SP as a user in the database.
For my deployment script however the chicken or egg problem remains, but again, that's not an issue on this action, it's a SQL issue.

@zijchen
Copy link
Contributor Author

zijchen commented Sep 28, 2022

Login succeeds and works like a charm when I first add my SP as a user in the database. For my deployment script however the chicken or egg problem remains, but again, that's not an issue on this action, it's a SQL issue.

That's good to hear. Maybe you can first create an AAD user and run this action with AAD Password auth to create your SP user?

@zijchen zijchen merged commit b62786f into master Sep 28, 2022
@zijchen zijchen deleted the v2 branch September 28, 2022 21:22
@vermegi
Copy link
Member

vermegi commented Sep 29, 2022

Well, thing is I don't have the permissions to do that on the tenant I'm using. So will unfortunately not be able to perform that test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants