Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

AWS Secret Manager에서 Boto3를 사용하여 임의의 암호를 생성하는 방법

<시간/>

문제 설명: boto3 사용 AWS Secret Manager에서 임의의 암호를 생성하는 Python 라이브러리

이 문제를 해결하기 위한 접근 방식/알고리즘

  • 1단계: boto3 가져오기 및 보토코어 예외를 처리하는 예외.

  • 2단계: 여기에는 매개변수가 없습니다.

  • 3단계: boto3 lib를 사용하여 AWS 세션 생성 . region_name을(를) 확인하십시오. 기본 프로필에 언급되어 있습니다. 언급되지 않은 경우 region_name을 명시적으로 전달합니다. 세션을 만드는 동안.

  • 4단계: secretmanager용 AWS 클라이언트 생성 .

  • 5단계: get_random_password 호출 원하는 복잡성에 따라 매개변수를 전달합니다.

  • 6단계: 임의의 비밀번호를 반환합니다.

  • 7단계: 임의의 암호를 생성하는 동안 문제가 발생한 경우 일반 예외를 처리합니다.

예시 코드

다음 코드를 사용하여 임의의 비밀번호 생성 -

import boto3
from botocore.exceptions import ClientError

def generate_random_password():
   session = boto3.session.Session()
   s3_client = session.client('secretmanager')
   try:
   response = s3_client.get_random_password(PasswordLength=18,
      ExcludeCharacters="",
       ExcludeNumbers=False,
      ExcludePunctuation=True,
      ExcludeUppercase=False,
      ExcludeLowercase = False,
      IncludeSpace=False,
      RequireEachIncludedType=True
   )
   return response
   except ClientError as e:
      raise Exception("boto3 client error in generate_random_password: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in generate_random_password: " + e.__str__())

a = generate_random_password()
print(a["RandomPassword"])

출력

mcwJ6tLfN0uidY9zcY