@localstack/appinspector-ui 1.0.222 → 1.0.223

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.
@@ -117,6 +117,54 @@
117
117
  return true;
118
118
  };
119
119
 
120
+ // src/utils/iam-suggestion-payload.ts
121
+ var HINT_CAUSES = /* @__PURE__ */ new Set([
122
+ "hidden_deny",
123
+ "resource_policy_unsupported",
124
+ "scp_level_empty",
125
+ "service_linked_role",
126
+ "source_unknown",
127
+ "trust_policy_required",
128
+ "unexplained",
129
+ "unsupported_policy_type"
130
+ ]);
131
+ var SUGGESTION_OUTCOMES = /* @__PURE__ */ new Set(["already_allowed", "fixes", "partial", "unfixable"]);
132
+ var SUGGESTION_TYPES = /* @__PURE__ */ new Set(["boundary", "identity", "resource", "scp"]);
133
+ var SUPPORTED_SUGGESTION_VERSION = 1;
134
+ var hasStatements = (value) => isStatementList(value) && value.length > 0;
135
+ var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
136
+ var isStatementList = (value) => Array.isArray(value) && value.every((statement) => isRecord(statement));
137
+ var isOptionalString = (value) => value === void 0 || typeof value === "string";
138
+ var isPolicyReference = (value) => isRecord(value) && typeof value.type === "string" && isOptionalString(value.arn) && isOptionalString(value.name) && isOptionalString(value.key) && (value.owner === void 0 || isRecord(value.owner) && typeof value.owner.arn === "string" && typeof value.owner.kind === "string");
139
+ var isPolicyTarget = (value) => value === void 0 || isRecord(value) && typeof value.id === "string" && typeof value.type === "string" && isOptionalString(value.name);
140
+ var isSuggestionEntry = (value) => isRecord(value) && isRecord(value.attach_to) && typeof value.attach_to.arn === "string" && isPolicyTarget(value.attach_to.attached_to) && SUGGESTION_TYPES.has(value.policy_type) && (value.policy === void 0 || isPolicyReference(value.policy)) && (value.new_policy === void 0 || isRecord(value.new_policy) && typeof value.new_policy.type === "string") && (value.alternatives === void 0 || Array.isArray(value.alternatives)) && (value.statements_to_add === void 0 || isStatementList(value.statements_to_add)) && (value.statements_to_remove === void 0 || isStatementList(value.statements_to_remove)) && (hasStatements(value.statements_to_add) || hasStatements(value.statements_to_remove));
141
+ var NEAR_MISS_REASONS = /* @__PURE__ */ new Set(["condition", "other_action", "other_principal", "other_resource"]);
142
+ var isNearMiss = (value) => isRecord(value) && typeof value.action === "string" && typeof value.resource === "string" && isPolicyReference(value.policy) && typeof value.statement_index === "number" && isRecord(value.statement) && NEAR_MISS_REASONS.has(value.reason);
143
+ var isSuggestionHint = (value) => isRecord(value) && typeof value.cause === "string" && typeof value.action === "string" && typeof value.resource === "string" && (value.policy === void 0 || isPolicyReference(value.policy)) && isOptionalString(value.holder);
144
+ var normalizeSuggestion = (value) => {
145
+ if (!isRecord(value) || typeof value.version !== "number") return void 0;
146
+ const { version } = value;
147
+ const rawEntries = Array.isArray(value.entries) ? value.entries : [];
148
+ const rawHints = Array.isArray(value.hints) ? value.hints : [];
149
+ const readEntries = rawEntries.filter((entry) => isSuggestionEntry(entry));
150
+ const entries = readEntries.map((entry) => entry.alternatives === void 0 ? entry : { ...entry, alternatives: entry.alternatives.filter((alternative) => isPolicyReference(alternative)) });
151
+ const droppedAlternatives = entries.some((entry, index) => (entry.alternatives?.length ?? 0) < (readEntries[index]?.alternatives?.length ?? 0));
152
+ const hints = rawHints.filter((hint) => isSuggestionHint(hint));
153
+ const rawNearMisses = Array.isArray(value.near_misses) ? value.near_misses : [];
154
+ const nearMisses = rawNearMisses.filter((nearMiss) => isNearMiss(nearMiss));
155
+ const newerVersion = version > SUPPORTED_SUGGESTION_VERSION;
156
+ const outcome = !newerVersion && SUGGESTION_OUTCOMES.has(value.outcome) ? value.outcome : void 0;
157
+ const incomplete = readEntries.length < rawEntries.length || droppedAlternatives || nearMisses.length < rawNearMisses.length || hints.length < rawHints.length || hints.some((hint) => !HINT_CAUSES.has(hint.cause)) || newerVersion || outcome === void 0;
158
+ return {
159
+ entries,
160
+ hints,
161
+ ...incomplete ? { incomplete } : {},
162
+ ...nearMisses.length > 0 ? { near_misses: nearMisses } : {},
163
+ ...outcome ? { outcome } : {},
164
+ version
165
+ };
166
+ };
167
+
120
168
  // src/utils/iam-utils.ts
