Skip to content

Commit c71c735

Browse files
committed
Merge pull request #7 from asgoodasnu/add-profiles
adds a define for adding awscli profiles
2 parents e036f28 + 78d0689 commit c71c735

File tree

8 files changed

+156
-1
lines changed

8 files changed

+156
-1
lines changed

.fixtures.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
fixtures:
33
repositories:
44
epel: https://github.com/stahnma/puppet-module-epel
5+
concat: https://github.com/puppetlabs/puppetlabs-concat
6+
stdlib: http://github.com/puppetlabs/puppetlabs-stdlib
57
symlinks:
68
awscli: "#{source_dir}"

Puppetfile.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
FORGE
22
remote: https://forgeapi.puppetlabs.com
33
specs:
4+
puppetlabs-concat (1.2.0)
5+
puppetlabs-stdlib (< 5.0.0, >= 3.2.0)
6+
puppetlabs-stdlib (4.5.1)
47
stahnma-epel (1.0.2)
58

69
DEPENDENCIES
10+
puppetlabs-concat (< 2.0.0, >= 1.0.0)
11+
puppetlabs-stdlib (< 5.0.0, >= 4.0.0)
712
stahnma-epel (< 2.0.0, >= 1.0.0)
813

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ OSX has been tested on Yosemite only and requires:
1919

2020
`class { 'awscli': }`
2121

22+
### Profiles
23+
24+
If you want to add a credentials for awscli you can do it by using awscli::profile:
25+
26+
If you just define access_key_id and secret key, these credentials will work only for the root user:
27+
28+
```
29+
awscli::profile {
30+
'default':
31+
aws_access_key_id => 'MYAWSACCESSKEYID',
32+
aws_secret_access_key => 'MYAWSSECRETACESSKEY'
33+
}
34+
```
35+
36+
You can also define a profile for a custom user:
37+
38+
```
39+
awscli::profile {
40+
'default':
41+
user => 'ubuntu',
42+
aws_access_key_id => 'MYAWSACCESSKEYID',
43+
aws_secret_access_key => 'MYAWSSECRETACESSKEY'
44+
}
45+
```
46+
2247
## Testing
2348
You can test this module with rspec:
2449

manifests/profile.pp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# == Define: awscli::profile
2+
#
3+
# Puts a profile into the awscred file
4+
#
5+
# === Options
6+
#
7+
# [*user*]
8+
# The user for whom the profile will be installed
9+
# [*aws_access_key_id*]
10+
# The aws_access_key_id for this profile
11+
#
12+
# [*aws_secret_access_key*]
13+
# The aws_secret_access_key for this profile
14+
#
15+
define awscli::profile(
16+
$user = 'root',
17+
$aws_access_key_id = undef,
18+
$aws_secret_access_key = undef,
19+
) {
20+
if $aws_access_key_id == undef {
21+
fail ('no aws_access_key_id provided')
22+
}
23+
24+
if $aws_secret_access_key == undef {
25+
fail ('no aws_secret_access_key provided')
26+
}
27+
28+
if $user != 'root' {
29+
$homedir = "/home/${user}"
30+
} else {
31+
$homedir = '/root'
32+
}
33+
34+
if !defined(File["${homedir}/.aws"]) {
35+
file { "${homedir}/.aws":
36+
ensure => 'directory',
37+
owner => $user,
38+
group => $user
39+
}
40+
}
41+
42+
if !defined(Concat["${homedir}/.aws/credentials"]) {
43+
concat { "${homedir}/.aws/credentials":
44+
ensure => 'present'
45+
}
46+
}
47+
48+
49+
concat::fragment{ $title:
50+
target => "${homedir}/.aws/credentials",
51+
content => template('awscli/credentials_concat.erb')
52+
}
53+
}
54+
55+

metadata.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"source": "https://github.com/justindowning/puppet-awscli.git",
1111
"issues_url": "https://github.com/justindowning/puppet-awscli/issues",
1212
"dependencies": [
13-
{ "name": "stahnma/epel", "version_requirement": ">= 1.0.0 <2.0.0" }
13+
{ "name": "stahnma/epel", "version_requirement": ">= 1.0.0 <2.0.0" },
14+
{ "name": "puppetlabs/stdlib", "version_requirement": ">= 4.0.0 <5.0.0" },
15+
{ "name": "puppetlabs/concat", "version_requirement": ">= 1.0.0 <2.0.0" }
1416
],
1517
"operatingsystem_support": [
1618
{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'spec_helper'
2+
3+
describe 'awscli::profile', :type => :define do
4+
context 'supported OS ' do
5+
['darwin', 'debian', 'redhat'].each do |osfamily|
6+
describe "#{osfamily} installation" do
7+
let(:facts) { {
8+
:osfamily => osfamily,
9+
:concat_basedir => '/var/lib/puppet/concat/'
10+
} }
11+
12+
let(:title) { 'test_profile' }
13+
14+
let(:params) { { } }
15+
16+
it 'should report an error if no aws_access_key_id is given' do
17+
is_expected.to raise_error(Puppet::Error, /no aws_access_key_id provided/)
18+
end
19+
20+
it 'should report an error if no aws_secret_access_key is given' do
21+
params.merge!({ 'aws_access_key_id' => 'TESTAWSACCESSKEYID' })
22+
is_expected.to raise_error(Puppet::Error, /no aws_secret_access_key provided/)
23+
end
24+
25+
it 'should create profile for root if no user is given' do
26+
params.merge!({
27+
'aws_access_key_id' => 'TESTAWSACCESSKEYID',
28+
'aws_secret_access_key' => 'TESTSECRETACCESSKEY'
29+
})
30+
is_expected.to contain_file('/root/.aws').with_ensure('directory')
31+
is_expected.to contain_concat('/root/.aws/credentials')
32+
is_expected.to contain_concat__fragment( 'test_profile' ).with
33+
({
34+
:target => '/root/.aws/credentials'
35+
})
36+
end
37+
38+
it 'should create profile for user test' do
39+
params.merge!({
40+
'user' => 'test',
41+
'aws_access_key_id' => 'TESTAWSACCESSKEYID',
42+
'aws_secret_access_key' => 'TESTSECRETACCESSKEY'
43+
})
44+
is_expected.to contain_file('/home/test/.aws').with_ensure('directory')
45+
is_expected.to contain_concat('/home/test/.aws/credentials')
46+
is_expected.to contain_concat__fragment( 'test_profile' ).with
47+
({
48+
:target => '/home/test/.aws/credentials'
49+
})
50+
end
51+
52+
53+
end
54+
end
55+
end
56+
end

templates/credentials_concat.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[<%=@title%>]
2+
aws_access_key_id=<%=@aws_access_key_id%>
3+
aws_secret_access_key=<%=@aws_secret_access_key%>
4+
5+

tests/vagrant.pp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
class { 'awscli': version => 'latest' }
2+
awscli::profile { 'default':
3+
user => 'vagrant',
4+
aws_access_key_id => 'MYTESTACCESSKEYID',
5+
aws_secret_access_key => 'MYTESTSECRETACCESSKEY'
6+
}

0 commit comments

Comments
 (0)