@intentius/chant-lexicon-gcp 0.124.34 → 0.124.36
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 +8 -3
- package/dist/api/read-client.d.ts.map +1 -1
- package/dist/api/read-client.js +37 -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 +176 -0
- package/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/dist/op/activities/gcp-apply.d.ts +62 -18
- package/dist/op/activities/gcp-apply.d.ts.map +1 -1
- package/dist/op/activities/gcp-apply.js +128 -16
- package/dist/op/activities/gcp-compute.d.ts +66 -0
- package/dist/op/activities/gcp-compute.d.ts.map +1 -0
- package/dist/op/activities/gcp-compute.js +349 -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/dist/serializer.d.ts +6 -0
- package/dist/serializer.d.ts.map +1 -1
- package/dist/serializer.js +21 -9
- package/package.json +2 -2
- package/src/api/read-client.ts +36 -16
- package/src/composites/cloud-run-service.ts +6 -0
- package/src/composites/composites.test.ts +7 -0
- package/src/deep-observe.test.ts +42 -1
- package/src/deep-observe.ts +171 -0
- package/src/describe-resources.test.ts +1 -1
- package/src/op/activities/gcp-apply.test.ts +75 -0
- package/src/op/activities/gcp-apply.ts +167 -22
- package/src/op/activities/gcp-compute.test.ts +306 -0
- package/src/op/activities/gcp-compute.ts +354 -0
- package/src/op/activities/gcp-iam.test.ts +152 -0
- package/src/op/activities/gcp-iam.ts +284 -0
- package/src/serializer.test.ts +26 -0
- package/src/serializer.ts +22 -9
|
@@ -0,0 +1,82 @@
|
|
|
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 { GcpResource, ResourceMapper } from "./gcp-apply.js";
|
|
18
|
+
/** The CNRM IAMPolicyMember spec chant reads. */
|
|
19
|
+
export interface IamPolicyMemberSpec {
|
|
20
|
+
member?: string;
|
|
21
|
+
role?: string;
|
|
22
|
+
resourceRef?: {
|
|
23
|
+
kind?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
external?: string;
|
|
26
|
+
apiVersion?: string;
|
|
27
|
+
};
|
|
28
|
+
condition?: {
|
|
29
|
+
title?: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
expression?: string;
|
|
32
|
+
};
|
|
33
|
+
location?: string;
|
|
34
|
+
}
|
|
35
|
+
interface Binding {
|
|
36
|
+
role: string;
|
|
37
|
+
members?: string[];
|
|
38
|
+
condition?: {
|
|
39
|
+
title?: string;
|
|
40
|
+
description?: string;
|
|
41
|
+
expression?: string;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
interface Policy {
|
|
45
|
+
version?: number;
|
|
46
|
+
etag?: string;
|
|
47
|
+
bindings?: Binding[];
|
|
48
|
+
}
|
|
49
|
+
/** Where a target's policy is read and written. */
|
|
50
|
+
export interface IamTarget {
|
|
51
|
+
/** Real-GCP host for the target's service. */
|
|
52
|
+
host: string;
|
|
53
|
+
/** The target's resource path, used in messages and in the read body. */
|
|
54
|
+
path: string;
|
|
55
|
+
get: (base: string) => {
|
|
56
|
+
method: "GET" | "POST";
|
|
57
|
+
url: string;
|
|
58
|
+
body?: unknown;
|
|
59
|
+
};
|
|
60
|
+
set: (base: string, policy: Policy) => {
|
|
61
|
+
method: "POST" | "PUT";
|
|
62
|
+
url: string;
|
|
63
|
+
body: unknown;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** The default host this mapper reports; a target's own host replaces it when there is no endpoint override. */
|
|
67
|
+
export declare const IAM_DEFAULT_HOST = "https://iam.googleapis.com";
|
|
68
|
+
/**
|
|
69
|
+
* Resolve `resourceRef` to the target's policy endpoints, or undefined for a
|
|
70
|
+
* target chant cannot reach. `manifest` is the rest of the applied manifest, so
|
|
71
|
+
* a RunService named by `name` takes its region from its own spec. Pure.
|
|
72
|
+
*/
|
|
73
|
+
export declare function iamTarget(spec: IamPolicyMemberSpec, project: string, manifest?: readonly GcpResource[], env?: NodeJS.ProcessEnv): IamTarget | undefined;
|
|
74
|
+
/** True when the policy grants the spec's member in its role (and condition). Pure. */
|
|
75
|
+
export declare function policyHasMember(policy: Policy, spec: IamPolicyMemberSpec): boolean;
|
|
76
|
+
/** The policy with the spec's member added to its binding, every other binding kept. Pure. */
|
|
77
|
+
export declare function withMember(policy: Policy, spec: IamPolicyMemberSpec): Policy;
|
|
78
|
+
/** The policy with the spec's member removed; a binding left with no members is dropped. Pure. */
|
|
79
|
+
export declare function withoutMember(policy: Policy, spec: IamPolicyMemberSpec): Policy;
|
|
80
|
+
export declare const iamPolicyMemberMapper: ResourceMapper;
|
|
81
|
+
export {};
|
|
82
|
+
//# sourceMappingURL=gcp-iam.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gcp-iam.d.ts","sourceRoot":"","sources":["../../../src/op/activities/gcp-iam.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAW,WAAW,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAExE,iDAAiD;AACjD,MAAM,WAAW,mBAAmB;IAClC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACvF,SAAS,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,UAAU,OAAO;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC3E;AAED,UAAU,MAAM;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;CACtB;AAED,mDAAmD;AACnD,MAAM,WAAW,SAAS;IACxB,8CAA8C;IAC9C,IAAI,EAAE,MAAM,CAAC;IACb,yEAAyE;IACzE,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK;QAAE,MAAM,EAAE,KAAK,GAAG,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IAC/E,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK;QAAE,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,OAAO,CAAA;KAAE,CAAC;CAC/F;AAED,gHAAgH;AAChH,eAAO,MAAM,gBAAgB,+BAA+B,CAAC;AAqB7D;;;;GAIG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,mBAAmB,EACzB,OAAO,EAAE,MAAM,EACf,QAAQ,GAAE,SAAS,WAAW,EAAO,EACrC,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,SAAS,GAAG,SAAS,CAgDvB;AAYD,uFAAuF;AACvF,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,mBAAmB,GAAG,OAAO,CAElF;AAED,8FAA8F;AAC9F,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,mBAAmB,GAAG,MAAM,CAY5E;AAED,kGAAkG;AAClG,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,mBAAmB,GAAG,MAAM,CAS/E;AAkED,eAAO,MAAM,qBAAqB,EAAE,cA8CnC,CAAC"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/** The default host this mapper reports; a target's own host replaces it when there is no endpoint override. */
|
|
2
|
+
export const IAM_DEFAULT_HOST = "https://iam.googleapis.com";
|
|
3
|
+
const enc = encodeURIComponent;
|
|
4
|
+
function region(env = process.env) {
|
|
5
|
+
return env.GCP_REGION || "us-central1";
|
|
6
|
+
}
|
|
7
|
+
/** A `:getIamPolicy` / `:setIamPolicy` pair on a REST resource path (the Google API convention). */
|
|
8
|
+
function verbTarget(host, version, path, getMethod) {
|
|
9
|
+
return {
|
|
10
|
+
host,
|
|
11
|
+
path,
|
|
12
|
+
get: (base) => getMethod === "GET"
|
|
13
|
+
? { method: "GET", url: `${base}/${version}/${path}:getIamPolicy?options.requestedPolicyVersion=3` }
|
|
14
|
+
: { method: "POST", url: `${base}/${version}/${path}:getIamPolicy`, body: { options: { requestedPolicyVersion: 3 } } },
|
|
15
|
+
set: (base, policy) => ({ method: "POST", url: `${base}/${version}/${path}:setIamPolicy`, body: { policy } }),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Resolve `resourceRef` to the target's policy endpoints, or undefined for a
|
|
20
|
+
* target chant cannot reach. `manifest` is the rest of the applied manifest, so
|
|
21
|
+
* a RunService named by `name` takes its region from its own spec. Pure.
|
|
22
|
+
*/
|
|
23
|
+
export function iamTarget(spec, project, manifest = [], env = process.env) {
|
|
24
|
+
const ref = spec.resourceRef ?? {};
|
|
25
|
+
const external = typeof ref.external === "string" && ref.external !== "" ? ref.external : undefined;
|
|
26
|
+
const name = typeof ref.name === "string" && ref.name !== "" ? ref.name : undefined;
|
|
27
|
+
switch (ref.kind) {
|
|
28
|
+
case "Project": {
|
|
29
|
+
const id = external ? external.replace(/^projects\//, "") : (name ?? project);
|
|
30
|
+
return verbTarget("https://cloudresourcemanager.googleapis.com", "v1", `projects/${enc(id)}`, "POST");
|
|
31
|
+
}
|
|
32
|
+
case "RunService": {
|
|
33
|
+
if (external)
|
|
34
|
+
return verbTarget("https://run.googleapis.com", "v2", external, "GET");
|
|
35
|
+
if (!name)
|
|
36
|
+
return undefined;
|
|
37
|
+
const declared = manifest.find((r) => r.kind === "RunService" && r.metadata?.name === name);
|
|
38
|
+
const location = declared?.spec?.location;
|
|
39
|
+
const loc = typeof location === "string" ? location : (spec.location ?? region(env));
|
|
40
|
+
return verbTarget("https://run.googleapis.com", "v2", `projects/${enc(project)}/locations/${enc(loc)}/services/${enc(name)}`, "GET");
|
|
41
|
+
}
|
|
42
|
+
case "IAMServiceAccount": {
|
|
43
|
+
if (external?.startsWith("projects/"))
|
|
44
|
+
return verbTarget(IAM_DEFAULT_HOST, "v1", external, "POST");
|
|
45
|
+
const email = external ?? (name ? `${name}@${project}.iam.gserviceaccount.com` : undefined);
|
|
46
|
+
if (!email)
|
|
47
|
+
return undefined;
|
|
48
|
+
return verbTarget(IAM_DEFAULT_HOST, "v1", `projects/${enc(project)}/serviceAccounts/${email}`, "POST");
|
|
49
|
+
}
|
|
50
|
+
case "PubSubTopic":
|
|
51
|
+
case "PubSubSubscription": {
|
|
52
|
+
const collection = ref.kind === "PubSubTopic" ? "topics" : "subscriptions";
|
|
53
|
+
const path = external ?? (name ? `projects/${enc(project)}/${collection}/${enc(name)}` : undefined);
|
|
54
|
+
return path ? verbTarget("https://pubsub.googleapis.com", "v1", path, "GET") : undefined;
|
|
55
|
+
}
|
|
56
|
+
case "SecretManagerSecret": {
|
|
57
|
+
const path = external ?? (name ? `projects/${enc(project)}/secrets/${enc(name)}` : undefined);
|
|
58
|
+
return path ? verbTarget("https://secretmanager.googleapis.com", "v1", path, "GET") : undefined;
|
|
59
|
+
}
|
|
60
|
+
case "StorageBucket": {
|
|
61
|
+
const bucket = external ? external.replace(/^(gs:\/\/|projects\/_\/buckets\/)/, "") : name;
|
|
62
|
+
if (!bucket)
|
|
63
|
+
return undefined;
|
|
64
|
+
// GCS has its own IAM shape: GET and PUT on `b/<bucket>/iam`, with the policy as the body.
|
|
65
|
+
const url = (base) => `${base}/storage/v1/b/${enc(bucket)}/iam`;
|
|
66
|
+
return {
|
|
67
|
+
host: "https://storage.googleapis.com",
|
|
68
|
+
path: `b/${bucket}`,
|
|
69
|
+
get: (base) => ({ method: "GET", url: `${url(base)}?optionsRequestedPolicyVersion=3` }),
|
|
70
|
+
set: (base, policy) => ({ method: "PUT", url: url(base), body: policy }),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
default:
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function sameCondition(a, b) {
|
|
78
|
+
if (!a && !b)
|
|
79
|
+
return true;
|
|
80
|
+
if (!a || !b)
|
|
81
|
+
return false;
|
|
82
|
+
return (a.expression ?? "") === (b.expression ?? "") && (a.title ?? "") === (b.title ?? "");
|
|
83
|
+
}
|
|
84
|
+
function findBinding(policy, spec) {
|
|
85
|
+
return policy.bindings?.find((b) => b.role === spec.role && sameCondition(b.condition, spec.condition));
|
|
86
|
+
}
|
|
87
|
+
/** True when the policy grants the spec's member in its role (and condition). Pure. */
|
|
88
|
+
export function policyHasMember(policy, spec) {
|
|
89
|
+
return findBinding(policy, spec)?.members?.includes(spec.member ?? "") ?? false;
|
|
90
|
+
}
|
|
91
|
+
/** The policy with the spec's member added to its binding, every other binding kept. Pure. */
|
|
92
|
+
export function withMember(policy, spec) {
|
|
93
|
+
const member = spec.member;
|
|
94
|
+
const bindings = (policy.bindings ?? []).map((b) => ({ ...b, members: [...(b.members ?? [])] }));
|
|
95
|
+
const existing = bindings.find((b) => b.role === spec.role && sameCondition(b.condition, spec.condition));
|
|
96
|
+
if (existing) {
|
|
97
|
+
if (!existing.members.includes(member))
|
|
98
|
+
existing.members.push(member);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
bindings.push({ role: spec.role, members: [member], ...(spec.condition ? { condition: spec.condition } : {}) });
|
|
102
|
+
}
|
|
103
|
+
// A conditional binding needs policy version 3; otherwise keep what was read.
|
|
104
|
+
const version = spec.condition || bindings.some((b) => b.condition) ? 3 : policy.version;
|
|
105
|
+
return { ...policy, bindings, ...(version !== undefined ? { version } : {}) };
|
|
106
|
+
}
|
|
107
|
+
/** The policy with the spec's member removed; a binding left with no members is dropped. Pure. */
|
|
108
|
+
export function withoutMember(policy, spec) {
|
|
109
|
+
const bindings = (policy.bindings ?? [])
|
|
110
|
+
.map((b) => b.role === spec.role && sameCondition(b.condition, spec.condition)
|
|
111
|
+
? { ...b, members: (b.members ?? []).filter((m) => m !== spec.member) }
|
|
112
|
+
: b)
|
|
113
|
+
.filter((b) => (b.members ?? []).length > 0);
|
|
114
|
+
return { ...policy, bindings };
|
|
115
|
+
}
|
|
116
|
+
function specOf(resource) {
|
|
117
|
+
const spec = (resource.spec ?? {});
|
|
118
|
+
const name = resource.metadata?.name ?? "?";
|
|
119
|
+
if (!spec.member)
|
|
120
|
+
throw new Error(`IAMPolicyMember ${name} has no spec.member`);
|
|
121
|
+
if (!spec.role)
|
|
122
|
+
throw new Error(`IAMPolicyMember ${name} has no spec.role`);
|
|
123
|
+
return spec;
|
|
124
|
+
}
|
|
125
|
+
function targetOf(resource, ctx, env) {
|
|
126
|
+
const target = iamTarget(specOf(resource), ctx.project, ctx.manifest, env);
|
|
127
|
+
if (!target) {
|
|
128
|
+
const ref = resource.spec?.resourceRef;
|
|
129
|
+
throw new Error(`IAMPolicyMember ${resource.metadata?.name ?? "?"}: no IAM policy endpoint for resourceRef kind ${ref?.kind ?? "(none)"}`);
|
|
130
|
+
}
|
|
131
|
+
// No endpoint override: each target's own service host. An override (floci-gcp) serves every service.
|
|
132
|
+
const base = ctx.base === IAM_DEFAULT_HOST ? target.host : ctx.base;
|
|
133
|
+
return { target, base };
|
|
134
|
+
}
|
|
135
|
+
async function readPolicy(target, base, http, signal) {
|
|
136
|
+
const req = target.get(base);
|
|
137
|
+
const res = await http(req.method, req.url, req.body, signal);
|
|
138
|
+
if (res.status >= 300)
|
|
139
|
+
return res;
|
|
140
|
+
let policy;
|
|
141
|
+
try {
|
|
142
|
+
policy = JSON.parse(res.text);
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
policy = {};
|
|
146
|
+
}
|
|
147
|
+
return { ...res, policy };
|
|
148
|
+
}
|
|
149
|
+
/** How many times a write that loses an `etag` race (409, or 412 on GCS) is retried. */
|
|
150
|
+
const ETAG_ATTEMPTS = 3;
|
|
151
|
+
async function writeWith(resource, ctx, http, signal, change) {
|
|
152
|
+
const spec = specOf(resource);
|
|
153
|
+
const { target, base } = targetOf(resource, ctx);
|
|
154
|
+
const name = resource.metadata?.name ?? "?";
|
|
155
|
+
for (let attempt = 1;; attempt++) {
|
|
156
|
+
const read = await readPolicy(target, base, http, signal);
|
|
157
|
+
if (!read.policy)
|
|
158
|
+
throw new Error(`IAMPolicyMember ${name}: reading the policy of ${target.path} failed (${read.status}): ${read.text}`);
|
|
159
|
+
const next = change(read.policy, spec);
|
|
160
|
+
if (!next)
|
|
161
|
+
return false;
|
|
162
|
+
const req = target.set(base, next);
|
|
163
|
+
const res = await http(req.method, req.url, req.body, signal);
|
|
164
|
+
if (res.status < 300)
|
|
165
|
+
return true;
|
|
166
|
+
if ((res.status === 409 || res.status === 412) && attempt < ETAG_ATTEMPTS)
|
|
167
|
+
continue;
|
|
168
|
+
throw new Error(`IAMPolicyMember ${name}: writing the policy of ${target.path} failed (${res.status}): ${res.text}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
export const iamPolicyMemberMapper = {
|
|
172
|
+
kind: "IAMPolicyMember",
|
|
173
|
+
defaultHost: IAM_DEFAULT_HOST,
|
|
174
|
+
accepts(resource, ctx) {
|
|
175
|
+
return iamTarget((resource.spec ?? {}), ctx.project, ctx.manifest) !== undefined;
|
|
176
|
+
},
|
|
177
|
+
plan(resource, ctx) {
|
|
178
|
+
// Only the read is a single URL here; apply and delete go through `custom`.
|
|
179
|
+
const { target, base } = targetOf(resource, ctx);
|
|
180
|
+
const get = target.get(base);
|
|
181
|
+
return { getUrl: get.url, create: { method: "POST", url: get.url, body: get.body ?? {} } };
|
|
182
|
+
},
|
|
183
|
+
custom: {
|
|
184
|
+
async apply(resource, ctx, http, signal) {
|
|
185
|
+
const created = await writeWith(resource, ctx, http, signal, (policy, spec) => policyHasMember(policy, spec) ? undefined : withMember(policy, spec));
|
|
186
|
+
return { created, updated: false };
|
|
187
|
+
},
|
|
188
|
+
async delete(resource, ctx, http, signal) {
|
|
189
|
+
return writeWith(resource, ctx, http, signal, (policy, spec) => policyHasMember(policy, spec) ? withoutMember(policy, spec) : undefined);
|
|
190
|
+
},
|
|
191
|
+
async read(resource, ctx, http, signal) {
|
|
192
|
+
const spec = specOf(resource);
|
|
193
|
+
const { target, base } = targetOf(resource, ctx);
|
|
194
|
+
const read = await readPolicy(target, base, http, signal);
|
|
195
|
+
if (!read.policy)
|
|
196
|
+
return read;
|
|
197
|
+
// The binding member is the resource: absent from the policy reads as absent.
|
|
198
|
+
if (!policyHasMember(read.policy, spec))
|
|
199
|
+
return { status: 404, text: `${spec.member} has no ${spec.role} on ${target.path}` };
|
|
200
|
+
const binding = findBinding(read.policy, spec);
|
|
201
|
+
return {
|
|
202
|
+
status: 200,
|
|
203
|
+
// `resourceRef` is the declared one: the policy read is that target's, so the binding is on it.
|
|
204
|
+
text: JSON.stringify({
|
|
205
|
+
member: spec.member,
|
|
206
|
+
role: spec.role,
|
|
207
|
+
resource: target.path,
|
|
208
|
+
...(spec.resourceRef ? { resourceRef: spec.resourceRef } : {}),
|
|
209
|
+
...(binding?.condition ? { condition: binding.condition } : {}),
|
|
210
|
+
...(read.policy.etag ? { etag: read.policy.etag } : {}),
|
|
211
|
+
}),
|
|
212
|
+
};
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
};
|
package/dist/serializer.d.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Converts Chant declarables to multi-document K8s YAML output
|
|
5
5
|
* for Config Connector resources with apiVersion, kind, metadata, and spec.
|
|
6
6
|
*/
|
|
7
|
+
import type { Declarable } from "@intentius/chant/declarable";
|
|
7
8
|
import type { Serializer } from "@intentius/chant/serializer";
|
|
8
9
|
/**
|
|
9
10
|
* Resolve entityType to apiVersion and kind.
|
|
@@ -17,6 +18,11 @@ export declare function resolveGVK(entityType: string): {
|
|
|
17
18
|
apiVersion: string;
|
|
18
19
|
kind: string;
|
|
19
20
|
} | null;
|
|
21
|
+
/**
|
|
22
|
+
* The metadata.name a resource is written with: a string `metadata.name` in its
|
|
23
|
+
* props, else its logical id in kebab case.
|
|
24
|
+
*/
|
|
25
|
+
export declare function cnrmName(logical: string, entity: Declarable): string;
|
|
20
26
|
/**
|
|
21
27
|
* GCP Config Connector YAML serializer implementation.
|
|
22
28
|
*/
|
package/dist/serializer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../src/serializer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../src/serializer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAE9D,OAAO,KAAK,EAAE,UAAU,EAAsC,MAAM,6BAA6B,CAAC;AAiClG;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAW1F;AA+CD;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,GAAG,MAAM,CAIpE;AA4BD;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,UAiG3B,CAAC"}
|
package/dist/serializer.js
CHANGED
|
@@ -62,15 +62,15 @@ function deriveGVKFromType(entityType) {
|
|
|
62
62
|
* GCP visitor for the generic serializer walker.
|
|
63
63
|
*/
|
|
64
64
|
function gcpVisitor(entityNames) {
|
|
65
|
+
// Config Connector resolves a `*Ref: { name }` by the target's metadata.name,
|
|
66
|
+
// which is its declared name or else the kebab-cased logical id (#4242).
|
|
67
|
+
const cnrmNames = new Map();
|
|
68
|
+
for (const [entity, logical] of entityNames)
|
|
69
|
+
cnrmNames.set(logical, cnrmName(logical, entity));
|
|
70
|
+
const resolve = (logical) => cnrmNames.get(logical) ?? logical;
|
|
65
71
|
return {
|
|
66
|
-
attrRef: (name, _attr) =>
|
|
67
|
-
|
|
68
|
-
return name;
|
|
69
|
-
},
|
|
70
|
-
resourceRef: (name) => {
|
|
71
|
-
// Config Connector uses resourceRef pattern: { name: "resource-name" }
|
|
72
|
-
return { name };
|
|
73
|
-
},
|
|
72
|
+
attrRef: (name, _attr) => resolve(name),
|
|
73
|
+
resourceRef: (name) => ({ name: resolve(name) }),
|
|
74
74
|
propertyDeclarable: (entity, walk) => {
|
|
75
75
|
if (!isResourceDeclarable(entity) || typeof entity.props !== "object" || entity.props === null) {
|
|
76
76
|
return undefined;
|
|
@@ -86,6 +86,18 @@ function gcpVisitor(entityNames) {
|
|
|
86
86
|
},
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* The metadata.name a resource is written with: a string `metadata.name` in its
|
|
91
|
+
* props, else its logical id in kebab case.
|
|
92
|
+
*/
|
|
93
|
+
export function cnrmName(logical, entity) {
|
|
94
|
+
const props = isResourceDeclarable(entity) ? entity.props : undefined;
|
|
95
|
+
const declared = props?.metadata?.name;
|
|
96
|
+
return typeof declared === "string" && declared.length > 0 ? declared : kebabName(logical);
|
|
97
|
+
}
|
|
98
|
+
function kebabName(logical) {
|
|
99
|
+
return logical.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
100
|
+
}
|
|
89
101
|
/**
|
|
90
102
|
* Convert a value to YAML-compatible form using the walker.
|
|
91
103
|
*/
|
|
@@ -152,7 +164,7 @@ export const gcpSerializer = {
|
|
|
152
164
|
// Build metadata
|
|
153
165
|
const metadata = props.metadata ?? {};
|
|
154
166
|
if (!metadata.name) {
|
|
155
|
-
metadata.name = name
|
|
167
|
+
metadata.name = kebabName(name);
|
|
156
168
|
}
|
|
157
169
|
// Merge default labels
|
|
158
170
|
if (Object.keys(defaultLabelEntries).length > 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intentius/chant-lexicon-gcp",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.36",
|
|
4
4
|
"description": "Google Cloud lexicon for chant — declarative IaC in TypeScript",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://intentius.io/chant",
|
|
@@ -76,6 +76,6 @@
|
|
|
76
76
|
"typescript": "^5.9.3"
|
|
77
77
|
},
|
|
78
78
|
"peerDependencies": {
|
|
79
|
-
"@intentius/chant": "^0.124.
|
|
79
|
+
"@intentius/chant": "^0.124.36"
|
|
80
80
|
}
|
|
81
81
|
}
|
package/src/api/read-client.ts
CHANGED
|
@@ -22,7 +22,12 @@
|
|
|
22
22
|
*
|
|
23
23
|
* Coverage is exactly the applier's dispatch table (`MAPPERS`) — StorageBucket,
|
|
24
24
|
* PubSubTopic, PubSubSubscription, SecretManagerSecret, IAMServiceAccount,
|
|
25
|
-
* RunService
|
|
25
|
+
* RunService, IAMPolicyMember and the Cloud Run load balancer kinds
|
|
26
|
+
* (ComputeAddress, ComputeRegionNetworkEndpointGroup, ComputeBackendService,
|
|
27
|
+
* ComputeURLMap, ComputeTargetHTTPProxy, ComputeForwardingRule; #4233), and
|
|
28
|
+
* ComputeNetwork and ComputeSubnetwork (#4248). An
|
|
29
|
+
* IAMPolicyMember is read through its target's policy and is present when the
|
|
30
|
+
* policy grants its member the role. The kubectl path could fetch any CNRM kind the cluster knew
|
|
26
31
|
* about, so this is narrower on paper. It is not narrower in practice for
|
|
27
32
|
* anything chant can act on: a kind with no mapper cannot be applied either, so
|
|
28
33
|
* observing it produced a live tree nothing could ever reconcile against.
|
|
@@ -44,11 +49,14 @@ export interface GcpReadClientOptions {
|
|
|
44
49
|
http?: GcpReadHttp;
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
/** Injectable HTTP client — mirrors the applier's `GcpHttp`. */
|
|
48
|
-
export type GcpReadHttp = (method: string, url: string) => Promise<{ status: number; text: string }>;
|
|
52
|
+
/** Injectable HTTP client — mirrors the applier's `GcpHttp`. A body is sent only for a POST read (`:getIamPolicy`). */
|
|
53
|
+
export type GcpReadHttp = (method: string, url: string, body?: unknown) => Promise<{ status: number; text: string }>;
|
|
49
54
|
|
|
50
|
-
const defaultHttp: GcpReadHttp = async (method, url) => {
|
|
51
|
-
const res = await fetch(url, {
|
|
55
|
+
const defaultHttp: GcpReadHttp = async (method, url, body) => {
|
|
56
|
+
const res = await fetch(url, {
|
|
57
|
+
method,
|
|
58
|
+
...(body === undefined ? {} : { headers: { "content-type": "application/json" }, body: JSON.stringify(body) }),
|
|
59
|
+
});
|
|
52
60
|
return { status: res.status, text: await res.text() };
|
|
53
61
|
};
|
|
54
62
|
|
|
@@ -100,21 +108,33 @@ export async function getResource(
|
|
|
100
108
|
spec: (declared?.spec as Record<string, unknown> | undefined) ?? declared ?? {},
|
|
101
109
|
};
|
|
102
110
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
} catch (err) {
|
|
107
|
-
// A mapper that cannot plan from the declared spec cannot produce a URL, so
|
|
108
|
-
// there is nothing to read — a hole, never an absence.
|
|
109
|
-
throw new GcpReadError(`could not build a read URL for ${kind}/${name}: ${err instanceof Error ? err.message : String(err)}`);
|
|
111
|
+
const ctx = { base, project: client.project };
|
|
112
|
+
if (mapper.accepts && !mapper.accepts(resource, ctx)) {
|
|
113
|
+
throw new GcpReadError(`no REST read for ${kind}/${name}: its target has no endpoint chant can read`);
|
|
110
114
|
}
|
|
111
115
|
|
|
112
116
|
const http = client.http ?? defaultHttp;
|
|
113
117
|
let res: { status: number; text: string };
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
+
if (mapper.custom) {
|
|
119
|
+
try {
|
|
120
|
+
res = await mapper.custom.read(resource, ctx, (method, url, body) => http(method, url, body));
|
|
121
|
+
} catch (err) {
|
|
122
|
+
throw new GcpReadError(`could not read ${kind}/${name}: ${err instanceof Error ? err.message : String(err)}`);
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
let getUrl: string;
|
|
126
|
+
try {
|
|
127
|
+
getUrl = mapper.plan(resource, ctx).getUrl;
|
|
128
|
+
} catch (err) {
|
|
129
|
+
// A mapper that cannot plan from the declared spec cannot produce a URL, so
|
|
130
|
+
// there is nothing to read — a hole, never an absence.
|
|
131
|
+
throw new GcpReadError(`could not build a read URL for ${kind}/${name}: ${err instanceof Error ? err.message : String(err)}`);
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
res = await http("GET", getUrl);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
throw new GcpReadError(err instanceof Error ? err.message : String(err));
|
|
137
|
+
}
|
|
118
138
|
}
|
|
119
139
|
|
|
120
140
|
if (res.status === 404) throw new GcpReadError(`${kind}/${name} not found`, 404);
|
|
@@ -34,6 +34,8 @@ export interface CloudRunServiceProps {
|
|
|
34
34
|
publicAccess?: boolean;
|
|
35
35
|
/** Environment variables. */
|
|
36
36
|
env?: Array<{ name: string; value: string }>;
|
|
37
|
+
/** Service account the service runs as: an email, or the account id of an IAMServiceAccount in the project. */
|
|
38
|
+
serviceAccount?: string;
|
|
37
39
|
/** Additional labels. */
|
|
38
40
|
labels?: Record<string, string>;
|
|
39
41
|
/** Namespace for all resources. */
|
|
@@ -75,6 +77,7 @@ export const CloudRunServiceComposite = Composite((props: CloudRunServiceProps)
|
|
|
75
77
|
maxInstanceCount = 100,
|
|
76
78
|
publicAccess = false,
|
|
77
79
|
env,
|
|
80
|
+
serviceAccount,
|
|
78
81
|
labels: extraLabels = {},
|
|
79
82
|
namespace,
|
|
80
83
|
defaults: defs,
|
|
@@ -112,6 +115,9 @@ export const CloudRunServiceComposite = Composite((props: CloudRunServiceProps)
|
|
|
112
115
|
maxInstanceCount,
|
|
113
116
|
},
|
|
114
117
|
containers,
|
|
118
|
+
...(serviceAccount && {
|
|
119
|
+
serviceAccountRef: serviceAccount.includes("@") ? { external: serviceAccount } : { name: serviceAccount },
|
|
120
|
+
}),
|
|
115
121
|
},
|
|
116
122
|
}, defs?.service));
|
|
117
123
|
|
|
@@ -83,6 +83,13 @@ describe("CloudRunServiceComposite", () => {
|
|
|
83
83
|
expect(result.service).toBeDefined();
|
|
84
84
|
});
|
|
85
85
|
|
|
86
|
+
test("serviceAccount sets serviceAccountRef: an account id by name, an email as external (#4240)", () => {
|
|
87
|
+
const byName = CloudRunServiceComposite({ name: "api", image: "i", serviceAccount: "run-sa" });
|
|
88
|
+
expect((p(byName.service).template as Record<string, unknown>).serviceAccountRef).toEqual({ name: "run-sa" });
|
|
89
|
+
const byEmail = CloudRunServiceComposite({ name: "api", image: "i", serviceAccount: "x@o.iam.gserviceaccount.com" });
|
|
90
|
+
expect((p(byEmail.service).template as Record<string, unknown>).serviceAccountRef).toEqual({ external: "x@o.iam.gserviceaccount.com" });
|
|
91
|
+
});
|
|
92
|
+
|
|
86
93
|
test("sets projectRef and location, which the RunService CRD requires", () => {
|
|
87
94
|
const result = CloudRunServiceComposite({
|
|
88
95
|
name: "api",
|
package/src/deep-observe.test.ts
CHANGED
|
@@ -124,6 +124,47 @@ describe("toCnrmTree — each REST shape in the declared vocabulary (#1210)", ()
|
|
|
124
124
|
expect(tree).toEqual({ metadata: { name: "t" }, messageRetentionDuration: "86400s" });
|
|
125
125
|
});
|
|
126
126
|
|
|
127
|
+
test("run service: the v2 serviceAccount email maps back to the declared serviceAccountRef (#4240)", () => {
|
|
128
|
+
const same = toCnrmTree(
|
|
129
|
+
"RunService",
|
|
130
|
+
{ name: "projects/my-project/locations/us-central1/services/web", template: { serviceAccount: "run-sa@my-project.iam.gserviceaccount.com" } },
|
|
131
|
+
ctx,
|
|
132
|
+
) as { template: Record<string, unknown> };
|
|
133
|
+
expect(same.template).toEqual({ serviceAccountRef: { name: "run-sa" } });
|
|
134
|
+
const other = toCnrmTree(
|
|
135
|
+
"RunService",
|
|
136
|
+
{ name: "projects/my-project/locations/us-central1/services/web", template: { serviceAccount: "x@other.iam.gserviceaccount.com" } },
|
|
137
|
+
ctx,
|
|
138
|
+
) as { template: Record<string, unknown> };
|
|
139
|
+
expect(other.template).toEqual({ serviceAccountRef: { external: "x@other.iam.gserviceaccount.com" } });
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("run service: vpcAccess network and subnetwork paths map back to refs (#4249)", () => {
|
|
143
|
+
const tree = toCnrmTree(
|
|
144
|
+
"RunService",
|
|
145
|
+
{
|
|
146
|
+
name: "projects/my-project/locations/europe-west1/services/web",
|
|
147
|
+
template: {
|
|
148
|
+
vpcAccess: {
|
|
149
|
+
egress: "PRIVATE_RANGES_ONLY",
|
|
150
|
+
networkInterfaces: [
|
|
151
|
+
{ network: "projects/my-project/global/networks/vpc", subnetwork: "projects/my-project/regions/europe-west1/subnetworks/apps" },
|
|
152
|
+
{ network: "projects/other/global/networks/shared", subnetwork: "apps" },
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
ctx,
|
|
158
|
+
) as { template: { vpcAccess: unknown } };
|
|
159
|
+
expect(tree.template.vpcAccess).toEqual({
|
|
160
|
+
egress: "PRIVATE_RANGES_ONLY",
|
|
161
|
+
networkInterfaces: [
|
|
162
|
+
{ networkRef: { name: "vpc" }, subnetworkRef: { name: "apps" } },
|
|
163
|
+
{ networkRef: { external: "projects/other/global/networks/shared" }, subnetworkRef: { name: "apps" } },
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
127
168
|
test("bucket: GCS's iamConfiguration and lifecycle map onto the CNRM field names", () => {
|
|
128
169
|
const tree = toCnrmTree(
|
|
129
170
|
"StorageBucket",
|
|
@@ -235,7 +276,7 @@ describe("observeResourcesDeepGcp — over REST (#1209)", () => {
|
|
|
235
276
|
});
|
|
236
277
|
|
|
237
278
|
test("a kind the applier cannot write is unsupported-kind, and is never queried", async () => {
|
|
238
|
-
const out = await observe(entities([["e", "GCP::Compute::
|
|
279
|
+
const out = await observe(entities([["e", "GCP::Compute::Firewall", { metadata: { name: "n" } }]]));
|
|
239
280
|
expect(out.unobserved?.e?.reason).toBe("unsupported-kind");
|
|
240
281
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
241
282
|
});
|