-
Notifications
You must be signed in to change notification settings - Fork 0
fix(provider-security): ChefVault Bearer auth for ref probes #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OnlineChef
wants to merge
2
commits into
main
Choose a base branch
from
fix/provider-security-bearer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { ChefVaultProviderSecurityClient } from "../src/provider-security/chefvault-client.js"; | ||
| import { defaultProviderSecurityConfig } from "../src/provider-security/config.js"; | ||
|
|
||
| const TOKEN = "test-bearer-token"; | ||
| const envKeys = [ | ||
| "CHEF_PROVIDER_SECURITY_TOKEN", | ||
| "CHEF_WORKLOAD_ID", | ||
| "CHEF_HOST_ID", | ||
| "CHEF_ACTOR", | ||
| ] as const; | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| for (const key of envKeys) delete process.env[key]; | ||
| }); | ||
|
|
||
| describe("ChefVaultProviderSecurityClient", () => { | ||
| it("sends Authorization Bearer and X-Chef-* headers on inspectRef", async () => { | ||
| process.env.CHEF_PROVIDER_SECURITY_TOKEN = TOKEN; | ||
| process.env.CHEF_WORKLOAD_ID = "cpm"; | ||
| process.env.CHEF_HOST_ID = "joep"; | ||
| process.env.CHEF_ACTOR = "policy-doctor"; | ||
|
|
||
| const calls: { url: string; init?: RequestInit }[] = []; | ||
| vi.stubGlobal("fetch", async (url: string, init?: RequestInit) => { | ||
| calls.push({ url, init }); | ||
| return new Response(JSON.stringify({ fingerprint: "sha256:abc" }), { | ||
| status: 200, | ||
| headers: { "content-type": "application/json" }, | ||
| }); | ||
| }); | ||
|
|
||
| const client = new ChefVaultProviderSecurityClient(defaultProviderSecurityConfig()); | ||
| const result = await client.inspectRef("chefvault://pools/zai-coding/primary"); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
| expect(calls).toHaveLength(1); | ||
| const headers = new Headers(calls[0]?.init?.headers as HeadersInit); | ||
| expect(headers.get("authorization")).toBe(`Bearer ${TOKEN}`); | ||
| expect(headers.get("x-chef-workload-id")).toBe("cpm"); | ||
| expect(headers.get("x-chef-host-id")).toBe("joep"); | ||
| expect(headers.get("x-chef-actor")).toBe("policy-doctor"); | ||
| }); | ||
|
|
||
| it("does not require a token for health()", async () => { | ||
| delete process.env.CHEF_PROVIDER_SECURITY_TOKEN; | ||
|
|
||
| vi.stubGlobal("fetch", async () => new Response("ok", { status: 200 })); | ||
|
|
||
| const client = new ChefVaultProviderSecurityClient(defaultProviderSecurityConfig()); | ||
| const health = await client.health(); | ||
| expect(health.ok).toBe(true); | ||
| }); | ||
|
|
||
| it("fails inspectRef when CHEF_PROVIDER_SECURITY_TOKEN is missing", async () => { | ||
| delete process.env.CHEF_PROVIDER_SECURITY_TOKEN; | ||
|
|
||
| const fetchMock = vi.fn(); | ||
| vi.stubGlobal("fetch", fetchMock); | ||
|
|
||
| const client = new ChefVaultProviderSecurityClient(defaultProviderSecurityConfig()); | ||
| const result = await client.inspectRef("chefvault://pools/zai-coding/primary"); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.error).toMatch(/CHEF_PROVIDER_SECURITY_TOKEN/); | ||
| expect(fetchMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns clear errors on 401 and 403", async () => { | ||
| process.env.CHEF_PROVIDER_SECURITY_TOKEN = TOKEN; | ||
|
|
||
| vi.stubGlobal("fetch", async () => new Response(JSON.stringify({ error: "nope" }), { status: 401 })); | ||
|
|
||
| const client = new ChefVaultProviderSecurityClient(defaultProviderSecurityConfig()); | ||
| const unauthorized = await client.inspectRef("chefvault://pools/test/primary"); | ||
| expect(unauthorized.ok).toBe(false); | ||
| expect(unauthorized.error).toMatch(/unauthorized \(401\)/); | ||
|
|
||
| vi.stubGlobal("fetch", async () => new Response(JSON.stringify({ error: "forbidden" }), { status: 403 })); | ||
| const forbidden = await client.inspectRef("chefvault://pools/test/primary"); | ||
| expect(forbidden.ok).toBe(false); | ||
| expect(forbidden.error).toMatch(/forbidden \(403\)/); | ||
| }); | ||
|
|
||
| it("probeAuthentication treats 404 as authenticated when token is accepted", async () => { | ||
| process.env.CHEF_PROVIDER_SECURITY_TOKEN = TOKEN; | ||
|
|
||
| vi.stubGlobal("fetch", async () => new Response(JSON.stringify({ error: "not found" }), { status: 404 })); | ||
|
|
||
| const client = new ChefVaultProviderSecurityClient(defaultProviderSecurityConfig()); | ||
| const probe = await client.probeAuthentication(); | ||
| expect(probe.ok).toBe(true); | ||
| }); | ||
|
|
||
| it("probeAuthentication fails on a 500 response", async () => { | ||
| process.env.CHEF_PROVIDER_SECURITY_TOKEN = TOKEN; | ||
|
|
||
| vi.stubGlobal("fetch", async () => new Response(JSON.stringify({ error: "boom" }), { status: 500 })); | ||
|
|
||
| const client = new ChefVaultProviderSecurityClient(defaultProviderSecurityConfig()); | ||
| const probe = await client.probeAuthentication(); | ||
| expect(probe.ok).toBe(false); | ||
| expect(probe.error).toMatch(/\(500\)/); | ||
| }); | ||
|
|
||
| it("probeAuthentication fails when the request cannot be sent", async () => { | ||
| process.env.CHEF_PROVIDER_SECURITY_TOKEN = TOKEN; | ||
|
|
||
| vi.stubGlobal("fetch", async () => { | ||
| throw new Error("connect ECONNREFUSED 127.0.0.1:8080"); | ||
| }); | ||
|
|
||
| const client = new ChefVaultProviderSecurityClient(defaultProviderSecurityConfig()); | ||
| const probe = await client.probeAuthentication(); | ||
| expect(probe.ok).toBe(false); | ||
| expect(probe.error).toMatch(/ECONNREFUSED/); | ||
| }); | ||
|
|
||
| it("probeAuthentication fails when token is rejected", async () => { | ||
| process.env.CHEF_PROVIDER_SECURITY_TOKEN = TOKEN; | ||
|
|
||
| vi.stubGlobal("fetch", async () => new Response(JSON.stringify({ error: "bad token" }), { status: 403 })); | ||
|
|
||
| const client = new ChefVaultProviderSecurityClient(defaultProviderSecurityConfig()); | ||
| const probe = await client.probeAuthentication(); | ||
| expect(probe.ok).toBe(false); | ||
| expect(probe.error).toMatch(/forbidden \(403\)/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.