import * as Output from "alchemy/Output";
import * as Effect from "effect/Effect";
import * as AWSBindings from "../AWSBindings.ts";
import type { ExperimentTemplate } from "./ExperimentTemplate.ts";
/**
* Shared scaffolding for the AWS Fault Injection Service (FIS) HTTP bindings.
*
* NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
* a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
* below. Everything except the operation, the IAM action list, and the
* injected identifier is boilerplate.
*/
/**
* Build the impl Effect for a FIS operation scoped to a bound
* {@link ExperimentTemplate}: the deploy-time half grants `actions` on the
* template's ARN (plus, when `grantExperimentWildcard` is set, the account's
* `experiment/*` ARNs — `fis:StartExperiment` authorizes against both the
* template being started and the experiment being created), and the runtime
* half injects the template's id into every request as `requestKey`.
*
* `clientToken` (an idempotency token) is auto-generated by distilled when
* absent, so it is omitted from the runtime request shape.
*/
export const makeFisTemplateHttpBinding = <I, A, E, R>(options: {
/** Fully-qualified binding tag, e.g. `AWS.FIS.StartExperiment`. */
tag: string;
/** The distilled operation. */
operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
/** IAM actions granted on the experiment template ARN. */
actions: readonly string[];
/** The request field the bound template's id is injected as. */
requestKey: "id" | "experimentTemplateId";
/**
* Additionally grant `actions` on the account's `experiment/*` ARN —
* `fis:StartExperiment` is authorized against both the template and the
* experiment it creates.
*/
grantExperimentWildcard?: boolean;
/**
* Additionally grant `iam:CreateServiceLinkedRole` scoped to FIS — the
* first `fis:StartExperiment` in an account creates the
* `AWSServiceRoleForFIS` service-linked role on the caller's behalf and is
* denied without it.
*/
grantServiceLinkedRole?: boolean;
}) =>
Effect.gen(function* () {
const bind = yield* AWSBindings.Bind;
const op = yield* AWSBindings.withCredentials(options.operation);
return Effect.fn(function* (template: ExperimentTemplate) {
const TemplateId = yield* template.id;
if (!globalThis.__ALCHEMY_RUNTIME__) {
yield* bind`${options.tag}(${template})`({
policyStatements: [
{
Effect: "Allow",
Action: [...options.actions],
Resource: [
Output.interpolate`${template.arn}`,
...(options.grantExperimentWildcard
? [
// arn:…:experiment-template/EXT… → arn:…:experiment/*
template.arn.pipe(
Output.map((arn) => `${arn.slice(0, arn.lastIndexOf(":"))}:experiment/*`),
),
]
: []),
],
},
...(options.grantServiceLinkedRole
? [
{
Effect: "Allow" as const,
Action: ["iam:CreateServiceLinkedRole"],
Resource: [
"arn:aws:iam::*:role/aws-service-role/fis.amazonaws.com/AWSServiceRoleForFIS",
],
Condition: {
StringEquals: {
"iam:AWSServiceName": "fis.amazonaws.com",
},
},
},
]
: []),
],
});
}
return Effect.fn(`${options.tag}(${template.LogicalId})`)(function* (
request?: Omit<I, "id" | "experimentTemplateId" | "clientToken">,
) {
return yield* op({
...request,
[options.requestKey]: yield* TemplateId,
} as I);
});
});
});
/**
* Build the impl Effect for an account-level FIS operation (experiment
* control and reads, the action/target-resource-type catalogs, the safety
* lever). The deploy-time half grants `actions` on `*` — experiments are
* created dynamically at runtime and the catalog/list actions are not scoped
* to a single resource.
*/
export const makeFisAccountHttpBinding = <I, A, E, R>(options: {
/** Fully-qualified binding tag, e.g. `AWS.FIS.GetExperiment`. */
tag: string;
/** The distilled operation, invoked with the caller's request as-is. */
operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
/** IAM actions granted on `*`. */
actions: readonly string[];
}) =>
Effect.gen(function* () {
const bind = yield* AWSBindings.Bind;
const op = yield* AWSBindings.withCredentials(options.operation);
return Effect.fn(function* () {
if (!globalThis.__ALCHEMY_RUNTIME__) {
yield* bind`${options.tag}()`({
policyStatements: [
{
Effect: "Allow",
Action: [...options.actions],
Resource: ["*"],
},
],
});
}
return Effect.fn(options.tag)(function* (request?: I) {
return yield* op((request ?? {}) as I);
});
});
});