@intentius/chant-lexicon-gcp 0.124.34 → 0.124.35
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.
- package/dist/api/read-client.d.ts +7 -3
- package/dist/api/read-client.d.ts.map +1 -1
- package/dist/api/read-client.js +36 -15
- package/dist/composites/cloud-run-service.d.ts +2 -0
- package/dist/composites/cloud-run-service.d.ts.map +1 -1
- package/dist/composites/cloud-run-service.js +4 -1
- package/dist/deep-observe.d.ts.map +1 -1
- package/dist/deep-observe.js +124 -0
- package/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/op/activities/gcp-apply.d.ts +55 -18
- package/dist/op/activities/gcp-apply.d.ts.map +1 -1
- package/dist/op/activities/gcp-apply.js +93 -16
- package/dist/op/activities/gcp-compute.d.ts +56 -0
- package/dist/op/activities/gcp-compute.d.ts.map +1 -0
- package/dist/op/activities/gcp-compute.js +313 -0
- package/dist/op/activities/gcp-iam.d.ts +82 -0
- package/dist/op/activities/gcp-iam.d.ts.map +1 -0
- package/dist/op/activities/gcp-iam.js +215 -0
- package/package.json +2 -2
- package/src/api/read-client.ts +35 -16
- package/src/composites/cloud-run-service.ts +6 -0
- package/src/composites/composites.test.ts +7 -0
- package/src/deep-observe.test.ts +16 -1
- package/src/deep-observe.ts +124 -0
- package/src/describe-resources.test.ts +1 -1
- package/src/op/activities/gcp-apply.test.ts +37 -0
- package/src/op/activities/gcp-apply.ts +132 -22
- package/src/op/activities/gcp-compute.test.ts +212 -0
- package/src/op/activities/gcp-compute.ts +319 -0
- package/src/op/activities/gcp-iam.test.ts +152 -0
- package/src/op/activities/gcp-iam.ts +284 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IAMPolicyMember (#4233): one member in one role on one resource's IAM policy.
|
|
3
|
+
*
|
|
4
|
+
* GCP has no REST resource for a single binding member. The member lives inside
|
|
5
|
+
* the target's policy, read with `getIamPolicy` and written whole with
|
|
6
|
+
* `setIamPolicy`. So this mapper does not fit the GET-then-create plan the other
|
|
7
|
+
* kinds use: apply reads the policy, adds the member to the role's binding when
|
|
8
|
+
* it is missing and writes the policy back with its `etag`; delete removes it
|
|
9
|
+
* the same way. Other members and bindings are kept as read (a merge, never an
|
|
10
|
+
* overwrite), and a write that loses an `etag` race is retried.
|
|
11
|
+
*
|
|
12
|
+
* Targets are the kinds `resourceRef` can name that have an IAM policy chant can
|
|
13
|
+
* reach: Project, RunService, IAMServiceAccount, StorageBucket, PubSubTopic,
|
|
14
|
+
* PubSubSubscription and SecretManagerSecret. Any other target is reported as
|
|
15
|
+
* not attempted, the way a kind with no mapper is.
|
|
16
|
+
*/
|
|
17
|
+
import type { GcpHttp, GcpResource, ResourceMapper } from "./gcp-apply";
|
|
18
|
+
|
|
19
|
+
/** The CNRM IAMPolicyMember spec chant reads. */
|
|
20
|
+
export interface IamPolicyMemberSpec {
|
|
21
|
+
member?: string;
|
|
22
|
+
role?: string;
|
|
23
|
+
resourceRef?: { kind?: string; name?: string; external?: string; apiVersion?: string };
|
|
24
|
+
condition?: { title?: string; description?: string; expression?: string };
|
|
25
|
+
location?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface Binding {
|
|
29
|
+
role: string;
|
|
30
|
+
members?: string[];
|
|
31
|
+
condition?: { title?: string; description?: string; expression?: string };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Policy {
|
|
35
|
+
version?: number;
|
|
36
|
+
etag?: string;
|
|
37
|
+
bindings?: Binding[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Where a target's policy is read and written. */
|
|
41
|
+
export interface IamTarget {
|
|
42
|
+
/** Real-GCP host for the target's service. */
|
|
43
|
+
host: string;
|
|
44
|
+
/** The target's resource path, used in messages and in the read body. */
|
|
45
|
+
path: string;
|
|
46
|
+
get: (base: string) => { method: "GET" | "POST"; url: string; body?: unknown };
|
|
47
|
+
set: (base: string, policy: Policy) => { method: "POST" | "PUT"; url: string; body: unknown };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The default host this mapper reports; a target's own host replaces it when there is no endpoint override. */
|
|
51
|
+
export const IAM_DEFAULT_HOST = "https://iam.googleapis.com";
|
|
52
|
+
|
|
53
|
+
const enc = encodeURIComponent;
|
|
54
|
+
|
|
55
|
+
function region(env: NodeJS.ProcessEnv = process.env): string {
|
|
56
|
+
return env.GCP_REGION || "us-central1";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A `:getIamPolicy` / `:setIamPolicy` pair on a REST resource path (the Google API convention). */
|
|
60
|
+
function verbTarget(host: string, version: string, path: string, getMethod: "GET" | "POST"): IamTarget {
|
|
61
|
+
return {
|
|
62
|
+
host,
|
|
63
|
+
path,
|
|
64
|
+
get: (base) =>
|
|
65
|
+
getMethod === "GET"
|
|
66
|
+
? { method: "GET", url: `${base}/${version}/${path}:getIamPolicy?options.requestedPolicyVersion=3` }
|
|
67
|
+
: { method: "POST", url: `${base}/${version}/${path}:getIamPolicy`, body: { options: { requestedPolicyVersion: 3 } } },
|
|
68
|
+
set: (base, policy) => ({ method: "POST", url: `${base}/${version}/${path}:setIamPolicy`, body: { policy } }),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Resolve `resourceRef` to the target's policy endpoints, or undefined for a
|
|
74
|
+
* target chant cannot reach. `manifest` is the rest of the applied manifest, so
|
|
75
|
+
* a RunService named by `name` takes its region from its own spec. Pure.
|
|
76
|
+
*/
|
|
77
|
+
export function iamTarget(
|
|
78
|
+
spec: IamPolicyMemberSpec,
|
|
79
|
+
project: string,
|
|
80
|
+
manifest: readonly GcpResource[] = [],
|
|
81
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
82
|
+
): IamTarget | undefined {
|
|
83
|
+
const ref = spec.resourceRef ?? {};
|
|
84
|
+
const external = typeof ref.external === "string" && ref.external !== "" ? ref.external : undefined;
|
|
85
|
+
const name = typeof ref.name === "string" && ref.name !== "" ? ref.name : undefined;
|
|
86
|
+
switch (ref.kind) {
|
|
87
|
+
case "Project": {
|
|
88
|
+
const id = external ? external.replace(/^projects\//, "") : (name ?? project);
|
|
89
|
+
return verbTarget("https://cloudresourcemanager.googleapis.com", "v1", `projects/${enc(id)}`, "POST");
|
|
90
|
+
}
|
|
91
|
+
case "RunService": {
|
|
92
|
+
if (external) return verbTarget("https://run.googleapis.com", "v2", external, "GET");
|
|
93
|
+
if (!name) return undefined;
|
|
94
|
+
const declared = manifest.find((r) => r.kind === "RunService" && r.metadata?.name === name);
|
|
95
|
+
const location = (declared?.spec as { location?: unknown } | undefined)?.location;
|
|
96
|
+
const loc = typeof location === "string" ? location : (spec.location ?? region(env));
|
|
97
|
+
return verbTarget("https://run.googleapis.com", "v2", `projects/${enc(project)}/locations/${enc(loc)}/services/${enc(name)}`, "GET");
|
|
98
|
+
}
|
|
99
|
+
case "IAMServiceAccount": {
|
|
100
|
+
if (external?.startsWith("projects/")) return verbTarget(IAM_DEFAULT_HOST, "v1", external, "POST");
|
|
101
|
+
const email = external ?? (name ? `${name}@${project}.iam.gserviceaccount.com` : undefined);
|
|
102
|
+
if (!email) return undefined;
|
|
103
|
+
return verbTarget(IAM_DEFAULT_HOST, "v1", `projects/${enc(project)}/serviceAccounts/${email}`, "POST");
|
|
104
|
+
}
|
|
105
|
+
case "PubSubTopic":
|
|
106
|
+
case "PubSubSubscription": {
|
|
107
|
+
const collection = ref.kind === "PubSubTopic" ? "topics" : "subscriptions";
|
|
108
|
+
const path = external ?? (name ? `projects/${enc(project)}/${collection}/${enc(name)}` : undefined);
|
|
109
|
+
return path ? verbTarget("https://pubsub.googleapis.com", "v1", path, "GET") : undefined;
|
|
110
|
+
}
|
|
111
|
+
case "SecretManagerSecret": {
|
|
112
|
+
const path = external ?? (name ? `projects/${enc(project)}/secrets/${enc(name)}` : undefined);
|
|
113
|
+
return path ? verbTarget("https://secretmanager.googleapis.com", "v1", path, "GET") : undefined;
|
|
114
|
+
}
|
|
115
|
+
case "StorageBucket": {
|
|
116
|
+
const bucket = external ? external.replace(/^(gs:\/\/|projects\/_\/buckets\/)/, "") : name;
|
|
117
|
+
if (!bucket) return undefined;
|
|
118
|
+
// GCS has its own IAM shape: GET and PUT on `b/<bucket>/iam`, with the policy as the body.
|
|
119
|
+
const url = (base: string) => `${base}/storage/v1/b/${enc(bucket)}/iam`;
|
|
120
|
+
return {
|
|
121
|
+
host: "https://storage.googleapis.com",
|
|
122
|
+
path: `b/${bucket}`,
|
|
123
|
+
get: (base) => ({ method: "GET", url: `${url(base)}?optionsRequestedPolicyVersion=3` }),
|
|
124
|
+
set: (base, policy) => ({ method: "PUT", url: url(base), body: policy }),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
default:
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function sameCondition(a: Binding["condition"], b: IamPolicyMemberSpec["condition"]): boolean {
|
|
133
|
+
if (!a && !b) return true;
|
|
134
|
+
if (!a || !b) return false;
|
|
135
|
+
return (a.expression ?? "") === (b.expression ?? "") && (a.title ?? "") === (b.title ?? "");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function findBinding(policy: Policy, spec: IamPolicyMemberSpec): Binding | undefined {
|
|
139
|
+
return policy.bindings?.find((b) => b.role === spec.role && sameCondition(b.condition, spec.condition));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** True when the policy grants the spec's member in its role (and condition). Pure. */
|
|
143
|
+
export function policyHasMember(policy: Policy, spec: IamPolicyMemberSpec): boolean {
|
|
144
|
+
return findBinding(policy, spec)?.members?.includes(spec.member ?? "") ?? false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The policy with the spec's member added to its binding, every other binding kept. Pure. */
|
|
148
|
+
export function withMember(policy: Policy, spec: IamPolicyMemberSpec): Policy {
|
|
149
|
+
const member = spec.member as string;
|
|
150
|
+
const bindings = (policy.bindings ?? []).map((b) => ({ ...b, members: [...(b.members ?? [])] }));
|
|
151
|
+
const existing = bindings.find((b) => b.role === spec.role && sameCondition(b.condition, spec.condition));
|
|
152
|
+
if (existing) {
|
|
153
|
+
if (!existing.members.includes(member)) existing.members.push(member);
|
|
154
|
+
} else {
|
|
155
|
+
bindings.push({ role: spec.role as string, members: [member], ...(spec.condition ? { condition: spec.condition } : {}) });
|
|
156
|
+
}
|
|
157
|
+
// A conditional binding needs policy version 3; otherwise keep what was read.
|
|
158
|
+
const version = spec.condition || bindings.some((b) => b.condition) ? 3 : policy.version;
|
|
159
|
+
return { ...policy, bindings, ...(version !== undefined ? { version } : {}) };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The policy with the spec's member removed; a binding left with no members is dropped. Pure. */
|
|
163
|
+
export function withoutMember(policy: Policy, spec: IamPolicyMemberSpec): Policy {
|
|
164
|
+
const bindings = (policy.bindings ?? [])
|
|
165
|
+
.map((b) =>
|
|
166
|
+
b.role === spec.role && sameCondition(b.condition, spec.condition)
|
|
167
|
+
? { ...b, members: (b.members ?? []).filter((m) => m !== spec.member) }
|
|
168
|
+
: b,
|
|
169
|
+
)
|
|
170
|
+
.filter((b) => (b.members ?? []).length > 0);
|
|
171
|
+
return { ...policy, bindings };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function specOf(resource: GcpResource): IamPolicyMemberSpec {
|
|
175
|
+
const spec = (resource.spec ?? {}) as IamPolicyMemberSpec;
|
|
176
|
+
const name = resource.metadata?.name ?? "?";
|
|
177
|
+
if (!spec.member) throw new Error(`IAMPolicyMember ${name} has no spec.member`);
|
|
178
|
+
if (!spec.role) throw new Error(`IAMPolicyMember ${name} has no spec.role`);
|
|
179
|
+
return spec;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function targetOf(resource: GcpResource, ctx: IamCtx, env?: NodeJS.ProcessEnv): { target: IamTarget; base: string } {
|
|
183
|
+
const target = iamTarget(specOf(resource), ctx.project, ctx.manifest, env);
|
|
184
|
+
if (!target) {
|
|
185
|
+
const ref = (resource.spec as IamPolicyMemberSpec | undefined)?.resourceRef;
|
|
186
|
+
throw new Error(`IAMPolicyMember ${resource.metadata?.name ?? "?"}: no IAM policy endpoint for resourceRef kind ${ref?.kind ?? "(none)"}`);
|
|
187
|
+
}
|
|
188
|
+
// No endpoint override: each target's own service host. An override (floci-gcp) serves every service.
|
|
189
|
+
const base = ctx.base === IAM_DEFAULT_HOST ? target.host : ctx.base;
|
|
190
|
+
return { target, base };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
interface IamCtx {
|
|
194
|
+
base: string;
|
|
195
|
+
project: string;
|
|
196
|
+
manifest?: readonly GcpResource[];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function readPolicy(target: IamTarget, base: string, http: GcpHttp, signal?: AbortSignal): Promise<{ status: number; text: string; policy?: Policy }> {
|
|
200
|
+
const req = target.get(base);
|
|
201
|
+
const res = await http(req.method, req.url, req.body, signal);
|
|
202
|
+
if (res.status >= 300) return res;
|
|
203
|
+
let policy: Policy;
|
|
204
|
+
try {
|
|
205
|
+
policy = JSON.parse(res.text) as Policy;
|
|
206
|
+
} catch {
|
|
207
|
+
policy = {};
|
|
208
|
+
}
|
|
209
|
+
return { ...res, policy };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** How many times a write that loses an `etag` race (409, or 412 on GCS) is retried. */
|
|
213
|
+
const ETAG_ATTEMPTS = 3;
|
|
214
|
+
|
|
215
|
+
async function writeWith(
|
|
216
|
+
resource: GcpResource,
|
|
217
|
+
ctx: IamCtx,
|
|
218
|
+
http: GcpHttp,
|
|
219
|
+
signal: AbortSignal | undefined,
|
|
220
|
+
change: (policy: Policy, spec: IamPolicyMemberSpec) => Policy | undefined,
|
|
221
|
+
): Promise<boolean> {
|
|
222
|
+
const spec = specOf(resource);
|
|
223
|
+
const { target, base } = targetOf(resource, ctx);
|
|
224
|
+
const name = resource.metadata?.name ?? "?";
|
|
225
|
+
for (let attempt = 1; ; attempt++) {
|
|
226
|
+
const read = await readPolicy(target, base, http, signal);
|
|
227
|
+
if (!read.policy) throw new Error(`IAMPolicyMember ${name}: reading the policy of ${target.path} failed (${read.status}): ${read.text}`);
|
|
228
|
+
const next = change(read.policy, spec);
|
|
229
|
+
if (!next) return false;
|
|
230
|
+
const req = target.set(base, next);
|
|
231
|
+
const res = await http(req.method, req.url, req.body, signal);
|
|
232
|
+
if (res.status < 300) return true;
|
|
233
|
+
if ((res.status === 409 || res.status === 412) && attempt < ETAG_ATTEMPTS) continue;
|
|
234
|
+
throw new Error(`IAMPolicyMember ${name}: writing the policy of ${target.path} failed (${res.status}): ${res.text}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export const iamPolicyMemberMapper: ResourceMapper = {
|
|
239
|
+
kind: "IAMPolicyMember",
|
|
240
|
+
defaultHost: IAM_DEFAULT_HOST,
|
|
241
|
+
accepts(resource, ctx) {
|
|
242
|
+
return iamTarget((resource.spec ?? {}) as IamPolicyMemberSpec, ctx.project, ctx.manifest) !== undefined;
|
|
243
|
+
},
|
|
244
|
+
plan(resource, ctx) {
|
|
245
|
+
// Only the read is a single URL here; apply and delete go through `custom`.
|
|
246
|
+
const { target, base } = targetOf(resource, ctx);
|
|
247
|
+
const get = target.get(base);
|
|
248
|
+
return { getUrl: get.url, create: { method: "POST", url: get.url, body: get.body ?? {} } };
|
|
249
|
+
},
|
|
250
|
+
custom: {
|
|
251
|
+
async apply(resource, ctx, http, signal) {
|
|
252
|
+
const created = await writeWith(resource, ctx, http, signal, (policy, spec) =>
|
|
253
|
+
policyHasMember(policy, spec) ? undefined : withMember(policy, spec),
|
|
254
|
+
);
|
|
255
|
+
return { created, updated: false };
|
|
256
|
+
},
|
|
257
|
+
async delete(resource, ctx, http, signal) {
|
|
258
|
+
return writeWith(resource, ctx, http, signal, (policy, spec) =>
|
|
259
|
+
policyHasMember(policy, spec) ? withoutMember(policy, spec) : undefined,
|
|
260
|
+
);
|
|
261
|
+
},
|
|
262
|
+
async read(resource, ctx, http, signal) {
|
|
263
|
+
const spec = specOf(resource);
|
|
264
|
+
const { target, base } = targetOf(resource, ctx);
|
|
265
|
+
const read = await readPolicy(target, base, http, signal);
|
|
266
|
+
if (!read.policy) return read;
|
|
267
|
+
// The binding member is the resource: absent from the policy reads as absent.
|
|
268
|
+
if (!policyHasMember(read.policy, spec)) return { status: 404, text: `${spec.member} has no ${spec.role} on ${target.path}` };
|
|
269
|
+
const binding = findBinding(read.policy, spec);
|
|
270
|
+
return {
|
|
271
|
+
status: 200,
|
|
272
|
+
// `resourceRef` is the declared one: the policy read is that target's, so the binding is on it.
|
|
273
|
+
text: JSON.stringify({
|
|
274
|
+
member: spec.member,
|
|
275
|
+
role: spec.role,
|
|
276
|
+
resource: target.path,
|
|
277
|
+
...(spec.resourceRef ? { resourceRef: spec.resourceRef } : {}),
|
|
278
|
+
...(binding?.condition ? { condition: binding.condition } : {}),
|
|
279
|
+
...(read.policy.etag ? { etag: read.policy.etag } : {}),
|
|
280
|
+
}),
|
|
281
|
+
};
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
};
|