-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to restrict which accounts can work with stacks #283
Changes from all commits
aef2c38
d3fa4ee
1a92477
c72a000
f79ef55
abbbd3e
4eff0f9
056aeea
83bd9a6
b30dede
3ee8a08
6351741
4233345
f02603c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
Feature: Apply command with allowed accounts | ||
|
||
Background: | ||
Given a file named "stack_master.yml" with: | ||
""" | ||
stack_defaults: | ||
allowed_accounts: | ||
- '11111111' | ||
stacks: | ||
us_east_1: | ||
myapp_vpc: | ||
template: myapp.rb | ||
myapp_db: | ||
template: myapp.rb | ||
allowed_accounts: '22222222' | ||
myapp_web: | ||
template: myapp.rb | ||
allowed_accounts: [] | ||
""" | ||
And a directory named "templates" | ||
And a file named "templates/myapp.rb" with: | ||
""" | ||
SparkleFormation.new(:myapp) do | ||
description "Test template" | ||
set!('AWSTemplateFormatVersion', '2010-09-09') | ||
end | ||
""" | ||
|
||
Scenario: Run apply with stack inheriting allowed accounts from stack defaults | ||
Given I stub the following stack events: | ||
| stack_id | event_id | stack_name | logical_resource_id | resource_status | resource_type | timestamp | | ||
| 1 | 1 | myapp-vpc | myapp-vpc | CREATE_COMPLETE | AWS::CloudFormation::Stack | 2020-10-29 00:00:00 | | ||
When I use the account "11111111" | ||
And I run `stack_master apply us-east-1 myapp-vpc` | ||
And the output should match /2020-10-29 00:00:00 (\+|\-)[0-9]{4} myapp-vpc AWS::CloudFormation::Stack CREATE_COMPLETE/ | ||
Then the exit status should be 0 | ||
|
||
Scenario: Run apply with stack overriding allowed accounts with its own list | ||
Given I stub the following stack events: | ||
| stack_id | event_id | stack_name | logical_resource_id | resource_status | resource_type | timestamp | | ||
| 1 | 1 | myapp-db | myapp-db | CREATE_COMPLETE | AWS::CloudFormation::Stack | 2020-10-29 00:00:00 | | ||
When I use the account "11111111" | ||
And I run `stack_master apply us-east-1 myapp-db` | ||
And the output should contain all of these lines: | ||
| Account '11111111' is not an allowed account. Allowed accounts are ["22222222"].| | ||
Then the exit status should be 0 | ||
|
||
Scenario: Run apply with stack overriding allowed accounts to allow all accounts | ||
Given I stub the following stack events: | ||
| stack_id | event_id | stack_name | logical_resource_id | resource_status | resource_type | timestamp | | ||
| 1 | 1 | myapp-web | myapp-web | CREATE_COMPLETE | AWS::CloudFormation::Stack | 2020-10-29 00:00:00 | | ||
When I use the account "33333333" | ||
And I run `stack_master apply us-east-1 myapp-web` | ||
And the output should match /2020-10-29 00:00:00 (\+|\-)[0-9]{4} myapp-web AWS::CloudFormation::Stack CREATE_COMPLETE/ | ||
Then the exit status should be 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Given(/^I use the account "([^"]*)"$/) do |account_id| | ||
Aws.config[:sts] = { | ||
stub_responses: { | ||
get_caller_identity: { | ||
account: account_id, | ||
arn: 'an-arn', | ||
user_id: 'a-user-id' | ||
} | ||
} | ||
} | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module StackMaster | ||
class Identity | ||
def running_in_allowed_account?(allowed_accounts) | ||
allowed_accounts.nil? || allowed_accounts.empty? || allowed_accounts.include?(account) | ||
end | ||
|
||
def account | ||
@account ||= sts.get_caller_identity.account | ||
end | ||
|
||
private | ||
|
||
attr_reader :sts | ||
|
||
def region | ||
@region ||= ENV['AWS_REGION'] || Aws.config[:region] || Aws.shared_config.region || 'us-east-1' | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method isn't necessary. If we don't specify a region the SDK does a lookup for us automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right it does, however I added this method to ensure that a default region is used if it couldn't find a region from the environment. It's also the same as the CloudFormation driver class except for the default value. This was me being more defensive, but happy to remove it if you think it'll be fine without it? |
||
|
||
def sts | ||
@sts ||= Aws::STS::Client.new(region: region) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems odd to pass a
nil
orempty
variable to a function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe if
nil
was the default value for allowed_accounts (in StackDefinition, indicating unspecified) the expected values would either benil
or an array of account IDsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Done in b30dede.