-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathCreateDeploymentUser.php
More file actions
165 lines (136 loc) · 4.67 KB
/
CreateDeploymentUser.php
File metadata and controls
165 lines (136 loc) · 4.67 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
<?php
declare(strict_types=1);
/**
* @author Aaron Francis <aarondfrancis@gmail.com|https://twitter.com/aarondfrancis>
*/
namespace Hammerstone\Sidecar\Commands\Actions;
use Aws\Iam\IamClient;
use Illuminate\Support\Arr;
use Throwable;
class CreateDeploymentUser extends BaseAction
{
protected IamClient $client;
public function invoke(): array
{
$this->client = $this->command->client(IamClient::class);
$this->createUser();
$this->attachPolicy();
return $this->issueCredentials();
}
protected function createUser()
{
try {
$this->client->getUser([
'UserName' => 'sidecar-deployment-user'
]);
$this->progress('Deployment user already exists');
} catch (Throwable $e) {
$this->progress('Creating deployment user...');
$this->client->createUser([
'UserName' => 'sidecar-deployment-user'
]);
}
}
protected function attachPolicy()
{
$this->progress('Attaching policy to deployment user...');
$this->client->putUserPolicy([
'PolicyName' => 'sidecar-deployment-policy',
'UserName' => 'sidecar-deployment-user',
'PolicyDocument' => json_encode($this->policy()),
]);
}
protected function policy()
{
return [
'Version' => '2012-10-17',
'Statement' => [[
'Effect' => 'Allow',
'Action' => 's3:*',
'Resource' => [
// We only need access to sidecar buckets.
'arn:aws:s3:::sidecar-*',
'arn:aws:s3:::sidecar-*/*',
]
], [
'Effect' => 'Allow',
'Action' => [
'lambda:*',
'states:*',
],
'Resource' => '*',
], [
'Effect' => 'Allow',
'Action' => 'iam:PassRole',
'Resource' => '*',
'Condition' => [
'StringEquals' => [
'iam:PassedToService' => 'lambda.amazonaws.com',
],
],
], [
'Effect' => 'Allow',
'Action' => [
'logs:DescribeLogStreams',
'logs:GetLogEvents',
'logs:FilterLogEvents',
],
'Resource' => 'arn:aws:logs:*:*:log-group:/aws/lambda/*',
], [
'Effect' => 'Allow',
'Action' => [
'ecr:GetRepositoryPolicy',
'ecr:SetRepositoryPolicy',
],
'Resource' => '*',
]],
];
}
protected function issueCredentials()
{
$keysPresentInConfiguration = config('sidecar.aws_key') && config('sidecar.aws_secret');
if ($keysPresentInConfiguration) {
$this->progress('Sidecar keys already exist in your configuration, not issuing new ones.');
return [
'key' => config('sidecar.aws_key'),
'secret' => config('sidecar.aws_secret'),
];
}
$keys = $this->client->listAccessKeys([
'UserName' => 'sidecar-deployment-user',
]);
if (!count($keys)) {
return $this->createAccessKey();
}
$question = '' .
"Sidecar AWS key and secret are not present in the .env file. \n" .
" Generating a new set of credentials, which will invalidate any outstanding credentials. \n" .
' Continue?';
if (!$this->command->confirm($question, $default = true)) {
$this->command->error('Not creating new keys, make sure you populate your keys.');
return [
'key' => 'UNABLE_TO_DETERMINE',
'secret' => 'UNABLE_TO_DETERMINE',
];
}
if ($accessKeyId = Arr::get($keys, 'AccessKeyMetadata.0.AccessKeyId')) {
$this->progress('Deleting old keys...');
$this->client->deleteAccessKey([
'AccessKeyId' => $accessKeyId,
'UserName' => 'sidecar-deployment-user',
]);
}
return $this->createAccessKey();
}
protected function createAccessKey()
{
$this->progress('Creating new keys...');
$result = $this->client->createAccessKey([
'UserName' => 'sidecar-deployment-user',
]);
return [
'key' => Arr::get($result, 'AccessKey.AccessKeyId'),
'secret' => Arr::get($result, 'AccessKey.SecretAccessKey'),
];
}
}