St_Hakky’s blog

Data Science / Human Resources / Web Applicationについて書きます

【AWS】Amazon Secrets Managerを使ってみた

こんにちは。

Amazon SagemakerのGit統合機能を使おうとした時に、Secrets Managerで認証が必要なPrivate Git リポジトリ用の認証情報を安全に保存できるっぽいので、それに合わせてちょっとやってみました。

Amazon Secrets Managerとは

公式の情報を見るのが一番良いと思います。

簡単に説明すると、「データベースやAPIキーなどのシークレットな情報をセキュアに管理でき、AWSのRDAなどのDBのシークレット情報の更新なども簡単にできる」という特徴があります。

特徴やチュートリアルについては、「新サービス「AWS Secrets Manager」をチュートリアル2種(基本設定、RDSローテーション)で基礎から学ぶ | DevelopersIO」の記事にあります。以下はSecrets Managerの特徴についての文章を引用したものです。

・各種アプリケーションやITリソースのアクセスに必要なシークレット情報を一元管理
 
・ユーザーはAWS Secrets Manager API経由でアクセスするため、各種シークレット情報をプレーンテキストで保持する必要が無い
 
・RDS(MySQL、PostgreSQL)、Amazon Auroraに統合されており、シークレット情報の更新とデータベースパスワードの更新を自動化可能
 
・APIキーや、OAuthトークンなどのシークレットにも拡張可能
 
・IAMポリシーを利用して、シークレット情報へのアクセス制御が可能

データベース接続時のパスワード管理・更新は確かにめちゃめちゃめんどくさいし、ぶっちゃけ私のようなセキュリティとかよくわかってない系データサイエンス系の人間には不安でしかありません。

なので、こういうサービスがあると普通に嬉しいですし、AWSの各種サービスと連携できるのは便利ですね。

解説動画

動画で解説を受けたい人は以下あたりとかお勧めです。

youtu.be

以下の動画とかは、簡単に概要から使い方まで見れるので、上の動画がなげぇなって思った人はぜひこちらを。

youtu.be

使ってみた

正直、使う前に私結構調べる系なのですが、ぶっちゃけ使ってみた方が今回のに関しては早いなと思いました笑。

まず、Secrets Managerの画面に行きます。

f:id:St_Hakky:20181231130247p:plain

ここから、「Store a new secret」ってボタンをクリックすると、以下の画面になります。データベース以外にも、普通にJSONで情報を格納することができるので、一応どんな形式にも対応できるっぽいです。

f:id:St_Hakky:20181231130615p:plain

今回作成するものに名前をつけるなどをします。

f:id:St_Hakky:20181231130622p:plain

パスワードの自動更新などをやるかどうかを書きます。指定した人すうがすぎると、Lambdaを定期的に実行して勝手にやってくれるみたいです。。これはめちゃめちゃ便利ですね。。

f:id:St_Hakky:20181231130625p:plain

以上で最後に確認画面が出て、作成完了なのですが、この時に利用するためにアクセスするサンプルのコードも合わせて表示してくれる優しさがすごいです。

f:id:St_Hakky:20181231130956p:plain

実際のコードはこちら。

# 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 = "<your_secret_name>"
    region_name = "ap-northeast-1"

    # 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 CMK.
        # 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. 

簡単だ。。

注意点

注意点は、こちら「新サービス「AWS Secrets Manager」をチュートリアル2種(基本設定、RDSローテーション)で基礎から学ぶ | DevelopersIO」によると、以下の三つっぽいです。

・ポイント1:DBにパブリック・アクセスさせないために、ローテーション用のLambdaはVPC内に置く
 
・ポイント2:DB接続ユーザーはadminユーザー以外を利用する
 
・ポイント3:シークレット情報取得のAPI課金に注意

特に、ポイント1では、記事でも紹介されていますが、以下の記事にやり方が書いてあります。

dev.classmethod.jp

参考


以上です。金額もそんなに高くないし、普通に便利そうなので、使ってみようと思います。それでは。