-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathprofile.pp
More file actions
170 lines (162 loc) · 5.42 KB
/
profile.pp
File metadata and controls
170 lines (162 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
87
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
123
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
# == Define: awscli::profile
#
# Configures an aws-cli profile
#
# === Variables
#
# [$ensure]
# Control whether the profile should be present or not
# Default: present
#
# [$user]
# The user for whom the profile will be installed
#
# [$group]
# The group for whom the profile will be installed
#
# [$homedir]
# The home directory where the config and credentials will be placed
#
# [$aws_access_key_id]
# The aws_access_key_id for this profile. If not specified, aws-cli can
# can use IAM roles to authenticate.
#
# [$aws_secret_access_key]
# The aws_secret_access_key for this profile. If not specified, aws-cli can
# can use IAM roles to authenticate.
#
# [$role_arn]
# The ARN for the role to use in this profile. The source_profile must
# be supplied when role_arn is specified
#
# [$source_profile]
# The profile to use for credentials to assume the specified role
#
# [credential_source]
# Used within EC2 instances or EC2 containers to specify where the AWS CLI can find credentials
# to use to assume the role you specified with the role_arn parameter.
# You cannot specify both source_profile and credential_source in the same profile.
# More info at https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#using-aws-iam-roles
#
# [$role_session_name]
# An identifier for the assumed role session
#
# [$aws_region]
# The aws_region for this profile
# Default: us-east-1
#
# [$profile_name]
# The name of the AWS profile
# Default: default
#
# [$output]
# The output format used for this profile
# Default: json
#
# === Example
#
# awscli::profile { 'tsmith-awscli':
# user => 'tsmith',
# aws_access_key_id => 'access_key',
# aws_secret_access_key => 'secret_key',
# aws_region => 'us-west-2',
# role_arn => 'arn:aws:iam::123456789012:role/MyRole',
# source_profile => 'user',
# role_session_name => 'mysession',
# profile_name => 'default',
# output => 'text',
# }
#
define awscli::profile(
$ensure = 'present',
$user = 'root',
$group = undef,
$homedir = undef,
$aws_access_key_id = undef,
$aws_secret_access_key = undef,
$role_arn = undef,
$source_profile = undef,
Optional[Enum['Environment', 'Ec2InstanceMetadata', 'EcsContainer']] $credential_source = undef,
$role_session_name = undef,
$aws_region = 'us-east-1',
$profile_name = 'default',
$output = 'json',
) {
if $aws_access_key_id == undef and $aws_secret_access_key == undef {
info ('AWS keys for awscli::profile. Your will need IAM roles configured.')
$skip_credentials = true
} else {
$skip_credentials = false
}
if $homedir {
$homedir_real = $homedir
} else {
if $user != 'root' {
$homedir_real = $::os['family']? {
'Darwin' => "/Users/${user}",
default => "/home/${user}"
}
} else {
$homedir_real = '/root'
}
}
if ($group == undef) {
if $user != 'root' {
$group_real = $::os['family']? {
'Darwin' => 'staff',
default => $user
}
} else {
$group_real = 'root'
}
} else {
$group_real = $group
}
if ($source_profile != undef and $credential_source != undef) {
fail('aws cli profile cannot contain both source_profile and credential_source config option')
}
# ensure $homedir/.aws is available
if !defined(File["${homedir_real}/.aws"]) {
file { "${homedir_real}/.aws":
ensure => 'directory',
owner => $user,
group => $group_real,
mode => '0700',
}
}
# setup credentials
if ! $skip_credentials {
if !defined(Concat["${homedir_real}/.aws/credentials"]) {
concat { "${homedir_real}/.aws/credentials":
ensure => 'present',
owner => $user,
group => $group_real,
mode => '0600',
show_diff => false,
require => File["${homedir_real}/.aws"],
}
}
if ( $ensure == 'present' ) {
concat::fragment { "${title}-credentials":
target => "${homedir_real}/.aws/credentials",
content => template('awscli/credentials_concat.erb'),
}
}
}
# setup config
if !defined(Concat["${homedir_real}/.aws/config"]) {
concat { "${homedir_real}/.aws/config":
ensure => 'present',
owner => $user,
group => $group_real,
mode => '0600',
require => File["${homedir_real}/.aws"],
}
}
if ( $ensure == 'present' ) {
concat::fragment { "${title}-config":
target => "${homedir_real}/.aws/config",
content => template('awscli/config_concat.erb'),
}
}
}