【问题标题】:Managing AWS Secrets Manager in AWS Glue在 AWS Glue 中管理 AWS Secrets Manager
【发布时间】:2022-09-23 15:36:51
【问题描述】:

我们如何可靠地管理 AWS Glue 作业中的机密?

import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from pyspark.sql import functions as f

## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, [\'JOB_NAME\'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
logger = glueContext.get_logger()
job = Job(glueContext)
job.init(args[\'JOB_NAME\'], args)
logger = glueContext.get_logger()

谢谢

  • boto3.client(\"secretsmanager\").get_secret_value(SecretId=\"whatever\")!?究竟是什么问题/问题?
  • 是的!而已

标签: python pyspark aws-glue aws-secrets-manager


【解决方案1】:

aws secrets manager 中创建秘密后,它会建议一个示例代码,我们可以使用它来检索秘密

# Use this code snippet in your app.
# If you need more information about configurations or implementing the sample code, visit the AWS docs:   
# https://aws.amazon.com/developers/getting-started/python/

import boto3
import base64
from botocore.exceptions import ClientError


def get_secret():

    secret_name = "myKafkaSecret"
    region_name = "eu-west-3"

    # Create a Secrets Manager client
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )

    # In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
    # See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
    # We rethrow the exception by default.

    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
        if e.response['Error']['Code'] == 'DecryptionFailureException':
            # Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InternalServiceErrorException':
            # An error occurred on the server side.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InvalidParameterException':
            # You provided an invalid value for a parameter.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InvalidRequestException':
            # You provided a parameter value that is not valid for the current state of the resource.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'ResourceNotFoundException':
            # We can't find the resource that you asked for.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
    else:
        # Decrypts secret using the associated KMS key.
        # Depending on whether the secret is a string or binary, one of these fields will be populated.
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
        else:
            decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
            
    # Your code goes here. 

【讨论】:

    猜你喜欢
    • 2018-11-20
    • 1970-01-01
    • 2019-02-15
    • 2022-06-10
    • 2020-08-23
    • 1970-01-01
    • 2020-09-29
    • 2021-01-19
    • 2021-03-17
    相关资源
    最近更新 更多