-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathaccess.test.ts
More file actions
212 lines (183 loc) · 7.94 KB
/
access.test.ts
File metadata and controls
212 lines (183 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import ci from "ci-info";
import { http, HttpResponse } from "msw";
import { beforeEach, describe, it, vi } from "vitest";
import {
clearAccessCaches,
domainUsesAccess,
getAccessToken,
} from "../user/access";
import { mockConsoleMethods } from "./helpers/mock-console";
import { useMockIsTTY } from "./helpers/mock-istty";
import { msw, mswAccessHandlers } from "./helpers/msw";
describe("access", () => {
const { setIsTTY } = useMockIsTTY();
const std = mockConsoleMethods();
beforeEach(() => {
clearAccessCaches();
msw.use(...mswAccessHandlers);
});
describe("domainUsesAccess", () => {
it("should correctly detect an access protected domain", async ({
expect,
}) => {
expect(await domainUsesAccess("access-protected.com")).toBeTruthy();
expect(await domainUsesAccess("not-access-protected.com")).toBeFalsy();
});
});
describe("getAccessToken", () => {
it("should return undefined for non-access-protected domains", async ({
expect,
}) => {
expect(await getAccessToken("not-access-protected.com")).toBeFalsy();
});
describe("service token authentication", () => {
it("should authenticate using service token env vars when both are set", async ({
expect,
}) => {
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "test-client-id.access");
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "test-client-secret");
// Handler that returns the Access 302 redirect for domainUsesAccess,
// or authenticates via service token headers.
msw.use(
http.get("https://access-protected.com/", ({ request }) => {
const clientId = request.headers.get("CF-Access-Client-Id");
// No service token headers = this is the domainUsesAccess check
if (!clientId) {
return HttpResponse.json(null, {
status: 302,
headers: {
location: "access-protected-com.cloudflareaccess.com",
},
});
}
const clientSecret = request.headers.get("CF-Access-Client-Secret");
if (
clientId === "test-client-id.access" &&
clientSecret === "test-client-secret"
) {
return new HttpResponse("OK", {
status: 200,
headers: {
"Set-Cookie":
"CF_Authorization=mock-jwt-token; Path=/; HttpOnly; Secure",
},
});
}
return HttpResponse.json(
{ error: "unauthorized" },
{ status: 403 }
);
})
);
const token = await getAccessToken("access-protected.com");
expect(token).toBe("mock-jwt-token");
});
it("should throw a clear error when service token authentication fails", async ({
expect,
}) => {
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "bad-client-id.access");
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "bad-secret");
// Handler that returns 302 for domainUsesAccess, but rejects
// service token auth (no CF_Authorization cookie).
msw.use(
http.get("https://access-protected.com/", ({ request }) => {
if (!request.headers.get("CF-Access-Client-Id")) {
return HttpResponse.json(null, {
status: 302,
headers: {
location: "access-protected-com.cloudflareaccess.com",
},
});
}
return HttpResponse.json(
{ error: "unauthorized" },
{ status: 403 }
);
})
);
await expect(
getAccessToken("access-protected.com")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: Failed to authenticate with Cloudflare Access using Service Token for domain "access-protected.com". The service token may be expired or invalid, or the Access application may not have a Service Auth policy. Verify your CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET are correct.
See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]`
);
});
it("should warn and throw when only CLOUDFLARE_ACCESS_CLIENT_ID is set", async ({
expect,
}) => {
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "test-client-id.access");
// Non-interactive so it will throw after the warning
setIsTTY(false);
await expect(
getAccessToken("access-protected.com")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The domain "access-protected.com" is behind Cloudflare Access, but no Access Service Token credentials were found and the current environment is non-interactive.
Set the CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET environment variables to authenticate with an Access Service Token.
See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]`
);
expect(std.warn).toMatchInlineSnapshot(`
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mBoth CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET must be set to use Access Service Token authentication. Only CLOUDFLARE_ACCESS_CLIENT_ID was found.[0m
"
`);
});
it("should warn and throw when only CLOUDFLARE_ACCESS_CLIENT_SECRET is set", async ({
expect,
}) => {
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "test-client-secret");
// Non-interactive so it will throw after the warning
setIsTTY(false);
await expect(
getAccessToken("access-protected.com")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The domain "access-protected.com" is behind Cloudflare Access, but no Access Service Token credentials were found and the current environment is non-interactive.
Set the CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET environment variables to authenticate with an Access Service Token.
See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]`
);
expect(std.warn).toMatchInlineSnapshot(`
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mBoth CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET must be set to use Access Service Token authentication. Only CLOUDFLARE_ACCESS_CLIENT_SECRET was found.[0m
"
`);
});
});
describe("non-interactive environment", () => {
it("should throw actionable error when non-interactive and no service token", async ({
expect,
}) => {
setIsTTY(false);
await expect(
getAccessToken("access-protected.com")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The domain "access-protected.com" is behind Cloudflare Access, but no Access Service Token credentials were found and the current environment is non-interactive.
Set the CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET environment variables to authenticate with an Access Service Token.
See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]`
);
});
it("should throw actionable error when in CI and no service token", async ({
expect,
}) => {
// Even with TTY, CI detection should trigger the error
setIsTTY(true);
vi.mocked(ci).isCI = true;
await expect(
getAccessToken("access-protected.com")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The domain "access-protected.com" is behind Cloudflare Access, but no Access Service Token credentials were found and the current environment is non-interactive.
Set the CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET environment variables to authenticate with an Access Service Token.
See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]`
);
});
});
describe("interactive environment (cloudflared fallback)", () => {
it("should error without cloudflared installed on an access protected domain", async ({
expect,
}) => {
setIsTTY(true);
await expect(
getAccessToken("access-protected.com")
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: To use Wrangler with Cloudflare Access, please install \`cloudflared\` from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation]`
);
});
});
});
});