The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import type * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2"; |
| 2 | import { toSeconds } from "alchemy/Util/Duration"; |
| 3 | import type * as Duration from "effect/Duration"; |
| 4 | import type * as Redacted from "effect/Redacted"; |
| 5 | import type { TargetGroupArn } from "./TargetGroup.ts"; |
| 6 | |
| 7 | /** |
| 8 | * A single forward target with an optional traffic weight. Used by the |
| 9 | * weighted-forward action to split traffic across multiple target groups. |
| 10 | */ |
| 11 | export interface ForwardTarget { |
| 12 | /** |
| 13 | * The target group to forward to. Accepts a `TargetGroup` resource reference |
| 14 | * — the engine resolves it to the ARN automatically (the `Input` machinery |
| 15 | * applies deeply to nested props). |
| 16 | */ |
| 17 | targetGroupArn: TargetGroupArn; |
| 18 | /** |
| 19 | * The weight applied to this target group when splitting traffic. The |
| 20 | * proportion a target group receives is its weight divided by the sum of all |
| 21 | * weights in the action. |
| 22 | * @default 1 |
| 23 | */ |
| 24 | weight?: number; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Forward action: routes the request to one or more target groups, optionally |
| 29 | * with session stickiness so a client stays pinned to a single target group. |
| 30 | */ |
| 31 | export interface ForwardAction { |
| 32 | /** Discriminator identifying this as a forward action. */ |
| 33 | type: "forward"; |
| 34 | /** One or more target groups to forward to (weighted). */ |
| 35 | targetGroups: ForwardTarget[]; |
| 36 | /** Session stickiness across the target groups in this action. */ |
| 37 | stickiness?: { |
| 38 | /** Whether target-group stickiness is enabled. */ |
| 39 | enabled: boolean; |
| 40 | /** |
| 41 | * The time a client remains pinned to a target group — e.g. `"1 hour"` |
| 42 | * or `Duration.hours(1)`. Sent to AWS as whole seconds. |
| 43 | * @default "1 hour" |
| 44 | */ |
| 45 | duration?: Duration.Input; |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Redirect action: returns an HTTP redirect (301/302) to a computed URL. Any |
| 51 | * component (protocol, host, path, query, port) may use the reserved keywords |
| 52 | * `#{protocol}`, `#{host}`, `#{path}`, `#{query}`, `#{port}` to copy from the |
| 53 | * original request. |
| 54 | */ |
| 55 | export interface RedirectAction { |
| 56 | /** Discriminator identifying this as a redirect action. */ |
| 57 | type: "redirect"; |
| 58 | /** The redirect status code. */ |
| 59 | statusCode: "HTTP_301" | "HTTP_302"; |
| 60 | /** The protocol (`HTTP`, `HTTPS`, or `#{protocol}`). */ |
| 61 | protocol?: string; |
| 62 | /** The port. */ |
| 63 | port?: string; |
| 64 | /** The hostname. */ |
| 65 | host?: string; |
| 66 | /** The absolute path, starting with `/`. */ |
| 67 | path?: string; |
| 68 | /** The query parameters, not including the leading `?`. */ |
| 69 | query?: string; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Fixed-response action: returns a static HTTP response directly from the load |
| 74 | * balancer without forwarding to any target. |
| 75 | */ |
| 76 | export interface FixedResponseAction { |
| 77 | /** Discriminator identifying this as a fixed-response action. */ |
| 78 | type: "fixedResponse"; |
| 79 | /** The HTTP response status code (2XX/4XX/5XX). */ |
| 80 | statusCode: string; |
| 81 | /** The content type of the response body. */ |
| 82 | contentType?: string; |
| 83 | /** The response body. */ |
| 84 | messageBody?: string; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Authenticate-OIDC action: authenticates the request through an OpenID Connect |
| 89 | * (OIDC) identity provider before forwarding to the next action. |
| 90 | */ |
| 91 | export interface AuthenticateOidcAction { |
| 92 | /** Discriminator identifying this as an authenticate-OIDC action. */ |
| 93 | type: "authenticateOidc"; |
| 94 | /** The OIDC issuer identifier of the identity provider (IdP). */ |
| 95 | issuer: string; |
| 96 | /** The authorization endpoint of the IdP. */ |
| 97 | authorizationEndpoint: string; |
| 98 | /** The token endpoint of the IdP. */ |
| 99 | tokenEndpoint: string; |
| 100 | /** The user info endpoint of the IdP. */ |
| 101 | userInfoEndpoint: string; |
| 102 | /** The OAuth 2.0 client identifier registered with the IdP. */ |
| 103 | clientId: string; |
| 104 | /** Required on first use; omit with `useExistingClientSecret: true` to keep the existing one. */ |
| 105 | clientSecret?: Redacted.Redacted<string>; |
| 106 | /** |
| 107 | * The set of user claims requested from the IdP. |
| 108 | * @default "openid" |
| 109 | */ |
| 110 | scope?: string; |
| 111 | /** |
| 112 | * The name of the cookie used to maintain session information. |
| 113 | * @default "AWSELBAuthSessionCookie" |
| 114 | */ |
| 115 | sessionCookieName?: string; |
| 116 | /** The maximum duration of the authentication session — e.g. `"7 days"`. Sent to AWS as whole seconds. */ |
| 117 | sessionTimeout?: Duration.Input; |
| 118 | /** |
| 119 | * Behavior when the user is not authenticated (`deny`, `allow`, or `authenticate`). |
| 120 | * @default "authenticate" |
| 121 | */ |
| 122 | onUnauthenticatedRequest?: "deny" | "allow" | "authenticate"; |
| 123 | /** Set to `true` on updates to keep the client secret already stored on the action. */ |
| 124 | useExistingClientSecret?: boolean; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Authenticate-Cognito action: authenticates the request through an Amazon |
| 129 | * Cognito user pool before forwarding to the next action. |
| 130 | */ |
| 131 | export interface AuthenticateCognitoAction { |
| 132 | /** Discriminator identifying this as an authenticate-Cognito action. */ |
| 133 | type: "authenticateCognito"; |
| 134 | /** The ARN of the Cognito user pool. */ |
| 135 | userPoolArn: string; |
| 136 | /** The ID of the Cognito user pool client. */ |
| 137 | userPoolClientId: string; |
| 138 | /** The domain prefix or fully-qualified domain of the Cognito user pool. */ |
| 139 | userPoolDomain: string; |
| 140 | /** |
| 141 | * The set of user claims requested from Cognito. |
| 142 | * @default "openid" |
| 143 | */ |
| 144 | scope?: string; |
| 145 | /** |
| 146 | * The name of the cookie used to maintain session information. |
| 147 | * @default "AWSELBAuthSessionCookie" |
| 148 | */ |
| 149 | sessionCookieName?: string; |
| 150 | /** The maximum duration of the authentication session — e.g. `"7 days"`. Sent to AWS as whole seconds. */ |
| 151 | sessionTimeout?: Duration.Input; |
| 152 | /** |
| 153 | * Behavior when the user is not authenticated (`deny`, `allow`, or `authenticate`). |
| 154 | * @default "authenticate" |
| 155 | */ |
| 156 | onUnauthenticatedRequest?: "deny" | "allow" | "authenticate"; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * A listener/rule action. The terminal action of a listener default-action or a |
| 161 | * rule must be `forward`, `redirect`, or `fixedResponse`; authentication |
| 162 | * actions are non-terminal and are ordered before the terminal action. |
| 163 | */ |
| 164 | export type ListenerAction = |
| 165 | | ForwardAction |
| 166 | | RedirectAction |
| 167 | | FixedResponseAction |
| 168 | | AuthenticateOidcAction |
| 169 | | AuthenticateCognitoAction; |
| 170 | |
| 171 | // At reconcile time the engine has already resolved Input/resource refs to |
| 172 | // plain ARN strings. |
| 173 | const resolveTargetGroupArn = (ref: TargetGroupArn): string => ref; |
| 174 | |
| 175 | /** |
| 176 | * Serialize a list of {@link ListenerAction} into the wire `Action[]` shape AWS |
| 177 | * expects, assigning `Order` so authentication actions run before the terminal |
| 178 | * action. Shared by both Listener default-actions and ListenerRule actions. |
| 179 | */ |
| 180 | export const serializeActions = (actions: ListenerAction[]): elbv2.Action[] => |
| 181 | actions.map((action, index): elbv2.Action => { |
| 182 | const Order = index + 1; |
| 183 | switch (action.type) { |
| 184 | case "forward": |
| 185 | return { |
| 186 | Type: "forward", |
| 187 | Order, |
| 188 | ForwardConfig: { |
| 189 | TargetGroups: action.targetGroups.map((t) => ({ |
| 190 | TargetGroupArn: resolveTargetGroupArn(t.targetGroupArn), |
| 191 | Weight: t.weight, |
| 192 | })), |
| 193 | TargetGroupStickinessConfig: action.stickiness |
| 194 | ? { |
| 195 | Enabled: action.stickiness.enabled, |
| 196 | DurationSeconds: toSeconds(action.stickiness.duration), |
| 197 | } |
| 198 | : undefined, |
| 199 | }, |
| 200 | // For a single, unweighted target group, AWS also accepts the legacy |
| 201 | // top-level TargetGroupArn; sending only ForwardConfig is canonical. |
| 202 | TargetGroupArn: |
| 203 | action.targetGroups.length === 1 && action.targetGroups[0].weight === undefined |
| 204 | ? resolveTargetGroupArn(action.targetGroups[0].targetGroupArn) |
| 205 | : undefined, |
| 206 | }; |
| 207 | case "redirect": |
| 208 | return { |
| 209 | Type: "redirect", |
| 210 | Order, |
| 211 | RedirectConfig: { |
| 212 | StatusCode: action.statusCode, |
| 213 | Protocol: action.protocol, |
| 214 | Port: action.port, |
| 215 | Host: action.host, |
| 216 | Path: action.path, |
| 217 | Query: action.query, |
| 218 | }, |
| 219 | }; |
| 220 | case "fixedResponse": |
| 221 | return { |
| 222 | Type: "fixed-response", |
| 223 | Order, |
| 224 | FixedResponseConfig: { |
| 225 | StatusCode: action.statusCode, |
| 226 | ContentType: action.contentType, |
| 227 | MessageBody: action.messageBody, |
| 228 | }, |
| 229 | }; |
| 230 | case "authenticateOidc": |
| 231 | return { |
| 232 | Type: "authenticate-oidc", |
| 233 | Order, |
| 234 | AuthenticateOidcConfig: { |
| 235 | Issuer: action.issuer, |
| 236 | AuthorizationEndpoint: action.authorizationEndpoint, |
| 237 | TokenEndpoint: action.tokenEndpoint, |
| 238 | UserInfoEndpoint: action.userInfoEndpoint, |
| 239 | ClientId: action.clientId, |
| 240 | // Redacted end-to-end: distilled's AuthenticateOidcActionConfig |
| 241 | // marks ClientSecret sensitive, so the Redacted passes straight |
| 242 | // through and is only unwrapped by the wire serializer. |
| 243 | ClientSecret: action.clientSecret, |
| 244 | Scope: action.scope, |
| 245 | SessionCookieName: action.sessionCookieName, |
| 246 | SessionTimeout: toSeconds(action.sessionTimeout), |
| 247 | OnUnauthenticatedRequest: action.onUnauthenticatedRequest, |
| 248 | UseExistingClientSecret: action.useExistingClientSecret, |
| 249 | }, |
| 250 | }; |
| 251 | case "authenticateCognito": |
| 252 | return { |
| 253 | Type: "authenticate-cognito", |
| 254 | Order, |
| 255 | AuthenticateCognitoConfig: { |
| 256 | UserPoolArn: action.userPoolArn, |
| 257 | UserPoolClientId: action.userPoolClientId, |
| 258 | UserPoolDomain: action.userPoolDomain, |
| 259 | Scope: action.scope, |
| 260 | SessionCookieName: action.sessionCookieName, |
| 261 | SessionTimeout: toSeconds(action.sessionTimeout), |
| 262 | OnUnauthenticatedRequest: action.onUnauthenticatedRequest, |
| 263 | }, |
| 264 | }; |
| 265 | } |
| 266 | }); |
| 267 | |
| 268 | /** |
| 269 | * A condition under which a {@link ListenerRule} matches a request. Exactly one |
| 270 | * of the config fields should be set per condition; multiple conditions on a |
| 271 | * rule are AND-ed together, while values within a single condition are OR-ed. |
| 272 | */ |
| 273 | export interface ListenerRuleCondition { |
| 274 | /** Match on the `Host` header. Supports `*` and `?` wildcards. */ |
| 275 | hostHeader?: { values?: string[]; regexValues?: string[] }; |
| 276 | /** Match on the request path. Supports `*` and `?` wildcards. */ |
| 277 | pathPattern?: { values?: string[]; regexValues?: string[] }; |
| 278 | /** Match on a named HTTP header. */ |
| 279 | httpHeader?: { |
| 280 | name: string; |
| 281 | values?: string[]; |
| 282 | regexValues?: string[]; |
| 283 | }; |
| 284 | /** Match on query-string key/value pairs. Supports `*` and `?` wildcards. */ |
| 285 | queryString?: { values: { key?: string; value: string }[] }; |
| 286 | /** Match on the HTTP request method (GET, POST, ...). */ |
| 287 | httpRequestMethod?: { values: string[]; regexValues?: string[] }; |
| 288 | /** Match on the source IP CIDR. */ |
| 289 | sourceIp?: { values: string[] }; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Serialize a list of {@link ListenerRuleCondition} into the wire |
| 294 | * `RuleCondition[]` shape AWS expects. |
| 295 | */ |
| 296 | export const serializeConditions = (conditions: ListenerRuleCondition[]): elbv2.RuleCondition[] => |
| 297 | conditions.flatMap((condition): elbv2.RuleCondition[] => { |
| 298 | const out: elbv2.RuleCondition[] = []; |
| 299 | if (condition.hostHeader) { |
| 300 | out.push({ |
| 301 | Field: "host-header", |
| 302 | HostHeaderConfig: { |
| 303 | Values: condition.hostHeader.values, |
| 304 | RegexValues: condition.hostHeader.regexValues, |
| 305 | }, |
| 306 | }); |
| 307 | } |
| 308 | if (condition.pathPattern) { |
| 309 | out.push({ |
| 310 | Field: "path-pattern", |
| 311 | PathPatternConfig: { |
| 312 | Values: condition.pathPattern.values, |
| 313 | RegexValues: condition.pathPattern.regexValues, |
| 314 | }, |
| 315 | }); |
| 316 | } |
| 317 | if (condition.httpHeader) { |
| 318 | out.push({ |
| 319 | Field: "http-header", |
| 320 | HttpHeaderConfig: { |
| 321 | HttpHeaderName: condition.httpHeader.name, |
| 322 | Values: condition.httpHeader.values, |
| 323 | RegexValues: condition.httpHeader.regexValues, |
| 324 | }, |
| 325 | }); |
| 326 | } |
| 327 | if (condition.queryString) { |
| 328 | out.push({ |
| 329 | Field: "query-string", |
| 330 | QueryStringConfig: { |
| 331 | Values: condition.queryString.values.map((v) => ({ |
| 332 | Key: v.key, |
| 333 | Value: v.value, |
| 334 | })), |
| 335 | }, |
| 336 | }); |
| 337 | } |
| 338 | if (condition.httpRequestMethod) { |
| 339 | out.push({ |
| 340 | Field: "http-request-method", |
| 341 | HttpRequestMethodConfig: { |
| 342 | Values: condition.httpRequestMethod.values, |
| 343 | }, |
| 344 | }); |
| 345 | } |
| 346 | if (condition.sourceIp) { |
| 347 | out.push({ |
| 348 | Field: "source-ip", |
| 349 | SourceIpConfig: { Values: condition.sourceIp.values }, |
| 350 | }); |
| 351 | } |
| 352 | return out; |
| 353 | }); |
| 354 | |
| 355 | /** |
| 356 | * Project an observed ELBv2 attribute list (load balancer, target group, or |
| 357 | * listener) onto the keys declared in `props.attributes`. AWS reports every |
| 358 | * attribute, defaults included, so comparing only the declared keys keeps |
| 359 | * defaults out of drift. Returns `undefined` when nothing is declared. |
| 360 | */ |
| 361 | export const observedAttributes = ( |
| 362 | declared: Record<string, string> | undefined, |
| 363 | observed: readonly { Key?: string; Value?: string }[] | undefined, |
| 364 | ): Record<string, string> | undefined => { |
| 365 | if (!declared || Object.keys(declared).length === 0) return undefined; |
| 366 | const values = new Map( |
| 367 | (observed ?? []).flatMap((a) => |
| 368 | a.Key !== undefined && a.Value !== undefined ? [[a.Key, a.Value] as const] : [], |
| 369 | ), |
| 370 | ); |
| 371 | return Object.fromEntries( |
| 372 | Object.keys(declared).flatMap((key) => { |
| 373 | const value = values.get(key); |
| 374 | return value === undefined ? [] : [[key, value] as const]; |
| 375 | }), |
| 376 | ); |
| 377 | }; |
| 378 | |
| 379 | /** The declared attributes whose observed value differs, shaped for `modify*Attributes`. */ |
| 380 | export const attributeDelta = ( |
| 381 | declared: Record<string, string> | undefined, |
| 382 | observed: Record<string, string> | undefined, |
| 383 | ): { Key: string; Value: string }[] => |
| 384 | Object.entries(declared ?? {}).flatMap(([Key, Value]) => |
| 385 | observed?.[Key] === Value ? [] : [{ Key, Value }], |
| 386 | ); |