Provision a Connect user programmatically

Connect creates a user the first time that person signs in. A Snowflake service user that authenticates with a key pair does not sign in through a browser, so it has no Connect account and cannot create a Connect API key for itself.

An administrator can create the Connect user ahead of time with the Connect Server API, then create an API key for that user. The service user then calls Connect with that key, as described in Programmatic access from outside Snowflake.

How it works

Posit Team signs users in to Connect with OpenID Connect. Connect identifies each user by a unique ID, which is the user’s Snowflake user name. When you create a Connect user, you supply that same value as the user’s external_id. If the service user ever signs in, Connect finds the account you created instead of making a second one.

Use the Snowflake user name exactly as Snowflake reports it. A different external_id is treated as a different person.

Before you begin

  • A Connect administrator API key. Provision one in Connect if you do not have one.
  • A Snowflake credential for the administrator that the Connect ingress accepts, such as a programmatic access token. See Allow token authentication.
  • A Snowflake role that is allowed to run DESCRIBE USER for the service user.
  • The service user holds a Posit Team application role that grants access to Connect, such as POSIT_TEAM_USER.

Describe the Snowflake user

Run DESCRIBE USER to read the service user’s details from Snowflake.

Snowflake SQL
DESCRIBE USER <service-user>;

The statement returns one row for each property. The property column names the property and the value column holds its value. Use these properties for the Connect user.

Connect field DESCRIBE USER property Notes
external_id NAME The Snowflake user name.
username NAME The name shown in Connect.
email EMAIL Connect requires an email address. Set one on the Snowflake user first if the property is empty.
first_name FIRST_NAME
last_name LAST_NAME
user_role Not a Snowflake property. See below.

Choose user_role from the application roles that the service user holds:

Application role Connect user_role
POSIT_CONNECT_VIEWER viewer
POSIT_CONNECT_PUBLISHER, POSIT_TEAM_USER publisher
POSIT_CONNECT_ADMIN, POSIT_TEAM_ADMIN administrator

Connect does not restrict the role you assign when you create a user through its API, so choose the role that matches the user’s Snowflake roles.

Create the user and API key

The example reads the service user’s properties, creates the Connect user, and creates an API key for it. Set the environment variables as shown in Set the values in your environment. Here, CONNECT_API_KEY is the administrator’s key and SNOWFLAKE_PAT is the administrator’s token.

Important

Connect manages user details through its authentication provider. The Connect API can create a user but does not permit changes. Modify users with the Connect SCIM API.

Terminal
python -m pip install requests snowflake-connector-python
provision_user.py
import os

import requests
import snowflake.connector

CONNECTION_NAME = "default"
SERVICE_USER = "<service-user>"
USER_ROLE = "publisher"

CONNECT_SERVER = os.environ["CONNECT_SERVER"].rstrip("/")
CONNECT_API_KEY = os.environ["CONNECT_API_KEY"]
SNOWFLAKE_PAT = os.environ["SNOWFLAKE_PAT"]
API = f"{CONNECT_SERVER}/__api__/v1"

# Read the service user's details from Snowflake.
with snowflake.connector.connect(connection_name=CONNECTION_NAME) as connection:
    rows = connection.cursor().execute(f"DESCRIBE USER {SERVICE_USER}").fetchall()
properties = {row[0]: row[1] for row in rows}

session = requests.Session()
# Snowflake reads the Authorization header, so Connect reads its API key from
# X-RSC-Authorization.
session.headers["Authorization"] = f'Snowflake Token="{SNOWFLAKE_PAT}"'
session.headers["X-RSC-Authorization"] = f"Key {CONNECT_API_KEY}"

# Create the Connect user.
response = session.post(
    f"{API}/users",
    json={
        "external_id": properties["NAME"],
        "username": properties["NAME"],
        "email": properties["EMAIL"],
        "first_name": properties["FIRST_NAME"],
        "last_name": properties["LAST_NAME"],
        "user_role": USER_ROLE,
    },
)
response.raise_for_status()
guid = response.json()["guid"]

# Create an API key for that user.
response = session.post(f"{API}/users/{guid}/keys", json={"name": "service-user"})
response.raise_for_status()
print(response.json()["key"])

Store the key somewhere safe, such as your secrets manager.

Modify a user

Use the Connect SCIM API to change the username, email, first name or last name of a user you created. SCIM does not change user_role.

The example reads the service user’s properties from Snowflake, finds the Connect user by externalId, and updates the fields. It uses an administrator API key to create a service token with the identity:manage scope, then deletes the token. The environment variables are the same as for provision_user.py.

Important

Connect manages user details through its authentication provider. Updates happen automatically whenever that person visits Connect in a browser. Use the SCIM API when users do not visit Connect.

modify_user.py
import os

import requests
import snowflake.connector

CONNECTION_NAME = "default"
SERVICE_USER = "<service-user>"

CONNECT_SERVER = os.environ["CONNECT_SERVER"].rstrip("/")
CONNECT_API_KEY = os.environ["CONNECT_API_KEY"]
SNOWFLAKE_PAT = os.environ["SNOWFLAKE_PAT"]
API = f"{CONNECT_SERVER}/__api__/v1"
SCIM = f"{CONNECT_SERVER}/scim/v2"

# Read the service user's details from Snowflake.
with snowflake.connector.connect(connection_name=CONNECTION_NAME) as connection:
    rows = connection.cursor().execute(f"DESCRIBE USER {SERVICE_USER}").fetchall()
properties = {row[0]: row[1] for row in rows}

session = requests.Session()
# Snowflake reads the Authorization header, so Connect reads its credentials
# from X-RSC-Authorization.
session.headers["Authorization"] = f'Snowflake Token="{SNOWFLAKE_PAT}"'
admin = {"X-RSC-Authorization": f"Key {CONNECT_API_KEY}"}

# Create a service token that can manage identities.
response = session.post(
    f"{API}/system/service-tokens",
    headers=admin,
    json={"name": "user-provisioning", "scopes": ["identity:manage"]},
)
response.raise_for_status()
service_token = response.json()
scim = {
    "X-RSC-Authorization": f"Bearer {service_token['key']}",
    "Content-Type": "application/scim+json",
}

try:
    # Find the Connect user by external ID, which is the Snowflake user name.
    response = session.get(
        f"{SCIM}/Users",
        headers=scim,
        params={"filter": f'externalId eq "{properties["NAME"]}"'},
    )
    response.raise_for_status()
    user_id = response.json()["Resources"][0]["id"]

    # Update the fields from the Snowflake properties.
    response = session.patch(
        f"{SCIM}/Users/{user_id}",
        headers=scim,
        json={
            "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
            "Operations": [
                {"op": "replace", "path": "name.givenName", "value": properties["FIRST_NAME"]},
                {"op": "replace", "path": "name.familyName", "value": properties["LAST_NAME"]},
                {
                    "op": "replace",
                    "path": "emails[primary eq true].value",
                    "value": properties["EMAIL"],
                },
            ],
        },
    )
    response.raise_for_status()
finally:
    # Delete the service token.
    session.delete(
        f"{API}/system/service-tokens/{service_token['guid']}", headers=admin
    ).raise_for_status()
Back to top