@coffre/client 0.16.6 → 0.16.7

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.
@@ -831,6 +831,15 @@ function surface(core) {
831
831
  if (typeof value === "string") return value;
832
832
  throw new CoffreError(403, "forbidden", `${core.service} reads no ${name} in ${placeOf()}: it reads plain values and those its binding lists in reads, and uses every other through coffre`, "use_only");
833
833
  },
834
+ write: async (path, value) => {
835
+ const parts = path.split("/");
836
+ if (parts.length !== 1 && parts.length !== 3 || parts.includes("")) throw new TypeError(`write a key as KEY, in the session's place, or as <project>/<environment>/KEY; got ${JSON.stringify(path)}`);
837
+ const key = parts.at(-1);
838
+ const place = parts.length === 3 ? `${parts[0]}/${parts[1]}` : placeOf();
839
+ const { token } = await core.current();
840
+ const { response, body } = await core.api(`/api/secrets/${place.split("/").map(encodeURIComponent).join("/")}`, token, { [key]: value }, void 0, "PATCH");
841
+ if (!response.ok) throw refusal(response.status, body, `coffre wrote no ${key} in ${place}`);
842
+ },
834
843
  narrow: (async (options) => {
835
844
  const own = options.key === void 0 ? await generateProofKey() : null;
836
845
  const { token } = await core.current();
@@ -958,32 +967,34 @@ function note(nonces, response, sent) {
958
967
  }
959
968
  }
960
969
  /**
961
- * Its proof signs the token and the newest nonce, and a body where there is
962
- * one (`hdr` and `req`: narrow's, the key and the limits it gives a child),
963
- * so no one who sees the request can make it say anything else; sent again
964
- * once if the gate asks for a fresh nonce.
970
+ * Its proof signs the token and the newest nonce, and a POST's body where
971
+ * there is one (`hdr` and `req`: narrow's, the key and the limits it gives a
972
+ * child), so no one who sees the request can make it say anything else; sent
973
+ * again once if the gate asks for a fresh nonce. A PATCH's body goes
974
+ * unsigned: coffre holds an API call's proof to its method and URL alone
975
+ * (`provenSession`), and refuses one that signs content it does not expect.
965
976
  */
966
977
  function apiOf({ origin, base, key, nonces }) {
967
- return async (path, token, body, signal) => {
978
+ return async (path, token, body, signal, method = "POST") => {
968
979
  const url = `${origin}${path}`;
969
980
  const json = body === void 0 ? void 0 : JSON.stringify(body);
970
981
  const headers = {
971
982
  accept: "application/json",
972
983
  ...json === void 0 ? {} : { "content-type": "application/json" }
973
984
  };
974
- const content = json === void 0 ? void 0 : {
985
+ const content = json === void 0 || method === "PATCH" ? void 0 : {
975
986
  headers,
976
987
  body: await bodyDigest(json)
977
988
  };
978
989
  const send = async () => {
979
990
  const sent = dispatch(nonces);
980
991
  const response = await base(url, {
981
- method: "POST",
992
+ method,
982
993
  headers: {
983
994
  ...headers,
984
995
  authorization: `DPoP ${token}`,
985
996
  dpop: await createProof(key, {
986
- method: "POST",
997
+ method,
987
998
  url,
988
999
  token,
989
1000
  nonce: nonces.value,
@@ -187,6 +187,14 @@ interface Workload {
187
187
  credential(name: string): UsedCredential;
188
188
  /** The escape hatch: a plain value, or one the binding lists in `reads`. Anything else rejects with `use_only`. */
189
189
  read(key: string): Promise<string>;
190
+ /**
191
+ * Writes a key's value as this session, where its grant lets it write:
192
+ * `write('rehearsals/prod/COPY_7F3K', value)`, or a bare key in the
193
+ * session's place; `null` archives the key, its versions kept. What it
194
+ * writes it may still be unable to read: a session reads only what
195
+ * `read` does.
196
+ */
197
+ write(key: string, value: string | null): Promise<void>;
190
198
  /**
191
199
  * A session with less authority (section 8.4), for a part of the app that
192
200
  * needs less: in this process, a `Workload` of its own key; for another
@@ -1,4 +1,4 @@
1
- import { r as Workload } from "./workload-3gHR2coV.js";
1
+ import { r as Workload } from "./workload-D3dEzGxn.js";
2
2
  import { Buffer } from "node:buffer";
3
3
  import { Duplex } from "node:stream";
4
4
  //#region src/workload-postgres.d.ts
@@ -1,4 +1,4 @@
1
- import { i as upgradeRefused, r as upgradeHeaders } from "./workload-CizRrO4x.js";
1
+ import { i as upgradeRefused, r as upgradeHeaders } from "./workload-BylB1uJV.js";
2
2
  import { Buffer } from "node:buffer";
3
3
  import { Duplex } from "node:stream";
4
4
  //#region src/workload-postgres.ts
@@ -1,4 +1,4 @@
1
1
  import { n as Sealed } from "./credential-DyAuH0om.js";
2
2
  import { r as CoffreError } from "./index-DZZ3DRTO.js";
3
- import { C as oidc, S as kubernetes, _ as enrollment, a as askedNonce, b as github, c as upgradeRefused, d as KUBERNETES_TOKEN, f as PlatformProof, g as azure, h as aws, i as WorkloadOptions, l as workload, m as ProofContext, n as UsedCredential, o as fromCoffre, p as Proof, r as Workload, s as upgradeHeaders, t as NarrowOptions, u as AwsCredentials, v as fly, w as vercel, x as keyAudience, y as gcp } from "./workload-3gHR2coV.js";
3
+ import { C as oidc, S as kubernetes, _ as enrollment, a as askedNonce, b as github, c as upgradeRefused, d as KUBERNETES_TOKEN, f as PlatformProof, g as azure, h as aws, i as WorkloadOptions, l as workload, m as ProofContext, n as UsedCredential, o as fromCoffre, p as Proof, r as Workload, s as upgradeHeaders, t as NarrowOptions, u as AwsCredentials, v as fly, w as vercel, x as keyAudience, y as gcp } from "./workload-D3dEzGxn.js";
4
4
  export { type AwsCredentials, CoffreError, KUBERNETES_TOKEN, NarrowOptions, type PlatformProof, type Proof, type ProofContext, type Sealed, UsedCredential, Workload, WorkloadOptions, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
package/dist/workload.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import { t as CoffreError } from "./src-JtgcfCXD.js";
2
- import { a as workload, c as azure, d as gcp, f as github, g as vercel, h as oidc, i as upgradeRefused, l as enrollment, m as kubernetes, n as fromCoffre, o as KUBERNETES_TOKEN, p as keyAudience, r as upgradeHeaders, s as aws, t as askedNonce, u as fly } from "./workload-CizRrO4x.js";
2
+ import { a as workload, c as azure, d as gcp, f as github, g as vercel, h as oidc, i as upgradeRefused, l as enrollment, m as kubernetes, n as fromCoffre, o as KUBERNETES_TOKEN, p as keyAudience, r as upgradeHeaders, s as aws, t as askedNonce, u as fly } from "./workload-BylB1uJV.js";
3
3
  export { CoffreError, KUBERNETES_TOKEN, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/client",
3
- "version": "0.16.6",
3
+ "version": "0.16.7",
4
4
  "description": "The coffre API as typed function calls.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/workload.ts CHANGED
@@ -82,6 +82,14 @@ export interface Workload {
82
82
  credential(name: string): UsedCredential;
83
83
  /** The escape hatch: a plain value, or one the binding lists in `reads`. Anything else rejects with `use_only`. */
84
84
  read(key: string): Promise<string>;
85
+ /**
86
+ * Writes a key's value as this session, where its grant lets it write:
87
+ * `write('rehearsals/prod/COPY_7F3K', value)`, or a bare key in the
88
+ * session's place; `null` archives the key, its versions kept. What it
89
+ * writes it may still be unable to read: a session reads only what
90
+ * `read` does.
91
+ */
92
+ write(key: string, value: string | null): Promise<void>;
85
93
  /**
86
94
  * A session with less authority (section 8.4), for a part of the app that
87
95
  * needs less: in this process, a `Workload` of its own key; for another
@@ -565,6 +573,15 @@ function surface(core: Core): Workload {
565
573
  if (typeof value === 'string') return value;
566
574
  throw new CoffreError(403, 'forbidden', `${core.service} reads no ${name} in ${placeOf()}: it reads plain values and those its binding lists in reads, and uses every other through coffre`, 'use_only');
567
575
  },
576
+ write: async (path, value) => {
577
+ const parts = path.split('/');
578
+ if ((parts.length !== 1 && parts.length !== 3) || parts.includes('')) throw new TypeError(`write a key as KEY, in the session's place, or as <project>/<environment>/KEY; got ${JSON.stringify(path)}`);
579
+ const key = parts.at(-1)!;
580
+ const place = parts.length === 3 ? `${parts[0]}/${parts[1]}` : placeOf();
581
+ const { token } = await core.current();
582
+ const { response, body } = await core.api(`/api/secrets/${place.split('/').map(encodeURIComponent).join('/')}`, token, { [key]: value }, undefined, 'PATCH');
583
+ if (!response.ok) throw refusal(response.status, body, `coffre wrote no ${key} in ${place}`);
584
+ },
568
585
  narrow: (async (options: NarrowOptions & { key?: PublicJwk }) => {
569
586
  const own = options.key === undefined ? await generateProofKey() : null;
570
587
  const { token } = await core.current();
@@ -666,26 +683,28 @@ function note(nonces: Nonces, response: { headers: { get(name: string): string |
666
683
  }
667
684
  }
668
685
 
669
- /** A POST to coffre's own API with a session (renew, narrow, logout), signed by its key. */
670
- type Api = (path: string, token: string, body?: unknown, signal?: AbortSignal) => Promise<{ response: Response; body: Record<string, unknown> | null }>;
686
+ /** A request to coffre's own API with a session, signed by its key: a POST (renew, narrow, logout), or a key's PATCH (write). */
687
+ type Api = (path: string, token: string, body?: unknown, signal?: AbortSignal, method?: 'POST' | 'PATCH') => Promise<{ response: Response; body: Record<string, unknown> | null }>;
671
688
 
672
689
  /**
673
- * Its proof signs the token and the newest nonce, and a body where there is
674
- * one (`hdr` and `req`: narrow's, the key and the limits it gives a child),
675
- * so no one who sees the request can make it say anything else; sent again
676
- * once if the gate asks for a fresh nonce.
690
+ * Its proof signs the token and the newest nonce, and a POST's body where
691
+ * there is one (`hdr` and `req`: narrow's, the key and the limits it gives a
692
+ * child), so no one who sees the request can make it say anything else; sent
693
+ * again once if the gate asks for a fresh nonce. A PATCH's body goes
694
+ * unsigned: coffre holds an API call's proof to its method and URL alone
695
+ * (`provenSession`), and refuses one that signs content it does not expect.
677
696
  */
678
697
  function apiOf({ origin, base, key, nonces }: { origin: string; base: Fetch; key: ProofKey; nonces: Nonces }): Api {
679
- return async (path, token, body, signal) => {
698
+ return async (path, token, body, signal, method = 'POST') => {
680
699
  const url = `${origin}${path}`;
681
700
  const json = body === undefined ? undefined : JSON.stringify(body);
682
701
  const headers = { accept: 'application/json', ...(json === undefined ? {} : { 'content-type': 'application/json' }) };
683
- const content = json === undefined ? undefined : { headers, body: await bodyDigest(json) };
702
+ const content = json === undefined || method === 'PATCH' ? undefined : { headers, body: await bodyDigest(json) };
684
703
  const send = async () => {
685
704
  const sent = dispatch(nonces);
686
705
  const response = await base(url, {
687
- method: 'POST',
688
- headers: { ...headers, authorization: `DPoP ${token}`, dpop: await createProof(key, { method: 'POST', url, token, nonce: nonces.value, content }) },
706
+ method,
707
+ headers: { ...headers, authorization: `DPoP ${token}`, dpop: await createProof(key, { method, url, token, nonce: nonces.value, content }) },
689
708
  body: json,
690
709
  signal,
691
710
  redirect: 'error',