@checkstack/satellite-backend 0.2.21 → 0.3.1
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/CHANGELOG.md +62 -0
- package/package.json +14 -12
- package/src/index.ts +14 -0
- package/src/router.ts +29 -1
- package/src/satellite-gitops-kinds.test.ts +239 -0
- package/src/satellite-gitops-kinds.ts +116 -0
- package/src/service.ts +66 -0
- package/tsconfig.json +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,67 @@
|
|
|
1
1
|
# @checkstack/satellite-backend
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [7c97b43]
|
|
8
|
+
- Updated dependencies [9016526]
|
|
9
|
+
- @checkstack/healthcheck-backend@1.1.0
|
|
10
|
+
- @checkstack/common@0.10.0
|
|
11
|
+
- @checkstack/healthcheck-common@1.1.0
|
|
12
|
+
- @checkstack/gitops-common@0.4.0
|
|
13
|
+
- @checkstack/satellite-common@0.5.0
|
|
14
|
+
- @checkstack/backend-api@0.15.2
|
|
15
|
+
- @checkstack/gitops-backend@0.3.1
|
|
16
|
+
- @checkstack/signal-common@0.2.3
|
|
17
|
+
- @checkstack/queue-api@0.3.1
|
|
18
|
+
|
|
19
|
+
## 0.3.0
|
|
20
|
+
|
|
21
|
+
### Minor Changes
|
|
22
|
+
|
|
23
|
+
- f6f9a5c: Add a GitOps `Satellite` kind plus a UI affordance for resetting tokens.
|
|
24
|
+
|
|
25
|
+
GitOps owns satellite **metadata only** — `metadata.name`,
|
|
26
|
+
`spec.region`, and `metadata.labels` (used as the satellite's runtime
|
|
27
|
+
tags). The bcrypt token is intentionally never expressed in YAML; on
|
|
28
|
+
first reconcile a satellite is created with a random token that is
|
|
29
|
+
discarded, and operators must use the Satellites page to retrieve a
|
|
30
|
+
working credential.
|
|
31
|
+
|
|
32
|
+
To support that flow:
|
|
33
|
+
|
|
34
|
+
- New service methods: `updateSatelliteMetadata`, `rotateSatelliteToken`,
|
|
35
|
+
`getSatelliteByName`.
|
|
36
|
+
- New RPC procs: `updateSatellite`, `rotateSatelliteToken`.
|
|
37
|
+
- New `RotateSatelliteTokenDialog` and a "Reset token" key icon on the
|
|
38
|
+
Satellites list. The dialog reuses the one-time-reveal layout from
|
|
39
|
+
`CreateSatelliteDialog`.
|
|
40
|
+
- The Satellites list shows a `GitOpsSourceBadge` next to managed
|
|
41
|
+
satellites and disables the delete button while leaving the
|
|
42
|
+
token-reset button enabled (so operators can always re-issue a
|
|
43
|
+
credential without touching YAML).
|
|
44
|
+
|
|
45
|
+
The satellite kind reconciler adopts pre-existing satellites by name on
|
|
46
|
+
first sync, so this is safe to roll out against installations that
|
|
47
|
+
already have manually-created satellites.
|
|
48
|
+
|
|
49
|
+
### Patch Changes
|
|
50
|
+
|
|
51
|
+
- Updated dependencies [42abfff]
|
|
52
|
+
- Updated dependencies [f6f9a5c]
|
|
53
|
+
- Updated dependencies [f6f9a5c]
|
|
54
|
+
- Updated dependencies [aa89bc5]
|
|
55
|
+
- @checkstack/common@0.9.0
|
|
56
|
+
- @checkstack/satellite-common@0.4.0
|
|
57
|
+
- @checkstack/gitops-common@0.3.0
|
|
58
|
+
- @checkstack/gitops-backend@0.3.0
|
|
59
|
+
- @checkstack/queue-api@0.3.0
|
|
60
|
+
- @checkstack/backend-api@0.15.1
|
|
61
|
+
- @checkstack/healthcheck-backend@1.0.4
|
|
62
|
+
- @checkstack/healthcheck-common@1.0.2
|
|
63
|
+
- @checkstack/signal-common@0.2.2
|
|
64
|
+
|
|
3
65
|
## 0.2.21
|
|
4
66
|
|
|
5
67
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@checkstack/satellite-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"license": "Elastic-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -14,22 +14,24 @@
|
|
|
14
14
|
"lint:code": "eslint . --max-warnings 0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@checkstack/backend-api": "0.
|
|
18
|
-
"@checkstack/satellite-common": "0.
|
|
19
|
-
"@checkstack/healthcheck-common": "1.0.
|
|
20
|
-
"@checkstack/signal-common": "0.2.
|
|
21
|
-
"@checkstack/healthcheck-backend": "1.0.
|
|
22
|
-
"@checkstack/
|
|
23
|
-
"@checkstack/
|
|
17
|
+
"@checkstack/backend-api": "0.15.1",
|
|
18
|
+
"@checkstack/satellite-common": "0.4.0",
|
|
19
|
+
"@checkstack/healthcheck-common": "1.0.2",
|
|
20
|
+
"@checkstack/signal-common": "0.2.2",
|
|
21
|
+
"@checkstack/healthcheck-backend": "1.0.4",
|
|
22
|
+
"@checkstack/gitops-backend": "0.3.0",
|
|
23
|
+
"@checkstack/gitops-common": "0.3.0",
|
|
24
|
+
"@checkstack/common": "0.9.0",
|
|
25
|
+
"@checkstack/queue-api": "0.3.0",
|
|
24
26
|
"drizzle-orm": "^0.45.0",
|
|
25
27
|
"zod": "^4.2.1",
|
|
26
28
|
"@orpc/server": "^1.13.2"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
|
-
"@checkstack/drizzle-helper": "0.0.
|
|
30
|
-
"@checkstack/scripts": "0.1
|
|
31
|
-
"@checkstack/test-utils-backend": "0.1.
|
|
32
|
-
"@checkstack/tsconfig": "0.0.
|
|
31
|
+
"@checkstack/drizzle-helper": "0.0.5",
|
|
32
|
+
"@checkstack/scripts": "0.3.1",
|
|
33
|
+
"@checkstack/test-utils-backend": "0.1.25",
|
|
34
|
+
"@checkstack/tsconfig": "0.0.7",
|
|
33
35
|
"@types/bun": "^1.0.0",
|
|
34
36
|
"drizzle-kit": "^0.31.10",
|
|
35
37
|
"typescript": "^5.0.0"
|
package/src/index.ts
CHANGED
|
@@ -14,6 +14,8 @@ import { createSatelliteRouter } from "./router";
|
|
|
14
14
|
import { HeartbeatMonitor } from "./heartbeat-monitor";
|
|
15
15
|
import { SatelliteWsHandler } from "./satellite-ws-handler";
|
|
16
16
|
import { ConfigRelay } from "./config-relay";
|
|
17
|
+
import { entityKindExtensionPoint } from "@checkstack/gitops-backend";
|
|
18
|
+
import { registerSatelliteGitOpsKinds } from "./satellite-gitops-kinds";
|
|
17
19
|
|
|
18
20
|
// Queue and job constants
|
|
19
21
|
const HEARTBEAT_QUEUE = "satellite-heartbeat";
|
|
@@ -25,6 +27,17 @@ export default createBackendPlugin({
|
|
|
25
27
|
register(env) {
|
|
26
28
|
env.registerAccessRules(satelliteAccessRules);
|
|
27
29
|
|
|
30
|
+
// ─── GitOps Entity Kind Registration ─────────────────────────────
|
|
31
|
+
let gitopsService: SatelliteService | undefined;
|
|
32
|
+
const kindRegistry = env.getExtensionPoint(entityKindExtensionPoint);
|
|
33
|
+
registerSatelliteGitOpsKinds({
|
|
34
|
+
kindRegistry,
|
|
35
|
+
getService: () => {
|
|
36
|
+
if (!gitopsService) throw new Error("SatelliteService not initialized");
|
|
37
|
+
return gitopsService;
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
28
41
|
env.registerInit({
|
|
29
42
|
schema,
|
|
30
43
|
deps: {
|
|
@@ -41,6 +54,7 @@ export default createBackendPlugin({
|
|
|
41
54
|
const service = new SatelliteService(
|
|
42
55
|
database as SafeDatabase<typeof schema>,
|
|
43
56
|
);
|
|
57
|
+
gitopsService = service;
|
|
44
58
|
|
|
45
59
|
const router = createSatelliteRouter({
|
|
46
60
|
service,
|
package/src/router.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { implement } from "@orpc/server";
|
|
1
|
+
import { implement, ORPCError } from "@orpc/server";
|
|
2
2
|
import {
|
|
3
3
|
satelliteContract,
|
|
4
4
|
SATELLITE_STATUS_CHANGED,
|
|
@@ -75,6 +75,34 @@ export function createSatelliteRouter(props: {
|
|
|
75
75
|
});
|
|
76
76
|
}),
|
|
77
77
|
|
|
78
|
+
updateSatellite: os.updateSatellite.handler(async ({ input }) => {
|
|
79
|
+
const updated = await service.updateSatelliteMetadata(input);
|
|
80
|
+
if (!updated) {
|
|
81
|
+
throw new ORPCError("NOT_FOUND", {
|
|
82
|
+
message: `Satellite not found: ${input.id}`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
logger.info(`Satellite metadata updated: ${updated.name}`);
|
|
86
|
+
await signalService.broadcast(SATELLITE_CONFIG_CHANGED, {
|
|
87
|
+
satelliteId: updated.id,
|
|
88
|
+
});
|
|
89
|
+
return updated;
|
|
90
|
+
}),
|
|
91
|
+
|
|
92
|
+
rotateSatelliteToken: os.rotateSatelliteToken.handler(async ({ input }) => {
|
|
93
|
+
const result = await service.rotateSatelliteToken(input.id);
|
|
94
|
+
if (!result) {
|
|
95
|
+
throw new ORPCError("NOT_FOUND", {
|
|
96
|
+
message: `Satellite not found: ${input.id}`,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
logger.info(`Satellite token rotated: ${result.satellite.name}`);
|
|
100
|
+
await signalService.broadcast(SATELLITE_CONFIG_CHANGED, {
|
|
101
|
+
satelliteId: result.satellite.id,
|
|
102
|
+
});
|
|
103
|
+
return { satellite: result.satellite, token: result.plaintextToken };
|
|
104
|
+
}),
|
|
105
|
+
|
|
78
106
|
getOnlineSatelliteIds: os.getOnlineSatelliteIds.handler(async () => {
|
|
79
107
|
const satelliteIds = await service.getOnlineSatelliteIds();
|
|
80
108
|
return { satelliteIds };
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { describe, it, expect, mock, beforeEach } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
CHECKSTACK_API_VERSION,
|
|
4
|
+
type ReconcileContext,
|
|
5
|
+
} from "@checkstack/gitops-common";
|
|
6
|
+
import type { SatelliteWithStatus } from "@checkstack/satellite-common";
|
|
7
|
+
import type { SatelliteService } from "./service";
|
|
8
|
+
import { buildSatelliteKind } from "./satellite-gitops-kinds";
|
|
9
|
+
|
|
10
|
+
const noopLogger = {
|
|
11
|
+
debug: () => {},
|
|
12
|
+
info: () => {},
|
|
13
|
+
warn: () => {},
|
|
14
|
+
error: () => {},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const buildContext = (
|
|
18
|
+
overrides: Partial<ReconcileContext> = {},
|
|
19
|
+
): ReconcileContext =>
|
|
20
|
+
({
|
|
21
|
+
logger: noopLogger,
|
|
22
|
+
resolveEntityRef: async () => undefined,
|
|
23
|
+
resolveSecretsBySchema: async ({ value }) => ({
|
|
24
|
+
resolved: value,
|
|
25
|
+
warnings: [],
|
|
26
|
+
}),
|
|
27
|
+
...overrides,
|
|
28
|
+
}) as ReconcileContext;
|
|
29
|
+
|
|
30
|
+
const stubService = (initial?: SatelliteWithStatus | undefined) => {
|
|
31
|
+
let stored = initial;
|
|
32
|
+
const getSatellite = mock(async (id: string) =>
|
|
33
|
+
stored && stored.id === id ? stored : undefined,
|
|
34
|
+
);
|
|
35
|
+
const getSatelliteByName = mock(async (name: string) =>
|
|
36
|
+
stored && stored.name === name ? stored : undefined,
|
|
37
|
+
);
|
|
38
|
+
const createSatellite = mock(
|
|
39
|
+
async (props: {
|
|
40
|
+
name: string;
|
|
41
|
+
region: string;
|
|
42
|
+
tags: Record<string, string>;
|
|
43
|
+
}) => {
|
|
44
|
+
const satellite: SatelliteWithStatus = {
|
|
45
|
+
id: "sat-new",
|
|
46
|
+
name: props.name,
|
|
47
|
+
region: props.region,
|
|
48
|
+
tags: props.tags,
|
|
49
|
+
createdAt: new Date(),
|
|
50
|
+
status: "offline",
|
|
51
|
+
};
|
|
52
|
+
stored = satellite;
|
|
53
|
+
return { satellite, plaintextToken: "csat_test-token" };
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
const updateSatelliteMetadata = mock(
|
|
57
|
+
async (props: {
|
|
58
|
+
id: string;
|
|
59
|
+
name?: string;
|
|
60
|
+
region?: string;
|
|
61
|
+
tags?: Record<string, string>;
|
|
62
|
+
}) => {
|
|
63
|
+
if (!stored || stored.id !== props.id) return undefined;
|
|
64
|
+
stored = {
|
|
65
|
+
...stored,
|
|
66
|
+
name: props.name ?? stored.name,
|
|
67
|
+
region: props.region ?? stored.region,
|
|
68
|
+
tags: props.tags ?? stored.tags,
|
|
69
|
+
};
|
|
70
|
+
return stored;
|
|
71
|
+
},
|
|
72
|
+
);
|
|
73
|
+
const deleteSatellite = mock(async (id: string) => {
|
|
74
|
+
if (stored && stored.id === id) stored = undefined;
|
|
75
|
+
});
|
|
76
|
+
return {
|
|
77
|
+
getSatellite,
|
|
78
|
+
getSatelliteByName,
|
|
79
|
+
createSatellite,
|
|
80
|
+
updateSatelliteMetadata,
|
|
81
|
+
deleteSatellite,
|
|
82
|
+
service: {
|
|
83
|
+
getSatellite,
|
|
84
|
+
getSatelliteByName,
|
|
85
|
+
createSatellite,
|
|
86
|
+
updateSatelliteMetadata,
|
|
87
|
+
deleteSatellite,
|
|
88
|
+
} as unknown as SatelliteService,
|
|
89
|
+
get current() {
|
|
90
|
+
return stored;
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const mkEntity = (
|
|
96
|
+
name: string,
|
|
97
|
+
spec: { region: string },
|
|
98
|
+
metadataExtras: { labels?: Record<string, string> } = {},
|
|
99
|
+
) => ({
|
|
100
|
+
apiVersion: CHECKSTACK_API_VERSION,
|
|
101
|
+
kind: "Satellite",
|
|
102
|
+
metadata: { name, ...metadataExtras },
|
|
103
|
+
spec,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("buildSatelliteKind", () => {
|
|
107
|
+
let kind: ReturnType<typeof buildSatelliteKind>;
|
|
108
|
+
let stub: ReturnType<typeof stubService>;
|
|
109
|
+
|
|
110
|
+
beforeEach(() => {
|
|
111
|
+
stub = stubService();
|
|
112
|
+
kind = buildSatelliteKind({ getService: () => stub.service });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("registers as Satellite kind", () => {
|
|
116
|
+
expect(kind.kind).toBe("Satellite");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("creates a satellite when none exists, discarding the plaintext token", async () => {
|
|
120
|
+
const result = await kind.reconcile({
|
|
121
|
+
entity: mkEntity(
|
|
122
|
+
"eu-west-1",
|
|
123
|
+
{ region: "eu-west-1" },
|
|
124
|
+
{ labels: { tier: "prod" } },
|
|
125
|
+
),
|
|
126
|
+
context: buildContext(),
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(stub.createSatellite).toHaveBeenCalledTimes(1);
|
|
130
|
+
expect(stub.createSatellite).toHaveBeenCalledWith({
|
|
131
|
+
name: "eu-west-1",
|
|
132
|
+
region: "eu-west-1",
|
|
133
|
+
tags: { tier: "prod" },
|
|
134
|
+
});
|
|
135
|
+
expect(result.entityId).toBe("sat-new");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("adopts an existing satellite by name on first sync", async () => {
|
|
139
|
+
const existing: SatelliteWithStatus = {
|
|
140
|
+
id: "sat-existing",
|
|
141
|
+
name: "eu-west-1",
|
|
142
|
+
region: "eu-west-1",
|
|
143
|
+
tags: {},
|
|
144
|
+
createdAt: new Date(),
|
|
145
|
+
status: "online",
|
|
146
|
+
};
|
|
147
|
+
stub = stubService(existing);
|
|
148
|
+
kind = buildSatelliteKind({ getService: () => stub.service });
|
|
149
|
+
|
|
150
|
+
const result = await kind.reconcile({
|
|
151
|
+
entity: mkEntity(
|
|
152
|
+
"eu-west-1",
|
|
153
|
+
{ region: "eu-west-2" },
|
|
154
|
+
{ labels: { tier: "prod" } },
|
|
155
|
+
),
|
|
156
|
+
context: buildContext(),
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
expect(stub.createSatellite).not.toHaveBeenCalled();
|
|
160
|
+
expect(stub.updateSatelliteMetadata).toHaveBeenCalledWith({
|
|
161
|
+
id: "sat-existing",
|
|
162
|
+
name: "eu-west-1",
|
|
163
|
+
region: "eu-west-2",
|
|
164
|
+
tags: { tier: "prod" },
|
|
165
|
+
});
|
|
166
|
+
expect(result.entityId).toBe("sat-existing");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("uses the provenance entityId hint when provided", async () => {
|
|
170
|
+
const existing: SatelliteWithStatus = {
|
|
171
|
+
id: "sat-known",
|
|
172
|
+
name: "renamed",
|
|
173
|
+
region: "eu-west-1",
|
|
174
|
+
tags: {},
|
|
175
|
+
createdAt: new Date(),
|
|
176
|
+
status: "online",
|
|
177
|
+
};
|
|
178
|
+
stub = stubService(existing);
|
|
179
|
+
kind = buildSatelliteKind({ getService: () => stub.service });
|
|
180
|
+
|
|
181
|
+
const result = await kind.reconcile({
|
|
182
|
+
entity: mkEntity("renamed", { region: "eu-west-1" }),
|
|
183
|
+
existingEntityId: "sat-known",
|
|
184
|
+
context: buildContext(),
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
expect(stub.getSatellite).toHaveBeenCalledWith("sat-known");
|
|
188
|
+
expect(stub.getSatelliteByName).not.toHaveBeenCalled();
|
|
189
|
+
expect(result.entityId).toBe("sat-known");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("treats pending-* entityIds as fresh and falls back to name lookup", async () => {
|
|
193
|
+
const existing: SatelliteWithStatus = {
|
|
194
|
+
id: "sat-existing",
|
|
195
|
+
name: "eu-west-1",
|
|
196
|
+
region: "eu-west-1",
|
|
197
|
+
tags: {},
|
|
198
|
+
createdAt: new Date(),
|
|
199
|
+
status: "online",
|
|
200
|
+
};
|
|
201
|
+
stub = stubService(existing);
|
|
202
|
+
kind = buildSatelliteKind({ getService: () => stub.service });
|
|
203
|
+
|
|
204
|
+
await kind.reconcile({
|
|
205
|
+
entity: mkEntity("eu-west-1", { region: "eu-west-1" }),
|
|
206
|
+
existingEntityId: "pending-foo",
|
|
207
|
+
context: buildContext(),
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
expect(stub.getSatellite).not.toHaveBeenCalledWith("pending-foo");
|
|
211
|
+
expect(stub.getSatelliteByName).toHaveBeenCalledWith("eu-west-1");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("normalizes missing metadata.labels to an empty tags object", async () => {
|
|
215
|
+
await kind.reconcile({
|
|
216
|
+
entity: mkEntity("eu-west-1", { region: "eu-west-1" }),
|
|
217
|
+
context: buildContext(),
|
|
218
|
+
});
|
|
219
|
+
expect(stub.createSatellite).toHaveBeenCalledWith({
|
|
220
|
+
name: "eu-west-1",
|
|
221
|
+
region: "eu-west-1",
|
|
222
|
+
tags: {},
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("delegates delete to the service", async () => {
|
|
227
|
+
await kind.delete!({
|
|
228
|
+
entityName: "eu-west-1",
|
|
229
|
+
entityId: "sat-1",
|
|
230
|
+
context: buildContext(),
|
|
231
|
+
});
|
|
232
|
+
expect(stub.deleteSatellite).toHaveBeenCalledWith("sat-1");
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("delete is a no-op without entityId", async () => {
|
|
236
|
+
await kind.delete!({ entityName: "x", context: buildContext() });
|
|
237
|
+
expect(stub.deleteSatellite).not.toHaveBeenCalled();
|
|
238
|
+
});
|
|
239
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
CHECKSTACK_API_VERSION,
|
|
4
|
+
type EntityKindDefinition,
|
|
5
|
+
type EntityKindRegistry,
|
|
6
|
+
type ReconcileContext,
|
|
7
|
+
} from "@checkstack/gitops-common";
|
|
8
|
+
import type { SatelliteService } from "./service";
|
|
9
|
+
|
|
10
|
+
interface SatelliteGitOpsKindsDeps {
|
|
11
|
+
/** Lazy accessor — populated during init(), invoked at reconcile time. */
|
|
12
|
+
getService: () => SatelliteService;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// ─── Satellite Spec Schema ─────────────────────────────────────────────────
|
|
16
|
+
//
|
|
17
|
+
// GitOps owns metadata only — name, region, and the satellite's runtime
|
|
18
|
+
// tags (sourced from the envelope's `metadata.labels`, since the envelope
|
|
19
|
+
// already provides a `Record<string, string>` for that purpose). The bcrypt
|
|
20
|
+
// token is never committed to YAML; operators retrieve and reset tokens via
|
|
21
|
+
// the Satellites page in the UI.
|
|
22
|
+
|
|
23
|
+
const satelliteSpecSchema = z.object({
|
|
24
|
+
region: z.string().min(1),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
type SatelliteSpec = z.infer<typeof satelliteSpecSchema>;
|
|
28
|
+
|
|
29
|
+
export function buildSatelliteKind(
|
|
30
|
+
deps: SatelliteGitOpsKindsDeps,
|
|
31
|
+
): EntityKindDefinition<SatelliteSpec> {
|
|
32
|
+
return {
|
|
33
|
+
apiVersion: CHECKSTACK_API_VERSION,
|
|
34
|
+
kind: "Satellite",
|
|
35
|
+
specSchema: satelliteSpecSchema,
|
|
36
|
+
|
|
37
|
+
reconcile: async ({
|
|
38
|
+
entity,
|
|
39
|
+
existingEntityId,
|
|
40
|
+
context,
|
|
41
|
+
}: {
|
|
42
|
+
entity: {
|
|
43
|
+
metadata: {
|
|
44
|
+
name: string;
|
|
45
|
+
title?: string;
|
|
46
|
+
description?: string;
|
|
47
|
+
labels?: Record<string, string>;
|
|
48
|
+
};
|
|
49
|
+
spec: SatelliteSpec;
|
|
50
|
+
};
|
|
51
|
+
existingEntityId?: string;
|
|
52
|
+
context: ReconcileContext;
|
|
53
|
+
}) => {
|
|
54
|
+
const service = deps.getService();
|
|
55
|
+
const { spec, metadata } = entity;
|
|
56
|
+
const tags = metadata.labels ?? {};
|
|
57
|
+
|
|
58
|
+
// Try the provenance hint first; fall back to a name lookup so we
|
|
59
|
+
// adopt pre-existing satellites that match by name on first sync.
|
|
60
|
+
const existing =
|
|
61
|
+
existingEntityId && !existingEntityId.startsWith("pending-")
|
|
62
|
+
? await service.getSatellite(existingEntityId)
|
|
63
|
+
: await service.getSatelliteByName(metadata.name);
|
|
64
|
+
|
|
65
|
+
if (existing) {
|
|
66
|
+
const updated = await service.updateSatelliteMetadata({
|
|
67
|
+
id: existing.id,
|
|
68
|
+
name: metadata.name,
|
|
69
|
+
region: spec.region,
|
|
70
|
+
tags,
|
|
71
|
+
});
|
|
72
|
+
const finalId = updated?.id ?? existing.id;
|
|
73
|
+
context.logger.info(
|
|
74
|
+
`GitOps: updated Satellite "${metadata.name}" (id: ${finalId})`,
|
|
75
|
+
);
|
|
76
|
+
return { entityId: finalId };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// First-time create. The plaintext token is intentionally discarded —
|
|
80
|
+
// an operator must use the UI's "Reset token" affordance to retrieve a
|
|
81
|
+
// working token. This keeps the secret out of YAML and out of logs.
|
|
82
|
+
const created = await service.createSatellite({
|
|
83
|
+
name: metadata.name,
|
|
84
|
+
region: spec.region,
|
|
85
|
+
tags,
|
|
86
|
+
});
|
|
87
|
+
context.logger.info(
|
|
88
|
+
`GitOps: created Satellite "${metadata.name}" (id: ${created.satellite.id}). ` +
|
|
89
|
+
`Reset the token via the Satellites page to obtain a working credential.`,
|
|
90
|
+
);
|
|
91
|
+
return { entityId: created.satellite.id };
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
delete: async ({
|
|
95
|
+
entityId,
|
|
96
|
+
context,
|
|
97
|
+
}: {
|
|
98
|
+
entityName: string;
|
|
99
|
+
entityId?: string;
|
|
100
|
+
context: ReconcileContext;
|
|
101
|
+
}) => {
|
|
102
|
+
if (!entityId) return;
|
|
103
|
+
await deps.getService().deleteSatellite(entityId);
|
|
104
|
+
context.logger.info(`GitOps: deleted Satellite (id: ${entityId})`);
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function registerSatelliteGitOpsKinds({
|
|
110
|
+
kindRegistry,
|
|
111
|
+
...deps
|
|
112
|
+
}: SatelliteGitOpsKindsDeps & {
|
|
113
|
+
kindRegistry: EntityKindRegistry;
|
|
114
|
+
}): void {
|
|
115
|
+
kindRegistry.registerKind(buildSatelliteKind(deps));
|
|
116
|
+
}
|
package/src/service.ts
CHANGED
|
@@ -80,6 +80,72 @@ export class SatelliteService {
|
|
|
80
80
|
await this.db.delete(satellites).where(eq(satellites.id, id));
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Update a satellite's metadata (name, region, tags). Token is left intact —
|
|
85
|
+
* use `rotateSatelliteToken` to issue a new one.
|
|
86
|
+
*/
|
|
87
|
+
async updateSatelliteMetadata(props: {
|
|
88
|
+
id: string;
|
|
89
|
+
name?: string;
|
|
90
|
+
region?: string;
|
|
91
|
+
tags?: Record<string, string>;
|
|
92
|
+
}): Promise<SatelliteWithStatus | undefined> {
|
|
93
|
+
const updates: Partial<typeof satellites.$inferInsert> = {};
|
|
94
|
+
if (props.name !== undefined) updates.name = props.name;
|
|
95
|
+
if (props.region !== undefined) updates.region = props.region;
|
|
96
|
+
if (props.tags !== undefined) updates.tags = props.tags;
|
|
97
|
+
if (Object.keys(updates).length === 0) return this.getSatellite(props.id);
|
|
98
|
+
|
|
99
|
+
const [row] = await this.db
|
|
100
|
+
.update(satellites)
|
|
101
|
+
.set(updates)
|
|
102
|
+
.where(eq(satellites.id, props.id))
|
|
103
|
+
.returning();
|
|
104
|
+
return row ? this.toSatelliteWithStatus(row) : undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Rotate the token for an existing satellite. Generates a fresh plaintext
|
|
109
|
+
* token, stores its bcrypt hash, and returns the plaintext for one-time
|
|
110
|
+
* display. The previous token is invalidated immediately.
|
|
111
|
+
*/
|
|
112
|
+
async rotateSatelliteToken(
|
|
113
|
+
id: string,
|
|
114
|
+
): Promise<{ satellite: SatelliteWithStatus; plaintextToken: string } | undefined> {
|
|
115
|
+
const randomBytes = crypto.getRandomValues(new Uint8Array(32));
|
|
116
|
+
const tokenBody = Buffer.from(randomBytes).toString("base64url");
|
|
117
|
+
const plaintextToken = `csat_${tokenBody}`;
|
|
118
|
+
|
|
119
|
+
const tokenHash = await Bun.password.hash(plaintextToken, {
|
|
120
|
+
algorithm: "bcrypt",
|
|
121
|
+
cost: 10,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const [row] = await this.db
|
|
125
|
+
.update(satellites)
|
|
126
|
+
.set({ tokenHash })
|
|
127
|
+
.where(eq(satellites.id, id))
|
|
128
|
+
.returning();
|
|
129
|
+
if (!row) return undefined;
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
satellite: this.toSatelliteWithStatus(row),
|
|
133
|
+
plaintextToken,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Lookup helper — returns a satellite by its `name`. Used by GitOps to
|
|
139
|
+
* resolve a YAML-declared satellite name to the persisted UUID.
|
|
140
|
+
*/
|
|
141
|
+
async getSatelliteByName(name: string): Promise<SatelliteWithStatus | undefined> {
|
|
142
|
+
const [row] = await this.db
|
|
143
|
+
.select()
|
|
144
|
+
.from(satellites)
|
|
145
|
+
.where(eq(satellites.name, name));
|
|
146
|
+
return row ? this.toSatelliteWithStatus(row) : undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
83
149
|
/**
|
|
84
150
|
* List all satellites with computed online/offline status.
|
|
85
151
|
*/
|