【AWS】機密データの管理 ~ パラメータストア 編 ~

■ はじめに

パスワードなどの認証情報をAWSでセキュアに管理する方法として
以下のサービスがあるらしいので、調べてみた

今回は、AWS SSM『【1】AWS Systems Manager パラメータストア』を
取り扱う。

AWSのデータ管理サービス

【1】AWS Systems Manager パラメータストア << ★今回のテーマ★
【2】AWS Secrets Manager

機密データの管理 ~ Secrets Manager 編 ~
https://dk521123.hatenablog.com/entry/2020/03/12/220717

公式サイト
https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/userguide/specifying-sensitive-data.html

■ 利点

* コードからデータを分離してセキュリティ体制を改善

AWS Systems Manager パラメータストア

https://dk521123.hatenablog.com/entry/2019/10/05/121119

で扱った AWS Systems Manager の機能のひとつで、
パスワードなどの設定データを階層型Key-Value形式で保管できるサービス

■ データ型

1)String:文字列
2)StringList:文字列リスト
3)SecureString:暗号化された文字列 (KMS で暗号化して格納)

※ KMSについては、以下の関連記事を参照のこと。

https://dk521123.hatenablog.com/entry/2020/02/27/232553

■ IAMロール

AmazonSSMFullAccess, AmazonSSMReadOnlyAccess などの
Systems Managerに関するロールが必要。

■ 設定値の取得

[A]AWS CLI
[B]boto3 API
etc...

[A]AWS CLI
https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/sysman-paramstore-walk-hierarchies.html
https://docs.aws.amazon.com/cli/latest/reference/ssm/get-parameter.html

#  get-parameter
aws ssm get-parameter --name /prd/DB_PASSWORD --with-decryption

# 環境変数に設定
export DB_PASSWORD ="$(aws ssm get-parameter --name /prd/DB_PASSWORD --with-decryption --output text --query Parameter.Value)"

[B]boto3 API
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ssm.html#SSM.Client.get_parameter

import boto3

def get_parameter(ssm_client, param_name):
  response = ssm_client.get_parameter(
    Name=param_name,
    WithDecryption=True)
    return response['Parameter']['Value']

def main():
  ssm_client = boto3.client('ssm', region_name='us-west-2')
  param_name = "/prd/DB_PASSWORD"
  result_value = get_parameters(ssm_client, param_name)
  print(result_value)

if __name__ == '__main__':
  main()

参考文献

https://qiita.com/tomoya_oka/items/a3dd44879eea0d1e3ef5
パラメータストア
https://www.noobs.tokyo/entry/2018/10/04/081412
https://www.m3tech.blog/entry/aws-ssm-env-gem
https://kakakakakku.hatenablog.com/entry/2017/06/10/181603
https://note.com/ryota_tk/n/n488767f6e312

関連記事

機密データの管理 ~ Secrets Manager 編 ~
https://dk521123.hatenablog.com/entry/2020/03/12/220717
CodeBuild で パラメータストア / Secrets Manager を使う
https://dk521123.hatenablog.com/entry/2020/02/18/230358
AWS CloudFormation ~ 認証情報の扱い ~
https://dk521123.hatenablog.com/entry/2021/12/28/224501
AWS Systems Manager (SSM)
https://dk521123.hatenablog.com/entry/2019/10/05/121119
CodeBuild ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/01/21/221122
Terraform ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2019/12/09/222057
AWS Key Management Service (KMS)
https://dk521123.hatenablog.com/entry/2020/02/27/232553