Skip to content

Commit

Permalink
generate a random data bag secret when generating bootstrap config
Browse files Browse the repository at this point in the history
  • Loading branch information
pghalliday committed Nov 18, 2015
1 parent 2706f6b commit 81e020c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/formatron/generators/bootstrap/formatronfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def initialize(params)
def guid
Util.guid
end

def databag_secret
Util.databag_secret
end
end

def self.write(directory, params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ formatron.bucket '<%= params[:s3_bucket] %>'
formatron.global do |global|
global.protect config['protected']
global.kms_key '<%= params[:kms_key] %>'
global.databag_secret '<%= databag_secret %>'
global.hosted_zone_id '<%= params[:hosted_zone_id] %>'

global.ec2 do |ec2|
Expand Down
4 changes: 4 additions & 0 deletions lib/formatron/generators/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module Util
def self.guid
Random.rand(36**8).to_s(36).upcase
end

def self.databag_secret
Random.rand(36**40).to_s(36).upcase
end
end
end
end
3 changes: 3 additions & 0 deletions spec/formatron/generators/bootstrap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
}
}
}
databag_secret = 'databag_secret'
vpc_guid = 'vpc_guid'
management_subnet_guid = 'management_subnet_guid'
bastion_guid = 'bastion_guid'
Expand Down Expand Up @@ -67,6 +68,7 @@
allow(util_class).to receive(:guid) do
guids.shift
end
allow(util_class).to receive(:databag_secret) { databag_secret }
Formatron::Generators::Bootstrap.generate directory, params
end

Expand Down Expand Up @@ -106,6 +108,7 @@
formatron.global do |global|
global.protect config['protected']
global.kms_key '#{params[:kms_key]}'
global.databag_secret '#{databag_secret}'
global.hosted_zone_id '#{params[:hosted_zone_id]}'
global.ec2 do |ec2|
Expand Down
20 changes: 20 additions & 0 deletions spec/formatron/generators/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ module Generators
expect(Util.guid).to eql guid
end
end

describe '#databag_secret' do
databag_secret = 'RJDY7W3TW99BMMBIVKGTRBUGW51MCYZC5121JA87'
# rubocop:disable Metrics/LineLength
random = 136_690_544_786_843_736_891_389_940_419_100_424_921_600_504_110_568_485_585_351_559
# rubocop:enable Metrics/LineLength

before :each do
random_class = class_double(
'Random'
).as_stubbed_const
allow(random_class).to receive(:rand).with(
36**40
) { random }
end

it 'should generate a random 40 character string' do
expect(Util.databag_secret).to eql databag_secret
end
end
end
end
end

0 comments on commit 81e020c

Please sign in to comment.