|
| 1 | +# How to set up Snowflake Key-Based Auth for CI Service Accounts |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Snowflake service accounts must be set up with key-based auth as password based auth is being deprecated. These accounts are typically used for CI/CD. |
| 6 | + |
| 7 | +## Creating key pair |
| 8 | + |
| 9 | +Outside of Snowflake create a key-pair following the information on the [Snowflake documentation](https://docs.snowflake.com/en/user-guide/key-pair-auth) |
| 10 | + |
| 11 | +First Generate the Private Key |
| 12 | +`openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8 -nocrypt` |
| 13 | + |
| 14 | +From the Private Key, generate the Public Key |
| 15 | +`openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub` |
| 16 | + |
| 17 | +Store the private and public keys somewhere secure. |
| 18 | + |
| 19 | +## Configure the service user in Snowflake |
| 20 | + |
| 21 | +Print out the public key and add to Snowflake |
| 22 | + |
| 23 | +`cat rsa_key.pub` |
| 24 | + |
| 25 | +This will show your public kay which will replace `<your public key>` below. |
| 26 | + |
| 27 | +>[!NOTE] Exclude the --BEGIN-- and --END-- lines from the public key |
| 28 | +
|
| 29 | +`ALTER USER SVC_GITHUB_ACTIONS SET RSA_PUBLIC_KEY='<your public key>';` |
| 30 | + |
| 31 | +## Verify the public key was set correctly |
| 32 | + |
| 33 | +Run the following command in Snowflake |
| 34 | +``` |
| 35 | +DESC USER SVC_GITHUB_ACTIONS; |
| 36 | +SELECT SUBSTR((SELECT "value" FROM TABLE(RESULT_SCAN(LAST_QUERY_ID())) |
| 37 | + WHERE "property" = 'RSA_PUBLIC_KEY_FP'), LEN('SHA256:') + 1); |
| 38 | +``` |
| 39 | + |
| 40 | +Run the following command in the terminal |
| 41 | +`openssl rsa -pubin -in rsa_key.pub -outform DER | openssl dgst -sha256 -binary | openssl enc -base64` |
| 42 | + |
| 43 | +Compare both outputs. If both outputs match, the user correctly configured their public key. |
| 44 | + |
| 45 | +## Configure Github Actions |
| 46 | + |
| 47 | +In Github, you must configure the Private Key. To do this visit the settings page of your repo. In the `Security` section click `Secrets and Variables` then select `Actions`. |
| 48 | + |
| 49 | +In the `Secrets` tab add a `New Repository Secret`. |
| 50 | +Give it a `Name` like `DATACOVES__MAIN__PRIVATE_KEY` |
| 51 | + |
| 52 | +Print the Private Key generated earlier. |
| 53 | +`cat rsa_key.p8` |
| 54 | + |
| 55 | +>[!NOTE] Exclude the --BEGIN-- and --END-- lines from the private key |
| 56 | +
|
| 57 | +Copy the content and of the private key and paste it as the value for the Github `Secret` and `Add Secret`. |
| 58 | + |
| 59 | +## Configure the dbt profile |
| 60 | + |
| 61 | +Update the profile you use for CI/CD. Typically this is located in `automate/dbt/profiles.yml` if using the recommended Datacoves location. |
| 62 | + |
| 63 | +It should look something like this: |
| 64 | + |
| 65 | +```yaml |
| 66 | +default: |
| 67 | + target: default_target |
| 68 | + outputs: |
| 69 | + default_target: |
| 70 | + type: snowflake |
| 71 | + threads: 16 |
| 72 | + client_session_keep_alive: true |
| 73 | + |
| 74 | + account: "{{ env_var('DATACOVES__MAIN__ACCOUNT') }}" |
| 75 | + database: "{{ env_var('DATACOVES__MAIN__DATABASE') }}" |
| 76 | + schema: "{{ env_var('DATACOVES__MAIN__SCHEMA') }}" |
| 77 | + user: "{{ env_var('DATACOVES__MAIN__USER') }}" |
| 78 | + private_key: "{{ env_var('DATACOVES__MAIN__PRIVATE_KEY') }}" |
| 79 | + role: "{{ env_var('DATACOVES__MAIN__ROLE') }}" |
| 80 | + warehouse: "{{ env_var('DATACOVES__MAIN__WAREHOUSE') }}" |
| 81 | +``` |
0 commit comments