|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace STS\Keep\Services; |
| 4 | + |
| 5 | +use Illuminate\Support\Collection; |
| 6 | +use STS\Keep\Data\VaultConfig; |
| 7 | + |
| 8 | +class IamPolicyGenerator |
| 9 | +{ |
| 10 | + public function generate(Collection $vaults, string $namespace, array $envs = []): array |
| 11 | + { |
| 12 | + $statements = []; |
| 13 | + |
| 14 | + $ssmVaults = $vaults->filter(fn (VaultConfig $v) => $v->driver() === 'ssm'); |
| 15 | + $smVaults = $vaults->filter(fn (VaultConfig $v) => $v->driver() === 'secretsmanager'); |
| 16 | + |
| 17 | + if ($ssmVaults->isNotEmpty()) { |
| 18 | + $statements = array_merge($statements, $this->ssmStatements($ssmVaults, $namespace, $envs)); |
| 19 | + } |
| 20 | + |
| 21 | + if ($smVaults->isNotEmpty()) { |
| 22 | + $statements = array_merge($statements, $this->secretsManagerStatements($smVaults, $namespace, $envs)); |
| 23 | + } |
| 24 | + |
| 25 | + return [ |
| 26 | + 'Version' => '2012-10-17', |
| 27 | + 'Statement' => $statements, |
| 28 | + ]; |
| 29 | + } |
| 30 | + |
| 31 | + protected function ssmStatements(Collection $vaults, string $namespace, array $envs): array |
| 32 | + { |
| 33 | + $resources = []; |
| 34 | + $kmsResources = []; |
| 35 | + |
| 36 | + foreach ($vaults as $vault) { |
| 37 | + $region = $vault->get('region', '*'); |
| 38 | + $scope = trim($vault->scope(), '/'); |
| 39 | + $base = $scope ? "{$namespace}/{$scope}" : $namespace; |
| 40 | + |
| 41 | + if (empty($envs)) { |
| 42 | + $resources[] = "arn:aws:ssm:{$region}:*:parameter/{$base}/*"; |
| 43 | + } else { |
| 44 | + foreach ($envs as $env) { |
| 45 | + $resources[] = "arn:aws:ssm:{$region}:*:parameter/{$base}/{$env}/*"; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + $customKey = $vault->get('key'); |
| 50 | + $kmsResources[] = $customKey ?: "arn:aws:kms:{$region}:*:alias/aws/ssm"; |
| 51 | + } |
| 52 | + |
| 53 | + $resources = array_values(array_unique($resources)); |
| 54 | + $kmsResources = array_values(array_unique($kmsResources)); |
| 55 | + |
| 56 | + return [ |
| 57 | + [ |
| 58 | + 'Sid' => 'KeepSsmAccess', |
| 59 | + 'Effect' => 'Allow', |
| 60 | + 'Action' => [ |
| 61 | + 'ssm:GetParameter', |
| 62 | + 'ssm:GetParameters', |
| 63 | + 'ssm:GetParametersByPath', |
| 64 | + 'ssm:GetParameterHistory', |
| 65 | + 'ssm:PutParameter', |
| 66 | + 'ssm:DeleteParameter', |
| 67 | + ], |
| 68 | + 'Resource' => count($resources) === 1 ? $resources[0] : $resources, |
| 69 | + ], |
| 70 | + [ |
| 71 | + 'Sid' => 'KeepSsmKms', |
| 72 | + 'Effect' => 'Allow', |
| 73 | + 'Action' => [ |
| 74 | + 'kms:Decrypt', |
| 75 | + 'kms:Encrypt', |
| 76 | + 'kms:GenerateDataKey', |
| 77 | + ], |
| 78 | + 'Resource' => count($kmsResources) === 1 ? $kmsResources[0] : $kmsResources, |
| 79 | + ], |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + protected function secretsManagerStatements(Collection $vaults, string $namespace, array $envs): array |
| 84 | + { |
| 85 | + $kmsResources = []; |
| 86 | + |
| 87 | + foreach ($vaults as $vault) { |
| 88 | + $region = $vault->get('region', '*'); |
| 89 | + $customKey = $vault->get('key'); |
| 90 | + $kmsResources[] = $customKey ?: "arn:aws:kms:{$region}:*:alias/aws/secretsmanager"; |
| 91 | + } |
| 92 | + |
| 93 | + $kmsResources = array_values(array_unique($kmsResources)); |
| 94 | + |
| 95 | + $tagKeys = ['ManagedBy', 'Namespace', 'Env', 'VaultSlug']; |
| 96 | + |
| 97 | + $hasScope = $vaults->contains(fn (VaultConfig $v) => trim($v->scope(), '/') !== ''); |
| 98 | + if ($hasScope) { |
| 99 | + $tagKeys[] = 'Scope'; |
| 100 | + } |
| 101 | + |
| 102 | + $readWriteCondition = [ |
| 103 | + 'StringEquals' => [ |
| 104 | + 'secretsmanager:ResourceTag/Namespace' => $namespace, |
| 105 | + ], |
| 106 | + ]; |
| 107 | + |
| 108 | + $createCondition = [ |
| 109 | + 'StringEquals' => [ |
| 110 | + 'aws:RequestTag/Namespace' => $namespace, |
| 111 | + ], |
| 112 | + 'ForAllValues:StringEquals' => [ |
| 113 | + 'aws:TagKeys' => $tagKeys, |
| 114 | + ], |
| 115 | + ]; |
| 116 | + |
| 117 | + if (! empty($envs)) { |
| 118 | + $envValue = count($envs) === 1 ? $envs[0] : $envs; |
| 119 | + |
| 120 | + $readWriteCondition['ForAnyValue:StringEquals'] = [ |
| 121 | + 'secretsmanager:ResourceTag/Env' => $envValue, |
| 122 | + ]; |
| 123 | + |
| 124 | + $createCondition['ForAnyValue:StringEquals'] = [ |
| 125 | + 'aws:RequestTag/Env' => $envValue, |
| 126 | + ]; |
| 127 | + } |
| 128 | + |
| 129 | + return [ |
| 130 | + [ |
| 131 | + 'Sid' => 'KeepSecretsManagerReadWrite', |
| 132 | + 'Effect' => 'Allow', |
| 133 | + 'Action' => [ |
| 134 | + 'secretsmanager:GetSecretValue', |
| 135 | + 'secretsmanager:DescribeSecret', |
| 136 | + 'secretsmanager:ListSecretVersionIds', |
| 137 | + 'secretsmanager:PutSecretValue', |
| 138 | + 'secretsmanager:UpdateSecret*', |
| 139 | + 'secretsmanager:DeleteSecret', |
| 140 | + 'secretsmanager:RestoreSecret', |
| 141 | + 'secretsmanager:TagResource', |
| 142 | + 'secretsmanager:UntagResource', |
| 143 | + ], |
| 144 | + 'Resource' => '*', |
| 145 | + 'Condition' => $readWriteCondition, |
| 146 | + ], |
| 147 | + [ |
| 148 | + 'Sid' => 'KeepSecretsManagerList', |
| 149 | + 'Effect' => 'Allow', |
| 150 | + 'Action' => [ |
| 151 | + 'secretsmanager:ListSecrets', |
| 152 | + 'secretsmanager:BatchGetSecretValue', |
| 153 | + ], |
| 154 | + 'Resource' => '*', |
| 155 | + ], |
| 156 | + [ |
| 157 | + 'Sid' => 'KeepSecretsManagerCreate', |
| 158 | + 'Effect' => 'Allow', |
| 159 | + 'Action' => 'secretsmanager:CreateSecret', |
| 160 | + 'Resource' => '*', |
| 161 | + 'Condition' => $createCondition, |
| 162 | + ], |
| 163 | + [ |
| 164 | + 'Sid' => 'KeepSecretsManagerKms', |
| 165 | + 'Effect' => 'Allow', |
| 166 | + 'Action' => [ |
| 167 | + 'kms:Decrypt', |
| 168 | + 'kms:Encrypt', |
| 169 | + 'kms:GenerateDataKey', |
| 170 | + ], |
| 171 | + 'Resource' => count($kmsResources) === 1 ? $kmsResources[0] : $kmsResources, |
| 172 | + ], |
| 173 | + ]; |
| 174 | + } |
| 175 | +} |
0 commit comments