Skip to content

Commit

Permalink
Merge pull request #27 from grafana/feat/webcrypto-options
Browse files Browse the repository at this point in the history
Webcrypto and options.cloud changes
  • Loading branch information
oleiade committed Mar 18, 2024
2 parents ade6745 + a5e5aed commit b217ee5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
23 changes: 18 additions & 5 deletions types/k6/experimental/webcrypto.d.ts
Expand Up @@ -91,14 +91,14 @@ export interface SubtleCrypto {
*
* To export a key, the key must have `CryptoKey.extractable` set to `true`.
*
* @param format the format in which to export the key. Currently, only "raw" is supported.
* @param format the format in which to export the key. Currently, only "raw" and "jwk" are supported.
* @param key the key to export.
* @throws {InvalidAccessError} - if the key is not extractable.
* @throws {NotSupportedError} - if the format is not supported.
* @throws {TypeError} - when trying to use an invalid format.
* @returns A promise that resolves with the exported key.
*/
exportKey(format: "raw", key: CryptoKey): Promise<ArrayBuffer>;
exportKey(format: "raw" | "jwk", key: CryptoKey): Promise<ArrayBuffer | JWK>;

/**
* Use the `generateKey()` method to generate a new key (for symmetric
Expand All @@ -121,7 +121,7 @@ export interface SubtleCrypto {
* It takes as input a key in an external, portable format and gives you
* a `CryptoKey` object that can be used in the Web Crypto API.
*
* @param format the format of the key to import. Currently, only "raw" is supported.
* @param format the format of the key to import. Currently, only "raw" and "jwk" are supported.
* @param keyData the key data to import.
* @param algorithm defines the algorithm to use and any extra-parameters.
* @param extractable indicates whether it will be possible to export the key using `SubtleCrypto.exportKey()` or `SubtleCrypto.wrapKey`.
Expand All @@ -131,8 +131,8 @@ export interface SubtleCrypto {
* @returns A promise that resolves with the imported `CryptoKey`.
*/
importKey(
format: "raw",
keyData: ArrayBuffer | ArrayBufferView | DataView,
format: "raw" | "jwk",
keyData: ArrayBuffer | ArrayBufferView | DataView | JWK,
algorithm: "AES-CBC" | "AES-CTR" | "AES-GCM" | Algorithm<"AES-CBC" | "AES-CTR" | "AES-GCM"> | HmacImportParams,
extractable: boolean,
keyUsages: Array<"encrypt" | "decrypt" | "sign" | "verify">,
Expand Down Expand Up @@ -395,3 +395,16 @@ export type TypedArray =
| Uint16Array
| Int32Array
| Uint32Array;

/**
* JSON Web Key Value.
* JWKs are not supported for now, since webcrypto doesn't support exporting key/pairs
*/
export type JWKValue = null | boolean | number | string | JWK;

/**
* Object representable with JSON Web Key.
*/
export interface JWK {
[key: string]: JWKValue;
}
10 changes: 10 additions & 0 deletions types/k6/options.d.ts
Expand Up @@ -47,6 +47,9 @@ export interface Options {
/** Third party collector configuration. */
ext?: { [name: string]: CollectorOptions };

/** Cloud options */
options?: CloudOptions;

/** Static hostname mapping. */
hosts?: { [name: string]: string };

Expand Down Expand Up @@ -139,6 +142,13 @@ export interface CollectorOptions {
[name: string]: any;
}

/**
* Options for the cloud.
*/
export interface CloudOptions {
[name: string]: any;
}

/**
* Test stage.
*/
Expand Down
6 changes: 6 additions & 0 deletions types/k6/test/options.ts
Expand Up @@ -126,3 +126,9 @@ const browserScenariosBad: Scenario[] = [
},
},
];

const optionsWithCloud: Options = {
cloud: {
name: "My cloud test",
},
};
16 changes: 16 additions & 0 deletions types/k6/test/webcrypto.ts
Expand Up @@ -72,6 +72,9 @@ crypto.subtle.encrypt({ name: "AES-CBC" }, aesCryptoKey, null);
// crypto.subtle.exportKey
//

crypto.subtle.exportKey("raw", aesCryptoKey);
crypto.subtle.exportKey("jwk", aesCryptoKey);

// @ts-expect-error
crypto.subtle.exportKey();
// @ts-expect-error
Expand All @@ -92,6 +95,19 @@ crypto.subtle.generateKey(8);
// crypto.subtle.importKey
//

crypto.subtle.importKey("raw", new Uint8Array([
109, 151, 76, 33, 232, 253, 176, 90, 94, 40, 146, 227, 139, 208, 245, 139,
69, 215, 55, 197, 43, 122, 160, 178, 228, 104, 4, 115, 138, 159, 119, 49,
]), { name: "AES-GCM", length: "256" }, true, ["decrypt"]);

crypto.subtle.importKey("jwk", {
kty: "oct",
ext: true,
key_ops: ["decrypt", "encrypt"],
alg: "A256GCM",
k: "9Id_8iG6FkGOWmc1S203vGVnTExtpDGxdQN7v7OV9Uc",
}, { name: "AES-GCM", length: "256" }, true, ["decrypt"]);

// @ts-expect-error
crypto.subtle.importKey();
// @ts-expect-error
Expand Down

0 comments on commit b217ee5

Please sign in to comment.