121
169
  function determinePermissionStatus(payload) {
122
170
  if (payload.explicit_deny_count && payload.explicit_deny_count > 0) {
@@ -127,43 +175,114 @@
127
175
  }
128
176
  return "implicitly_denied";
129
177
  }
178
+ var ROOT_PRINCIPAL_REGEX = /^arn:[\w-]+:iam::\d{12}:root$/;
179
+ var ORGANIZATION_POLICY_TYPES = /* @__PURE__ */ new Set(["resource control policy", "service control policy"]);
180
+ var CALLER_POLICY_TYPES = /* @__PURE__ */ new Set(["group inline policy", "identity-based policy", "role inline policy", "user inline policy"]);
181
+ var ACCOUNT_ID_REGEX = /^\d{12}$/;
182
+ var attributePolicy = (policy, decision, principalAccount) => {
183
+ if (ORGANIZATION_POLICY_TYPES.has(policy.type)) {
184
+ const account = policy.arn?.split(":")[4];
185
+ return account && ACCOUNT_ID_REGEX.test(account) ? { ...policy, account, side: "organization" } : { ...policy, side: "organization" };
186
+ }
187
+ if (CALLER_POLICY_TYPES.has(policy.type) || policy.type === "permissions boundary") {
188
+ return { ...policy, account: principalAccount, side: "caller" };
189
+ }
190
+ if (policy.type === "resource-based policy") {
191
+ return { ...policy, account: decision.account, side: "target" };
192
+ }
193
+ return policy;
194
+ };
195
+ var policyKey = (policy) => {
196
+ const { account, arn, attached_to: attachedTo, hash, holder, name, owner, side, type } = policy;
197
+ return JSON.stringify({ account, arn, attachedTo, hash, holder, name, owner: owner?.arn, side, type });
198
+ };
199
+ var collectPolicies = (decisions, principalAccount, policiesOf = (decision) => decision.caused_by) => {
200
+ const policies = /* @__PURE__ */ new Map();
201
+ for (const decision of decisions ?? []) {
202
+ for (const policy of policiesOf(decision) ?? []) {
203
+ const attributed = attributePolicy(policy, decision, principalAccount);
204
+ policies.set(policyKey(attributed), attributed);
205
+ }
206
+ }
207
+ return [...policies.values()];
208
+ };
209
+ var dedupe = (policies) => [...new Map(policies.map((policy) => [policyKey(policy), policy])).values()];
210
+ var toRequiredPermissions = (decisions, result, principalAccount) => (decisions ?? []).map((decision) => ({
211
+ account: decision.account,
212
+ action: decision.action,
213
+ allowedBy: (decision.allowed_by ?? []).map((policy) => attributePolicy(policy, decision, principalAccount)),
214
+ decidedBy: (decision.caused_by ?? []).map((policy) => attributePolicy(policy, decision, principalAccount)),
215
+ policyType: decision.policy_type,
216
+ resource: decision.resource,
217
+ result
218
+ }));
219
+ var resolvePolicies = (references, policies = {}) => references?.flatMap(({ policy: key, statements }) => {
220
+ const policy = policies[key];
221
+ if (!policy) return [];
222
+ return statements ? { ...policy, deciding_statements: statements, key } : { ...policy, key };
223
+ });
224
+ var resolveDecisions = (decisions, policies) => decisions?.map(({ allowed_by: allowedBy, caused_by: causedBy, ...decision }) => {
225
+ const resolved = { ...decision };
226
+ if (allowedBy) resolved.allowed_by = resolvePolicies(allowedBy, policies);
227
+ if (causedBy) resolved.caused_by = resolvePolicies(causedBy, policies);
228
+ return resolved;
229
+ });
230
+ var resolvePayload = (payload) => ({
231
+ ...payload,
232
+ explicit_allows: resolveDecisions(payload.explicit_allows, payload.policies),
233
+ explicit_denies: resolveDecisions(payload.explicit_denies, payload.policies),
234
+ implicit_denies: resolveDecisions(payload.implicit_denies, payload.policies)
235
+ });
130
236
  function parseIAMEvent(payloadString) {
131
237
  try {
132
- const payload = JSON.parse(payloadString);
238
+ const payload = resolvePayload(JSON.parse(payloadString));
133
239
  const permission = determinePermissionStatus(payload);
134
240
  const actions = [];
135
241
  const resources = [];
136
- if (payload.explicit_allows) {
137
- for (const allow of payload.explicit_allows) {
138
- if (!actions.includes(allow.action)) actions.push(allow.action);
139
- if (!resources.includes(allow.resource)) resources.push(allow.resource);
140
- }
141
- }
142
- if (payload.explicit_denies) {
143
- for (const deny of payload.explicit_denies) {
144
- if (!actions.includes(deny.action)) actions.push(deny.action);
145
- if (!resources.includes(deny.resource)) resources.push(deny.resource);
146
- }
147
- }
148
- if (payload.implicit_denies) {
149
- for (const deny of payload.implicit_denies) {
150
- if (!actions.includes(deny.action)) actions.push(deny.action);
151
- if (!resources.includes(deny.resource)) resources.push(deny.resource);
152
- }
242
+ const decisions = [
243
+ ...payload.explicit_allows ?? [],
244
+ ...payload.explicit_denies ?? [],
245
+ ...payload.implicit_denies ?? []
246
+ ];
247
+ for (const decision of decisions) {
248
+ if (!actions.includes(decision.action)) actions.push(decision.action);
249
+ if (!resources.includes(decision.resource)) resources.push(decision.resource);
153
250
  }
251
+ const principalAccount = payload.principal_account ?? "";
252
+ const deniedDecisions = [...payload.explicit_denies ?? [], ...payload.implicit_denies ?? []];
253
+ const allowedBy = dedupe([
254
+ ...collectPolicies(payload.explicit_allows, principalAccount),
255
+ ...collectPolicies(deniedDecisions, principalAccount, (decision) => decision.allowed_by)
256
+ ]);
257
+ const otherAccounts = [...new Set(decisions.filter((decision) => decision.cross_account === true).map((decision) => decision.account))];
154
258
  return {
155
259
  details: {
156
260
  actions,
261
+ allowedAsRoot: permission === "explicitly_allowed" && allowedBy.length === 0 && ROOT_PRINCIPAL_REGEX.test(payload.principal_arn),
262
+ allowedBy,
263
+ crossAccount: otherAccounts.length > 0 ? { callerAccount: principalAccount, resourceAccounts: otherAccounts } : void 0,
264
+ denials: payload.explicit_denies ?? [],
265
+ deniedBy: collectPolicies(payload.explicit_denies, principalAccount),
157
266
  explicitAllows: payload.explicit_allow_count,
158
267
  explicitDenies: payload.explicit_deny_count,
159
268
  implicitDenies: payload.implicit_deny_count,
269
+ notAllowed: payload.implicit_denies ?? [],
270
+ notAllowedBy: collectPolicies(payload.implicit_denies, principalAccount),
271
+ requiredPermissions: [
272
+ ...toRequiredPermissions(payload.explicit_denies, "explicitly_denied", principalAccount),
273
+ ...toRequiredPermissions(payload.implicit_denies, "not_allowed", principalAccount),
274
+ ...toRequiredPermissions(payload.explicit_allows, "allowed", principalAccount)
275
+ ],
160
276
  resources
161
277
  },
162
278
  enforcement: payload.enforcement,
163
279
  operation: payload.operation,
164
280
  permission,
281
+ policies: payload.policies,
165
282
  principal: payload.principal_arn,
166
- service: payload.service
283
+ principalAccount,
284
+ service: payload.service,
285
+ suggestion: normalizeSuggestion(payload.suggestion)
167
286
  };
168
287
  } catch (error) {
169
288
  console.error("Error parsing IAM event payload:", error);
@@ -237,6 +356,13 @@
237
356
  warningType: warning ? firstText(warningMessages[0]?.message) ?? (unenforcedDenial ? `IAM ${denials[0] ?? "implicit"} deny` : iamWarned ? "IAM permission warning" : "Warning") : void 0
238
357
  };
239
358
  };
359
+ var classifyProblem = (span, issues = getSpanIssues(span)) => {
360
+ const { error, warning } = issues;
361
+ const { iamDenied, iamWarned, unenforcedDenial } = getIamOutcome(span);
362
+ if (error) return iamDenied ? "iam_denied" : "error";
363
+ if (warning) return iamWarned || unenforcedDenial ? "iam_warning" : "warning";
364
+ return void 0;
365
+ };
240
366
 
241
367
  // src/utils/span-parents.ts
242
368
  var resolveParents = (spans) => {
@@ -306,6 +432,7 @@
306
432
  issues,
307
433
  operationName: projection.operation_name,
308
434
  parents: projection,
435
+ problem: classifyProblem(projection, issues),
309
436
  producer: projection.parent_service_name ?? EXTERNAL_PRODUCER,
310
437
  region: projection.region,
311
438
  resourceName: projection.resource_name,
@@ -432,6 +559,7 @@
432
559
  const accounts = /* @__PURE__ */ new Map();
433
560
  const clients = /* @__PURE__ */ new Map();
434
561
  const issues = /* @__PURE__ */ new Map();
562
+ const problemCounts = { error: 0, iam_denied: 0, iam_warning: 0, warning: 0 };
435
563
  for (const entry of entries) {
436
564
  const row = rows[entry.spanId];
437
565
  if (!row) continue;
@@ -445,6 +573,7 @@
445
573
  if (entry.error) countFacet(issues, "error", row.rootSpanId);
446
574
  if (entry.warning) countFacet(issues, "warning", row.rootSpanId);
447
575
  if (!entry.error && !entry.warning) countFacet(issues, "ok", row.rootSpanId);
576
+ if (entry.problem) problemCounts[entry.problem]++;
448
577
  }
449
578
  return {
450
579
  allIds: allIds.toReversed(),
@@ -457,6 +586,7 @@
457
586
  region: toFacetValues(regions),
458
587
  service: toFacetValues(services)
459
588
  },
589
+ problemCounts,
460
590
  queryActive,
461
591
  rows,
462
592
  topLevelIds: topLevelIds.toReversed(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@localstack/appinspector-ui",
3
- "version": "1.0.222",
3
+ "version": "1.0.223",
4
4
  "description": "LocalStack AppInspector UI",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",