@tenonhq/dovetail-core 0.0.130 → 0.0.131
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 +38 -0
- package/dist/authFailure.js +243 -0
- package/dist/bootstrap.js +4 -0
- package/dist/index.js +4 -0
- package/dist/reconcileCommand.js +1 -0
- package/dist/snClient.js +77 -5
- package/dist/tests/authExitCode.process.test.js +81 -0
- package/dist/tests/authFailure.test.js +227 -0
- package/dist/tests/retryOnHttpErr.test.js +37 -2
- package/dist/tests/snClient.authInterceptor.test.js +250 -0
- package/package.json +2 -2
- package/release-manifest.json +546 -734
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const authFailure_1 = require("../authFailure");
|
|
4
|
+
var SECRET = "hunter2-do-not-print";
|
|
5
|
+
var HOST = "example.invalid";
|
|
6
|
+
describe("authFailure (#185)", function () {
|
|
7
|
+
beforeEach(function () {
|
|
8
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
9
|
+
});
|
|
10
|
+
it("uses a distinct exit code", function () {
|
|
11
|
+
expect(authFailure_1.AUTH_EXIT_CODE).toBe(77);
|
|
12
|
+
expect((0, authFailure_1.isAuthStatus)(401)).toBe(true);
|
|
13
|
+
expect((0, authFailure_1.isAuthStatus)(403)).toBe(true);
|
|
14
|
+
expect((0, authFailure_1.isAuthStatus)(404)).toBe(false);
|
|
15
|
+
expect((0, authFailure_1.isAuthStatus)("401")).toBe(false);
|
|
16
|
+
});
|
|
17
|
+
describe("instanceHost", function () {
|
|
18
|
+
it("reduces an instance value to a bare host", function () {
|
|
19
|
+
expect((0, authFailure_1.instanceHost)(HOST)).toBe(HOST);
|
|
20
|
+
expect((0, authFailure_1.instanceHost)("https://" + HOST + "/")).toBe(HOST);
|
|
21
|
+
expect((0, authFailure_1.instanceHost)("https://" + HOST + "/api/now?x=1")).toBe(HOST);
|
|
22
|
+
});
|
|
23
|
+
it("drops userinfo so a credential in the value cannot leak", function () {
|
|
24
|
+
expect((0, authFailure_1.instanceHost)("https://admin:" + SECRET + "@" + HOST)).toBe(HOST);
|
|
25
|
+
});
|
|
26
|
+
it("returns empty for anything that is not a plain host", function () {
|
|
27
|
+
expect((0, authFailure_1.instanceHost)("")).toBe("");
|
|
28
|
+
expect((0, authFailure_1.instanceHost)(undefined)).toBe("");
|
|
29
|
+
expect((0, authFailure_1.instanceHost)("not a host; echo x")).toBe("");
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
describe("classifyAuthRejection", function () {
|
|
33
|
+
it("treats a 401 on any method as a rejection, a write only when it is not a GET", function () {
|
|
34
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 401, method: "get" })).toEqual({ status: 401, write: false });
|
|
35
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 401, method: "POST" })).toEqual({ status: 401, write: true });
|
|
36
|
+
});
|
|
37
|
+
it("treats a 403 as a rejection only on a write", function () {
|
|
38
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 403, method: "GET" })).toBeNull();
|
|
39
|
+
["post", "PUT", "patch", "DELETE"].forEach(function (method) {
|
|
40
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 403, method: method, url: "api/now/table/x" })).toEqual({
|
|
41
|
+
status: 403,
|
|
42
|
+
write: true,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
it("counts the read-only POST endpoints as reads", function () {
|
|
47
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 403, method: "POST", url: "api/cadso/dovetail_sync/getManifest/x_s" })).toBeNull();
|
|
48
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 403, method: "POST", url: "/api/cadso/dovetail_sync/bulkDownload?x=1" })).toBeNull();
|
|
49
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 401, method: "POST", url: "api/cadso/dovetail_sync/bulkDownload" })).toEqual({ status: 401, write: false });
|
|
50
|
+
// A look-alike name is still a write.
|
|
51
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 403, method: "POST", url: "api/cadso/dovetail_sync/getManifestX" })).toEqual({ status: 403, write: true });
|
|
52
|
+
});
|
|
53
|
+
it("ignores other statuses and never calls a request with no method a write", function () {
|
|
54
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 404, method: "POST" })).toBeNull();
|
|
55
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: "403", method: "POST" })).toBeNull();
|
|
56
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 403, method: undefined })).toBeNull();
|
|
57
|
+
expect((0, authFailure_1.classifyAuthRejection)({ status: 401, method: 7 })).toEqual({ status: 401, write: false });
|
|
58
|
+
});
|
|
59
|
+
it("reports the first rejection of each kind once", function () {
|
|
60
|
+
expect((0, authFailure_1.firstRejectionOfKind)({ status: 403, write: true })).toBe(true);
|
|
61
|
+
expect((0, authFailure_1.firstRejectionOfKind)({ status: 403, write: true })).toBe(false);
|
|
62
|
+
expect((0, authFailure_1.firstRejectionOfKind)({ status: 401, write: true })).toBe(true);
|
|
63
|
+
expect((0, authFailure_1.firstRejectionOfKind)({ status: 401, write: false })).toBe(true);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe("requestPath", function () {
|
|
67
|
+
it("keeps only the path", function () {
|
|
68
|
+
expect((0, authFailure_1.requestPath)("api/now/table/x/abc")).toBe("api/now/table/x/abc");
|
|
69
|
+
expect((0, authFailure_1.requestPath)("/api/now/table/x?sysparm_query=a%3Db#frag")).toBe("api/now/table/x");
|
|
70
|
+
expect((0, authFailure_1.requestPath)("https://admin:" + SECRET + "@" + HOST + "/api/now/table/x")).toBe("api/now/table/x");
|
|
71
|
+
});
|
|
72
|
+
it("returns empty for anything it cannot echo safely", function () {
|
|
73
|
+
expect((0, authFailure_1.requestPath)(undefined)).toBe("");
|
|
74
|
+
expect((0, authFailure_1.requestPath)("")).toBe("");
|
|
75
|
+
expect((0, authFailure_1.requestPath)("api/now/table/x y")).toBe("");
|
|
76
|
+
expect((0, authFailure_1.requestPath)("api/$(whoami)")).toBe("");
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
describe("reauthCommand", function () {
|
|
80
|
+
it("is plain dove login when no --env was used", function () {
|
|
81
|
+
expect((0, authFailure_1.reauthCommand)()).toBe("dove login");
|
|
82
|
+
});
|
|
83
|
+
it("carries the --env selector the user typed", function () {
|
|
84
|
+
(0, authFailure_1.setAuthContext)({ envPath: "/work/.env.loft", envArg: "loft" });
|
|
85
|
+
expect((0, authFailure_1.reauthCommand)()).toBe("dove login --env loft");
|
|
86
|
+
expect((0, authFailure_1.reauthCommand)("../envs/shop.env")).toBe("dove login --env ../envs/shop.env");
|
|
87
|
+
});
|
|
88
|
+
it("never echoes a selector with shell metacharacters", function () {
|
|
89
|
+
expect((0, authFailure_1.reauthCommand)("loft; echo x")).toBe("dove login --env <name>");
|
|
90
|
+
expect((0, authFailure_1.reauthCommand)("$(whoami)")).toBe("dove login --env <name>");
|
|
91
|
+
expect((0, authFailure_1.reauthCommand)("a b")).toBe("dove login --env <name>");
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
describe("buildAuthFailureMessage", function () {
|
|
95
|
+
it("names the host, the env file and the re-auth command for a 401", function () {
|
|
96
|
+
(0, authFailure_1.setAuthContext)({ envPath: "/work/.env.loft", envArg: "loft" });
|
|
97
|
+
var msg = (0, authFailure_1.buildAuthFailureMessage)({
|
|
98
|
+
status: 401,
|
|
99
|
+
action: "pushing sys_script_include > Thing",
|
|
100
|
+
instance: HOST,
|
|
101
|
+
});
|
|
102
|
+
expect(msg).toContain("Unauthorized (401) pushing sys_script_include > Thing");
|
|
103
|
+
expect(msg).toContain(HOST + " rejected the credentials loaded from /work/.env.loft");
|
|
104
|
+
expect(msg).toContain("Re-authenticate with: dove login --env loft");
|
|
105
|
+
expect(msg).toContain("exits 77");
|
|
106
|
+
});
|
|
107
|
+
it("explains a 403 as a refused write and does not offer dove login as the remedy", function () {
|
|
108
|
+
(0, authFailure_1.setAuthContext)({ envPath: "/work/.env", envArg: "" });
|
|
109
|
+
var msg = (0, authFailure_1.buildAuthFailureMessage)({
|
|
110
|
+
status: 403,
|
|
111
|
+
action: "calling POST api/x",
|
|
112
|
+
instance: HOST,
|
|
113
|
+
table: "sys_script_include",
|
|
114
|
+
});
|
|
115
|
+
expect(msg).toContain("Forbidden (403) calling POST api/x");
|
|
116
|
+
expect(msg).toContain(HOST + " accepted the sign-in but refused the write for this user");
|
|
117
|
+
expect(msg).toContain("Check the role or ACL for table sys_script_include for the user in /work/.env");
|
|
118
|
+
expect(msg).toContain("exits 77");
|
|
119
|
+
expect(msg).not.toContain("dove login");
|
|
120
|
+
expect(msg).not.toContain("Re-authenticate");
|
|
121
|
+
});
|
|
122
|
+
it("a 403 with no usable table name says 'the table' and never echoes an odd value", function () {
|
|
123
|
+
var plain = (0, authFailure_1.buildAuthFailureMessage)({ status: 403, action: "calling POST api/x" });
|
|
124
|
+
expect(plain).toContain("Check the role or ACL for the table for the user in the loaded env file");
|
|
125
|
+
var odd = (0, authFailure_1.buildAuthFailureMessage)({
|
|
126
|
+
status: 403,
|
|
127
|
+
action: "calling POST api/x",
|
|
128
|
+
table: "x; echo " + SECRET,
|
|
129
|
+
});
|
|
130
|
+
expect(odd).toContain("for the table for the user");
|
|
131
|
+
expect(odd).not.toContain(SECRET);
|
|
132
|
+
});
|
|
133
|
+
it("a 401 still offers dove login", function () {
|
|
134
|
+
var msg = (0, authFailure_1.buildAuthFailureMessage)({ status: 401, action: "calling GET api/x", instance: HOST });
|
|
135
|
+
expect(msg).toContain("Re-authenticate with: dove login");
|
|
136
|
+
});
|
|
137
|
+
it("falls back to neutral wording when the host and env file are unknown", function () {
|
|
138
|
+
var msg = (0, authFailure_1.buildAuthFailureMessage)({ status: 401, action: "pushing x" });
|
|
139
|
+
expect(msg).toContain("the ServiceNow instance rejected the credentials in the loaded env file");
|
|
140
|
+
expect(msg).toContain("dove login");
|
|
141
|
+
});
|
|
142
|
+
it("never prints a credential, whatever the environment holds", function () {
|
|
143
|
+
var saved = { user: process.env.SN_USER, password: process.env.SN_PASSWORD, key: process.env.SN_API_KEY };
|
|
144
|
+
process.env.SN_USER = "svc-" + SECRET;
|
|
145
|
+
process.env.SN_PASSWORD = SECRET;
|
|
146
|
+
process.env.SN_API_KEY = "key-" + SECRET;
|
|
147
|
+
try {
|
|
148
|
+
(0, authFailure_1.setAuthContext)({ envPath: "/work/.env.loft", envArg: "loft" });
|
|
149
|
+
[401, 403].forEach(function (status) {
|
|
150
|
+
var msg = (0, authFailure_1.buildAuthFailureMessage)({
|
|
151
|
+
status: status,
|
|
152
|
+
action: "pushing x",
|
|
153
|
+
instance: "https://admin:" + SECRET + "@" + HOST,
|
|
154
|
+
});
|
|
155
|
+
expect(msg).not.toContain(SECRET);
|
|
156
|
+
expect(msg).toContain(HOST);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
if (saved.user === undefined)
|
|
161
|
+
delete process.env.SN_USER;
|
|
162
|
+
else
|
|
163
|
+
process.env.SN_USER = saved.user;
|
|
164
|
+
if (saved.password === undefined)
|
|
165
|
+
delete process.env.SN_PASSWORD;
|
|
166
|
+
else
|
|
167
|
+
process.env.SN_PASSWORD = saved.password;
|
|
168
|
+
if (saved.key === undefined)
|
|
169
|
+
delete process.env.SN_API_KEY;
|
|
170
|
+
else
|
|
171
|
+
process.env.SN_API_KEY = saved.key;
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
describe("resolveExitCode", function () {
|
|
176
|
+
it("leaves every code alone when nothing was rejected", function () {
|
|
177
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
178
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
179
|
+
expect((0, authFailure_1.resolveExitCode)(2)).toBe(2);
|
|
180
|
+
expect((0, authFailure_1.resolveExitCode)(undefined)).toBe(0);
|
|
181
|
+
});
|
|
182
|
+
it("re-maps only a generic failure after a rejected read", function () {
|
|
183
|
+
(0, authFailure_1.recordAuthFailure)();
|
|
184
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(77);
|
|
185
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
186
|
+
expect((0, authFailure_1.resolveExitCode)(2)).toBe(2);
|
|
187
|
+
});
|
|
188
|
+
it("never lets a rejected write exit clean, and keeps other codes", function () {
|
|
189
|
+
(0, authFailure_1.recordAuthFailure)({ write: true });
|
|
190
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(77);
|
|
191
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(77);
|
|
192
|
+
expect((0, authFailure_1.resolveExitCode)(2)).toBe(2);
|
|
193
|
+
expect((0, authFailure_1.resolveExitCode)(130)).toBe(130);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
describe("installAuthExitCode", function () {
|
|
197
|
+
function fakeProcess() {
|
|
198
|
+
var listeners = [];
|
|
199
|
+
var proc = {
|
|
200
|
+
exitCode: undefined,
|
|
201
|
+
on: function (_event, listener) {
|
|
202
|
+
listeners.push(listener);
|
|
203
|
+
return proc;
|
|
204
|
+
},
|
|
205
|
+
emitExit: function (code) {
|
|
206
|
+
listeners.forEach(function (l) {
|
|
207
|
+
l(code);
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
return proc;
|
|
212
|
+
}
|
|
213
|
+
it("sets the auth exit code on exit after a rejected write", function () {
|
|
214
|
+
var proc = fakeProcess();
|
|
215
|
+
(0, authFailure_1.installAuthExitCode)(proc);
|
|
216
|
+
(0, authFailure_1.recordAuthFailure)({ write: true });
|
|
217
|
+
proc.emitExit(1);
|
|
218
|
+
expect(proc.exitCode).toBe(77);
|
|
219
|
+
});
|
|
220
|
+
it("does not touch the exit code when nothing was rejected", function () {
|
|
221
|
+
var proc = fakeProcess();
|
|
222
|
+
(0, authFailure_1.installAuthExitCode)(proc);
|
|
223
|
+
proc.emitExit(1);
|
|
224
|
+
expect(proc.exitCode).toBeUndefined();
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
});
|
|
@@ -21,6 +21,7 @@ jest.mock("../genericUtils", function () {
|
|
|
21
21
|
});
|
|
22
22
|
const snClient_1 = require("../snClient");
|
|
23
23
|
const genericUtils_1 = require("../genericUtils");
|
|
24
|
+
const authFailure_1 = require("../authFailure");
|
|
24
25
|
function makeAxiosError(status, headers) {
|
|
25
26
|
var error = new Error("Request failed with status " + status);
|
|
26
27
|
error.isAxiosError = true;
|
|
@@ -49,12 +50,46 @@ describe("retryOnHttpErr", function () {
|
|
|
49
50
|
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("Unauthorized (401)"));
|
|
50
51
|
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("credentials"));
|
|
51
52
|
});
|
|
52
|
-
it("fails immediately on 403 with
|
|
53
|
+
it("fails immediately on 403 with a refused-write message", async function () {
|
|
53
54
|
var fn = jest.fn().mockRejectedValue(makeAxiosError(403));
|
|
54
55
|
await expect((0, snClient_1.retryOnHttpErr)(fn, "test > rec1")).rejects.toThrow();
|
|
55
56
|
expect(fn).toHaveBeenCalledTimes(1);
|
|
56
57
|
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("Forbidden (403)"));
|
|
57
|
-
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("
|
|
58
|
+
expect(mockLogger.error).toHaveBeenCalledWith(expect.stringContaining("refused the write"));
|
|
59
|
+
});
|
|
60
|
+
it("401/403: the per-record line names the record and prints no credential; recording is the client interceptor's job (#185)", async function () {
|
|
61
|
+
var saved = { password: process.env.SN_PASSWORD };
|
|
62
|
+
process.env.SN_PASSWORD = "hunter2-do-not-print";
|
|
63
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
64
|
+
try {
|
|
65
|
+
var statuses = [401, 403];
|
|
66
|
+
for (var i = 0; i < statuses.length; i += 1) {
|
|
67
|
+
mockLogger.error.mockClear();
|
|
68
|
+
var err = makeAxiosError(statuses[i]);
|
|
69
|
+
var fn = jest.fn().mockRejectedValue(err);
|
|
70
|
+
// The original error is still what is thrown — callers keep its status.
|
|
71
|
+
await expect((0, snClient_1.retryOnHttpErr)(fn, "test > rec1")).rejects.toBe(err);
|
|
72
|
+
var logged = String(mockLogger.error.mock.calls[0][0]);
|
|
73
|
+
expect(logged).toContain("pushing test > rec1");
|
|
74
|
+
expect(logged).toContain("See the message above");
|
|
75
|
+
expect(logged).not.toContain("hunter2-do-not-print");
|
|
76
|
+
}
|
|
77
|
+
// A function that never went through an snClient records nothing here;
|
|
78
|
+
// snClient.authInterceptor.test.ts covers the recording.
|
|
79
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
83
|
+
if (saved.password === undefined)
|
|
84
|
+
delete process.env.SN_PASSWORD;
|
|
85
|
+
else
|
|
86
|
+
process.env.SN_PASSWORD = saved.password;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
it("a 404 does not record an auth failure", async function () {
|
|
90
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
91
|
+
await expect((0, snClient_1.retryOnHttpErr)(jest.fn().mockRejectedValue(makeAxiosError(404)), "test > rec1")).rejects.toThrow();
|
|
92
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
58
93
|
});
|
|
59
94
|
// 404: Fail immediately
|
|
60
95
|
it("fails immediately on 404 with record not found message", async function () {
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Every request an snClient makes passes its response interceptor, which is the
|
|
3
|
+
// ONE place an HTTP 401/403 is recorded (#185). These tests drive real client
|
|
4
|
+
// methods through a stub axios adapter, so the interceptor itself is exercised:
|
|
5
|
+
// a rejected write that is NOT `push` (reconcile CREATE/DELETE, create-record,
|
|
6
|
+
// delete-record, createUpdateSet) must exit 77 like a rejected push does.
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
var mockLogger = {
|
|
9
|
+
info: jest.fn(),
|
|
10
|
+
debug: jest.fn(),
|
|
11
|
+
warn: jest.fn(),
|
|
12
|
+
error: jest.fn(),
|
|
13
|
+
success: jest.fn(),
|
|
14
|
+
getLogLevel: function () {
|
|
15
|
+
return "debug";
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
jest.mock("../Logger", function () {
|
|
19
|
+
return { logger: mockLogger };
|
|
20
|
+
});
|
|
21
|
+
jest.mock("../FileLogger", function () {
|
|
22
|
+
return {
|
|
23
|
+
fileLogger: {
|
|
24
|
+
debug: jest.fn(),
|
|
25
|
+
info: jest.fn(),
|
|
26
|
+
warn: jest.fn(),
|
|
27
|
+
error: jest.fn(),
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
jest.mock("../genericUtils", function () {
|
|
32
|
+
return { wait: jest.fn().mockResolvedValue(undefined) };
|
|
33
|
+
});
|
|
34
|
+
jest.mock("../config", function () {
|
|
35
|
+
return {
|
|
36
|
+
getSourcePathForScope: function () {
|
|
37
|
+
return "/nonexistent-reconcile-source";
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
var mockHolder = { client: undefined };
|
|
42
|
+
jest.mock("../snClient", function () {
|
|
43
|
+
var actual = jest.requireActual("../snClient");
|
|
44
|
+
return Object.assign({}, actual, {
|
|
45
|
+
defaultClient: function () {
|
|
46
|
+
return mockHolder.client;
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
const axios_1 = require("axios");
|
|
51
|
+
const snClient_1 = require("../snClient");
|
|
52
|
+
const reconcileCommand_1 = require("../reconcileCommand");
|
|
53
|
+
const authFailure_1 = require("../authFailure");
|
|
54
|
+
var SECRET = "hunter2-do-not-print";
|
|
55
|
+
// A client whose transport answers every request with `status`. Only the adapter
|
|
56
|
+
// is replaced, so the request still runs the client's real interceptors.
|
|
57
|
+
function clientAnswering(status) {
|
|
58
|
+
var seen = [];
|
|
59
|
+
var c = (0, snClient_1.snClient)("https://admin:" + SECRET + "@example.invalid/", "svc-user", SECRET);
|
|
60
|
+
c.client.defaults.adapter = function (config) {
|
|
61
|
+
seen.push({
|
|
62
|
+
method: String(config.method).toUpperCase(),
|
|
63
|
+
url: String(config.url),
|
|
64
|
+
});
|
|
65
|
+
var response = {
|
|
66
|
+
status: status,
|
|
67
|
+
statusText: "",
|
|
68
|
+
headers: {},
|
|
69
|
+
config: config,
|
|
70
|
+
data: { error: { message: "denied" } },
|
|
71
|
+
};
|
|
72
|
+
return Promise.reject(new axios_1.AxiosError("Request failed with status code " + status, "ERR_BAD_REQUEST", config, null, response));
|
|
73
|
+
};
|
|
74
|
+
return { client: c, seen: seen };
|
|
75
|
+
}
|
|
76
|
+
function loggedErrors() {
|
|
77
|
+
return mockLogger.error.mock.calls
|
|
78
|
+
.map(function (c) {
|
|
79
|
+
return String(c[0]);
|
|
80
|
+
})
|
|
81
|
+
.join("\n");
|
|
82
|
+
}
|
|
83
|
+
function change(kind) {
|
|
84
|
+
return {
|
|
85
|
+
kind: kind,
|
|
86
|
+
table: "sys_script_include",
|
|
87
|
+
scope: "x_s",
|
|
88
|
+
sys_id: "abc123",
|
|
89
|
+
name: "Foo",
|
|
90
|
+
fieldDeltas: [],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
describe("snClient auth interceptor (#185)", function () {
|
|
94
|
+
beforeEach(function () {
|
|
95
|
+
jest.clearAllMocks();
|
|
96
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
97
|
+
(0, snClient_1._resetDovetailApiFallback)();
|
|
98
|
+
(0, authFailure_1.setAuthContext)({ envPath: "/work/.env.loft", envArg: "loft" });
|
|
99
|
+
});
|
|
100
|
+
afterAll(function () {
|
|
101
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
102
|
+
});
|
|
103
|
+
it("a 403 on createRecord is a rejected write: exit 77 even over a clean exit", async function () {
|
|
104
|
+
var t = clientAnswering(403);
|
|
105
|
+
await expect(t.client.createRecord({
|
|
106
|
+
table: "sys_script_include",
|
|
107
|
+
fields: { name: "Foo" },
|
|
108
|
+
})).rejects.toMatchObject({ response: { status: 403 } });
|
|
109
|
+
expect(t.seen).toEqual([
|
|
110
|
+
{ method: "POST", url: "api/cadso/dovetail_core/createRecord" },
|
|
111
|
+
]);
|
|
112
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
113
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
114
|
+
var lines = loggedErrors();
|
|
115
|
+
expect(lines).toContain("Forbidden (403) calling POST api/cadso/dovetail_core/createRecord");
|
|
116
|
+
expect(lines).toContain("example.invalid");
|
|
117
|
+
expect(lines).toContain("/work/.env.loft");
|
|
118
|
+
// The table comes from the request body. A 403 does not send the user to dove login.
|
|
119
|
+
expect(lines).toContain("Check the role or ACL for table sys_script_include");
|
|
120
|
+
expect(lines).not.toContain("dove login");
|
|
121
|
+
expect(lines).not.toContain(SECRET);
|
|
122
|
+
});
|
|
123
|
+
it("a 403 on a Table API write names the table from the path", async function () {
|
|
124
|
+
var t = clientAnswering(403);
|
|
125
|
+
await expect(t.client.updateRecord("sys_script", "abc123", { script: "x" })).rejects.toMatchObject({ response: { status: 403 } });
|
|
126
|
+
expect(loggedErrors()).toContain("Check the role or ACL for table sys_script for the user");
|
|
127
|
+
});
|
|
128
|
+
it("a 403 on deleteRecord is a rejected write", async function () {
|
|
129
|
+
var t = clientAnswering(403);
|
|
130
|
+
await expect(t.client.deleteRecord({ table: "sys_script_include", sys_id: "abc123" })).rejects.toMatchObject({ response: { status: 403 } });
|
|
131
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
132
|
+
});
|
|
133
|
+
it("a 403 on createUpdateSet through unwrapSNResponse is a rejected write", async function () {
|
|
134
|
+
var t = clientAnswering(403);
|
|
135
|
+
await expect((0, snClient_1.unwrapSNResponse)(t.client.createUpdateSet("My set", "scope1"))).rejects.toMatchObject({
|
|
136
|
+
response: { status: 403 },
|
|
137
|
+
});
|
|
138
|
+
expect(t.seen[0].method).toBe("POST");
|
|
139
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
140
|
+
});
|
|
141
|
+
it("reconcile --apply: a 403 on CREATE and on DELETE fails the record and exits 77", async function () {
|
|
142
|
+
var t = clientAnswering(403);
|
|
143
|
+
mockHolder.client = t.client;
|
|
144
|
+
var deleted = await (0, reconcileCommand_1.applyDeletes)("x_s", [change("delete")]);
|
|
145
|
+
expect(deleted).toHaveLength(1);
|
|
146
|
+
expect(deleted[0].ok).toBe(false);
|
|
147
|
+
expect(t.seen[t.seen.length - 1].url).toBe("api/cadso/dovetail_core/deleteRecord");
|
|
148
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
149
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
150
|
+
var created = await (0, reconcileCommand_1.applyCreates)("x_s", [change("create")], "us1");
|
|
151
|
+
expect(created).toHaveLength(1);
|
|
152
|
+
expect(created[0].ok).toBe(false);
|
|
153
|
+
expect(t.seen[t.seen.length - 1].url).toBe("api/cadso/dovetail_core/createRecord");
|
|
154
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
155
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
156
|
+
});
|
|
157
|
+
it("push: a 401 or 403 on updateRecord through retryOnHttpErr is still a rejected write", async function () {
|
|
158
|
+
var statuses = [401, 403];
|
|
159
|
+
for (var i = 0; i < statuses.length; i += 1) {
|
|
160
|
+
(0, authFailure_1.resetAuthFailureState)();
|
|
161
|
+
var t = clientAnswering(statuses[i]);
|
|
162
|
+
await expect((0, snClient_1.retryOnHttpErr)(function () {
|
|
163
|
+
return t.client.updateRecord("sys_script_include", "abc123", {
|
|
164
|
+
script: "x",
|
|
165
|
+
});
|
|
166
|
+
}, "sys_script_include > Foo")).rejects.toMatchObject({ response: { status: statuses[i] } });
|
|
167
|
+
expect(t.seen).toHaveLength(1);
|
|
168
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
it("a 403 on a GET changes nothing and prints no auth message", async function () {
|
|
172
|
+
var t = clientAnswering(403);
|
|
173
|
+
await expect((0, snClient_1.unwrapSNResponse)(t.client.getAppList())).rejects.toMatchObject({ response: { status: 403 } });
|
|
174
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
175
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
176
|
+
expect(loggedErrors()).not.toContain("Forbidden (403) calling");
|
|
177
|
+
});
|
|
178
|
+
it("a 403 on a read-only POST (getManifest, bulkDownload) is a read, not a rejected write", async function () {
|
|
179
|
+
var t = clientAnswering(403);
|
|
180
|
+
await expect(t.client.getManifest("x_s", {
|
|
181
|
+
sourceDirectory: "src",
|
|
182
|
+
buildDirectory: "build",
|
|
183
|
+
tableOptions: {},
|
|
184
|
+
refreshInterval: 30,
|
|
185
|
+
})).rejects.toMatchObject({
|
|
186
|
+
response: { status: 403 },
|
|
187
|
+
});
|
|
188
|
+
await expect(t.client.getMissingFiles({}, {})).rejects.toMatchObject({
|
|
189
|
+
response: { status: 403 },
|
|
190
|
+
});
|
|
191
|
+
expect(t.seen.map(function (r) {
|
|
192
|
+
return r.method;
|
|
193
|
+
})).toEqual(["POST", "POST"]);
|
|
194
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
195
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
196
|
+
});
|
|
197
|
+
it("a 401 on a GET re-maps only a failing exit and names the re-auth command", async function () {
|
|
198
|
+
var t = clientAnswering(401);
|
|
199
|
+
await expect((0, snClient_1.unwrapSNResponse)(t.client.getAppList())).rejects.toMatchObject({ response: { status: 401 } });
|
|
200
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
201
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
202
|
+
var lines = loggedErrors();
|
|
203
|
+
expect(lines).toContain("Unauthorized (401) calling GET api/cadso/dovetail_sync/getAppList");
|
|
204
|
+
expect(lines).toContain("dove login --env loft");
|
|
205
|
+
expect(lines).not.toContain(SECRET);
|
|
206
|
+
});
|
|
207
|
+
it("a 401 on a write is a rejected write", async function () {
|
|
208
|
+
var t = clientAnswering(401);
|
|
209
|
+
await expect(t.client.createRecord({
|
|
210
|
+
table: "sys_script_include",
|
|
211
|
+
fields: { name: "Foo" },
|
|
212
|
+
})).rejects.toMatchObject({ response: { status: 401 } });
|
|
213
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(authFailure_1.AUTH_EXIT_CODE);
|
|
214
|
+
});
|
|
215
|
+
it("prints the full message once per kind, however many records are rejected", async function () {
|
|
216
|
+
var t = clientAnswering(403);
|
|
217
|
+
for (var i = 0; i < 3; i += 1) {
|
|
218
|
+
await expect(t.client.deleteRecord({
|
|
219
|
+
table: "sys_script_include",
|
|
220
|
+
sys_id: "abc12" + i,
|
|
221
|
+
})).rejects.toBeDefined();
|
|
222
|
+
}
|
|
223
|
+
var full = mockLogger.error.mock.calls.filter(function (c) {
|
|
224
|
+
return String(c[0]).indexOf("Forbidden (403) calling") !== -1;
|
|
225
|
+
});
|
|
226
|
+
expect(full).toHaveLength(1);
|
|
227
|
+
});
|
|
228
|
+
it("a 404 or 500 records nothing", async function () {
|
|
229
|
+
var statuses = [404, 500];
|
|
230
|
+
for (var i = 0; i < statuses.length; i += 1) {
|
|
231
|
+
var t = clientAnswering(statuses[i]);
|
|
232
|
+
await expect(t.client.updateRecord("sys_script_include", "abc123", { script: "x" })).rejects.toBeDefined();
|
|
233
|
+
}
|
|
234
|
+
expect((0, authFailure_1.resolveExitCode)(1)).toBe(1);
|
|
235
|
+
});
|
|
236
|
+
it("_noteAuthRejection never throws on a malformed error", function () {
|
|
237
|
+
expect(function () {
|
|
238
|
+
(0, snClient_1._noteAuthRejection)(undefined);
|
|
239
|
+
(0, snClient_1._noteAuthRejection)(new Error("boom"));
|
|
240
|
+
(0, snClient_1._noteAuthRejection)({ isAxiosError: true, response: { status: 403 } });
|
|
241
|
+
(0, snClient_1._noteAuthRejection)({
|
|
242
|
+
isAxiosError: true,
|
|
243
|
+
response: { status: 401 },
|
|
244
|
+
config: { method: 7, url: {} },
|
|
245
|
+
});
|
|
246
|
+
}).not.toThrow();
|
|
247
|
+
// No method on the 403 means it cannot be called a write.
|
|
248
|
+
expect((0, authFailure_1.resolveExitCode)(0)).toBe(0);
|
|
249
|
+
});
|
|
250
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tenonhq/dovetail-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.131",
|
|
4
4
|
"description": "Next-gen file syncer",
|
|
5
5
|
"license": "GPL-3.0",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@tenonhq/dovetail-clickup": "~0.0.15",
|
|
42
|
-
"@tenonhq/dovetail-schema": "~0.0.
|
|
42
|
+
"@tenonhq/dovetail-schema": "~0.0.16",
|
|
43
43
|
"axios": "^1.20.0",
|
|
44
44
|
"axios-cookiejar-support": "^4.0.7",
|
|
45
45
|
"axios-rate-limit": "^1.3.0",
|