Class: Putpaws::Provision::PolicyGenerator
- Inherits:
-
Object
- Object
- Putpaws::Provision::PolicyGenerator
- Defined in:
- lib/putpaws/provision/policy_generator.rb
Overview
Generates drafts (たたき台) of IAM policies and roles. These are starting points to review and edit by hand before creating.
Constant Summary collapse
- ECS_TASKS_TRUST =
{ Version: '2012-10-17', Statement: [{Effect: 'Allow', Principal: {Service: 'ecs-tasks.amazonaws.com'}, Action: 'sts:AssumeRole'}] }
- CODEBUILD_TRUST =
{ Version: '2012-10-17', Statement: [{Effect: 'Allow', Principal: {Service: 'codebuild.amazonaws.com'}, Action: 'sts:AssumeRole'}] }
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #account_id ⇒ Object
- #codebuild_role_draft ⇒ Object
-
#initialize(config) ⇒ PolicyGenerator
constructor
A new instance of PolicyGenerator.
- #region ⇒ Object
-
#scheduler_role_draft ⇒ Object
Assumed by EventBridge Scheduler to launch tasks (scheduler:deploy).
- #scheduler_trust ⇒ Object
- #service_name ⇒ Object
- #task_execution_role_draft ⇒ Object
- #task_role_draft ⇒ Object
- #write_step1_drafts! ⇒ Object
Constructor Details
#initialize(config) ⇒ PolicyGenerator
Returns a new instance of PolicyGenerator.
32 33 34 |
# File 'lib/putpaws/provision/policy_generator.rb', line 32 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
31 32 33 |
# File 'lib/putpaws/provision/policy_generator.rb', line 31 def config @config end |
Instance Method Details
#account_id ⇒ Object
38 |
# File 'lib/putpaws/provision/policy_generator.rb', line 38 def account_id; config.account_id; end |
#codebuild_role_draft ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/putpaws/provision/policy_generator.rb', line 124 def codebuild_role_draft { SuggestedRoleName: "#{service_name}-codebuild", SuggestedPolicyName: "#{service_name}-codebuild-policy", AssumeRolePolicyDocument: CODEBUILD_TRUST, PolicyDocument: { Version: '2012-10-17', Statement: [ { Sid: 'WriteBuildLogs', Effect: 'Allow', Action: %w[logs:CreateLogStream logs:PutLogEvents], Resource: "arn:aws:logs:#{region}:#{account_id}:log-group:#{config.build_log_group}:*", }, { Sid: 'EcrAuth', Effect: 'Allow', Action: %w[ecr:GetAuthorizationToken], Resource: '*', }, { Sid: 'EcrPushPull', Effect: 'Allow', Action: %w[ ecr:BatchCheckLayerAvailability ecr:GetDownloadUrlForLayer ecr:BatchGetImage ecr:InitiateLayerUpload ecr:UploadLayerPart ecr:CompleteLayerUpload ecr:PutImage ], Resource: config.base[:ecr_repository_arn], }, # Deploy at the end of the build: # aws ecs update-service --force-new-deployment (+ wait services-stable) { Sid: 'DeployService', Effect: 'Allow', Action: %w[ecs:UpdateService ecs:DescribeServices], Resource: "arn:aws:ecs:#{region}:#{account_id}:service/#{config.cluster_name}/#{service_name}", }, # Required because builds run inside the VPC (vpc_config). { Sid: 'ManageBuildEni', Effect: 'Allow', Action: %w[ ec2:CreateNetworkInterface ec2:DescribeNetworkInterfaces ec2:DeleteNetworkInterface ec2:DescribeSubnets ec2:DescribeSecurityGroups ec2:DescribeDhcpOptions ec2:DescribeVpcs ], Resource: '*', }, { Sid: 'CreateBuildEniPermission', Effect: 'Allow', Action: %w[ec2:CreateNetworkInterfacePermission], Resource: "arn:aws:ec2:#{region}:#{account_id}:network-interface/*", Condition: { StringEquals: { 'ec2:AuthorizedService' => 'codebuild.amazonaws.com', 'ec2:Subnet' => (config.base[:subnets] || []).map{|s| "arn:aws:ec2:#{region}:#{account_id}:subnet/#{s}" }, } }, }, ] }, } end |
#region ⇒ Object
37 |
# File 'lib/putpaws/provision/policy_generator.rb', line 37 def region; config.region; end |
#scheduler_role_draft ⇒ Object
Assumed by EventBridge Scheduler to launch tasks (scheduler:deploy). Created for every service so that schedules can be added any time.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/putpaws/provision/policy_generator.rb', line 192 def scheduler_role_draft { SuggestedRoleName: "#{service_name}-scheduler", SuggestedPolicyName: "#{service_name}-scheduler-policy", AssumeRolePolicyDocument: scheduler_trust, PolicyDocument: { Version: '2012-10-17', Statement: [ { Sid: 'RunScheduledTask', Effect: 'Allow', Action: %w[ecs:RunTask], Resource: [ "arn:aws:ecs:#{region}:#{account_id}:task-definition/#{service_name}-*", "arn:aws:ecs:#{region}:#{account_id}:task-definition/#{service_name}-*:*", ], }, { Sid: 'PassRolesToTasks', Effect: 'Allow', Action: %w[iam:PassRole], Resource: "arn:aws:iam::#{account_id}:role/#{service_name}-*", }, ] }, } end |
#scheduler_trust ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/putpaws/provision/policy_generator.rb', line 18 def scheduler_trust { Version: '2012-10-17', Statement: [{ Effect: 'Allow', Principal: {Service: 'scheduler.amazonaws.com'}, Action: 'sts:AssumeRole', # confused deputy protection: only schedules in this account Condition: {StringEquals: {'aws:SourceAccount' => account_id}}, }] } end |
#service_name ⇒ Object
36 |
# File 'lib/putpaws/provision/policy_generator.rb', line 36 def service_name; config.service_name; end |
#task_execution_role_draft ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/putpaws/provision/policy_generator.rb', line 51 def task_execution_role_draft statements = [ { Sid: 'EcrAuth', Effect: 'Allow', Action: %w[ecr:GetAuthorizationToken], Resource: '*', }, { Sid: 'EcrPull', Effect: 'Allow', Action: %w[ecr:BatchCheckLayerAvailability ecr:GetDownloadUrlForLayer ecr:BatchGetImage], Resource: config.base[:ecr_repository_arn], }, { Sid: 'WriteLogs', Effect: 'Allow', Action: %w[logs:CreateLogStream logs:PutLogEvents], Resource: "arn:aws:logs:#{region}:#{account_id}:log-group:#{config.log_group}:*", }, ] unless config.resolved_secrets.empty? statements << { Sid: 'ReadSecrets', Effect: 'Allow', Action: %w[ssm:GetParameters], Resource: "arn:aws:ssm:#{region}:#{account_id}:parameter#{config.ssm_parameter_prefix}/*", } end { SuggestedRoleName: "#{service_name}-task-execution", SuggestedPolicyName: "#{service_name}-task-execution-policy", AssumeRolePolicyDocument: ECS_TASKS_TRUST, PolicyDocument: {Version: '2012-10-17', Statement: statements}, } end |
#task_role_draft ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/putpaws/provision/policy_generator.rb', line 88 def task_role_draft statements = [ { Sid: 'EcsExec', Effect: 'Allow', Action: %w[ ssmmessages:CreateControlChannel ssmmessages:CreateDataChannel ssmmessages:OpenControlChannel ssmmessages:OpenDataChannel ], Resource: '*', }, ] unless Util.blank?(config.base[:ses_identity_arn]) statements << { Sid: 'SendMail', Effect: 'Allow', Action: %w[ses:SendEmail ses:SendRawEmail], Resource: config.base[:ses_identity_arn], } end unless Util.blank?(config.base[:s3_bucket_arn]) statements << { Sid: 'UseBucket', Effect: 'Allow', Action: %w[s3:GetObject s3:PutObject s3:DeleteObject s3:ListBucket], Resource: [config.base[:s3_bucket_arn], "#{config.base[:s3_bucket_arn]}/*"], } end { SuggestedRoleName: "#{service_name}-task", SuggestedPolicyName: "#{service_name}-task-policy", AssumeRolePolicyDocument: ECS_TASKS_TRUST, PolicyDocument: {Version: '2012-10-17', Statement: statements}, } end |
#write_step1_drafts! ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/putpaws/provision/policy_generator.rb', line 40 def write_step1_drafts! dir = config.policies_dir FileUtils.mkdir_p(dir) Util.write_json(dir.join('role-task-execution.json'), task_execution_role_draft) Util.write_json(dir.join('role-task.json'), task_role_draft) Util.write_json(dir.join('role-codebuild.json'), codebuild_role_draft) Util.write_json(dir.join('role-scheduler.json'), scheduler_role_draft) %w[role-task-execution.json role-task.json role-codebuild.json role-scheduler.json] .map{|f| dir.join(f).to_s} end |