經(jīng)過(guò)研究文檔以及和aws溝通铣耘,咱們可以使用以下方案去實(shí)現(xiàn)我們的場(chǎng)景。
? ?1,?我們需要一個(gè)IAM?用戶,此用戶可以有redshift:GetClusterCredentials和redshift:CreateClusterUser策略榔昔。也就是可以執(zhí)行獲取用戶臨時(shí)憑證的權(quán)限驹闰。
{
? ? "Version": "2012-10-17",
? ? "Statement": {
? ? ? ? "Effect": "Allow",
? ? ? ? "Action": [
? ? ? ? ? ? "redshift:GetClusterCredentials",
? ? ? ? ? ? "redshift:CreateClusterUser"
? ? ? ? ],
? ? ? ? "Resource": "*"
? ? }
}
? ?2,拿到上一部用戶的id?和secret,配置到服務(wù)器上撒会。
? ?3嘹朗,用戶在portal登陸后,我們可以拿到用戶的itcode,
? ? ?使用第一步獲得的用戶的身份去執(zhí)行獲取?當(dāng)前用戶的臨時(shí)登錄憑證诵肛。
{'DbUser': 'IAMA:wangfang12', 'DbPassword': 'AaLWa9+T2IXgxh5T09G1nNNrfg2jzjeBCfuNeeJewUoIKZWVhmslVS/n+RKidt61TjAcuFQ==', 'Expiration': datetime.datetime(2021, 10, 12, 10, 35, 54, 613000, tzinfo=tzutc()), 'ResponseMetadata': {'RequestId': '9ac9554b-f9fe-44a8-9db7-ad538c072e15', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '9ac9554b-f9fe-44a8-9db7-ad538c072e15', 'content-type': 'text/xml', 'content-length': '485', 'date': 'Tue, 12 Oct 2021 09:35:54 GMT'}, 'RetryAttempts': 0}}
? 4屹培,使用第三步獲取的臨時(shí)憑證,即可通過(guò)jdbc?怔檩,python?等去連接?query數(shù)據(jù)褪秀。
我在本地已經(jīng)跑通了上述流程。
query?一個(gè)有權(quán)限的表:
([3],)
query沒(méi)權(quán)限的表:
.ProgrammingError: {'S': 'ERROR', 'C': '42501', 'M': 'permission denied for relation mayb11_read', 'F': '/home/ec2-user/padb/src/pg/src/backend/catalog/aclchk.c', 'L': '2700', 'R': 'aclcheck_error'}
大家看這種方案可行不薛训?
import redshift_connector
import boto3
# Connects to Redshift cluster using AWS credentials
conn = redshift_connector.connect(
? ? host='lenovo-test.cjtp8lvs5yxn.us-east-2.redshift.amazonaws.com',
? ? database='dev',
? ? user='IAMA:wangfang12',
? ? password='A6X44Il1axleXiD19ZK8YfaPUQqmdc+VKagamdP5gOwVNPGVhM+bn4RQ8RWGxCkqryQwhGA=='
)
print(conn)
try:
? ? client = boto3.client('redshift',region_name='us-east-2')
? ? creds = client.get_cluster_credentials(
? ? DbUser="wangfang12",
? ? DbName="dev",
? ? ClusterIdentifier="lenovo-test",
? ? DurationSeconds=3600,AutoCreate=True)
? ? print("cred....",creds)
except Exception as ERROR:
? ? print("Credentials Issue: " + ERROR)
? ? sys.exit(1)