0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

S3上のユーザ名フォルダに、自動で権限付与するCFnテンプレート

0
Posted at

はじめに

S3の中に、ユーザ個人がプライベートに利用できる領域を作成してみました。

概要

以下を行います。

  • ユーザ名と同じフォルダのみ操作可能なS3バケットを作成
    • 共通フォルダも作成し、そこは全ユーザが操作可能

作成するリソースは以下です。

  • IAMポリシー
    • アクセス権はここで定義します
  • ユーザーグループ
  • S3バケット
    • "share-"から始める制約をかけています

リソース作成

まずIAMポリシーを作成するCFnテンプレートです。

createPrivateFolderPolicy.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Standard IAM Policy for S3 Folder Isolation using share-* naming convention'

Resources:
  # 1. 共通管理ポリシー(固定名)
  StandardS3FolderAccessPolicy:
    Type: 'AWS::IAM::ManagedPolicy'
    Properties:
      # 用途が直感的にわかる固定名
      ManagedPolicyName: 'S3-Folder-Access-Standard-Policy'
      Description: 'Allows private and common folder access only for buckets starting with share-'
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          # Sid 1: バケット一覧の表示
          - Sid: AllowListAllBuckets
            Effect: Allow
            Action:
              - 's3:GetBucketLocation'
              - 's3:ListAllMyBuckets'
            Resource: '*'

          # Sid 2: 階層構造のブラウズ許可
          # 対象を 'share-*' で始まるバケットに限定
          - Sid: AllowListSpecificBucketStructure
            Effect: Allow
            Action: 's3:ListBucket'
            Resource: 'arn:aws:s3:::share-*'
            Condition:
              StringLike:
                's3:prefix':
                  - ''              # バケットルート
                  - 'common/'       # 共通エリア入口
                  - 'common/*'      # 共通エリア中身
                  - 'private/'      # 個人エリア入口
                  - !Sub 'private/${!aws:username}/' # 自分の名前のフォルダ入口
                  - !Sub 'private/${!aws:username}/*' # 自分の名前のフォルダ中身

          # Sid 3: 実際のデータ操作
          # 対象を 'share-*' で始まるバケットの、許可されたパスのみに限定
          - Sid: AllowReadWriteInSpecificPaths
            Effect: Allow
            Action: 's3:*'
            Resource:
              - 'arn:aws:s3:::share-*/common/*'
              - !Sub 'arn:aws:s3:::share-*/private/${!aws:username}/*'

Outputs:
  PolicyArn:
    Description: 'The ARN of the shared S3 access policy'
    Value: !Ref StandardS3FolderAccessPolicy
    Export:
      Name: 'StandardS3FolderPolicy-Arn'

次はS3バケットとユーザーグループを作成します。上記のIAMポリシーを、作成するユーザーグループに割り当てます。

createS3_UserGroupForPrivateFolder.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: 'S3 Bucket and IAM Group with Regex Naming Rule'

Parameters:
  BucketName:
    Type: String
    Description: 'Enter the bucket name (Must start with "share-")'
    Default: 'share-'
    # --- 正規表現で "share-" から始まることを強制 ---
    AllowedPattern: '^share-.*'
    ConstraintDescription: 'Bucket name must start with "share-".'

  GroupName:
    Type: String
    Description: 'Enter the IAM User Group name'

Resources:
  # 1. S3バケット
  ShareBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref BucketName

  # 2. IAMユーザーグループ
  ShareUserGroup:
    Type: 'AWS::IAM::Group'
    Properties:
      GroupName: !Ref GroupName
      ManagedPolicyArns:
        - !ImportValue 'StandardS3FolderPolicy-Arn'

Outputs:
  CreatedBucketName:
    Value: !Ref ShareBucket

使用

作成したリソースを使用するために、以下のユーザを作ってユーザーグループに紐づけておきました。

image.png

作成したS3バケットには、以下のようにユーザ名のフォルダを持つ構造にしておきます。

image.png

この状態でアクセスしてみます。ツールにはWinSCPを用いました。

自分の名前のフォルダに、ファイルを置けることが確認できます。
image.png

他の人のフォルダは、エラーになりました。
image.png

commonフォルダも、操作できます。
image.png

おわりに

今回は、S3上の個人領域に権限付与するCFnテンプレートを作ってみました。
新年度にあたり急遽、個人領域のフォルダを作成するような要望があった際に参考にしてください。
この記事がどなたかのお役に立てれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?