@codyswann/lisa 2.332.5 → 2.333.0

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.
Files changed (66) hide show
  1. package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
  2. package/dist/core/upstream-evidence-manifest.js +4 -2
  3. package/dist/core/upstream-evidence-manifest.js.map +1 -1
  4. package/package.json +1 -1
  5. package/plugins/lisa/.claude-plugin/plugin.json +1 -1
  6. package/plugins/lisa/.codex-plugin/plugin.json +1 -1
  7. package/plugins/lisa/.codex-plugin/skills/lisa-secrets-access/scripts/aws-bootstrap.mjs +154 -9
  8. package/plugins/lisa/.codex-plugin/skills/lisa-secrets-access/scripts/materialize-secrets.mjs +93 -2
  9. package/plugins/lisa/skills/lisa-secrets-access/scripts/aws-bootstrap.mjs +154 -9
  10. package/plugins/lisa/skills/lisa-secrets-access/scripts/materialize-secrets.mjs +93 -2
  11. package/plugins/lisa-agy/plugin.json +1 -1
  12. package/plugins/lisa-agy/skills/lisa-secrets-access/scripts/aws-bootstrap.mjs +154 -9
  13. package/plugins/lisa-agy/skills/lisa-secrets-access/scripts/materialize-secrets.mjs +93 -2
  14. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  15. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  16. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  17. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  18. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  19. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  20. package/plugins/lisa-copilot/skills/lisa-secrets-access/scripts/aws-bootstrap.mjs +154 -9
  21. package/plugins/lisa-copilot/skills/lisa-secrets-access/scripts/materialize-secrets.mjs +93 -2
  22. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  23. package/plugins/lisa-cursor/skills/lisa-secrets-access/scripts/aws-bootstrap.mjs +154 -9
  24. package/plugins/lisa-cursor/skills/lisa-secrets-access/scripts/materialize-secrets.mjs +93 -2
  25. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  26. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  27. package/plugins/lisa-expo-agy/plugin.json +1 -1
  28. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  29. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  30. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  31. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  32. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  33. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  34. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  35. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  36. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  37. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  38. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  39. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  40. package/plugins/lisa-openclaw/.claude-plugin/plugin.json +1 -1
  41. package/plugins/lisa-openclaw/.codex-plugin/plugin.json +1 -1
  42. package/plugins/lisa-openclaw-agy/plugin.json +1 -1
  43. package/plugins/lisa-openclaw-copilot/.claude-plugin/plugin.json +1 -1
  44. package/plugins/lisa-openclaw-cursor/.claude-plugin/plugin.json +1 -1
  45. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  46. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  47. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  48. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  49. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  50. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  51. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  52. package/plugins/lisa-rails-agy/plugin.json +1 -1
  53. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  54. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  55. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  56. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  57. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  58. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  59. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  60. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  61. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  62. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  63. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  64. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  65. package/plugins/src/base/skills/lisa-secrets-access/scripts/aws-bootstrap.mjs +154 -9
  66. package/plugins/src/base/skills/lisa-secrets-access/scripts/materialize-secrets.mjs +93 -2
@@ -33,9 +33,106 @@ const ACCESS_KEY = "AWS_ACCESS_KEY_ID";
33
33
  const SECRET_KEY = "AWS_SECRET_ACCESS_KEY";
34
34
  const REGION = "AWS_DEFAULT_REGION";
35
35
 
36
+ /** Selects which environment's role a session assumes. */
37
+ const PROFILE = "AWS_PROFILE";
38
+
36
39
  /** The bundle that carries the agent's identity. */
37
40
  export const BOOTSTRAP_KEY = "LISA_AWS_BOOTSTRAP_JSON";
38
41
 
