Skip to content
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

Webcrypto and options.cloud changes #27

Merged
merged 2 commits into from Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link

@joanlopez joanlopez Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully get this sentence 🤔 By reading it, I'd not expect the following to be possible:

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

But, in theory, it is 👍🏻

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully get this sentence

It's an attempt to mention that we have yet to support multiply (JWKs) since there is no functionality in our webcrypto implementation to export/import 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