

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用身分增強 IAM 角色工作階段連線至 Redshift
<a name="identity-center-authentication"></a>

您可以使用 IAM Identity Center 來提供 Amazon Redshift 叢集和無伺服器工作群組的聯合存取。此方法可讓使用者使用其 Identity Center 登入資料進行身分驗證。

Amazon Redshift 提供 `GetIdentityCenterAuthToken` API 操作，以產生包含使用者身分資訊的授權字符。這些 APIs適用於佈建叢集和無伺服器工作群組。字符可讓您使用現有的 Identity Center 設定，無縫存取 Amazon Redshift 資料庫的單一登入。

## 先決條件
<a name="identity-center-auth-prerequisites"></a>

將 Identity Center 身分驗證與 Amazon Redshift 搭配使用之前，請確定您有下列項目：
+ **Identity Center 設定：**您的帳戶必須使用使用者身分和適當的應用程式指派來設定 IAM Identity Center。如需設定說明，請參閱[設定 IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/enable-identity-center.html)。
**重要**  
如果您想要連線至 Redshift，您必須使用 redshift：connect 範圍。
+ **身分增強憑證：**您的應用程式必須使用包含內嵌使用者身分資訊的身分增強憑證。如需詳細資訊，請參閱[使用身分增強的 IAM 角色工作階段](https://docs.aws.amazon.com/singlesignon/latest/userguide/trustedidentitypropagation-identity-enhanced-iam-role-sessions.html)。
+ **IAM 許可：**您的 IAM 角色或使用者必須具有呼叫 `GetIdentityCenterAuthToken` API 和存取指定叢集或工作群組的許可。必要許可：
  + 對於佈建的叢集：在叢集 ARN `redshift:GetIdentityCenterAuthToken`上 （格式：`arn:aws:redshift:region:account:cluster:cluster-name`) ARNs 
  + 對於無伺服器工作群組：在工作群組 ARNs `redshift-serverless:GetIdentityCenterAuthToken`上 （格式：`arn:aws:redshift-serverless:region:account:workgroup/workgroup-name`)
