-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from envato/stack-allowed-accounts
Add ability to restrict which accounts can work with stacks
- Loading branch information
Showing
13 changed files
with
331 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
def sts | ||
@sts ||= Aws::STS::Client.new(region: region) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.