# Redshift

## Establishing a Connection <a href="#default" id="default"></a>

The following connection properties are usually required in order to connect to Amazon Redshift.

* Server: The host name or IP of the server hosting the Amazon Redshift database.
* Database: The database that you created for your Amazon Redshift cluster.

You can also optionally set the following:

* Port: The port of the server hosting the Amazon Redshift database. **5439** by default.

You can obtain these values in the AWS Management Console:

1. Open the Amazon Redshift console (<http://console.aws.amazon.com/redshift>).
2. On the Clusters page, click the name of the cluster.
3. On the Configuration tab, obtain the properties from the Cluster Database Properties section. The connection property values will be the same as the values set in the ODBC URL.

The provider provides secure communication with Amazon Redshift server using SSL encryption. You can optionally turn off SSL encryption by setting UseSSL to false.

You can also leverage SSL authentication to connect to Amazon Redshift data. For that configure the following connection properties:

* SSLClientCert: Set this to the name of the certificate store for the client certificate. Used in the case of 2-way SSL, where truststore and keystore are kept on both the client and server machines.
* SSLClientCertPassword: If a client certificate store is password-protected, set this value to the store's password.
* SSLClientCertSubject: The subject of the TLS/SSL client certificate. Used to locate the certificate in the store.
* SSLClientCertType: The certificate type of the client store.
* SSLServerCert: The certificate to be accepted from the server.

The following is the example connection string to connect Amazon Redshift using standard user/password and inactive SSL encryption:<br>

| `User=username;Password=password;Server=example.us-west-2.redshift.amazonaws.com;Database=your_database;UseSSL=false;"` |
| ----------------------------------------------------------------------------------------------------------------------- |

### Connecting to Amazon Redshift with Azure Active Directory <a href="#connecting-to-amazon-redshift-with-azure-active-directory" id="connecting-to-amazon-redshift-with-azure-active-directory"></a>

#### Create a New User <a href="#create-a-new-user" id="create-a-new-user"></a>

**Note:** If you already have an active Azure AD user account, skip to Connecting with Azure AD.

In the Azure Active Directory tenant, click:

1. **Users**
2. **New user**
3. **Create new user**
4. Fill out your details and click **Create**.
5. Log in and choose the **Consent on behalf of your organization** option, then the Need admin approval window appears.
6. Click **Accept**.

#### Connecting with Azure Active Directory Authentication <a href="#connecting-with-azure-active-directory-authentication" id="connecting-with-azure-active-directory-authentication"></a>

**Note:** Only non-B2C Azure tenants will be able to complete the Azure AD authentication scheme.

**Note:** You must have an active Azure AD account. If you do not have an active account, make one before beginning this process.

1. First, create an OAuth app for logging into your Amazon Redshift database via Azure. On the **Azure Active Directory Overview** page, in the left navigation bar:
   1. Click **App registrations**.
   2. Click **New registrations** at the top of the **App registrations** page.
2. On the **Register an application** page, fill in your details and click **Register** at the bottom of the page. Make note of your CallbackURL.
3. From the newly registered application, click **Expose an API** in the left navigation bar.
4. Next to the Application ID URI, click **Set**.
5. The **Set the App ID URI** dialog appears with the information filled in from registering. Click **Save**.
6. Click **Add a scope**.
7. Fill in your details and click **Add scope** at the bottom of the form.
8. Next, create another OAuth app, which will serve as the client application for your Amazon Redshift database. In the left navigation bar, click **App registrations**.
   1. From the Azure Active Directory management page, click **New registrations** at the top of the **App registrations** page.
   2. On the **Register an application** page, fill in your details and click **Register** at the bottom of the page.
9. Following the creation of the app, you will be brought to its overview page. From there, in the left navigation bar:
   1. Click on **Certificates & secrets**.
   2. Click on **New client secret**.
   3. In the **Add a client secret** window, add in your details and click **Add** at the bottom of the window.
   4. Make a note of your OAuthClientSecret (the **Value** field of the OAuth secret that is displayed).
10. In the left navigation bar of the client app's management page:
    1. Click **API permissions**.
    2. Click **Add a permission**.
    3. Choose **Microsoft Graph API**.
    4. Click **Application permissions**.
    5. Set Client app permissions to **Directory.Read.All**.
    6. Click **Add** at the bottom.
    7. Click **Grant admin consent**.
    8. Click **Yes**.
11. In the Azure Active Directory left navigation bar:
    1. Click **Groups**.
    2. On the Groups page, click **New group** and fill in the details.
    3. Click **No owners selected**.
    4. The **Add owners** window appears. Select the user.
    5. Click **Create**.
12. In the Azure Active Directory left navigation bar:
    1. Click **App registrations**.
    2. Click the tab **All applications**, and choose your first OAuth application.
13. On the OAuth screen, in the left navigation bar, click **Manifest**. Look in the editor for the **accessTokenAcceptedVersion**. If the value is null, it is a v1.0 token. If the value is set to 2, this is a v2.0 token.
14. From the Amazon Redshift instance's query box, submit the identity provider query, following the example below:<br>

    | `CREATE IDENTITY PROVIDER oauth_standard TYPE azureNAMESPACE 'mynamespace'PARAMETERS '{"issuer":"https://sts.windows.net/your_tenant_here/","client_id":"YourClientId","client_secret":"YourClientSecret","audience":["your_application_id_uri_here"]}'` |
    | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

    \
    Terminology Guide:

    * **Your issuer ID:** The issuer ID to trust when a token is received. The unique identifier for the tenant\_id is appended to the issuer. If using a v1.0 token, use <https://sts.windows.net/\\>\<your\_tenant\_id\_here>/. If using a v2.0 token, use <https://login.microsoftonline.com/\\>\<your\_tenant\_id\_here>/v2.0.
    * **Your client\_id:** The unique, public identifier of the application registered with the identity provider. This is referred to as the application ID.
    * **Your client\_secret:** A secret identifier, or password, known only to the identity provider and the registered application.
    * **audience:** The Application ID (URI) assigned to the OAuth app.

    \
    You can use any name you like for the NAMESPACe.
15. In Amazon Redshift, place the CREATE IDENTITY PROVIDER query (like above example) into the query text box.
16. Click **Run** at the bottom of the query box.
17. In the query text box, create a role on the Redshift database in this format:<br>

    | `create role "mynamespace:myazuregroup";` |
    | ----------------------------------------- |

    Replace with your identity provider's namespace provided in the CREATE IDENTITY PROVIDER query and the name of Azure group you created earlier. Click the **Run** button at the bottom of the query box.
18. In the query text box, grant table access to this new role as follows:<br>

    | `grant select on all tables in` `schema public` `to role "mynamespace:myazuregroup";` |
    | ------------------------------------------------------------------------------------- |

    Replace the above example with your namespace and Azure group name.
19. Click **Run** at the bottom of the query box.

    * AuthScheme: Set this to **AzureAD**.
    * Server: Set this to the name of your Amazon Redshift server endpoint.
    * Database: Set this to the name of your Amazon Redshift database that you'd like to connect to.
    * User: Set this to the name of the authenticating Amazon Redshift user.
    * AzureTenant: Set this to the ID of the Azure Tenant that your OAuth and client apps were created under. Find this in the Overview page of one of the apps under **Directory (tenant) ID**.
    * SSOLoginURL: Set this to the value of the **Application ID URI**, visible on the Overview page of your OAuth app.
    * Scope: For v1.0 OAuth tokens, set this to the **Scopes** field in the **Expose an API** page of your OAuth app. For v2.0 OAuth tokens, this will be the same as the OAuth app's Client ID.
    * OAuthClientID: Set this to the **Application (client) ID** in the Overview page of the Amazon Redshift client app you created.
    * OAuthClientSecret: Set this to the **Value** of the OAuth client secret noted upon its creation in your client app's **Certificates & secrets** page.
    * CallbackURL: Set this to the callback URL of the OAuth app.

    \
    **Troubleshooting Note:** If an "Azure JWT token does not have 'upn' field" error is encountered:

    1. On the Azure Active Directory management page, navigate to App Registrations and select your OAuth app.
    2. Click **Token configuration** in the left navigation bar.
    3. Click **Add optional claim**.
    4. In the **Add optional claim** screen, under **Token type**, click **Access**.
    5. Under the **Claim** column, select **upn**.
    6. Click **Add** at the bottom.
    7. Select **Turn on the Microsoft Graph profile permission (required for claims to appear in token)**.
    8. Click **Add**.
    9. Repeat this process for the client app.
    10. Attempt the connection again.

#### Connect Using Standard Authentication <a href="#connect-using-standard-authentication" id="connect-using-standard-authentication"></a>

Set the AuthScheme to **Basic** in order to connect to Amazon Redshift with login credentials. In addition, set the following connection properties:

* User: The user which will be used to authenticate with the Amazon Redshift server.
* Password: The password which will be used to authenticate with the Amazon Redshift server.

The following is an example connection string:<br>

| `AuthScheme=Basic;User=user;Password=password;Server=example.us-west-2.redshift.amazonaws.com;Database=your_database;` |
| ---------------------------------------------------------------------------------------------------------------------- |

#### Authenticating with IAM credentials <a href="#authenticating-with-iam-credentials" id="authenticating-with-iam-credentials"></a>

Set the AuthScheme to **IAMCredentials**. The following is an example connection string:<br>

| `AuthScheme=IAMCredentials;Server=example.us-west-2.redshift.amazonaws.com;Database=your_database;User=your_user;AWSAccessKey=your_access_key;AWSSecretKey=your_secretkey;` |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

If you are connecting IAM role with temporary credentials you are also required to apply AWSSessionToken.

You can optionally apply:

* AutoCreate: Create a database user with the name specified for User if one does not exist while connecting.
* DbGroups: Database groups the database user joins for the current session.

#### Authenticating with ADFS <a href="#authenticating-with-adfs" id="authenticating-with-adfs"></a>

Set the AuthScheme to **ADFS**. The following connection properties need to be set:

* User: Set this to the ADFS user.
* Password: Set this to ADFS password for the user.
* SSOLoginURL: Set this to the login url used by the SSO provider.

Below is an example connection string:<br>

| `AuthScheme=ADFS;User=username;Password=password;SSOLoginURL='https://sts.company.com';` |
| ---------------------------------------------------------------------------------------- |

### ADFS Integrated <a href="#adfs-integrated" id="adfs-integrated"></a>

The ADFS Integrated flow indicates you are connecting with the **currently logged in Windows user credentials**. To use the ADFS Integrated flow, simply do not specify the User and Password, but otherwise follow the same steps in the ADFS guide above.

#### Authenticating with PingFederate <a href="#authenticating-with-pingfederate" id="authenticating-with-pingfederate"></a>

Set the AuthScheme to **PingFederate**. The following connection properties need to be set:

* User: Set this to the PingFederate user.
* Password: Set this to PingFederate password for the user.
* SSOLoginURL: Set this to the login url used by the SSO provider.

The following SSOProperties are needed to authenticate to PingFederate:

* AuthScheme (optional): The authorization scheme to be used for the IdP endpoint. The allowed values for this IdP are None or Basic.

Additionally, you can use the following SSOProperties to configure mutual SSL authentication for SSOLoginURL, the WS-Trust STS endpoint:

* SSLClientCert
* SSLClientCertType
* SSLClientCertSubject
* SSLClientCertPassword

Below is an example connection string:<br>

| `Server=redshift-cluster-1.xxxxxxxxxxxx.us-east-1.redshift.amazonaws.com;Database=dev;Port=5439;UseSSL=true;SSLServerCert=*;AuthScheme=PingFederate;AutoCreate=TRUE;SSOLoginURL=https://mycustomserver.com:9033/idp/sts.wst;SSOExchangeUrl=https://us-east-1.signin.aws.amazon.com/platform/saml/acs/764ef411-xxxxxx;User=admin;Password=PassValue;AWSRegion=NORTHERNVIRGINIA;` |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