42
+ /**
43
+ * The profile that holds the bootstrap key pair and is assumed FROM.
44
+ *
45
+ * Named rather than `default` so it can never be picked up by accident: this
46
+ * identity can assume roles and do nothing else, so a call that silently ran as
47
+ * it would fail with a permissions error far from its cause.
48
+ */
49
+ export const SOURCE_PROFILE = "lisa-bootstrap";
50
+
51
+ /**
52
+ * Read the bundle's `profiles`, which may be an object OR a JSON string.
53
+ *
54
+ * The real bundle stores it double-encoded — a string containing JSON — so a
55
+ * plain `typeof === "object"` check silently yields zero profiles and every
56
+ * environment quietly disappears. Failing that way is worse than throwing:
57
+ * everything still "works", as the assume-only identity, with no permissions.
58
+ * @param {object|null} bundle Parsed bootstrap bundle.
59
+ * @returns {Record<string, {roleArn?: string, region?: string}>} Profiles by name.
60
+ */
61
+ export function readProfiles(bundle) {
62
+ const raw = bundle?.profiles;
63
+ if (!raw) return {};
64
+ if (typeof raw === "object") return raw;
65
+ if (typeof raw !== "string") return {};
66
+ try {
67
+ const parsed = JSON.parse(raw);
68
+ return parsed && typeof parsed === "object" ? parsed : {};
69
+ } catch {
70
+ return {};
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Render `~/.aws/credentials` and `~/.aws/config` from the bundle.
76
+ *
77
+ * The bundle carries a `profiles` map — one entry per environment, each a
78
+ * `roleArn` in a DIFFERENT account — plus the `externalId` those roles require.
79
+ * Nothing consumed any of it, so `--profile agent-dev` failed with "profile not
80
+ * found" and every call fell back to the bootstrap identity in the shared
81
+ * account. That identity can assume roles and nothing else, which is why
82
+ * `aws sts get-caller-identity` could succeed while real work had no
83
+ * permissions anywhere: a green check that proved only the first link.
84
+ *
85
+ * Writing the pair here rather than exporting it is deliberate. Exported
86
+ * environment credentials OUTRANK `AWS_PROFILE`, so a session that exports them
87
+ * ignores whichever profile it selects — the profiles would exist and never be
88
+ * used.
89
+ * @param {object} bundle Parsed bootstrap bundle.
90
+ * @returns {{credentials: string, config: string, profiles: string[]}|null}
91
+ * Rendered file contents and the profile names, or null when unusable.
92
+ */
93
+ export function renderAwsProfiles(bundle) {
94
+ if (!bundle) return null;
95
+
96
+ const accessKeyId = bundle.accessKeyId ?? bundle.aws_access_key_id;
97
+ const secretAccessKey =
98
+ bundle.secretAccessKey ?? bundle.aws_secret_access_key;
99
+ if (!accessKeyId || !secretAccessKey) return null;
100
+
101
+ const profiles = readProfiles(bundle);
102
+
103
+ const credentials = [
104
+ `[${SOURCE_PROFILE}]`,
105
+ `aws_access_key_id = ${accessKeyId}`,
106
+ `aws_secret_access_key = ${secretAccessKey}`,
107
+ "",
108
+ ].join("\n");
109
+
110
+ const sections = [];
111
+ const names = [];
112
+ for (const [name, entry] of Object.entries(profiles)) {
113
+ const roleArn = entry?.roleArn ?? entry?.role_arn;
114
+ if (!roleArn) continue;
115
+
116
+ // A name is only usable if it can be written as an ini section header and
117
+ // read back as the same string. Anything with a bracket or newline would
118
+ // either truncate or inject extra lines into ~/.aws/config, and a config
119
+ // file this corrupts is worse than one it never wrote.
120
+ if (!/^[\w.@-]+$/.test(name)) continue;
121
+
122
+ const lines = [`[profile ${name}]`, `role_arn = ${roleArn}`];
123
+ lines.push(`source_profile = ${SOURCE_PROFILE}`);
124
+ if (bundle.externalId) lines.push(`external_id = ${bundle.externalId}`);
125
+ if (entry.region) lines.push(`region = ${entry.region}`);
126
+ sections.push(`${lines.join("\n")}\n`);
127
+ // Collected here, where the name is already in scope. Recovering it by
128
+ // re-parsing the rendered text made the header format load-bearing: a
129
+ // change to it would silently corrupt every returned name.
130
+ names.push(name);
131
+ }
132
+
133
+ return { credentials, config: sections.join("\n"), profiles: names };
134
+ }
135
+
39
136
  /**
40
137
  * Read the bootstrap bundle, treating anything malformed as absent.
41
138
  *
@@ -80,16 +177,64 @@ export function deriveAwsEnvironment(selected) {
80
177
  // explicit. Overriding that would be this module guessing against an operator.
81
178
  if (selected.has(ACCESS_KEY) || selected.has(SECRET_KEY)) return derived;
82
179
 
83
- const note =
84
- `Derived from ${BOOTSTRAP_KEY} by lisa-secrets-access. Exported ` +
85
- `deliberately so it overrides any ambient ${ACCESS_KEY} the host injects ` +
86
- `environment variables outrank profile files in the AWS credential chain, ` +
87
- `so without this a stale host value wins and every call fails with ` +
88
- `InvalidClientTokenId. This is the assume-only bootstrap identity; real ` +
89
- `work assumes a role from it.`;
180
+ // The key pair is deliberately NOT exported.
181
+ //
182
+ // It used to be, to out-shout the ambient pair a cloud container injects. That
183
+ // worked and cost more than it bought: exported environment credentials
184
+ // outrank `AWS_PROFILE`, so every call ran as the bootstrap identity which
185
+ // can assume roles and do nothing else. `aws sts get-caller-identity`
186
+ // succeeded while real work had no permissions in any environment account,
187
+ // because each environment is a SEPARATE account reached by assuming a role.
188
+ //
189
+ // The pair now lives in ~/.aws/credentials as the source profile, and the
190
+ // session selects an environment instead. The ambient pair is unset by the
191
+ // shell profile rather than overridden — removing the poison beats out-
192
+ // shouting it, and it is what makes `--profile agent-staging` behave exactly
193
+ // as it does on a developer's machine.
194
+ // Only profiles that actually get WRITTEN are candidates. Selecting a name
195
+ // that ~/.aws/config never contains — an entry with no roleArn, or a name too
196
+ // exotic to be an ini header — fails with "profile not found", which is the
197
+ // exact failure this whole change removes.
198
+ const names = renderAwsProfiles(bundle)?.profiles ?? [];
90
199
 
91
- derived.set(ACCESS_KEY, { value: String(accessKeyId), note });
92
- derived.set(SECRET_KEY, { value: String(secretAccessKey), note });
200
+ // No usable profiles is not a reason to hand back a session with NOTHING.
201
+ //
202
+ // The pair stops being exported only because a profile supersedes it. With no
203
+ // profile to select, exporting it is the difference between a degraded
204
+ // session (the assume-only identity, which at least authenticates) and a dead
205
+ // one — and the managed shell block unsets the ambient pair, so nothing would
206
+ // fill the gap.
207
+ if (names.length === 0) {
208
+ const note =
209
+ `Derived from ${BOOTSTRAP_KEY} by lisa-secrets-access. The bundle ` +
210
+ `declares no usable per-environment profile, so this falls back to the ` +
211
+ `bootstrap identity. It can assume roles and little else — if AWS calls ` +
212
+ `fail with permission errors, the bundle's "profiles" map is the thing ` +
213
+ `to check.`;
214
+ derived.set(ACCESS_KEY, { value: String(accessKeyId), note });
215
+ derived.set(SECRET_KEY, { value: String(secretAccessKey), note });
216
+ }
217
+
218
+ if (names.length > 0 && !selected.has(PROFILE)) {
219
+ // Never production by default. An implicit production profile is one
220
+ // careless command away from a bad afternoon; that one must be typed.
221
+ const preferred =
222
+ names.find(name => /dev/i.test(name)) ??
223
+ names.find(name => !/prod/i.test(name)) ??
224
+ names[0];
225
+
226
+ derived.set(PROFILE, {
227
+ value: preferred,
228
+ note:
229
+ `Derived from ${BOOTSTRAP_KEY} by lisa-secrets-access. Selects the ` +
230
+ `environment whose role this session assumes, via ~/.aws/config. ` +
231
+ `Defaults to a non-production profile deliberately — reach production ` +
232
+ `by naming it (\`--profile ${names.find(n => /prod/i.test(n)) ?? "…"}\`). ` +
233
+ `The bootstrap key pair is NOT exported: environment credentials ` +
234
+ `outrank AWS_PROFILE, so exporting them would ignore this selection ` +
235
+ `and run everything as the assume-only identity.`,
236
+ });
237
+ }
93
238
 
94
239
  const region = bundle.region ?? bundle.defaultRegion;
95
240
  if (region && !selected.has(REGION)) {
@@ -31,7 +31,12 @@ import {
31
31
  import { homedir } from "node:os";
32
32
  import { join } from "node:path";
33
33
 
34
- import { deriveAwsEnvironment } from "./aws-bootstrap.mjs";
34
+ import {
35
+ BOOTSTRAP_KEY,
36
+ deriveAwsEnvironment,
37
+ parseBootstrap,
38
+ renderAwsProfiles,
39
+ } from "./aws-bootstrap.mjs";
35
40
  import { renderEnv, renderNotes } from "./envfile.mjs";
36
41
  import { fetchAll } from "./providers.mjs";
37
42
  import { materializedPaths, readConfig } from "./surfaces.mjs";
@@ -68,6 +73,15 @@ const PROFILE_MARKER = "# >>> lisa secrets (managed) >>>";
68
73
  /** Closes the managed block. */
69
74
  const PROFILE_END = "# <<< lisa secrets (managed) <<<";
70
75
 
76
+ /**
77
+ * Identifies an `~/.aws` file as one this wrote, and may therefore replace.
78
+ *
79
+ * `#` is a comment in the AWS shared-config format, so this is inert to every
80
+ * consumer while still being the thing that distinguishes "our file, refresh
81
+ * it" from "someone else's file, leave it alone".
82
+ */
83
+ const MANAGED_MARKER = "# managed by lisa-secrets-access";
84
+
71
85
  /**
72
86
  * Make every shell in this container load the materialized secrets.
73
87
  *
@@ -95,6 +109,14 @@ export function installProfileSourcing(valuesFile, options = {}) {
95
109
 
96
110
  const block = [
97
111
  PROFILE_MARKER,
112
+ // Unset BEFORE sourcing. A cloud container injects its own AWS key pair,
113
+ // and environment credentials outrank both ~/.aws profiles and AWS_PROFILE
114
+ // — so leaving them set means the session ignores whichever environment it
115
+ // selected and runs as whatever the host injected. That is how a perfectly
116
+ // valid credential produced InvalidClientTokenId for a day. Removing the
117
+ // poison beats out-shouting it, and it is what lets `--profile agent-dev`
118
+ // behave here exactly as it does on a developer's machine.
119
+ `unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN`,
98
120
  `if [ -f "${valuesFile}" ]; then`,
99
121
  ` set -a`,
100
122
  ` . "${valuesFile}"`,
@@ -126,6 +148,61 @@ export function installProfileSourcing(valuesFile, options = {}) {
126
148
  return updated;
127
149
  }
128
150
 
151
+ /**
152
+ * Write `~/.aws/credentials` and `~/.aws/config` from the bootstrap bundle.
153
+ *
154
+ * Both at 0600 inside a 0700 directory, matching how the materialized secrets
155
+ * themselves are protected — the credentials file holds the source key pair.
156
+ * @param {object|null} bundle Parsed bootstrap bundle.
157
+ * @param {object} [options] Home directory and file seams, for tests.
158
+ * @returns {string[]} The profile names written.
159
+ */
160
+ export function installAwsProfiles(bundle, options = {}) {
161
+ const {
162
+ home = process.env.HOME || homedir(),
163
+ mkdir = mkdirSync,
164
+ write = writeFileSync,
165
+ read = readFileSync,
166
+ exists = existsSync,
167
+ chmod = chmodSync,
168
+ } = options;
169
+
170
+ const rendered = renderAwsProfiles(bundle);
171
+ if (!rendered) return [];
172
+
173
+ const dir = join(home, ".aws");
174
+
175
+ // Never overwrite an ~/.aws this did not write.
176
+ //
177
+ // These are whole-file writes, so a pre-existing credentials file — a
178
+ // developer's own profiles, or something another tool set up — would be
179
+ // destroyed rather than merged. This only runs on a surface allowed to write
180
+ // secrets to disk (a disposable container), but "usually disposable" is not a
181
+ // reason to be able to delete someone's credentials. The marker makes our own
182
+ // file re-writable while anything else is left alone and reported.
183
+ for (const name of ["credentials", "config"]) {
184
+ const file = join(dir, name);
185
+ if (exists(file) && !String(read(file, "utf8")).includes(MANAGED_MARKER)) {
186
+ return [];
187
+ }
188
+ }
189
+
190
+ mkdir(dir, { recursive: true, mode: 0o700 });
191
+ chmod(dir, 0o700);
192
+ write(
193
+ join(dir, "credentials"),
194
+ `${MANAGED_MARKER}\n${rendered.credentials}`,
195
+ {
196
+ mode: 0o600,
197
+ }
198
+ );
199
+ write(join(dir, "config"), `${MANAGED_MARKER}\n${rendered.config}`, {
200
+ mode: 0o600,
201
+ });
202
+
203
+ return rendered.profiles;
204
+ }
205
+
129
206
  export function materialize(cfg = readConfig()) {
130
207
  if (!cfg.capabilities.mayWriteValues) {
131
208
  throw new Error(
@@ -170,7 +247,21 @@ export function materialize(cfg = readConfig()) {
170
247
  // materialized" and "the credential is usable" the same statement.
171
248
  const sourced = installProfileSourcing(valuesFile);
172
249
 
173
- return { count: selected.size, derived: derived.size, dir, sourced };
250
+ // The environments live in separate AWS accounts, reached by assuming a role
251
+ // per environment. Without these files `--profile agent-dev` fails with
252
+ // "profile not found" and everything silently falls back to the bootstrap
253
+ // identity, which can assume roles and do nothing else.
254
+ const profiles = installAwsProfiles(
255
+ parseBootstrap(selected.get(BOOTSTRAP_KEY)?.value)
256
+ );
257
+
258
+ return {
259
+ count: selected.size,
260
+ derived: derived.size,
261
+ dir,
262
+ sourced,
263
+ profiles,
264
+ };
174
265
  }
175
266
 
176
267
  function main() {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisa-cdk",
3
- "version": "2.332.5",
3
+ "version": "2.333.0",
4
4
  "description": "AWS CDK-specific plugin",
5
5
  "author": {
6
6
  "name": "Cody Swann"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisa-cdk",
3
- "version": "2.332.5",
3
+ "version": "2.333.0",
4
4
  "description": "AWS CDK-specific Lisa plugin.",
5
5
  "author": {
6
6
  "name": "Cody Swann"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisa-cdk",
3
- "version": "2.332.5",
3
+ "version": "2.333.0",
4
4
  "description": "AWS CDK-specific plugin",
5
5
  "author": {
6
6
  "name": "Cody Swann"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisa-cdk",
3
- "version": "2.332.5",
3
+ "version": "2.333.0",
4
4
  "description": "AWS CDK-specific plugin",
5
5
  "author": {
6
6
  "name": "Cody Swann"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisa-cdk",
3
- "version": "2.332.5",
3
+ "version": "2.333.0",
4
4
  "description": "AWS CDK-specific plugin",
5
5
  "author": {
6
6
  "name": "Cody Swann"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lisa",
3
- "version": "2.332.5",
3
+ "version": "2.333.0",
4
4
  "description": "Universal governance — agents, skills, commands, hooks, and rules for all projects",
5
5
  "author": {
6
6
  "name": "Cody Swann"
@@ -33,9 +33,106 @@ const ACCESS_KEY = "AWS_ACCESS_KEY_ID";
33
33
  const SECRET_KEY = "AWS_SECRET_ACCESS_KEY";
34
34
  const REGION = "AWS_DEFAULT_REGION";
35
35
 
36
+ /** Selects which environment's role a session assumes. */
37
+ const PROFILE = "AWS_PROFILE";
38
+
36
39
  /** The bundle that carries the agent's identity. */
37
40
  export const BOOTSTRAP_KEY = "LISA_AWS_BOOTSTRAP_JSON";
38
41
 
42
+ /**
43
+ * The profile that holds the bootstrap key pair and is assumed FROM.
44
+ *
45
+ * Named rather than `default` so it can never be picked up by accident: this
46
+ * identity can assume roles and do nothing else, so a call that silently ran as
47
+ * it would fail with a permissions error far from its cause.
48
+ */
49
+ export const SOURCE_PROFILE = "lisa-bootstrap";
50
+
51
+ /**
52
+ * Read the bundle's `profiles`, which may be an object OR a JSON string.
53
+ *
54
+ * The real bundle stores it double-encoded — a string containing JSON — so a
55
+ * plain `typeof === "object"` check silently yields zero profiles and every
56
+ * environment quietly disappears. Failing that way is worse than throwing:
57
+ * everything still "works", as the assume-only identity, with no permissions.
58
+ * @param {object|null} bundle Parsed bootstrap bundle.
59
+ * @returns {Record<string, {roleArn?: string, region?: string}>} Profiles by name.
60
+ */
61
+ export function readProfiles(bundle) {
62
+ const raw = bundle?.profiles;
63
+ if (!raw) return {};
64
+ if (typeof raw === "object") return raw;
65
+ if (typeof raw !== "string") return {};
66
+ try {
67
+ const parsed = JSON.parse(raw);
68
+ return parsed && typeof parsed === "object" ? parsed : {};
69
+ } catch {
70
+ return {};
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Render `~/.aws/credentials` and `~/.aws/config` from the bundle.
76
+ *
77
+ * The bundle carries a `profiles` map — one entry per environment, each a
78
+ * `roleArn` in a DIFFERENT account — plus the `externalId` those roles require.
79
+ * Nothing consumed any of it, so `--profile agent-dev` failed with "profile not
80
+ * found" and every call fell back to the bootstrap identity in the shared
81
+ * account. That identity can assume roles and nothing else, which is why
82
+ * `aws sts get-caller-identity` could succeed while real work had no
83
+ * permissions anywhere: a green check that proved only the first link.
84
+ *
85
+ * Writing the pair here rather than exporting it is deliberate. Exported
86
+ * environment credentials OUTRANK `AWS_PROFILE`, so a session that exports them
87
+ * ignores whichever profile it selects — the profiles would exist and never be
88
+ * used.
89
+ * @param {object} bundle Parsed bootstrap bundle.
90
+ * @returns {{credentials: string, config: string, profiles: string[]}|null}
91
+ * Rendered file contents and the profile names, or null when unusable.
92
+ */
93
+ export function renderAwsProfiles(bundle) {
94
+ if (!bundle) return null;
95
+
96
+ const accessKeyId = bundle.accessKeyId ?? bundle.aws_access_key_id;
97
+ const secretAccessKey =
98
+ bundle.secretAccessKey ?? bundle.aws_secret_access_key;
99
+ if (!accessKeyId || !secretAccessKey) return null;
100
+
101
+ const profiles = readProfiles(bundle);
102
+
103
+ const credentials = [
104
+ `[${SOURCE_PROFILE}]`,
105
+ `aws_access_key_id = ${accessKeyId}`,
106
+ `aws_secret_access_key = ${secretAccessKey}`,
107
+ "",
108
+ ].join("\n");
109
+
110
+ const sections = [];
111
+ const names = [];
112
+ for (const [name, entry] of Object.entries(profiles)) {
113
+ const roleArn = entry?.roleArn ?? entry?.role_arn;
114
+ if (!roleArn) continue;
115
+
116
+ // A name is only usable if it can be written as an ini section header and
117
+ // read back as the same string. Anything with a bracket or newline would
118
+ // either truncate or inject extra lines into ~/.aws/config, and a config
119
+ // file this corrupts is worse than one it never wrote.
120
+ if (!/^[\w.@-]+$/.test(name)) continue;
121
+
122
+ const lines = [`[profile ${name}]`, `role_arn = ${roleArn}`];
123
+ lines.push(`source_profile = ${SOURCE_PROFILE}`);
124
+ if (bundle.externalId) lines.push(`external_id = ${bundle.externalId}`);
125
+ if (entry.region) lines.push(`region = ${entry.region}`);
126
+ sections.push(`${lines.join("\n")}\n`);
127
+ // Collected here, where the name is already in scope. Recovering it by
128
+ // re-parsing the rendered text made the header format load-bearing: a
129
+ // change to it would silently corrupt every returned name.
130
+ names.push(name);
131
+ }
132
+
133
+ return { credentials, config: sections.join("\n"), profiles: names };
134
+ }
135
+
39
136
  /**
40
137
  * Read the bootstrap bundle, treating anything malformed as absent.
41
138
  *
@@ -80,16 +177,64 @@ export function deriveAwsEnvironment(selected) {
80
177
  // explicit. Overriding that would be this module guessing against an operator.
81
178
  if (selected.has(ACCESS_KEY) || selected.has(SECRET_KEY)) return derived;
82
179
 
83
- const note =
84
- `Derived from ${BOOTSTRAP_KEY} by lisa-secrets-access. Exported ` +
85
- `deliberately so it overrides any ambient ${ACCESS_KEY} the host injects ` +
86
- `environment variables outrank profile files in the AWS credential chain, ` +
87
- `so without this a stale host value wins and every call fails with ` +
88
- `InvalidClientTokenId. This is the assume-only bootstrap identity; real ` +
89
- `work assumes a role from it.`;
180
+ // The key pair is deliberately NOT exported.
181
+ //
182
+ // It used to be, to out-shout the ambient pair a cloud container injects. That
183
+ // worked and cost more than it bought: exported environment credentials
184
+ // outrank `AWS_PROFILE`, so every call ran as the bootstrap identity which
185
+ // can assume roles and do nothing else. `aws sts get-caller-identity`
186
+ // succeeded while real work had no permissions in any environment account,
187
+ // because each environment is a SEPARATE account reached by assuming a role.
188
+ //
189
+ // The pair now lives in ~/.aws/credentials as the source profile, and the
190
+ // session selects an environment instead. The ambient pair is unset by the
191
+ // shell profile rather than overridden — removing the poison beats out-
192
+ // shouting it, and it is what makes `--profile agent-staging` behave exactly
193
+ // as it does on a developer's machine.
194
+ // Only profiles that actually get WRITTEN are candidates. Selecting a name
195
+ // that ~/.aws/config never contains — an entry with no roleArn, or a name too
196
+ // exotic to be an ini header — fails with "profile not found", which is the
197
+ // exact failure this whole change removes.
198
+ const names = renderAwsProfiles(bundle)?.profiles ?? [];
90
199
 
91
- derived.set(ACCESS_KEY, { value: String(accessKeyId), note });
92
- derived.set(SECRET_KEY, { value: String(secretAccessKey), note });
200
+ // No usable profiles is not a reason to hand back a session with NOTHING.
201
+ //
202
+ // The pair stops being exported only because a profile supersedes it. With no
203
+ // profile to select, exporting it is the difference between a degraded
204
+ // session (the assume-only identity, which at least authenticates) and a dead
205
+ // one — and the managed shell block unsets the ambient pair, so nothing would
206
+ // fill the gap.
207
+ if (names.length === 0) {
208
+ const note =
209
+ `Derived from ${BOOTSTRAP_KEY} by lisa-secrets-access. The bundle ` +
210
+ `declares no usable per-environment profile, so this falls back to the ` +
211
+ `bootstrap identity. It can assume roles and little else — if AWS calls ` +
212
+ `fail with permission errors, the bundle's "profiles" map is the thing ` +
213
+ `to check.`;
214
+ derived.set(ACCESS_KEY, { value: String(accessKeyId), note });
215
+ derived.set(SECRET_KEY, { value: String(secretAccessKey), note });
216
+ }
217
+
218
+ if (names.length > 0 && !selected.has(PROFILE)) {
219
+ // Never production by default. An implicit production profile is one
220
+ // careless command away from a bad afternoon; that one must be typed.
221
+ const preferred =
222
+ names.find(name => /dev/i.test(name)) ??
223
+ names.find(name => !/prod/i.test(name)) ??
224
+ names[0];
225
+
226
+ derived.set(PROFILE, {
227
+ value: preferred,
228
+ note:
229
+ `Derived from ${BOOTSTRAP_KEY} by lisa-secrets-access. Selects the ` +
230
+ `environment whose role this session assumes, via ~/.aws/config. ` +
231
+ `Defaults to a non-production profile deliberately — reach production ` +
232
+ `by naming it (\`--profile ${names.find(n => /prod/i.test(n)) ?? "…"}\`). ` +
233
+ `The bootstrap key pair is NOT exported: environment credentials ` +
234
+ `outrank AWS_PROFILE, so exporting them would ignore this selection ` +
235
+ `and run everything as the assume-only identity.`,
236
+ });
237
+ }
93
238
 
94
239
  const region = bundle.region ?? bundle.defaultRegion;
95
240
  if (region && !selected.has(REGION)) {
@@ -31,7 +31,12 @@ import {
31
31
  import { homedir } from "node:os";
32
32
  import { join } from "node:path";
33
33
 
34
- import { deriveAwsEnvironment } from "./aws-bootstrap.mjs";
34
+ import {
35
+ BOOTSTRAP_KEY,
36
+ deriveAwsEnvironment,
37
+ parseBootstrap,
38
+ renderAwsProfiles,
39
+ } from "./aws-bootstrap.mjs";
35
40
  import { renderEnv, renderNotes } from "./envfile.mjs";
36
41
  import { fetchAll } from "./providers.mjs";
37
42
  import { materializedPaths, readConfig } from "./surfaces.mjs";
@@ -68,6 +73,15 @@ const PROFILE_MARKER = "# >>> lisa secrets (managed) >>>";
68
73
  /** Closes the managed block. */
69
74
  const PROFILE_END = "# <<< lisa secrets (managed) <<<";
70
75
 
76
+ /**
77
+ * Identifies an `~/.aws` file as one this wrote, and may therefore replace.
78
+ *
79
+ * `#` is a comment in the AWS shared-config format, so this is inert to every
80
+ * consumer while still being the thing that distinguishes "our file, refresh
81
+ * it" from "someone else's file, leave it alone".
82
+ */
83
+ const MANAGED_MARKER = "# managed by lisa-secrets-access";
84
+
71
85
  /**
72
86
  * Make every shell in this container load the materialized secrets.
73
87
  *
@@ -95,6 +109,14 @@ export function installProfileSourcing(valuesFile, options = {}) {
95
109
 
96
110
  const block = [
97
111
  PROFILE_MARKER,
112
+ // Unset BEFORE sourcing. A cloud container injects its own AWS key pair,
113
+ // and environment credentials outrank both ~/.aws profiles and AWS_PROFILE
114
+ // — so leaving them set means the session ignores whichever environment it
115
+ // selected and runs as whatever the host injected. That is how a perfectly
116
+ // valid credential produced InvalidClientTokenId for a day. Removing the
117
+ // poison beats out-shouting it, and it is what lets `--profile agent-dev`
118
+ // behave here exactly as it does on a developer's machine.
119
+ `unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN`,
98
120
  `if [ -f "${valuesFile}" ]; then`,
99
121
  ` set -a`,
100
122
  ` . "${valuesFile}"`,
@@ -126,6 +148,61 @@ export function installProfileSourcing(valuesFile, options = {}) {
126
148
  return updated;
127
149
  }
128
150
 
151
+ /**
152
+ * Write `~/.aws/credentials` and `~/.aws/config` from the bootstrap bundle.
153
+ *
154
+ * Both at 0600 inside a 0700 directory, matching how the materialized secrets
155
+ * themselves are protected — the credentials file holds the source key pair.
156
+ * @param {object|null} bundle Parsed bootstrap bundle.
157
+ * @param {object} [options] Home directory and file seams, for tests.
158
+ * @returns {string[]} The profile names written.
159
+ */
160
+ export function installAwsProfiles(bundle, options = {}) {
161
+ const {
162
+ home = process.env.HOME || homedir(),
163
+ mkdir = mkdirSync,
164
+ write = writeFileSync,
165
+ read = readFileSync,
166
+ exists = existsSync,
167
+ chmod = chmodSync,
168
+ } = options;
169
+
170
+ const rendered = renderAwsProfiles(bundle);
171
+ if (!rendered) return [];
172
+
173
+ const dir = join(home, ".aws");
174
+
175
+ // Never overwrite an ~/.aws this did not write.
176
+ //
177
+ // These are whole-file writes, so a pre-existing credentials file — a
178
+ // developer's own profiles, or something another tool set up — would be
179
+ // destroyed rather than merged. This only runs on a surface allowed to write
180
+ // secrets to disk (a disposable container), but "usually disposable" is not a
181
+ // reason to be able to delete someone's credentials. The marker makes our own
182
+ // file re-writable while anything else is left alone and reported.
183
+ for (const name of ["credentials", "config"]) {
184
+ const file = join(dir, name);
185
+ if (exists(file) && !String(read(file, "utf8")).includes(MANAGED_MARKER)) {
186
+ return [];
187
+ }
188
+ }
189
+
190
+ mkdir(dir, { recursive: true, mode: 0o700 });
191
+ chmod(dir, 0o700);
192
+ write(
193
+ join(dir, "credentials"),
194
+ `${MANAGED_MARKER}\n${rendered.credentials}`,
195
+ {
196
+ mode: 0o600,
197
+ }
198
+ );
199
+ write(join(dir, "config"), `${MANAGED_MARKER}\n${rendered.config}`, {
200
+ mode: 0o600,
201
+ });
202
+
203
+ return rendered.profiles;
204
+ }
205
+
129
206
  export function materialize(cfg = readConfig()) {
130
207
  if (!cfg.capabilities.mayWriteValues) {
131
208
  throw new Error(
@@ -170,7 +247,21 @@ export function materialize(cfg = readConfig()) {
170
247
  // materialized" and "the credential is usable" the same statement.
171
248
  const sourced = installProfileSourcing(valuesFile);
172
249
 
173
- return { count: selected.size, derived: derived.size, dir, sourced };
250
+ // The environments live in separate AWS accounts, reached by assuming a role
251
+ // per environment. Without these files `--profile agent-dev` fails with
252
+ // "profile not found" and everything silently falls back to the bootstrap
253
+ // identity, which can assume roles and do nothing else.
254
+ const profiles = installAwsProfiles(
255
+ parseBootstrap(selected.get(BOOTSTRAP_KEY)?.value)
256
+ );
257
+
258
+ return {
259
+ count: selected.size,
260
+ derived: derived.size,
261
+ dir,
262
+ sourced,
263
+ profiles,
264
+ };
174
265
  }
175
266
 
176
267
  function main() {