-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathDestroyAdminKeys.php
More file actions
75 lines (56 loc) · 2.29 KB
/
DestroyAdminKeys.php
File metadata and controls
75 lines (56 loc) · 2.29 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
<?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 Throwable;
class DestroyAdminKeys extends BaseAction
{
public ?string $key = null;
public function setKey(string $key): static
{
$this->key = $key;
return $this;
}
public function invoke(): mixed
{
$client = $this->command->client(IamClient::class);
try {
$user = $client->getUser();
} catch (Throwable $e) {
return null;
}
$name = $user['User']['UserName'];
$isSidecar = $name === 'sidecar-cli-helper';
if (!$isSidecar) {
$this->command->info('');
$this->command->error(' ********************************************************************************* ');
$this->command->error(' * * ');
$this->command->error(' * The admin keys you provided are not Sidecar specific. Be cautious deleting. * ');
$this->command->error(' * * ');
$this->command->error(' ********************************************************************************* ');
}
$question = '' .
"Now that everything is setup, would you like to remove the admin access keys for user `$name` from AWS? \n" .
' Sidecar no longer needs them.';
// If the keys were created specifically for us, we can default to `true`,
// because that is most safe. If not, defaulting to `false` is most safe!
if (!$this->command->confirm($question, $default = $isSidecar)) {
$this->progress('Not deleting keys');
return null;
}
$this->progress('Deleting admin keys...');
try {
$client->deleteAccessKey([
'AccessKeyId' => $this->key,
'UserName' => $name,
]);
} catch (Throwable $e) {
$this->command->error('Unable to delete keys! You may do it manually.');
}
$this->progress('Admin keys deleted');
return null;
}
}