grada-run 0.49.0 → 0.49.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/destroy.js +57 -3
- package/src/commands/init.js +74 -10
- package/src/utils/prompts.js +14 -1
- package/src/utils/terraform-errors.js +9 -0
- package/src/utils/terraform.js +23 -3
- package/templates/docker/django.Dockerfile +3 -2
- package/templates/docker/go.Dockerfile +4 -0
- package/templates/docker/nestjs.Dockerfile +4 -2
- package/templates/docker/nextjs.Dockerfile +1 -0
- package/templates/docker/node.Dockerfile +4 -2
- package/templates/docker/nuxt.Dockerfile +1 -0
- package/templates/docker/python.Dockerfile +3 -2
- package/templates/docker/rails.Dockerfile +1 -1
- package/templates/docker/svelte.Dockerfile +4 -2
- package/templates/github/plan.yml +1 -0
- package/templates/github/preview.yml +2 -0
- package/templates/terraform/addons/cron-lambda.tf +2 -1
- package/templates/terraform/addons/cron.tf +2 -1
- package/templates/terraform/main-lambda.tf +6 -4
- package/templates/terraform/main.tf +6 -4
- package/templates/terraform/oidc.tf +241 -1
package/package.json
CHANGED
package/src/commands/destroy.js
CHANGED
|
@@ -3,9 +3,9 @@ import path from 'path';
|
|
|
3
3
|
import { intro, outro, confirm, spinner, cancel } from '@clack/prompts';
|
|
4
4
|
import color from 'picocolors';
|
|
5
5
|
import { RDSClient, DescribeDBInstancesCommand, DescribeDBClustersCommand, DescribeDBSnapshotsCommand, DescribeDBClusterSnapshotsCommand, StartDBInstanceCommand, StartDBClusterCommand, CreateDBSnapshotCommand, CreateDBClusterSnapshotCommand, ModifyDBInstanceCommand, ModifyDBClusterCommand } from '@aws-sdk/client-rds';
|
|
6
|
-
import { S3Client } from '@aws-sdk/client-s3';
|
|
6
|
+
import { S3Client, ListObjectVersionsCommand } from '@aws-sdk/client-s3';
|
|
7
7
|
import { teardownStateBucket, drainBucketObjects, resolveClient } from '../utils/aws.js';
|
|
8
|
-
import { checkDependency, pollUntil } from '../utils/system.js';
|
|
8
|
+
import { checkDependency, pollUntil, sleep } from '../utils/system.js';
|
|
9
9
|
import { trackEvent, flushTelemetry, trackSuccess, setActiveCommandName, resetActiveCommandName } from '../core/telemetry.js';
|
|
10
10
|
import { failCommand, shouldAutoApprove, refuseIfNonInteractive, refuseUnknownFlags, isProgrammaticCall } from '../utils/command.js';
|
|
11
11
|
import { runTerraformCommand, readLastRunMarker, isInterruptedMarker, ensureTerraformInit, getTerraformStateBuckets, DEFAULT_TERRAFORM_PHASE_TIMEOUT_MS, PROGRESS_MODES } from '../utils/terraform.js';
|
|
@@ -17,6 +17,9 @@ import { normalizeOptions, parseFlags, normalizeArgv } from '../utils/args.js';
|
|
|
17
17
|
import { truncateSpinnerText } from '../utils/spinner-text.js';
|
|
18
18
|
|
|
19
19
|
export const DEFAULT_DESTROY_DB_TIMEOUT_MS = 600000;
|
|
20
|
+
export const DEFAULT_DESTROY_DRAIN_SETTLE_TIMEOUT_MS = 120000;
|
|
21
|
+
export const DEFAULT_DESTROY_DRAIN_SETTLE_INTERVAL_MS = 5000;
|
|
22
|
+
export const DEFAULT_DESTROY_NOTEMPTY_RETRY_SETTLE_MS = 10000;
|
|
20
23
|
export const DEFAULT_DESTROY_DB_SETTLE_TIMEOUT_MS = 1800000;
|
|
21
24
|
export const DEFAULT_DESTROY_DB_POLL_INTERVAL_MS = 5000;
|
|
22
25
|
export const DEFAULT_DESTROY_SNAPSHOT_TIMEOUT_MS = 1800000;
|
|
@@ -120,6 +123,21 @@ export async function handleDestroyTerraformError({ options, tfDirPath, attemptD
|
|
|
120
123
|
return namedFail('STATE_LOCK_HELD');
|
|
121
124
|
}
|
|
122
125
|
|
|
126
|
+
// BUG-210: a drained bucket can still report versions when terraform
|
|
127
|
+
// runs (S3 empty-visibility race). Settle briefly, then retry the
|
|
128
|
+
// destroy exactly once — a second BucketNotEmpty is terminal.
|
|
129
|
+
if (code === 'BUCKET_NOT_EMPTY') {
|
|
130
|
+
trackEvent('bucket_not_empty_retry', { projectName: actualProjectName, command: 'destroy' });
|
|
131
|
+
const sleepFn = typeof options.sleepFn === 'function' ? options.sleepFn : sleep;
|
|
132
|
+
await sleepFn(DEFAULT_DESTROY_NOTEMPTY_RETRY_SETTLE_MS);
|
|
133
|
+
try {
|
|
134
|
+
await attemptDestroy();
|
|
135
|
+
} catch (retryError) {
|
|
136
|
+
return namedFail('BUCKET_NOT_EMPTY');
|
|
137
|
+
}
|
|
138
|
+
return { retried: true };
|
|
139
|
+
}
|
|
140
|
+
|
|
123
141
|
if (code === 'BACKEND_RECONFIGURE_REQUIRED' || code === 'DESTROY_DEPENDENCY_VIOLATION'
|
|
124
142
|
|| code === 'CLOUDFRONT_IN_PROGRESS' || code === 'DELETION_PROTECTION_ACTIVE') {
|
|
125
143
|
return namedFail(code);
|
|
@@ -555,7 +573,7 @@ async function destroyStackMain(input = {}) {
|
|
|
555
573
|
const drainTargets = (await getTerraformStateBuckets(tfDirPath)).filter((name) => name !== bucketName);
|
|
556
574
|
if (drainTargets.length > 0) {
|
|
557
575
|
s.start(`Draining ${drainTargets.length} S3 bucket(s) before destroy...`);
|
|
558
|
-
const drainClient =
|
|
576
|
+
const drainClient = resolveClient(options.s3Client, S3Client, { region });
|
|
559
577
|
let drainedObjects = 0;
|
|
560
578
|
const drainFailures = [];
|
|
561
579
|
for (const target of drainTargets) {
|
|
@@ -595,6 +613,42 @@ async function destroyStackMain(input = {}) {
|
|
|
595
613
|
resultExtra: { region },
|
|
596
614
|
});
|
|
597
615
|
}
|
|
616
|
+
// BUG-210: versioned deletes can lag S3's empty-reads, so
|
|
617
|
+
// verify each drained bucket reads empty before terraform
|
|
618
|
+
// destroy runs — otherwise destroy flakes on BucketNotEmpty.
|
|
619
|
+
// A bucket deleted out-of-band counts as empty.
|
|
620
|
+
const settleTimeoutMs = options.drainSettleTimeoutMs ?? DEFAULT_DESTROY_DRAIN_SETTLE_TIMEOUT_MS;
|
|
621
|
+
const settleIntervalMs = options.drainSettleIntervalMs ?? DEFAULT_DESTROY_DRAIN_SETTLE_INTERVAL_MS;
|
|
622
|
+
for (const target of drainTargets) {
|
|
623
|
+
const settled = await pollUntil({
|
|
624
|
+
timeoutMs: settleTimeoutMs,
|
|
625
|
+
intervalMs: settleIntervalMs,
|
|
626
|
+
onTick: async () => {
|
|
627
|
+
let listed;
|
|
628
|
+
try {
|
|
629
|
+
listed = await drainClient.send(new ListObjectVersionsCommand({ Bucket: target, MaxKeys: 1 }));
|
|
630
|
+
} catch (error) {
|
|
631
|
+
if (error?.name === 'NoSuchBucket') return { done: true };
|
|
632
|
+
throw error;
|
|
633
|
+
}
|
|
634
|
+
const remaining = (listed.Versions || []).length + (listed.DeleteMarkers || []).length;
|
|
635
|
+
return remaining === 0 ? { done: true } : null;
|
|
636
|
+
},
|
|
637
|
+
});
|
|
638
|
+
if (!settled.done) {
|
|
639
|
+
s.stop(color.red('❌ S3 bucket drain failed.'));
|
|
640
|
+
return failCommand({
|
|
641
|
+
message: `\n✖ S3 bucket ${target} still reports object versions after draining.`,
|
|
642
|
+
hint: ' Re-run destroy (drained buckets usually read empty within a minute), or empty the bucket by hand in the AWS Console.\n',
|
|
643
|
+
useErrorStream: true,
|
|
644
|
+
event: 'infrastructure_destroyed',
|
|
645
|
+
telemetry: { projectName: projectNameForTelemetry },
|
|
646
|
+
errorCode: 'BUCKET_DRAIN_UNSETTLED',
|
|
647
|
+
reason: 'bucket-drain-unsettled',
|
|
648
|
+
resultExtra: { bucket: target, region },
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
}
|
|
598
652
|
s.stop(`Drained ${drainedObjects} object(s) from ${drainTargets.length} bucket(s).`);
|
|
599
653
|
trackEvent('bucket_drain_completed', {
|
|
600
654
|
projectName: projectNameForTelemetry,
|
package/src/commands/init.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fsSync from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
|
+
import { execSync } from 'child_process';
|
|
3
4
|
import { intro, outro, spinner, log, confirm, multiselect, select, cancel, isCancel } from '@clack/prompts';
|
|
4
5
|
import color from 'picocolors';
|
|
5
6
|
|
|
@@ -114,20 +115,52 @@ export function renderOidcSubClaim(sub) {
|
|
|
114
115
|
return `["${sub}", "${companion}"]`;
|
|
115
116
|
}
|
|
116
117
|
|
|
118
|
+
// Renders a list of classic subs as an HCL fragment with each entry's
|
|
119
|
+
// immutable-claim companion, deduplicated. A single entry renders
|
|
120
|
+
// byte-identical to renderOidcSubClaim.
|
|
121
|
+
function renderOidcSubList(subs) {
|
|
122
|
+
const companions = [];
|
|
123
|
+
for (const sub of subs) {
|
|
124
|
+
const companion = immutableCompanion(sub);
|
|
125
|
+
if (companion !== null) companions.push(companion);
|
|
126
|
+
}
|
|
127
|
+
return `[${[...new Set([...subs, ...companions])].map((entry) => `"${entry}"`).join(', ')}]`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Detects the repo's default branch (where scheduled workflows run) from
|
|
131
|
+
// origin/HEAD, falling back to the deploy branch when git is absent,
|
|
132
|
+
// has no origin, or never set HEAD. execSyncImpl is injectable for tests.
|
|
133
|
+
export function resolveDefaultBranch(targetDir, deployBranch, execSyncImpl = execSync) {
|
|
134
|
+
try {
|
|
135
|
+
const head = execSyncImpl('git symbolic-ref --short refs/remotes/origin/HEAD', { cwd: targetDir, stdio: 'pipe' }).toString().trim();
|
|
136
|
+
const branch = head.startsWith('origin/') ? head.slice('origin/'.length) : '';
|
|
137
|
+
if (branch !== '') return branch;
|
|
138
|
+
} catch {
|
|
139
|
+
// Fall through to the deploy branch.
|
|
140
|
+
}
|
|
141
|
+
return deployBranch;
|
|
142
|
+
}
|
|
143
|
+
|
|
117
144
|
// Renders both CI roles' trust claims as HCL fragments from the resolved
|
|
118
145
|
// repo trust: the deploy role binds to the effective deploy branch (the
|
|
119
146
|
// same value rendered into the deploy trigger, so the two stay in sync),
|
|
120
|
-
// the preview role binds to pull request events.
|
|
121
|
-
//
|
|
122
|
-
|
|
147
|
+
// the preview role binds to pull request events. BUG-209: scheduled drift
|
|
148
|
+
// runs on the default branch, so when it differs from the deploy branch
|
|
149
|
+
// the deploy role trusts both refs. The open trust and hand-customized
|
|
150
|
+
// claims render verbatim — never silently widened.
|
|
151
|
+
export function renderOidcRoleSubs(resolvedSub, deployBranch, defaultBranch = deployBranch) {
|
|
123
152
|
const match = typeof resolvedSub === 'string' && resolvedSub.match(/^repo:([^:]+\/[^:]+)(?::(.*))?$/);
|
|
124
153
|
if (!match) return { deploySub: renderOidcSubClaim(resolvedSub), previewSub: renderOidcSubClaim(resolvedSub) };
|
|
125
154
|
const [, slug, rest] = match;
|
|
126
155
|
const preview = `repo:${slug}:pull_request`;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
156
|
+
if (rest === undefined || rest === '*' || rest.startsWith('ref:refs/heads/')) {
|
|
157
|
+
const refs = [`repo:${slug}:ref:refs/heads/${deployBranch}`];
|
|
158
|
+
if (typeof defaultBranch === 'string' && defaultBranch !== '' && defaultBranch !== deployBranch) {
|
|
159
|
+
refs.push(`repo:${slug}:ref:refs/heads/${defaultBranch}`);
|
|
160
|
+
}
|
|
161
|
+
return { deploySub: renderOidcSubList(refs), previewSub: renderOidcSubClaim(preview) };
|
|
162
|
+
}
|
|
163
|
+
return { deploySub: renderOidcSubClaim(resolvedSub), previewSub: renderOidcSubClaim(preview) };
|
|
131
164
|
}
|
|
132
165
|
|
|
133
166
|
// Centralized CI role-name construction (R1): the deploy/preview role
|
|
@@ -160,6 +193,22 @@ export function buildRoleArn(accountId, roleName) {
|
|
|
160
193
|
// dual-format pair resolves back to its classic entry so re-init stays
|
|
161
194
|
// stable; anything richer is described for warnings and guarded against
|
|
162
195
|
// silent widening.
|
|
196
|
+
// Recognizes our own canonical dual-branch trust (BUG-209): two classic
|
|
197
|
+
// branch refs for one slug plus each entry's immutable companion, in
|
|
198
|
+
// render order. Returns { deploySub, branches } or null.
|
|
199
|
+
function parseCanonicalDualTrust(entries) {
|
|
200
|
+
const [first, second, third, fourth] = entries;
|
|
201
|
+
if (immutableCompanion(first) !== third || immutableCompanion(second) !== fourth) return null;
|
|
202
|
+
const branchOf = (entry) => {
|
|
203
|
+
const match = typeof entry === 'string' && entry.match(/^repo:([^:]+):ref:refs\/heads\/(.+)$/);
|
|
204
|
+
return match ? { slug: match[1], branch: match[2] } : null;
|
|
205
|
+
};
|
|
206
|
+
const a = branchOf(first);
|
|
207
|
+
const b = branchOf(second);
|
|
208
|
+
if (!a || !b || a.slug !== b.slug || a.branch === b.branch) return null;
|
|
209
|
+
return { deploySub: first, branches: [a.branch, b.branch] };
|
|
210
|
+
}
|
|
211
|
+
|
|
163
212
|
function classifyTreeTrust(claim) {
|
|
164
213
|
if (!claim) return { trust: null, complex: null };
|
|
165
214
|
if (claim.kind === 'string') return { trust: claim.value, complex: null };
|
|
@@ -170,6 +219,10 @@ function classifyTreeTrust(claim) {
|
|
|
170
219
|
&& immutableCompanion(claim.entries[0]) === claim.entries[1]) {
|
|
171
220
|
return { trust: claim.entries[0], complex: null };
|
|
172
221
|
}
|
|
222
|
+
if (claim.kind === 'list' && claim.entries.length === 4) {
|
|
223
|
+
const dual = parseCanonicalDualTrust(claim.entries);
|
|
224
|
+
if (dual) return { trust: dual.deploySub, complex: null, branches: dual.branches };
|
|
225
|
+
}
|
|
173
226
|
if (claim.kind === 'list') {
|
|
174
227
|
const entries = claim.entries.length > 0 ? ` (${claim.entries.join(', ')})` : '';
|
|
175
228
|
return { trust: null, complex: `${claim.entries.length}-entry list${entries}` };
|
|
@@ -197,7 +250,7 @@ export async function resolveOidcTrust({
|
|
|
197
250
|
const answer = await confirm({ message, initialValue: false });
|
|
198
251
|
return answer === true && !isCancel(answer);
|
|
199
252
|
});
|
|
200
|
-
const { trust: treeTrust, complex: complexClaim } = classifyTreeTrust(treeClaim);
|
|
253
|
+
const { trust: treeTrust, complex: complexClaim, branches: treeBranches } = classifyTreeTrust(treeClaim);
|
|
201
254
|
if (explicitSlug) {
|
|
202
255
|
if (complexClaim) {
|
|
203
256
|
log(color.yellow(`⚠️ --github-repo replaces this tree's ${complexClaim} — the next apply trusts ${explicitSlug} only.`));
|
|
@@ -210,7 +263,7 @@ export async function resolveOidcTrust({
|
|
|
210
263
|
if (treeTrust === 'repo:*') {
|
|
211
264
|
log(color.yellow('⚠️ Keeping this tree\'s open CI trust (repo:*): any GitHub repo\'s Actions can assume the deploy role. Re-scope with --github-repo owner/name before going live.'));
|
|
212
265
|
}
|
|
213
|
-
return { sub: treeTrust, source: 'tree' };
|
|
266
|
+
return { sub: treeTrust, source: 'tree', ...(treeBranches ? { treeBranches } : {}) };
|
|
214
267
|
}
|
|
215
268
|
const remoteSlug = readRemote(targetDir);
|
|
216
269
|
if (remoteSlug) {
|
|
@@ -909,7 +962,18 @@ async function mainStackMain(input = {}) {
|
|
|
909
962
|
|
|
910
963
|
// 7. Synthesize Templates
|
|
911
964
|
s.message('Synthesizing Terraform templates...');
|
|
912
|
-
|
|
965
|
+
// BUG-209: live origin/HEAD wins when it names a real branch; when
|
|
966
|
+
// detection falls back, a re-init keeps the tree's own default branch
|
|
967
|
+
// so dual trust never narrows silently.
|
|
968
|
+
const treeDefaultBranch = oidcResolution.treeBranches?.[1] ?? null;
|
|
969
|
+
const detectedDefaultBranch = resolveDefaultBranch(dirConfig.targetDir, config.branch);
|
|
970
|
+
const defaultBranch = detectedDefaultBranch !== config.branch
|
|
971
|
+
? detectedDefaultBranch
|
|
972
|
+
: (treeDefaultBranch ?? config.branch);
|
|
973
|
+
if (defaultBranch !== config.branch && treeDefaultBranch === null) {
|
|
974
|
+
log(color.yellow(`⚠️ Trusting default branch "${defaultBranch}" alongside deploy branch "${config.branch}" so scheduled drift can assume the deploy role.`));
|
|
975
|
+
}
|
|
976
|
+
const { deploySub, previewSub } = renderOidcRoleSubs(oidcTrustSub, config.branch, defaultBranch);
|
|
913
977
|
const renderConfig = {
|
|
914
978
|
PROJECT_NAME: dirConfig.actualProjectName,
|
|
915
979
|
RESOURCE_SUFFIX: resourceSuffix,
|
package/src/utils/prompts.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
+
import fsSync from 'fs';
|
|
2
3
|
import { text, select, multiselect, confirm, group, cancel } from '@clack/prompts';
|
|
3
4
|
import color from 'picocolors';
|
|
4
5
|
import { execSync } from 'child_process';
|
|
@@ -89,8 +90,20 @@ export async function getProjectConfig(isHeadless, headlessOptions, targetDir, d
|
|
|
89
90
|
const skipsContainerSizing = hints && typeof hints === 'object'
|
|
90
91
|
&& (hints.target === 'lambda' || hints.target === 'static');
|
|
91
92
|
if (isHeadless) {
|
|
93
|
+
// BUG-211: an undetectable framework with a package.json present is
|
|
94
|
+
// a plain-Node project, not a static site — defaulting to 'static'
|
|
95
|
+
// renders an nginx Dockerfile that can neither build scriptless
|
|
96
|
+
// projects nor run node payloads. No package.json keeps 'static'.
|
|
97
|
+
let headlessFramework = headlessOptions.framework || (detectedFramework ? detectedFramework.id : null);
|
|
98
|
+
if (!headlessFramework) {
|
|
99
|
+
try {
|
|
100
|
+
headlessFramework = fsSync.existsSync(path.join(targetDir, 'package.json')) ? 'node' : 'static';
|
|
101
|
+
} catch {
|
|
102
|
+
headlessFramework = 'static';
|
|
103
|
+
}
|
|
104
|
+
}
|
|
92
105
|
return {
|
|
93
|
-
framework:
|
|
106
|
+
framework: headlessFramework,
|
|
94
107
|
region: headlessOptions.region || 'us-east-2',
|
|
95
108
|
port: headlessOptions.port || (headlessOptions.framework === 'static' ? '8080' : '3000'),
|
|
96
109
|
size: headlessOptions.size || 'micro',
|
|
@@ -12,6 +12,7 @@ export const ERROR_CODES = [
|
|
|
12
12
|
'RECREATE_COLLISION',
|
|
13
13
|
'OIDC_PROVIDER_CONFLICT',
|
|
14
14
|
'CLOUDFRONT_IN_PROGRESS',
|
|
15
|
+
'BUCKET_NOT_EMPTY',
|
|
15
16
|
];
|
|
16
17
|
|
|
17
18
|
// AWS AlreadyExists family: every create-path "reality has what state
|
|
@@ -63,6 +64,9 @@ export function classifyTerraformError(stderr, ctx = {}) {
|
|
|
63
64
|
if (/cloudfront/i.test(text) && /in.?progress|in use|precondition/i.test(text)) {
|
|
64
65
|
return { code: 'CLOUDFRONT_IN_PROGRESS', lockId: null };
|
|
65
66
|
}
|
|
67
|
+
if (/BucketNotEmpty/i.test(text)) {
|
|
68
|
+
return { code: 'BUCKET_NOT_EMPTY', lockId: null };
|
|
69
|
+
}
|
|
66
70
|
return null;
|
|
67
71
|
}
|
|
68
72
|
|
|
@@ -107,6 +111,11 @@ const RUNBOOKS = {
|
|
|
107
111
|
headless: 'Wait 5–15 minutes for the distribution to settle, then re-run the same command.',
|
|
108
112
|
interactive: 'Wait 5–15 minutes for the distribution to settle, then re-run the same command.',
|
|
109
113
|
},
|
|
114
|
+
BUCKET_NOT_EMPTY: {
|
|
115
|
+
message: 'Terraform destroy failed twice: an S3 bucket still holds object versions.',
|
|
116
|
+
headless: 'Re-run destroy — drained buckets usually read empty within a minute; if it persists, empty the bucket by hand in the AWS Console.',
|
|
117
|
+
interactive: 'Re-run destroy — drained buckets usually read empty within a minute; if it persists, empty the bucket by hand in the AWS Console.',
|
|
118
|
+
},
|
|
110
119
|
};
|
|
111
120
|
|
|
112
121
|
// Returns `{ message, hint }` for a named code, or null for unknown
|
package/src/utils/terraform.js
CHANGED
|
@@ -218,9 +218,13 @@ export function runTerraformCommand(args, cwd, spin, loadingPrefix, options = {}
|
|
|
218
218
|
if (timer && typeof timer.unref === 'function') timer.unref();
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
221
|
+
// BUG-213: pipe chunks split lines at arbitrary byte offsets, so
|
|
222
|
+
// each chunk's trailing fragment is carried over and only
|
|
223
|
+
// newline-terminated lines are delivered. Splitting per chunk
|
|
224
|
+
// injects newlines into giant single-line JSON (show -json),
|
|
225
|
+
// which breaks parsing downstream.
|
|
226
|
+
let pendingFragment = '';
|
|
227
|
+
const handleLines = (lines) => {
|
|
224
228
|
if (typeof onStdoutLine === 'function') {
|
|
225
229
|
for (const line of lines) onStdoutLine(line);
|
|
226
230
|
}
|
|
@@ -237,6 +241,21 @@ export function runTerraformCommand(args, cwd, spin, loadingPrefix, options = {}
|
|
|
237
241
|
const latestLine = lines[lines.length - 1].trim();
|
|
238
242
|
emit(`${loadingPrefix} - ${color.dim(truncateSpinnerText(latestLine, { fixedOverhead: lineOverhead }))}`);
|
|
239
243
|
}
|
|
244
|
+
};
|
|
245
|
+
const flushFragment = () => {
|
|
246
|
+
if (pendingFragment.trim() === '') {
|
|
247
|
+
pendingFragment = '';
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const trailing = pendingFragment;
|
|
251
|
+
pendingFragment = '';
|
|
252
|
+
handleLines([trailing]);
|
|
253
|
+
};
|
|
254
|
+
child.stdout.on('data', (data) => {
|
|
255
|
+
if (isDone) return;
|
|
256
|
+
const parts = (pendingFragment + data.toString()).split('\n');
|
|
257
|
+
pendingFragment = parts.pop() ?? '';
|
|
258
|
+
handleLines(parts.filter(line => line.trim() !== ''));
|
|
240
259
|
});
|
|
241
260
|
|
|
242
261
|
child.stderr.on('data', (data) => {
|
|
@@ -247,6 +266,7 @@ export function runTerraformCommand(args, cwd, spin, loadingPrefix, options = {}
|
|
|
247
266
|
if (isDone) return;
|
|
248
267
|
isDone = true;
|
|
249
268
|
cleanup();
|
|
269
|
+
flushFragment();
|
|
250
270
|
if (code === 0) {
|
|
251
271
|
settleMarker('completed', { exitCode: code });
|
|
252
272
|
resolve();
|
|
@@ -34,9 +34,10 @@ WORKDIR /app
|
|
|
34
34
|
RUN addgroup -g 1001 appgroup && \
|
|
35
35
|
adduser -u 1001 -G appgroup -s /bin/sh -D appuser
|
|
36
36
|
|
|
37
|
-
# Runtime libraries only (native wheels arrive inside the venv)
|
|
37
|
+
# Runtime libraries only (native wheels arrive inside the venv), plus a
|
|
38
|
+
# CA bundle: without it no verified TLS egress works (RDS, AWS APIs).
|
|
38
39
|
RUN apk update && apk upgrade --no-cache && \
|
|
39
|
-
apk add --no-cache libpq
|
|
40
|
+
apk add --no-cache libpq ca-certificates
|
|
40
41
|
|
|
41
42
|
# Strip the base image's system pip (the venv already ships without one)
|
|
42
43
|
RUN /usr/local/bin/python -m pip uninstall -y setuptools wheel pip && \
|
|
@@ -10,6 +10,10 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o /main .
|
|
|
10
10
|
FROM alpine:3.20@sha256:d9e853e87e55526f6b2917df91a2115c36dd7c696a35be12163d44e6e2a4b6bc
|
|
11
11
|
WORKDIR /app
|
|
12
12
|
|
|
13
|
+
# Ship a CA bundle: without it the static binary cannot verify any TLS
|
|
14
|
+
# egress (RDS, AWS APIs, webhooks).
|
|
15
|
+
RUN apk add --no-cache ca-certificates
|
|
16
|
+
|
|
13
17
|
# Create unprivileged user
|
|
14
18
|
RUN addgroup -g 1001 -S appgroup && \
|
|
15
19
|
adduser -S appuser -u 1001 -G appgroup
|
|
@@ -9,8 +9,10 @@ RUN npm run build
|
|
|
9
9
|
# --- Stage 2: Production ---
|
|
10
10
|
FROM node:22-alpine@sha256:fe4f6d74e20370acb04ad889077d14d0f64a9c8ea6a42bc60cddf1e844699914
|
|
11
11
|
|
|
12
|
-
# 1. DevSecOps: Patch Alpine OS
|
|
13
|
-
|
|
12
|
+
# 1. DevSecOps: Patch Alpine OS, and ship a CA bundle: without it no
|
|
13
|
+
# verified TLS egress works (RDS, AWS APIs, webhooks) from runtimes
|
|
14
|
+
# using the system store.
|
|
15
|
+
RUN apk update && apk upgrade --no-cache && apk add --no-cache ca-certificates
|
|
14
16
|
|
|
15
17
|
ENV NODE_ENV=production
|
|
16
18
|
WORKDIR /app
|
|
@@ -24,6 +24,7 @@ WORKDIR /app
|
|
|
24
24
|
# FIX 2: Upgrade Alpine packages and absolutely destroy npm, yarn, and corepack
|
|
25
25
|
# to eliminate all remaining Critical/High container vulnerabilities.
|
|
26
26
|
RUN apk upgrade --no-cache && \
|
|
27
|
+
apk add --no-cache ca-certificates && \
|
|
27
28
|
rm -rf /usr/local/lib/node_modules/npm \
|
|
28
29
|
/usr/local/bin/npm \
|
|
29
30
|
/usr/local/bin/npx \
|
|
@@ -9,8 +9,10 @@ RUN npm ci --omit=dev && npm cache clean --force && mkdir -p node_modules
|
|
|
9
9
|
# --- Stage 2: Production runner (no package managers) ---
|
|
10
10
|
FROM node:22-alpine@sha256:fe4f6d74e20370acb04ad889077d14d0f64a9c8ea6a42bc60cddf1e844699914 AS runner
|
|
11
11
|
|
|
12
|
-
# 1. DevSecOps: Patch underlying Alpine OS vulnerabilities
|
|
13
|
-
|
|
12
|
+
# 1. DevSecOps: Patch underlying Alpine OS vulnerabilities, and ship a
|
|
13
|
+
# CA bundle: without it no verified TLS egress works (RDS, AWS APIs,
|
|
14
|
+
# webhooks) from runtimes using the system store.
|
|
15
|
+
RUN apk update && apk upgrade --no-cache && apk add --no-cache ca-certificates
|
|
14
16
|
|
|
15
17
|
# 2. Set production environment (optimizes Node and prevents dev dependencies)
|
|
16
18
|
ENV NODE_ENV=production
|
|
@@ -26,6 +26,7 @@ ENV NITRO_HOST="0.0.0.0"
|
|
|
26
26
|
# 1. Upgrade Alpine OS packages to clear OpenSSL/crypto CVEs
|
|
27
27
|
# 2. Vaporize Node package managers (npm, yarn, corepack) to clear ghost CVEs
|
|
28
28
|
RUN apk update && apk upgrade --no-cache && \
|
|
29
|
+
apk add --no-cache ca-certificates && \
|
|
29
30
|
rm -rf /usr/local/lib/node_modules \
|
|
30
31
|
/usr/local/bin/npm \
|
|
31
32
|
/usr/local/bin/npx \
|
|
@@ -34,9 +34,10 @@ WORKDIR /app
|
|
|
34
34
|
RUN addgroup -S appgroup && \
|
|
35
35
|
adduser -S appuser -G appgroup -D -s /bin/sh
|
|
36
36
|
|
|
37
|
-
# Runtime libraries only (native wheels arrive inside the venv)
|
|
37
|
+
# Runtime libraries only (native wheels arrive inside the venv), plus a
|
|
38
|
+
# CA bundle: without it no verified TLS egress works (RDS, AWS APIs).
|
|
38
39
|
RUN apk update && apk upgrade --no-cache && \
|
|
39
|
-
apk add --no-cache libpq
|
|
40
|
+
apk add --no-cache libpq ca-certificates
|
|
40
41
|
|
|
41
42
|
# Strip the base image's system pip (the venv already ships without one)
|
|
42
43
|
RUN /usr/local/bin/python -m pip uninstall -y setuptools wheel pip && \
|
|
@@ -33,7 +33,7 @@ ENV RAILS_ENV=production \
|
|
|
33
33
|
# Upgrade Alpine OS to patch system CVEs and install runtime libs
|
|
34
34
|
# Explicitly patch Ruby's default gems and remove their legacy gemspecs so Trivy ignores them
|
|
35
35
|
RUN apk update && apk upgrade --no-cache && \
|
|
36
|
-
apk add --no-cache postgresql-libs tzdata && \
|
|
36
|
+
apk add --no-cache postgresql-libs tzdata ca-certificates && \
|
|
37
37
|
apk add --no-cache --virtual .build-deps build-base zlib-dev && \
|
|
38
38
|
gem install erb net-imap resolv rexml uri zlib && \
|
|
39
39
|
rm -f /usr/local/lib/ruby/gems/*/specifications/default/erb-*.gemspec \
|
|
@@ -26,8 +26,10 @@ RUN GRADA_BYPASS=true npm run build
|
|
|
26
26
|
# ==========================================
|
|
27
27
|
FROM node:22-alpine@sha256:fe4f6d74e20370acb04ad889077d14d0f64a9c8ea6a42bc60cddf1e844699914
|
|
28
28
|
|
|
29
|
-
# 1. DevSecOps: Patch underlying Alpine OS vulnerabilities
|
|
30
|
-
|
|
29
|
+
# 1. DevSecOps: Patch underlying Alpine OS vulnerabilities, and ship a
|
|
30
|
+
# CA bundle: without it no verified TLS egress works (RDS, AWS APIs,
|
|
31
|
+
# webhooks) from runtimes using the system store.
|
|
32
|
+
RUN apk update && apk upgrade --no-cache && apk add --no-cache ca-certificates
|
|
31
33
|
|
|
32
34
|
# 2. Set production environment
|
|
33
35
|
ENV NODE_ENV=production
|
|
@@ -122,6 +122,7 @@ jobs:
|
|
|
122
122
|
const body = `${marker}\n${heading}\n\n**${summary}** ([workflow run](${runUrl})).\n\n<details><summary>Show Plan</summary>\n\n\`\`\`diff\n${plan}\n\`\`\`\n\n</details>`;
|
|
123
123
|
const { data: comments } = await github.rest.issues.listComments({
|
|
124
124
|
issue_number: context.issue.number,
|
|
125
|
+
per_page: 100,
|
|
125
126
|
owner: context.repo.owner,
|
|
126
127
|
repo: context.repo.repo,
|
|
127
128
|
});
|
|
@@ -129,6 +129,7 @@ jobs:
|
|
|
129
129
|
const body = `${marker}\n🚀 **Preview Environment Deployed!**\n\nYour ephemeral AWS Fargate environment is live at:\n[${process.env.PREVIEW_URL}](${process.env.PREVIEW_URL})\n\n_This environment will be automatically destroyed when the PR is merged or closed._\n\n---\n<div align="right"><sup>☁️ Generated by <a href="https://github.com/grada-run/grada">Grada</a></sup></div>`;
|
|
130
130
|
const { data: comments } = await github.rest.issues.listComments({
|
|
131
131
|
issue_number: context.issue.number,
|
|
132
|
+
per_page: 100,
|
|
132
133
|
owner: context.repo.owner,
|
|
133
134
|
repo: context.repo.repo,
|
|
134
135
|
});
|
|
@@ -159,6 +160,7 @@ jobs:
|
|
|
159
160
|
const body = `${marker}\n❌ **Preview Environment Failed**\n\nThe preview deployment for this PR failed — the URL below (if any) is stale. See the [workflow run](${runUrl}) for details.`;
|
|
160
161
|
const { data: comments } = await github.rest.issues.listComments({
|
|
161
162
|
issue_number: context.issue.number,
|
|
163
|
+
per_page: 100,
|
|
162
164
|
owner: context.repo.owner,
|
|
163
165
|
repo: context.repo.repo,
|
|
164
166
|
});
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
|
|
14
14
|
# --- IAM Role assumed by EventBridge Scheduler ---
|
|
15
15
|
resource "aws_iam_role" "scheduler_cron_role" {
|
|
16
|
-
name
|
|
16
|
+
name = "${local.app_name}-cron-scheduler-role"
|
|
17
|
+
permissions_boundary = local.preview_boundary_arn
|
|
17
18
|
assume_role_policy = jsonencode({
|
|
18
19
|
Version = "2012-10-17"
|
|
19
20
|
Statement = [
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
# --- IAM Role assumed by EventBridge Scheduler ---
|
|
9
9
|
resource "aws_iam_role" "scheduler_cron_role" {
|
|
10
|
-
name
|
|
10
|
+
name = "${local.app_name}-cron-scheduler-role"
|
|
11
|
+
permissions_boundary = local.preview_boundary_arn
|
|
11
12
|
assume_role_policy = jsonencode({
|
|
12
13
|
Version = "2012-10-17"
|
|
13
14
|
Statement = [
|
|
@@ -85,8 +85,9 @@ data "aws_iam_policy_document" "ecs_trust" {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
resource "aws_iam_role" "execution_role" {
|
|
88
|
-
name
|
|
89
|
-
assume_role_policy
|
|
88
|
+
name = "${local.app_name}-execution-role"
|
|
89
|
+
assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
|
|
90
|
+
permissions_boundary = local.preview_boundary_arn
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
resource "aws_iam_role_policy_attachment" "execution_role_policy" {
|
|
@@ -109,8 +110,9 @@ data "aws_iam_policy_document" "lambda_task_trust" {
|
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
resource "aws_iam_role" "task_role" {
|
|
112
|
-
name
|
|
113
|
-
assume_role_policy
|
|
113
|
+
name = "${local.app_name}-task-role"
|
|
114
|
+
assume_role_policy = data.aws_iam_policy_document.lambda_task_trust.json
|
|
115
|
+
permissions_boundary = local.preview_boundary_arn
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
# VPC ENI management plus CloudWatch Logs delivery (this single managed
|
|
@@ -94,8 +94,9 @@ data "aws_iam_policy_document" "ecs_trust" {
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
resource "aws_iam_role" "execution_role" {
|
|
97
|
-
name
|
|
98
|
-
assume_role_policy
|
|
97
|
+
name = "${local.app_name}-execution-role"
|
|
98
|
+
assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
|
|
99
|
+
permissions_boundary = local.preview_boundary_arn
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
resource "aws_iam_role_policy_attachment" "execution_role_policy" {
|
|
@@ -106,8 +107,9 @@ resource "aws_iam_role_policy_attachment" "execution_role_policy" {
|
|
|
106
107
|
# --- IAM: Task Role ---
|
|
107
108
|
# Allows your application code running INSIDE the container to access AWS services
|
|
108
109
|
resource "aws_iam_role" "task_role" {
|
|
109
|
-
name
|
|
110
|
-
assume_role_policy
|
|
110
|
+
name = "${local.app_name}-task-role"
|
|
111
|
+
assume_role_policy = data.aws_iam_policy_document.ecs_trust.json
|
|
112
|
+
permissions_boundary = local.preview_boundary_arn
|
|
111
113
|
}
|
|
112
114
|
|
|
113
115
|
# --- IAM: ECS Exec (SSM) permissions for the task role ---
|
|
@@ -36,6 +36,11 @@ locals {
|
|
|
36
36
|
# (not local.app_name): app_name carries the workspace suffix in preview
|
|
37
37
|
# workspaces, while these denies must always name production.
|
|
38
38
|
prod_prefix = "{{PROJECT_NAME}}{{RESOURCE_SUFFIX}}"
|
|
39
|
+
# Boundary ARN resolving in every workspace: created in default, read
|
|
40
|
+
# everywhere else (mirrors the secrets-vault pattern). Consumed by the
|
|
41
|
+
# task/execution/cron roles, which static trees lack.
|
|
42
|
+
# tflint-ignore: terraform_unused_declarations
|
|
43
|
+
preview_boundary_arn = terraform.workspace == "default" ? aws_iam_policy.preview_boundary[0].arn : data.aws_iam_policy.preview_boundary[0].arn
|
|
39
44
|
}
|
|
40
45
|
|
|
41
46
|
# Deploy role FIRST: readTerraformOidcTrust anchors on this block so re-init
|
|
@@ -178,6 +183,22 @@ resource "aws_iam_role_policy" "preview_guardrails" {
|
|
|
178
183
|
Action = ["iam:CreateUser", "iam:CreateAccessKey"]
|
|
179
184
|
Resource = "*"
|
|
180
185
|
},
|
|
186
|
+
{
|
|
187
|
+
# BUG-207: the preview role carries IAMFullAccess, so without this
|
|
188
|
+
# statement it can delete its own guardrails, delete the deploy
|
|
189
|
+
# role, or widen trust in one call. Scoped to the CI role ARNs
|
|
190
|
+
# (not *): preview stacks legitimately manage their own
|
|
191
|
+
# pr-suffixed IAM roles. The deploy role keeps full IAM power on
|
|
192
|
+
# purpose — it runs on the trusted branch and needs it for the
|
|
193
|
+
# re-init trust migration.
|
|
194
|
+
Sid = "DenyCIRoleMutation"
|
|
195
|
+
Effect = "Deny"
|
|
196
|
+
Action = ["iam:DeleteRole", "iam:UpdateAssumeRolePolicy", "iam:PutRolePolicy", "iam:DeleteRolePolicy", "iam:AttachRolePolicy", "iam:DetachRolePolicy"]
|
|
197
|
+
Resource = [
|
|
198
|
+
aws_iam_role.github_actions_deploy[0].arn,
|
|
199
|
+
aws_iam_role.github_actions_preview[0].arn,
|
|
200
|
+
]
|
|
201
|
+
},
|
|
181
202
|
{
|
|
182
203
|
Sid = "DenySnapshotSharing"
|
|
183
204
|
Effect = "Deny"
|
|
@@ -200,7 +221,7 @@ resource "aws_iam_role_policy" "preview_guardrails" {
|
|
|
200
221
|
Resource = [
|
|
201
222
|
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:cluster/${local.prod_prefix}-cluster",
|
|
202
223
|
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:service/${local.prod_prefix}-cluster/${local.prod_prefix}-service",
|
|
203
|
-
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:service/${local.prod_prefix}-service",
|
|
224
|
+
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:service/${local.prod_prefix}-cluster/${local.prod_prefix}-worker-service",
|
|
204
225
|
]
|
|
205
226
|
},
|
|
206
227
|
{
|
|
@@ -253,6 +274,225 @@ resource "aws_iam_role_policy" "preview_guardrails" {
|
|
|
253
274
|
"arn:aws:dynamodb:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:table/${local.prod_prefix}-table",
|
|
254
275
|
]
|
|
255
276
|
},
|
|
277
|
+
{
|
|
278
|
+
# BUG-208: manual snapshots (grada names them
|
|
279
|
+
# <db-identifier>-manual-<stamp>) are directly deletable;
|
|
280
|
+
# automated snapshots can only go away via retention changes or
|
|
281
|
+
# instance deletion, both already denied above.
|
|
282
|
+
Sid = "DenyProdSnapshotDeletion"
|
|
283
|
+
Effect = "Deny"
|
|
284
|
+
Action = ["rds:DeleteDBSnapshot", "rds:DeleteDBClusterSnapshot"]
|
|
285
|
+
Resource = [
|
|
286
|
+
"arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:snapshot:${local.prod_prefix}-db-*",
|
|
287
|
+
"arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:cluster-snapshot:${local.prod_prefix}-db-cluster-*",
|
|
288
|
+
]
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
# The vault ARN carries a random suffix and static trees have no
|
|
292
|
+
# vault at all (no secrets.tf), so match the tight name prefix —
|
|
293
|
+
# on static it simply matches nothing.
|
|
294
|
+
Sid = "DenyProdSecretsDeletion"
|
|
295
|
+
Effect = "Deny"
|
|
296
|
+
Action = ["secretsmanager:DeleteSecret"]
|
|
297
|
+
Resource = [
|
|
298
|
+
"arn:aws:secretsmanager:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:secret:${local.prod_prefix}-secrets-*",
|
|
299
|
+
]
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
# Web and worker containers share one log group; deleting it
|
|
303
|
+
# wipes the audit trail for both.
|
|
304
|
+
Sid = "DenyProdLogDeletion"
|
|
305
|
+
Effect = "Deny"
|
|
306
|
+
Action = ["logs:DeleteLogGroup"]
|
|
307
|
+
Resource = [
|
|
308
|
+
"arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/ecs/${local.prod_prefix}",
|
|
309
|
+
]
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
# BUG-212: every role preview credentials create must carry the
|
|
313
|
+
# boundary — a missing key fails StringNotEquals, so unbounded
|
|
314
|
+
# creation is denied and born-capped roles cannot escalate.
|
|
315
|
+
Sid = "DenyRoleWithoutBoundary"
|
|
316
|
+
Effect = "Deny"
|
|
317
|
+
Action = ["iam:CreateRole"]
|
|
318
|
+
Resource = "*"
|
|
319
|
+
Condition = {
|
|
320
|
+
StringNotEquals = {
|
|
321
|
+
"iam:PermissionsBoundary" = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${local.prod_prefix}-preview-boundary"
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
# The boundary cannot be swapped or removed after creation:
|
|
327
|
+
# legitimate Terraform never touches it (stable ARN, and
|
|
328
|
+
# deleting a role needs no boundary call).
|
|
329
|
+
Sid = "DenyBoundarySwap"
|
|
330
|
+
Effect = "Deny"
|
|
331
|
+
Action = ["iam:PutRolePermissionsBoundary", "iam:DeleteRolePermissionsBoundary"]
|
|
332
|
+
Resource = "*"
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
# The boundary policy itself cannot be neutered (preview
|
|
336
|
+
# Terraform never manages it: this file is
|
|
337
|
+
# default-workspace-only).
|
|
338
|
+
Sid = "DenyBoundaryMutation"
|
|
339
|
+
Effect = "Deny"
|
|
340
|
+
Action = ["iam:*"]
|
|
341
|
+
Resource = [
|
|
342
|
+
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/${local.prod_prefix}-preview-boundary",
|
|
343
|
+
]
|
|
344
|
+
},
|
|
256
345
|
]
|
|
257
346
|
})
|
|
258
347
|
}
|
|
348
|
+
|
|
349
|
+
# BUG-212: permissions boundary capping every role preview credentials
|
|
350
|
+
# can create. The guardrail above denies CreateRole unless the request
|
|
351
|
+
# carries this boundary, so a backdoor role is born capped: its
|
|
352
|
+
# effective permissions are its own policies intersected with this one.
|
|
353
|
+
# Deny-list shape (default-allow) on purpose: a missing entry weakens
|
|
354
|
+
# security instead of breaking prod tasks. Keep the data denies in sync
|
|
355
|
+
# with preview_guardrails above.
|
|
356
|
+
resource "aws_iam_policy" "preview_boundary" {
|
|
357
|
+
count = terraform.workspace == "default" ? 1 : 0
|
|
358
|
+
name = "${local.prod_prefix}-preview-boundary"
|
|
359
|
+
description = "Permissions boundary capping roles created by preview CI runs"
|
|
360
|
+
|
|
361
|
+
policy = jsonencode({
|
|
362
|
+
Version = "2012-10-17"
|
|
363
|
+
Statement = [
|
|
364
|
+
{
|
|
365
|
+
Sid = "BoundaryBaseAllow"
|
|
366
|
+
Effect = "Allow"
|
|
367
|
+
Action = "*"
|
|
368
|
+
Resource = "*"
|
|
369
|
+
},
|
|
370
|
+
{
|
|
371
|
+
Sid = "BoundaryDenyIAM"
|
|
372
|
+
Effect = "Deny"
|
|
373
|
+
Action = ["iam:*"]
|
|
374
|
+
Resource = "*"
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
Sid = "BoundaryDenyAuditBilling"
|
|
378
|
+
Effect = "Deny"
|
|
379
|
+
Action = ["cloudtrail:*", "organizations:*", "aws-portal:*", "billing:*"]
|
|
380
|
+
Resource = "*"
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
Sid = "BoundaryDenyComputeAbuse"
|
|
384
|
+
Effect = "Deny"
|
|
385
|
+
Action = ["ec2:RunInstances"]
|
|
386
|
+
Resource = "*"
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
Sid = "BoundaryDenySnapshotSharing"
|
|
390
|
+
Effect = "Deny"
|
|
391
|
+
Action = ["rds:ModifyDBSnapshotAttribute"]
|
|
392
|
+
Resource = "*"
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
Sid = "BoundaryDenyStateDestruction"
|
|
396
|
+
Effect = "Deny"
|
|
397
|
+
Action = ["s3:DeleteBucket", "s3:DeleteObjectVersion"]
|
|
398
|
+
Resource = [
|
|
399
|
+
"arn:aws:s3:::{{STATE_BUCKET}}",
|
|
400
|
+
"arn:aws:s3:::{{STATE_BUCKET}}/*",
|
|
401
|
+
]
|
|
402
|
+
},
|
|
403
|
+
{
|
|
404
|
+
Sid = "BoundaryDenyECSShutdown"
|
|
405
|
+
Effect = "Deny"
|
|
406
|
+
Action = ["ecs:UpdateService", "ecs:DeleteService", "ecs:DeleteCluster"]
|
|
407
|
+
Resource = [
|
|
408
|
+
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:cluster/${local.prod_prefix}-cluster",
|
|
409
|
+
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:service/${local.prod_prefix}-cluster/${local.prod_prefix}-service",
|
|
410
|
+
"arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:service/${local.prod_prefix}-cluster/${local.prod_prefix}-worker-service",
|
|
411
|
+
]
|
|
412
|
+
},
|
|
413
|
+
{
|
|
414
|
+
Sid = "BoundaryDenyImageDeletion"
|
|
415
|
+
Effect = "Deny"
|
|
416
|
+
Action = ["ecr:BatchDeleteImage"]
|
|
417
|
+
Resource = [
|
|
418
|
+
"arn:aws:ecr:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:repository/${local.prod_prefix}-repo",
|
|
419
|
+
]
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
Sid = "BoundaryDenyLambdaRewrite"
|
|
423
|
+
Effect = "Deny"
|
|
424
|
+
Action = ["lambda:UpdateFunctionCode", "lambda:DeleteFunction"]
|
|
425
|
+
Resource = [
|
|
426
|
+
"arn:aws:lambda:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:function:${local.prod_prefix}-fn",
|
|
427
|
+
]
|
|
428
|
+
},
|
|
429
|
+
{
|
|
430
|
+
Sid = "BoundaryDenyQueueDestruction"
|
|
431
|
+
Effect = "Deny"
|
|
432
|
+
Action = ["sqs:PurgeQueue", "sqs:DeleteQueue"]
|
|
433
|
+
Resource = [
|
|
434
|
+
"arn:aws:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${local.prod_prefix}-queue",
|
|
435
|
+
"arn:aws:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${local.prod_prefix}-dlq",
|
|
436
|
+
]
|
|
437
|
+
},
|
|
438
|
+
{
|
|
439
|
+
Sid = "BoundaryDenyDatabaseDestruction"
|
|
440
|
+
Effect = "Deny"
|
|
441
|
+
Action = ["rds:DeleteDBInstance", "rds:ModifyDBInstance"]
|
|
442
|
+
Resource = [
|
|
443
|
+
"arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:db:${local.prod_prefix}-db",
|
|
444
|
+
"arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:db:${local.prod_prefix}-db-instance-1",
|
|
445
|
+
]
|
|
446
|
+
},
|
|
447
|
+
{
|
|
448
|
+
Sid = "BoundaryDenyAuroraDestruction"
|
|
449
|
+
Effect = "Deny"
|
|
450
|
+
Action = ["rds:DeleteDBCluster", "rds:ModifyDBCluster"]
|
|
451
|
+
Resource = [
|
|
452
|
+
"arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:cluster:${local.prod_prefix}-db-cluster",
|
|
453
|
+
]
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
Sid = "BoundaryDenyTableDeletion"
|
|
457
|
+
Effect = "Deny"
|
|
458
|
+
Action = ["dynamodb:DeleteTable"]
|
|
459
|
+
Resource = [
|
|
460
|
+
"arn:aws:dynamodb:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:table/${local.prod_prefix}-table",
|
|
461
|
+
]
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
Sid = "BoundaryDenySnapshotDeletion"
|
|
465
|
+
Effect = "Deny"
|
|
466
|
+
Action = ["rds:DeleteDBSnapshot", "rds:DeleteDBClusterSnapshot"]
|
|
467
|
+
Resource = [
|
|
468
|
+
"arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:snapshot:${local.prod_prefix}-db-*",
|
|
469
|
+
"arn:aws:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:cluster-snapshot:${local.prod_prefix}-db-cluster-*",
|
|
470
|
+
]
|
|
471
|
+
},
|
|
472
|
+
{
|
|
473
|
+
Sid = "BoundaryDenySecretsDeletion"
|
|
474
|
+
Effect = "Deny"
|
|
475
|
+
Action = ["secretsmanager:DeleteSecret"]
|
|
476
|
+
Resource = [
|
|
477
|
+
"arn:aws:secretsmanager:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:secret:${local.prod_prefix}-secrets-*",
|
|
478
|
+
]
|
|
479
|
+
},
|
|
480
|
+
{
|
|
481
|
+
Sid = "BoundaryDenyLogDeletion"
|
|
482
|
+
Effect = "Deny"
|
|
483
|
+
Action = ["logs:DeleteLogGroup"]
|
|
484
|
+
Resource = [
|
|
485
|
+
"arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/ecs/${local.prod_prefix}",
|
|
486
|
+
]
|
|
487
|
+
},
|
|
488
|
+
]
|
|
489
|
+
})
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
# Preview workspaces create pr-suffixed roles through the same templates,
|
|
493
|
+
# so they read the default workspace's boundary instead of creating one
|
|
494
|
+
# (IAM names are account-global).
|
|
495
|
+
data "aws_iam_policy" "preview_boundary" {
|
|
496
|
+
count = terraform.workspace == "default" ? 0 : 1
|
|
497
|
+
name = "${local.prod_prefix}-preview-boundary"
|
|
498
|
+
}
|