+ **相容驅動程式：**使用支援 Identity Center 授權字符的 Amazon Redshift JDBC 或 ODBC 驅動程式：
  + JDBC 驅動程式：請參閱[安裝和設定 Amazon Redshift JDBC 驅動程式 2.0 版](https://docs.aws.amazon.com/redshift/latest/mgmt/jdbc20-install.html)
  + ODBC 驅動程式：請參閱[安裝和設定 Amazon Redshift ODBC 驅動程式 2.0 版](https://docs.aws.amazon.com/redshift/latest/mgmt/odbc20-install.html)

## Identity Center 身分驗證的運作方式
<a name="identity-center-auth-overview"></a>

Amazon Redshift 的 Identity Center 身分驗證使用以下工作流程：

1. 您的應用程式會使用包含內嵌使用者身分資訊的身分增強憑證來呼叫 `GetIdentityCenterAuthToken` API。

1. Amazon Redshift 會驗證 Identity Center 身分，並產生範圍限定於特定叢集或工作群組的加密授權字符。請參閱範例 IAM 政策。

1. 您的應用程式使用此權杖連線到指定的 Amazon Redshift 叢集或工作群組。

1. Amazon Redshift 資料平面會驗證字符，並根據 Identity Center 應用程式內的 Identity Center 使用者許可授予存取權。

**重要**  
此 API 需要身分增強憑證。如需詳細資訊，請參閱[使用身分增強型 IAM 角色工作階段](https://docs.aws.amazon.com/singlesignon/latest/userguide/trustedidentitypropagation-identity-enhanced-iam-role-sessions.html)。  
如果您在沒有身分增強憑證的情況下呼叫 API，您會收到`UnsupportedOperationFault`錯誤。

## GetIdentityCenterAuthToken API 操作
<a name="identity-center-auth-apis"></a>

Amazon Redshift 提供兩個不同的 `GetIdentityCenterAuthToken` API 操作：一個用於佈建的叢集，另一個用於無伺服器工作群組。這兩個操作的名稱都相同，但接受不同的參數，具體取決於目標資源類型。

### 已佈建叢集的 GetIdentityCenterAuthToken
<a name="provisioned-identity-center-auth"></a>

對於佈建的 Amazon Redshift 叢集，請使用 Amazon Redshift 服務中的 `GetIdentityCenterAuthToken` API 來產生授權字符。

#### 請求語法
<a name="provisioned-request-syntax"></a>

```
{
   "ClusterIds": [ "string" ]
}
```

#### 請求參數
<a name="provisioned-request-parameters"></a>

ClusterIds  
有權存取字符的 Amazon Redshift 叢集識別符清單。字符只能用來驗證此清單中指定的叢集。  
類型：字串陣列  
長度限制：最少 1 個項目。最多 20 個項目。  
必要：是

#### CLI 範例
<a name="provisioned-cli-examples"></a>

**範例：取得單一叢集的授權字符**

```
aws redshift get-identity-center-auth-token \
    --cluster-ids my-redshift-cluster
```

**範例：取得多個叢集的授權字符**

```
aws redshift get-identity-center-auth-token \
    --cluster-ids my-cluster-1 my-cluster-2
```

### 適用於無伺服器工作群組的 GetIdentityCenterAuthToken
<a name="serverless-identity-center-auth"></a>

對於 Amazon Redshift Serverless 工作群組，請使用 Amazon Redshift Serverless 服務中的 `GetIdentityCenterAuthToken` API 來產生授權字符。

#### 請求語法
<a name="serverless-request-syntax"></a>

```
{
   "WorkgroupNames": [ "string" ]
}
```

#### 請求參數
<a name="serverless-request-parameters"></a>

WorkgroupNames  
有權存取字符的 Amazon Redshift Serverless 工作群組名稱清單。字符只能用來驗證此清單中指定的工作群組。  
類型：字串陣列  
長度限制：最少 1 個項目。最多 20 個項目。  
必要：是

#### CLI 範例
<a name="serverless-cli-examples"></a>

**範例：取得單一工作群組的授權字符**

```
aws redshift-serverless get-identity-center-auth-token \
    --workgroup-names my-workgroup
```

**範例：取得多個工作群組的授權字符**

```
aws redshift-serverless get-identity-center-auth-token \
    --workgroup-names workgroup-1 workgroup-2
```

### 回應語法
<a name="identity-center-auth-response"></a>

兩個 APIs都會傳回相同的回應結構：

```
{
   "AuthorizedToken": "string",
   "ExpirationTime": "timestamp"
}
```

#### 回應參數
<a name="identity-center-response-parameters"></a>

AuthorizedToken  
加密的授權字符，其中包含使用者身分資訊和授權叢集或工作群組的清單。此字符應視為敏感資料。  
類型：字串

ExpirationTime  
字符過期的日期和時間，以 UTC 為單位。字符自產生時起 1 小時內有效。  
類型：Timestamp

#### 回應範例
<a name="identity-center-response-example"></a>

```
{
    "AuthorizedToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyQGV4YW1wbGUuY29tIiwiaWF0IjoxNjQwOTk1MjAwLCJleHAiOjE2NDA5OTg4MDAsImNsdXN0ZXJzIjpbIm15LWNsdXN0ZXIiXX0...",
    "ExpirationTime": "2024-01-01T12:00:00Z"
}
```

## 驅動程式整合
<a name="identity-center-auth-driver-integration"></a>

Amazon Redshift 驅動程式支援透過直接字符使用方式進行 Identity Center 身分驗證：

### 直接字符用量
<a name="direct-token-usage"></a>

呼叫 `GetIdentityCenterAuthToken` API 以取得字符之後，請使用 `IdpTokenAuthPlugin`搭配`SUBJECT_TOKEN`字符類型。

連線組態：

```
plugin_name = com.amazon.redshift.plugin.IdpTokenAuthPlugin
token_type = SUBJECT_TOKEN
token = {encrypted_token_from_api_response}
```

如需 Identity Center 身分驗證外掛程式和驅動程式組態的詳細資訊，請參閱[連線至 Amazon Redshift 叢集](https://docs.aws.amazon.com/redshift/latest/mgmt/connecting-to-cluster.html)。

### Java 程式碼範例
<a name="java-code-example"></a>

使用 Identity Center 身分驗證連線的範例 Java 程式碼：

```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

// Get token from GetIdentityCenterAuthToken API
String token = "your_encrypted_token_from_api_response";

// Configure connection properties
Properties props = new Properties();
props.setProperty("user", "your_username");
props.setProperty("plugin_name", "com.amazon.redshift.plugin.IdpTokenAuthPlugin");
props.setProperty("token_type", "SUBJECT_TOKEN");
props.setProperty("token", token);

// Connect to Redshift
String url = "jdbc:redshift://your-cluster.region.redshift.amazonaws.com:5439/your_database";
try (Connection conn = DriverManager.getConnection(url, props)) {
    // Use connection
    System.out.println("Connected successfully!");
} catch (SQLException e) {
    e.printStackTrace();
}
```

## IAM 政策要求
<a name="identity-center-auth-iam-permissions"></a>

若要搭配 Amazon Redshift 使用 Identity Center 身分驗證，除了連線至 Amazon Redshift 叢集和工作群組所需的標準許可之外，還需要特定的 IAM 許可。

### API 許可
<a name="identity-center-auth-iam-api-permissions"></a>

對於佈建的叢集，增強型 IAM 角色工作階段必須具有：
+ `redshift:GetIdentityCenterAuthToken` 叢集 ARNs上的 （格式：`arn:aws:redshift:region:account:cluster:cluster-name`)

對於無伺服器工作群組，增強型 IAM 角色工作階段必須具有：
+ `redshift-serverless:GetIdentityCenterAuthToken` 工作群組 ARNs上的 （格式：`arn:aws:redshift-serverless:region:account:workgroup/workgroup-name`)

### 範例 IAM 政策
<a name="identity-center-auth-iam-policy-examples"></a>

**已佈建叢集的範例政策：**

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "redshift:GetIdentityCenterAuthToken"
            ],
            "Resource": [
                "arn:aws:redshift:us-east-1:123456789012:cluster:my-cluster"
            ]
        }
    ]
}
```

**無伺服器工作群組的範例政策：**

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "redshift-serverless:GetIdentityCenterAuthToken"
            ],
            "Resource": [
                "arn:aws:redshift-serverless:us-east-1:123456789012:workgroup/my-workgroup"
            ]
        }
    ]
}
```

**多個資源的政策範例：**

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "redshift:GetIdentityCenterAuthToken"
            ],
            "Resource": [
                "arn:aws:redshift:*:123456789012:cluster/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "redshift-serverless:GetIdentityCenterAuthToken"
            ],
            "Resource": [
                "arn:aws:redshift-serverless:*:123456789012:workgroup/*"
            ]
        }
    ]
}
```

## 區域可用性
<a name="identity-center-auth-regional-availability"></a>

Identity Center 身分驗證可在下列 AWS 區域使用：
+ 商業區域：所有支援的 Amazon Redshift 區域
+ AWS GovCloud：適用於 us-gov-east-1 和 us-gov-west-1
+ 中國區域：適用於 cn-north-1 和 cn-northwest-1

**注意**  
功能可用性在初始推出期間可能會有所不同。