@engini/sdk 0.18.0 → 0.20.0
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/README.md +81 -0
- package/dist/client.d.ts +4 -0
- package/dist/client.js +6 -0
- package/dist/errors.d.ts +108 -2
- package/dist/errors.js +95 -2
- package/dist/http.d.ts +17 -0
- package/dist/http.js +47 -5
- package/dist/index.d.ts +6 -3
- package/dist/index.js +2 -2
- package/dist/resources/index.d.ts +4 -0
- package/dist/resources/index.js +2 -0
- package/dist/resources/opa.d.ts +175 -0
- package/dist/resources/opa.js +322 -0
- package/dist/resources/triggers.d.ts +355 -0
- package/dist/resources/triggers.js +851 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* On-premise agents resource — provision, inspect, configure and credential OPA agents.
|
|
3
|
+
*
|
|
4
|
+
* An on-premise agent (OPA) is the Engini component a customer runs on their own
|
|
5
|
+
* hardware, behind their firewall, to reach systems the cloud cannot: SQL Server,
|
|
6
|
+
* Oracle, Priority ERP, file shares. It polls Engini for work; Engini never connects
|
|
7
|
+
* to it. Every operation here that "talks to the agent" therefore waits for the
|
|
8
|
+
* agent's next poll, bounded server-side.
|
|
9
|
+
*
|
|
10
|
+
* Agents are NOT connections on this surface: `client.connections` never returns them,
|
|
11
|
+
* and `connections.get(agentId)` is a 404.
|
|
12
|
+
*/
|
|
13
|
+
import { type OpaAgentCreateResponse, type OpaAgentDetail, type OpaAgentListItem, type OpaAgentTokenResponse, type OpaReleaseManifest } from "@engini/client";
|
|
14
|
+
import { type Transport } from "../http.js";
|
|
15
|
+
/** The settings members every write accepts. Each is independently optional. */
|
|
16
|
+
interface OpaSettingsParams {
|
|
17
|
+
parallelTasks?: number;
|
|
18
|
+
pullPeriodSeconds?: number;
|
|
19
|
+
logLevel?: string;
|
|
20
|
+
}
|
|
21
|
+
/** Minimal client surface this resource needs (satisfied by `Engini`). */
|
|
22
|
+
export interface OpaClient {
|
|
23
|
+
_transport: Transport;
|
|
24
|
+
}
|
|
25
|
+
export declare class OpaResource {
|
|
26
|
+
private readonly client;
|
|
27
|
+
constructor(client: OpaClient);
|
|
28
|
+
/**
|
|
29
|
+
* Remap a 404 onto the OPA-specific not-found, which the CLI maps to its
|
|
30
|
+
* NOT_FOUND exit code. Anything else passes through untouched.
|
|
31
|
+
*
|
|
32
|
+
* Unlike the MCP equivalent the id is echoed back: an agent id is an ordinary
|
|
33
|
+
* integer, not a secret-equivalent URL component, so it is safe in an error
|
|
34
|
+
* string and it is the one thing that makes the message actionable.
|
|
35
|
+
*/
|
|
36
|
+
private retypeNotFound;
|
|
37
|
+
/**
|
|
38
|
+
* Every on-premise agent on the account, paginated.
|
|
39
|
+
*
|
|
40
|
+
* `status` is one of `WaitingForConnection`, `Online`, `Offline` or `Disabled`
|
|
41
|
+
* (case-insensitive on the wire; an unknown value is a 400, not a silently-ignored
|
|
42
|
+
* filter). `name` is a substring match.
|
|
43
|
+
*/
|
|
44
|
+
list(params?: {
|
|
45
|
+
status?: string;
|
|
46
|
+
name?: string;
|
|
47
|
+
}): Promise<OpaAgentListItem[]>;
|
|
48
|
+
/**
|
|
49
|
+
* One agent's status, self-reported version, last heartbeat and settings.
|
|
50
|
+
*
|
|
51
|
+
* Throws `EnginiOpaAgentNotFoundError` when the id is unknown, belongs to another
|
|
52
|
+
* account, or is not an on-premise agent — the API answers 404 for all three
|
|
53
|
+
* deliberately and does not distinguish them.
|
|
54
|
+
*/
|
|
55
|
+
get(agentId: number): Promise<OpaAgentDetail>;
|
|
56
|
+
/**
|
|
57
|
+
* Where to download the current agent installer.
|
|
58
|
+
*
|
|
59
|
+
* `degraded: true` means the release manifest was not reachable and the result was
|
|
60
|
+
* reconstructed from the installer URL alone: `sha256`, `sizeBytes` and `releasedAt`
|
|
61
|
+
* are then null. A caller that needs integrity verification should refuse a degraded
|
|
62
|
+
* manifest.
|
|
63
|
+
*/
|
|
64
|
+
latestRelease(): Promise<OpaReleaseManifest>;
|
|
65
|
+
/**
|
|
66
|
+
* Build a settings object carrying ONLY what the caller supplied, or `undefined`
|
|
67
|
+
* when nothing was.
|
|
68
|
+
*
|
|
69
|
+
* Every member is nullable on the wire and absent means "leave it alone / use the
|
|
70
|
+
* server default". A client-side default here would either override a real value on
|
|
71
|
+
* update or be rejected on create: the minimums are the connector's own declared
|
|
72
|
+
* metadata, read server-side. Duplicating those numbers here is how they drifted
|
|
73
|
+
* before.
|
|
74
|
+
*/
|
|
75
|
+
private static settings;
|
|
76
|
+
/**
|
|
77
|
+
* Retype a 409 `OPA_APPLICATION_AMBIGUOUS` so it carries the candidate slugs.
|
|
78
|
+
*
|
|
79
|
+
* They arrive as `details[].issue === "applicationSlug: <slug>"`, and are worth the
|
|
80
|
+
* parsing: `/v1/applications` does not list OPA applications, so this error is the
|
|
81
|
+
* only way a caller can discover what to pass as `applicationSlug`.
|
|
82
|
+
*/
|
|
83
|
+
private retypeAmbiguous;
|
|
84
|
+
/**
|
|
85
|
+
* Register a new on-premise agent. The response carries its token.
|
|
86
|
+
*
|
|
87
|
+
* The token is the credential the agent uses to authenticate to Engini; it is
|
|
88
|
+
* returned here so a box can be provisioned in one round-trip. Treat the result as a
|
|
89
|
+
* secret. Settings you omit take the server's defaults (2 parallel tasks, 10s pull,
|
|
90
|
+
* `Information`).
|
|
91
|
+
*
|
|
92
|
+
* `applicationSlug` is only needed when more than one on-premise-agent application
|
|
93
|
+
* exists; `EnginiOpaApplicationAmbiguousError.candidateSlugs` tells you what to pass.
|
|
94
|
+
*/
|
|
95
|
+
create(params: {
|
|
96
|
+
name: string;
|
|
97
|
+
description?: string;
|
|
98
|
+
applicationSlug?: string;
|
|
99
|
+
} & OpaSettingsParams): Promise<OpaAgentCreateResponse>;
|
|
100
|
+
/**
|
|
101
|
+
* Change an agent's name, description or runtime settings.
|
|
102
|
+
*
|
|
103
|
+
* **Merges, does not replace**: anything you omit is left unchanged. This is
|
|
104
|
+
* deliberately unlike `connections.update`, which replaces its whole field map.
|
|
105
|
+
*
|
|
106
|
+
* Check `appliedToAgent` on the result. `false` means the change is stored but the
|
|
107
|
+
* agent was not Online to receive it — it picks the new settings up when it
|
|
108
|
+
* reconnects. That is an outcome, not an error.
|
|
109
|
+
*/
|
|
110
|
+
update(agentId: number, params: {
|
|
111
|
+
name?: string;
|
|
112
|
+
description?: string;
|
|
113
|
+
} & OpaSettingsParams): Promise<OpaAgentDetail>;
|
|
114
|
+
/** Permanently delete an agent. Workflows routed through it stop working. */
|
|
115
|
+
delete(agentId: number): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Re-enable a disabled agent; it returns to `WaitingForConnection`.
|
|
118
|
+
*
|
|
119
|
+
* A no-op on an agent that is not disabled — its reported liveness is left alone
|
|
120
|
+
* rather than being reset.
|
|
121
|
+
*/
|
|
122
|
+
enable(agentId: number): Promise<OpaAgentDetail>;
|
|
123
|
+
/**
|
|
124
|
+
* Disable an agent so it stops being marked online.
|
|
125
|
+
*
|
|
126
|
+
* Not a delete and not a pause of in-flight work: tasks it already holds still run
|
|
127
|
+
* to completion.
|
|
128
|
+
*/
|
|
129
|
+
disable(agentId: number): Promise<OpaAgentDetail>;
|
|
130
|
+
/**
|
|
131
|
+
* The agent's token — the credential it authenticates to Engini with.
|
|
132
|
+
*
|
|
133
|
+
* Treat the result as a secret: it is equivalent to the agent's identity. Reads are
|
|
134
|
+
* audit-logged server-side.
|
|
135
|
+
*/
|
|
136
|
+
getToken(agentId: number): Promise<OpaAgentTokenResponse>;
|
|
137
|
+
/**
|
|
138
|
+
* Issue a new token, invalidating the current one immediately.
|
|
139
|
+
*
|
|
140
|
+
* **Destructive to a running agent**: it stops authenticating the moment this
|
|
141
|
+
* resolves and stays down until it is reconfigured with the new token.
|
|
142
|
+
*
|
|
143
|
+
* The API requires `confirm=true` and the SDK sends it unconditionally — the human
|
|
144
|
+
* confirmation gate belongs in the CLI, not in library code where a prompt would be
|
|
145
|
+
* unreachable.
|
|
146
|
+
*/
|
|
147
|
+
rotateToken(agentId: number): Promise<OpaAgentTokenResponse>;
|
|
148
|
+
/**
|
|
149
|
+
* Map the OPA agent-side wire codes onto their typed errors.
|
|
150
|
+
*
|
|
151
|
+
* Returns the error untouched for anything else, so an unrelated 409 or 5xx stays
|
|
152
|
+
* exactly what the status mapping made it. Discriminating on `errorCode` rather than
|
|
153
|
+
* the status matters: OPA puts three distinct conditions on 409/5xx.
|
|
154
|
+
*/
|
|
155
|
+
private retypeAgentError;
|
|
156
|
+
/**
|
|
157
|
+
* Collect the agent's logs as a zip archive, returned as bytes.
|
|
158
|
+
*
|
|
159
|
+
* A live round-trip to the agent, bounded server-side. Throws
|
|
160
|
+
* `EnginiOpaAgentOfflineError` when the agent is not Online,
|
|
161
|
+
* `EnginiOpaAgentTimeoutError` when it did not answer in time, and
|
|
162
|
+
* `EnginiOpaAgentDispatchError` when it answered with a failure.
|
|
163
|
+
*
|
|
164
|
+
* Returns bytes rather than writing a file: the SDK does not know where the caller
|
|
165
|
+
* wants it. `engini opa logs -o` does the writing.
|
|
166
|
+
*
|
|
167
|
+
* `parseAs: "blob"` is passed explicitly rather than relying on hey-api's default
|
|
168
|
+
* `"auto"`. Auto picks a parser from the response Content-Type, so a server or proxy
|
|
169
|
+
* that answered `application/octet-stream` — or nothing — would land on a different
|
|
170
|
+
* branch and hand back something that is not a Blob. This is the only operation in
|
|
171
|
+
* the SDK whose body is genuinely binary, so it is the only one where that matters.
|
|
172
|
+
*/
|
|
173
|
+
downloadLogs(agentId: number): Promise<Uint8Array>;
|
|
174
|
+
}
|
|
175
|
+
export {};
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* On-premise agents resource — provision, inspect, configure and credential OPA agents.
|
|
3
|
+
*
|
|
4
|
+
* An on-premise agent (OPA) is the Engini component a customer runs on their own
|
|
5
|
+
* hardware, behind their firewall, to reach systems the cloud cannot: SQL Server,
|
|
6
|
+
* Oracle, Priority ERP, file shares. It polls Engini for work; Engini never connects
|
|
7
|
+
* to it. Every operation here that "talks to the agent" therefore waits for the
|
|
8
|
+
* agent's next poll, bounded server-side.
|
|
9
|
+
*
|
|
10
|
+
* Agents are NOT connections on this surface: `client.connections` never returns them,
|
|
11
|
+
* and `connections.get(agentId)` is a 404.
|
|
12
|
+
*/
|
|
13
|
+
import { opaAgentsCreateAgent, opaAgentsDeleteAgent, opaAgentsDisableAgent, opaAgentsEnableAgent, opaAgentsGetAgent, opaAgentsGetAgentLogs, opaAgentsGetAgentToken, opaAgentsGetLatestRelease, opaAgentsListAgents, opaAgentsRotateAgentToken, opaAgentsUpdateAgent, } from "@engini/client";
|
|
14
|
+
import { call } from "../http.js";
|
|
15
|
+
import { paginate, collect } from "../pagination.js";
|
|
16
|
+
import { EnginiAPIError, EnginiNotFoundError, EnginiOpaAgentDispatchError, EnginiOpaAgentNotFoundError, EnginiOpaAgentOfflineError, EnginiOpaAgentTimeoutError, EnginiOpaApplicationAmbiguousError, } from "../errors.js";
|
|
17
|
+
export class OpaResource {
|
|
18
|
+
client;
|
|
19
|
+
constructor(client) {
|
|
20
|
+
this.client = client;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Remap a 404 onto the OPA-specific not-found, which the CLI maps to its
|
|
24
|
+
* NOT_FOUND exit code. Anything else passes through untouched.
|
|
25
|
+
*
|
|
26
|
+
* Unlike the MCP equivalent the id is echoed back: an agent id is an ordinary
|
|
27
|
+
* integer, not a secret-equivalent URL component, so it is safe in an error
|
|
28
|
+
* string and it is the one thing that makes the message actionable.
|
|
29
|
+
*/
|
|
30
|
+
retypeNotFound(err, agentId) {
|
|
31
|
+
if (err instanceof EnginiNotFoundError) {
|
|
32
|
+
return new EnginiOpaAgentNotFoundError(`Unknown on-premise agent id ${agentId}.`);
|
|
33
|
+
}
|
|
34
|
+
return err;
|
|
35
|
+
}
|
|
36
|
+
// ---- reads -----------------------------------------------------------------------
|
|
37
|
+
/**
|
|
38
|
+
* Every on-premise agent on the account, paginated.
|
|
39
|
+
*
|
|
40
|
+
* `status` is one of `WaitingForConnection`, `Online`, `Offline` or `Disabled`
|
|
41
|
+
* (case-insensitive on the wire; an unknown value is a 400, not a silently-ignored
|
|
42
|
+
* filter). `name` is a substring match.
|
|
43
|
+
*/
|
|
44
|
+
async list(params = {}) {
|
|
45
|
+
const transport = this.client._transport;
|
|
46
|
+
return collect(paginate((offset, top) => call(opaAgentsListAgents, {
|
|
47
|
+
client: transport,
|
|
48
|
+
query: { offset, top, status: params.status, name: params.name },
|
|
49
|
+
})));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* One agent's status, self-reported version, last heartbeat and settings.
|
|
53
|
+
*
|
|
54
|
+
* Throws `EnginiOpaAgentNotFoundError` when the id is unknown, belongs to another
|
|
55
|
+
* account, or is not an on-premise agent — the API answers 404 for all three
|
|
56
|
+
* deliberately and does not distinguish them.
|
|
57
|
+
*/
|
|
58
|
+
async get(agentId) {
|
|
59
|
+
try {
|
|
60
|
+
return await call(opaAgentsGetAgent, {
|
|
61
|
+
client: this.client._transport,
|
|
62
|
+
path: { id: agentId },
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
throw this.retypeNotFound(err, agentId);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Where to download the current agent installer.
|
|
71
|
+
*
|
|
72
|
+
* `degraded: true` means the release manifest was not reachable and the result was
|
|
73
|
+
* reconstructed from the installer URL alone: `sha256`, `sizeBytes` and `releasedAt`
|
|
74
|
+
* are then null. A caller that needs integrity verification should refuse a degraded
|
|
75
|
+
* manifest.
|
|
76
|
+
*/
|
|
77
|
+
async latestRelease() {
|
|
78
|
+
return call(opaAgentsGetLatestRelease, { client: this.client._transport });
|
|
79
|
+
}
|
|
80
|
+
// ---- lifecycle -------------------------------------------------------------------
|
|
81
|
+
/**
|
|
82
|
+
* Build a settings object carrying ONLY what the caller supplied, or `undefined`
|
|
83
|
+
* when nothing was.
|
|
84
|
+
*
|
|
85
|
+
* Every member is nullable on the wire and absent means "leave it alone / use the
|
|
86
|
+
* server default". A client-side default here would either override a real value on
|
|
87
|
+
* update or be rejected on create: the minimums are the connector's own declared
|
|
88
|
+
* metadata, read server-side. Duplicating those numbers here is how they drifted
|
|
89
|
+
* before.
|
|
90
|
+
*/
|
|
91
|
+
static settings(params) {
|
|
92
|
+
const { parallelTasks, pullPeriodSeconds, logLevel } = params;
|
|
93
|
+
if (parallelTasks === undefined && pullPeriodSeconds === undefined && logLevel === undefined) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
return { parallelTasks, pullPeriodSeconds, logLevel };
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Retype a 409 `OPA_APPLICATION_AMBIGUOUS` so it carries the candidate slugs.
|
|
100
|
+
*
|
|
101
|
+
* They arrive as `details[].issue === "applicationSlug: <slug>"`, and are worth the
|
|
102
|
+
* parsing: `/v1/applications` does not list OPA applications, so this error is the
|
|
103
|
+
* only way a caller can discover what to pass as `applicationSlug`.
|
|
104
|
+
*/
|
|
105
|
+
retypeAmbiguous(err) {
|
|
106
|
+
if (!(err instanceof EnginiAPIError) || err.errorCode !== "OPA_APPLICATION_AMBIGUOUS") {
|
|
107
|
+
return err;
|
|
108
|
+
}
|
|
109
|
+
const prefix = "applicationSlug: ";
|
|
110
|
+
const slugs = err.details
|
|
111
|
+
.map((d) => d.issue)
|
|
112
|
+
.filter((issue) => typeof issue === "string" && issue.startsWith(prefix))
|
|
113
|
+
.map((issue) => issue.slice(prefix.length));
|
|
114
|
+
return new EnginiOpaApplicationAmbiguousError(err.message, slugs, {
|
|
115
|
+
errorCode: err.errorCode,
|
|
116
|
+
details: err.details,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Register a new on-premise agent. The response carries its token.
|
|
121
|
+
*
|
|
122
|
+
* The token is the credential the agent uses to authenticate to Engini; it is
|
|
123
|
+
* returned here so a box can be provisioned in one round-trip. Treat the result as a
|
|
124
|
+
* secret. Settings you omit take the server's defaults (2 parallel tasks, 10s pull,
|
|
125
|
+
* `Information`).
|
|
126
|
+
*
|
|
127
|
+
* `applicationSlug` is only needed when more than one on-premise-agent application
|
|
128
|
+
* exists; `EnginiOpaApplicationAmbiguousError.candidateSlugs` tells you what to pass.
|
|
129
|
+
*/
|
|
130
|
+
async create(params) {
|
|
131
|
+
try {
|
|
132
|
+
return await call(opaAgentsCreateAgent, {
|
|
133
|
+
client: this.client._transport,
|
|
134
|
+
body: {
|
|
135
|
+
name: params.name,
|
|
136
|
+
description: params.description,
|
|
137
|
+
applicationSlug: params.applicationSlug,
|
|
138
|
+
settings: OpaResource.settings(params),
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
throw this.retypeAmbiguous(err);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Change an agent's name, description or runtime settings.
|
|
148
|
+
*
|
|
149
|
+
* **Merges, does not replace**: anything you omit is left unchanged. This is
|
|
150
|
+
* deliberately unlike `connections.update`, which replaces its whole field map.
|
|
151
|
+
*
|
|
152
|
+
* Check `appliedToAgent` on the result. `false` means the change is stored but the
|
|
153
|
+
* agent was not Online to receive it — it picks the new settings up when it
|
|
154
|
+
* reconnects. That is an outcome, not an error.
|
|
155
|
+
*/
|
|
156
|
+
async update(agentId, params) {
|
|
157
|
+
try {
|
|
158
|
+
return await call(opaAgentsUpdateAgent, {
|
|
159
|
+
client: this.client._transport,
|
|
160
|
+
path: { id: agentId },
|
|
161
|
+
body: {
|
|
162
|
+
name: params.name,
|
|
163
|
+
description: params.description,
|
|
164
|
+
settings: OpaResource.settings(params),
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
throw this.retypeNotFound(err, agentId);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Permanently delete an agent. Workflows routed through it stop working. */
|
|
173
|
+
async delete(agentId) {
|
|
174
|
+
try {
|
|
175
|
+
await call(opaAgentsDeleteAgent, {
|
|
176
|
+
client: this.client._transport,
|
|
177
|
+
path: { id: agentId },
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
throw this.retypeNotFound(err, agentId);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Re-enable a disabled agent; it returns to `WaitingForConnection`.
|
|
186
|
+
*
|
|
187
|
+
* A no-op on an agent that is not disabled — its reported liveness is left alone
|
|
188
|
+
* rather than being reset.
|
|
189
|
+
*/
|
|
190
|
+
async enable(agentId) {
|
|
191
|
+
try {
|
|
192
|
+
return await call(opaAgentsEnableAgent, {
|
|
193
|
+
client: this.client._transport,
|
|
194
|
+
path: { id: agentId },
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
throw this.retypeNotFound(err, agentId);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Disable an agent so it stops being marked online.
|
|
203
|
+
*
|
|
204
|
+
* Not a delete and not a pause of in-flight work: tasks it already holds still run
|
|
205
|
+
* to completion.
|
|
206
|
+
*/
|
|
207
|
+
async disable(agentId) {
|
|
208
|
+
try {
|
|
209
|
+
return await call(opaAgentsDisableAgent, {
|
|
210
|
+
client: this.client._transport,
|
|
211
|
+
path: { id: agentId },
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
catch (err) {
|
|
215
|
+
throw this.retypeNotFound(err, agentId);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// ---- credential ------------------------------------------------------------------
|
|
219
|
+
/**
|
|
220
|
+
* The agent's token — the credential it authenticates to Engini with.
|
|
221
|
+
*
|
|
222
|
+
* Treat the result as a secret: it is equivalent to the agent's identity. Reads are
|
|
223
|
+
* audit-logged server-side.
|
|
224
|
+
*/
|
|
225
|
+
async getToken(agentId) {
|
|
226
|
+
try {
|
|
227
|
+
return await call(opaAgentsGetAgentToken, {
|
|
228
|
+
client: this.client._transport,
|
|
229
|
+
path: { id: agentId },
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
throw this.retypeNotFound(err, agentId);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Issue a new token, invalidating the current one immediately.
|
|
238
|
+
*
|
|
239
|
+
* **Destructive to a running agent**: it stops authenticating the moment this
|
|
240
|
+
* resolves and stays down until it is reconfigured with the new token.
|
|
241
|
+
*
|
|
242
|
+
* The API requires `confirm=true` and the SDK sends it unconditionally — the human
|
|
243
|
+
* confirmation gate belongs in the CLI, not in library code where a prompt would be
|
|
244
|
+
* unreachable.
|
|
245
|
+
*/
|
|
246
|
+
async rotateToken(agentId) {
|
|
247
|
+
try {
|
|
248
|
+
return await call(opaAgentsRotateAgentToken, {
|
|
249
|
+
client: this.client._transport,
|
|
250
|
+
path: { id: agentId },
|
|
251
|
+
query: { confirm: true },
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
catch (err) {
|
|
255
|
+
throw this.retypeNotFound(err, agentId);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
// ---- logs ------------------------------------------------------------------------
|
|
259
|
+
/**
|
|
260
|
+
* Map the OPA agent-side wire codes onto their typed errors.
|
|
261
|
+
*
|
|
262
|
+
* Returns the error untouched for anything else, so an unrelated 409 or 5xx stays
|
|
263
|
+
* exactly what the status mapping made it. Discriminating on `errorCode` rather than
|
|
264
|
+
* the status matters: OPA puts three distinct conditions on 409/5xx.
|
|
265
|
+
*/
|
|
266
|
+
retypeAgentError(err) {
|
|
267
|
+
if (!(err instanceof EnginiAPIError))
|
|
268
|
+
return err;
|
|
269
|
+
const options = { errorCode: err.errorCode, details: err.details };
|
|
270
|
+
switch (err.errorCode) {
|
|
271
|
+
case "OPA_AGENT_OFFLINE":
|
|
272
|
+
return new EnginiOpaAgentOfflineError(err.message, options);
|
|
273
|
+
case "OPA_AGENT_TIMEOUT":
|
|
274
|
+
return new EnginiOpaAgentTimeoutError(err.message, options);
|
|
275
|
+
case "OPA_AGENT_DISPATCH_FAILED":
|
|
276
|
+
return new EnginiOpaAgentDispatchError(err.message, options);
|
|
277
|
+
default:
|
|
278
|
+
return err;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Collect the agent's logs as a zip archive, returned as bytes.
|
|
283
|
+
*
|
|
284
|
+
* A live round-trip to the agent, bounded server-side. Throws
|
|
285
|
+
* `EnginiOpaAgentOfflineError` when the agent is not Online,
|
|
286
|
+
* `EnginiOpaAgentTimeoutError` when it did not answer in time, and
|
|
287
|
+
* `EnginiOpaAgentDispatchError` when it answered with a failure.
|
|
288
|
+
*
|
|
289
|
+
* Returns bytes rather than writing a file: the SDK does not know where the caller
|
|
290
|
+
* wants it. `engini opa logs -o` does the writing.
|
|
291
|
+
*
|
|
292
|
+
* `parseAs: "blob"` is passed explicitly rather than relying on hey-api's default
|
|
293
|
+
* `"auto"`. Auto picks a parser from the response Content-Type, so a server or proxy
|
|
294
|
+
* that answered `application/octet-stream` — or nothing — would land on a different
|
|
295
|
+
* branch and hand back something that is not a Blob. This is the only operation in
|
|
296
|
+
* the SDK whose body is genuinely binary, so it is the only one where that matters.
|
|
297
|
+
*/
|
|
298
|
+
async downloadLogs(agentId) {
|
|
299
|
+
let payload;
|
|
300
|
+
try {
|
|
301
|
+
payload = await call(opaAgentsGetAgentLogs, {
|
|
302
|
+
client: this.client._transport,
|
|
303
|
+
path: { id: agentId },
|
|
304
|
+
// `as const` is load-bearing: through `call`'s generic inference the literal
|
|
305
|
+
// would otherwise widen to `string` and fail the generated option type.
|
|
306
|
+
parseAs: "blob",
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
catch (err) {
|
|
310
|
+
throw this.retypeAgentError(this.retypeNotFound(err, agentId));
|
|
311
|
+
}
|
|
312
|
+
if (payload instanceof Blob)
|
|
313
|
+
return new Uint8Array(await payload.arrayBuffer());
|
|
314
|
+
if (payload instanceof Uint8Array)
|
|
315
|
+
return payload;
|
|
316
|
+
if (payload instanceof ArrayBuffer)
|
|
317
|
+
return new Uint8Array(payload);
|
|
318
|
+
// A 2xx with no body at all: report it as the agent having produced nothing rather
|
|
319
|
+
// than returning a Uint8Array built from something that is not bytes.
|
|
320
|
+
throw new EnginiOpaAgentDispatchError(`OPA agent ${agentId} returned no log archive.`);
|
|
321
|
+
}
|
|
322
|
+
}
|