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, 04:45 UTC
| 1 | import * as transfer from "@distilled.cloud/aws/transfer"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | export class TransferUserUnexpectedResponse extends Data.TaggedError( |
| 12 | "TransferUserUnexpectedResponse", |
| 13 | )<{ |
| 14 | readonly message: string; |
| 15 | readonly cause?: unknown; |
| 16 | }> {} |
| 17 | |
| 18 | export interface UserProps { |
| 19 | /** |
| 20 | * ID of the Transfer Family server the user belongs to (e.g. |
| 21 | * `server.serverId`). Changing it replaces the user. |
| 22 | */ |
| 23 | serverId: string; |
| 24 | /** |
| 25 | * User name clients authenticate as. Must be 3-100 characters. Changing it |
| 26 | * replaces the user. |
| 27 | */ |
| 28 | userName: string; |
| 29 | /** |
| 30 | * IAM role ARN granting the user access to the storage backend (S3/EFS). |
| 31 | */ |
| 32 | role: string; |
| 33 | /** |
| 34 | * Landing directory when the user connects (PATH mode). |
| 35 | */ |
| 36 | homeDirectory?: string; |
| 37 | /** |
| 38 | * Whether the user sees the absolute bucket path (`PATH`) or a virtual |
| 39 | * chroot built from `homeDirectoryMappings` (`LOGICAL`). |
| 40 | * @default "PATH" |
| 41 | */ |
| 42 | homeDirectoryType?: transfer.HomeDirectoryType; |
| 43 | /** |
| 44 | * Virtual-to-actual path mappings for `LOGICAL` home-directory mode. |
| 45 | */ |
| 46 | homeDirectoryMappings?: transfer.HomeDirectoryMapEntry[]; |
| 47 | /** |
| 48 | * Inline session policy scoping the user's access, as a JSON string. |
| 49 | */ |
| 50 | policy?: string; |
| 51 | /** |
| 52 | * POSIX identity (uid/gid) applied to the user, required for EFS servers. |
| 53 | */ |
| 54 | posixProfile?: transfer.PosixProfile; |
| 55 | /** |
| 56 | * SSH public key body to register for the user at creation (service-managed |
| 57 | * identity provider). Additional keys can be managed out of band. |
| 58 | */ |
| 59 | sshPublicKeyBody?: string; |
| 60 | /** |
| 61 | * User-defined tags for the user. |
| 62 | */ |
| 63 | tags?: Record<string, string>; |
| 64 | } |
| 65 | |
| 66 | export interface User extends Resource< |
| 67 | "AWS.Transfer.User", |
| 68 | UserProps, |
| 69 | { |
| 70 | /** |
| 71 | * User name clients authenticate as. |
| 72 | */ |
| 73 | userName: string; |
| 74 | /** |
| 75 | * ID of the Transfer Family server the user belongs to. |
| 76 | */ |
| 77 | serverId: string; |
| 78 | /** |
| 79 | * ARN of the user. |
| 80 | */ |
| 81 | arn: string; |
| 82 | /** |
| 83 | * IAM role ARN granting the user access to the storage backend. |
| 84 | */ |
| 85 | role: string | undefined; |
| 86 | /** |
| 87 | * Landing directory when the user connects. |
| 88 | */ |
| 89 | homeDirectory: string | undefined; |
| 90 | /** |
| 91 | * Current tags reported for the user. |
| 92 | */ |
| 93 | tags: Record<string, string>; |
| 94 | }, |
| 95 | never, |
| 96 | Providers |
| 97 | > {} |
| 98 | |
| 99 | /** |
| 100 | * A user of an AWS Transfer Family server (service-managed identity |
| 101 | * provider). Users are free configuration objects attached to a |
| 102 | * {@link Server}; the server itself is what incurs hourly cost. |
| 103 | * ### Creating a User |
| 104 | * **Example:** Service-Managed SFTP User |
| 105 | * ```typescript |
| 106 | * const user = yield* User("Alice", { |
| 107 | * serverId: server.serverId, |
| 108 | * userName: "alice", |
| 109 | * role: transferRole.roleArn, |
| 110 | * homeDirectory: "/my-bucket/alice", |
| 111 | * sshPublicKeyBody: "ssh-ed25519 AAAA...", |
| 112 | * }); |
| 113 | * ``` |
| 114 | * |
| 115 | * @resource |
| 116 | */ |
| 117 | export const User = Resource<User>("AWS.Transfer.User"); |
| 118 | |
| 119 | const toTagRecord = ( |
| 120 | tags: Array<{ Key?: string; Value?: string }> | undefined, |
| 121 | ): Record<string, string> => |
| 122 | Object.fromEntries( |
| 123 | (tags ?? []) |
| 124 | .filter( |
| 125 | (tag): tag is { Key: string; Value: string } => |
| 126 | typeof tag.Key === "string" && typeof tag.Value === "string", |
| 127 | ) |
| 128 | .map((tag) => [tag.Key, tag.Value]), |
| 129 | ); |
| 130 | |
| 131 | export const UserProvider = () => |
| 132 | Provider.effect( |
| 133 | User, |
| 134 | Effect.gen(function* () { |
| 135 | const describe = Effect.fn(function* (serverId: string, userName: string) { |
| 136 | const response = yield* transfer |
| 137 | .describeUser({ ServerId: serverId, UserName: userName }) |
| 138 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 139 | return response?.User; |
| 140 | }); |
| 141 | |
| 142 | const readTags = Effect.fn(function* (arn: string) { |
| 143 | const response = yield* transfer |
| 144 | .listTagsForResource({ Arn: arn }) |
| 145 | .pipe(Effect.orElseSucceed(() => undefined)); |
| 146 | return toTagRecord(response?.Tags); |
| 147 | }); |
| 148 | |
| 149 | const toAttrs = Effect.fn(function* (serverId: string, user: transfer.DescribedUser) { |
| 150 | if (!user.UserName) { |
| 151 | return yield* new TransferUserUnexpectedResponse({ |
| 152 | message: "Transfer user is missing its UserName", |
| 153 | }); |
| 154 | } |
| 155 | return { |
| 156 | userName: user.UserName, |
| 157 | serverId, |
| 158 | arn: user.Arn, |
| 159 | role: user.Role, |
| 160 | homeDirectory: user.HomeDirectory, |
| 161 | tags: yield* readTags(user.Arn), |
| 162 | }; |
| 163 | }); |
| 164 | |
| 165 | return { |
| 166 | stables: ["userName", "serverId", "arn"], |
| 167 | |
| 168 | diff: Effect.fn(function* ({ olds, news }) { |
| 169 | if (!isResolved(news)) return undefined; |
| 170 | // ServerId and UserName are the resource identity. |
| 171 | if (news.serverId !== olds?.serverId || news.userName !== olds?.userName) { |
| 172 | return { action: "replace" } as const; |
| 173 | } |
| 174 | }), |
| 175 | |
| 176 | read: Effect.fn(function* ({ id, olds, output }) { |
| 177 | const serverId = output?.serverId ?? olds?.serverId; |
| 178 | const userName = output?.userName ?? olds?.userName; |
| 179 | if (!serverId || !userName) return undefined; |
| 180 | const user = yield* describe(serverId, userName); |
| 181 | if (!user?.UserName) return undefined; |
| 182 | const attrs = yield* toAttrs(serverId, user); |
| 183 | return (yield* hasAlchemyTags(id, attrs.tags)) ? attrs : Unowned(attrs); |
| 184 | }), |
| 185 | |
| 186 | reconcile: Effect.fn(function* ({ id, news, session }) { |
| 187 | const internalTags = yield* createInternalTags(id); |
| 188 | const desiredTags = { ...internalTags, ...news.tags }; |
| 189 | |
| 190 | // 1. Observe — cloud state is authoritative. |
| 191 | let observed = yield* describe(news.serverId, news.userName); |
| 192 | |
| 193 | // 2. Ensure — create if missing; tolerate AlreadyExists as a race. |
| 194 | if (observed === undefined) { |
| 195 | yield* transfer |
| 196 | .createUser({ |
| 197 | ServerId: news.serverId, |
| 198 | UserName: news.userName, |
| 199 | Role: news.role, |
| 200 | HomeDirectory: news.homeDirectory, |
| 201 | HomeDirectoryType: news.homeDirectoryType, |
| 202 | HomeDirectoryMappings: news.homeDirectoryMappings, |
| 203 | Policy: news.policy, |
| 204 | PosixProfile: news.posixProfile, |
| 205 | SshPublicKeyBody: news.sshPublicKeyBody, |
| 206 | Tags: Object.entries(desiredTags).map(([Key, Value]) => ({ |
| 207 | Key, |
| 208 | Value, |
| 209 | })), |
| 210 | }) |
| 211 | .pipe(Effect.catchTag("ResourceExistsException", () => Effect.void)); |
| 212 | } else { |
| 213 | // 3. Sync — push mutable configuration. |
| 214 | yield* transfer.updateUser({ |
| 215 | ServerId: news.serverId, |
| 216 | UserName: news.userName, |
| 217 | Role: news.role, |
| 218 | HomeDirectory: news.homeDirectory, |
| 219 | HomeDirectoryType: news.homeDirectoryType, |
| 220 | HomeDirectoryMappings: news.homeDirectoryMappings, |
| 221 | Policy: news.policy, |
| 222 | PosixProfile: news.posixProfile, |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | observed = yield* describe(news.serverId, news.userName); |
| 227 | if (!observed?.UserName) { |
| 228 | return yield* new TransferUserUnexpectedResponse({ |
| 229 | message: `Transfer user '${news.userName}' not found after reconcile`, |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 234 | const arn = observed.Arn; |
| 235 | const observedTags = yield* readTags(arn); |
| 236 | const { upsert, removed } = diffTags(observedTags, desiredTags); |
| 237 | if (upsert.length > 0) { |
| 238 | yield* transfer.tagResource({ Arn: arn, Tags: upsert }); |
| 239 | } |
| 240 | if (removed.length > 0) { |
| 241 | yield* transfer.untagResource({ Arn: arn, TagKeys: removed }); |
| 242 | } |
| 243 | |
| 244 | yield* session.note(`${news.serverId}/${news.userName}`); |
| 245 | return yield* toAttrs(news.serverId, observed); |
| 246 | }), |
| 247 | |
| 248 | delete: Effect.fn(function* ({ output }) { |
| 249 | yield* transfer |
| 250 | .deleteUser({ |
| 251 | ServerId: output.serverId, |
| 252 | UserName: output.userName, |
| 253 | }) |
| 254 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 255 | }), |
| 256 | |
| 257 | // Users are sub-resources keyed by their parent server; there is no |
| 258 | // account-wide enumeration without a server id. |
| 259 | list: () => Effect.succeed([]), |
| 260 | }; |
| 261 | }), |
| 262 | ); |