From d444ac62fce480e07559963447f70d5a598be368 Mon Sep 17 00:00:00 2001 From: myarmolinsky Date: Mon, 2 Dec 2024 16:18:19 -0500 Subject: [PATCH] `device.generateDeviceKey`: Require `expiryDate` parameter and require it before the optional description Change-type: major --- DOCUMENTATION.md | 13 +++++++------ src/models/device.ts | 7 ++++--- tests/integration/models/api-key.spec.ts | 10 ++++++++-- tests/integration/models/device.spec.ts | 16 +++++++++++++--- tests/integration/setup.ts | 5 ++++- 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 1a817cda6..ec756e928 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -277,7 +277,7 @@ const sdk = fromSharedOptions(); * [.getSupervisorTargetStateForApp(uuidOrId, release)](#balena.models.device.getSupervisorTargetStateForApp) ⇒ Promise * [.generateUniqueKey()](#balena.models.device.generateUniqueKey) ⇒ String * [.register(applicationSlugOrUuidOrId, uuid, [deviceTypeSlug])](#balena.models.device.register) ⇒ Promise - * [.generateDeviceKey(uuidOrId, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ Promise + * [.generateDeviceKey(uuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ Promise * [.hasDeviceUrl(uuidOrId)](#balena.models.device.hasDeviceUrl) ⇒ Promise * [.getDeviceUrl(uuidOrId)](#balena.models.device.getDeviceUrl) ⇒ Promise * [.enableDeviceUrl(uuidOrIdOrArray)](#balena.models.device.enableDeviceUrl) ⇒ Promise @@ -680,7 +680,7 @@ balena.models.device.get(123).catch(function (error) { * [.getSupervisorTargetStateForApp(uuidOrId, release)](#balena.models.device.getSupervisorTargetStateForApp) ⇒ Promise * [.generateUniqueKey()](#balena.models.device.generateUniqueKey) ⇒ String * [.register(applicationSlugOrUuidOrId, uuid, [deviceTypeSlug])](#balena.models.device.register) ⇒ Promise - * [.generateDeviceKey(uuidOrId, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ Promise + * [.generateDeviceKey(uuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ Promise * [.hasDeviceUrl(uuidOrId)](#balena.models.device.hasDeviceUrl) ⇒ Promise * [.getDeviceUrl(uuidOrId)](#balena.models.device.getDeviceUrl) ⇒ Promise * [.enableDeviceUrl(uuidOrIdOrArray)](#balena.models.device.enableDeviceUrl) ⇒ Promise @@ -2221,7 +2221,7 @@ balena.models.application.revokeSupportAccess(123); * [.getSupervisorTargetStateForApp(uuidOrId, release)](#balena.models.device.getSupervisorTargetStateForApp) ⇒ Promise * [.generateUniqueKey()](#balena.models.device.generateUniqueKey) ⇒ String * [.register(applicationSlugOrUuidOrId, uuid, [deviceTypeSlug])](#balena.models.device.register) ⇒ Promise - * [.generateDeviceKey(uuidOrId, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ Promise + * [.generateDeviceKey(uuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ Promise * [.hasDeviceUrl(uuidOrId)](#balena.models.device.hasDeviceUrl) ⇒ Promise * [.getDeviceUrl(uuidOrId)](#balena.models.device.getDeviceUrl) ⇒ Promise * [.enableDeviceUrl(uuidOrIdOrArray)](#balena.models.device.enableDeviceUrl) ⇒ Promise @@ -3469,7 +3469,7 @@ balena.models.device.register(123, uuid).then(function(registrationInfo) { ``` -##### device.generateDeviceKey(uuidOrId, [keyName], [keyDescription]) ⇒ Promise +##### device.generateDeviceKey(uuidOrId, keyExpiryDate, [keyName], [keyDescription]) ⇒ Promise **Kind**: static method of [device](#balena.models.device) **Summary**: Generate a device key **Access**: public @@ -3477,18 +3477,19 @@ balena.models.device.register(123, uuid).then(function(registrationInfo) { | Param | Type | Description | | --- | --- | --- | | uuidOrId | String \| Number | device uuid (string) or id (number) | +| keyExpiryDate | String | Device key expiry date | | [keyName] | String | Device key name | | [keyDescription] | String | Description for device key | **Example** ```js -balena.models.device.generateDeviceKey('7cf02a6').then(function(deviceApiKey) { +balena.models.device.generateDeviceKey('7cf02a6', '2030-12-10').then(function(deviceApiKey) { console.log(deviceApiKey); }); ``` **Example** ```js -balena.models.device.generateDeviceKey(123).then(function(deviceApiKey) { +balena.models.device.generateDeviceKey(123, '2030-12-10').then(function(deviceApiKey) { console.log(deviceApiKey); }); ``` diff --git a/src/models/device.ts b/src/models/device.ts index a599f6c75..87bffa819 100644 --- a/src/models/device.ts +++ b/src/models/device.ts @@ -1422,25 +1422,26 @@ const getDeviceModel = function ( * @memberof balena.models.device * * @param {String|Number} uuidOrId - device uuid (string) or id (number) + * @param {String} keyExpiryDate - Device key expiry date * @param {String} [keyName] - Device key name * @param {String} [keyDescription] - Description for device key * @returns {Promise} * * @example - * balena.models.device.generateDeviceKey('7cf02a6').then(function(deviceApiKey) { + * balena.models.device.generateDeviceKey('7cf02a6', '2030-12-10').then(function(deviceApiKey) { * console.log(deviceApiKey); * }); * * @example - * balena.models.device.generateDeviceKey(123).then(function(deviceApiKey) { + * balena.models.device.generateDeviceKey(123, '2030-12-10').then(function(deviceApiKey) { * console.log(deviceApiKey); * }); */ generateDeviceKey: async ( uuidOrId: string | number, + keyExpiryDate: string, keyName?: string, keyDescription?: string, - keyExpiryDate?: string, ): Promise => { const deviceId = (await sdkInstance.models.device.get(uuidOrId)).id; const { body } = await request.send({ diff --git a/tests/integration/models/api-key.spec.ts b/tests/integration/models/api-key.spec.ts index d7e776f3b..fbd9469d9 100644 --- a/tests/integration/models/api-key.spec.ts +++ b/tests/integration/models/api-key.spec.ts @@ -171,9 +171,12 @@ describe('API Key model', function () { ] as const; before(async function () { + const tomorrowDate = new Date( + Date.now() + 1000 * 60 * 60 * 24, + ).toISOString(); await balena.models.apiKey.create( `${TEST_KEY_NAME_PREFIX}_apiKeyToBeUpdated`, - new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), + tomorrowDate, 'apiKeyDescriptionToBeUpdated', ); const [apiKey] = await balena.models.apiKey.getAll({ @@ -185,7 +188,10 @@ describe('API Key model', function () { this.application.id, ); - await balena.models.device.generateDeviceKey(this.device.id); + await balena.models.device.generateDeviceKey( + this.device.id, + tomorrowDate, + ); }); describe('balena.models.apiKey.getProvisioningApiKeysByApplication', function () { diff --git a/tests/integration/models/device.spec.ts b/tests/integration/models/device.spec.ts index 6c2f20794..519c13472 100644 --- a/tests/integration/models/device.spec.ts +++ b/tests/integration/models/device.spec.ts @@ -904,6 +904,7 @@ describe('Device Model', function () { it('should be able to generate a device key by uuid', async function () { const deviceApiKey = await balena.models.device.generateDeviceKey( this.device.uuid, + new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), ); expect(deviceApiKey).to.be.a('string'); return expect(deviceApiKey).to.have.length(32); @@ -912,6 +913,7 @@ describe('Device Model', function () { it('should be able to generate a device key by id', async function () { const deviceApiKey = await balena.models.device.generateDeviceKey( this.device.id, + new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), ); expect(deviceApiKey).to.be.a('string'); return expect(deviceApiKey).to.have.length(32); @@ -925,6 +927,7 @@ describe('Device Model', function () { const key = await balena.models.device.generateDeviceKey( this.device.id, + new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), 'device_key', `Device key generated for device ${this.device.id}`, ); @@ -959,9 +962,9 @@ describe('Device Model', function () { const key = await balena.models.device.generateDeviceKey( this.device.id, + tomorrowDate, 'device_key_with_expiry', `Device key generated for device ${this.device.id}`, - tomorrowDate, ); expect(key).to.be.a('string'); @@ -986,14 +989,20 @@ describe('Device Model', function () { }); it('should be rejected if the device name does not exist', function () { - const promise = balena.models.device.generateDeviceKey('asdfghjkl'); + const promise = balena.models.device.generateDeviceKey( + 'asdfghjkl', + new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), + ); return expect(promise).to.be.rejectedWith( 'Device not found: asdfghjkl', ); }); it('should be rejected if the device id does not exist', function () { - const promise = balena.models.device.generateDeviceKey(999999); + const promise = balena.models.device.generateDeviceKey( + 999999, + new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), + ); return expect(promise).to.be.rejectedWith( 'Device not found: 999999', ); @@ -1002,6 +1011,7 @@ describe('Device Model', function () { it('should be able to use a shorter uuid', async function () { const deviceApiKey = await balena.models.device.generateDeviceKey( this.device.uuid.slice(0, 8), + new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), ); expect(deviceApiKey).to.be.a('string'); return expect(deviceApiKey).to.have.length(32); diff --git a/tests/integration/setup.ts b/tests/integration/setup.ts index 3adf07d87..6a06f0be5 100644 --- a/tests/integration/setup.ts +++ b/tests/integration/setup.ts @@ -375,7 +375,10 @@ export function givenLoggedInWithADeviceApiKey(beforeFn: Mocha.HookFunction) { givenADevice(beforeFn); beforeFn(async function () { - const key = await balena.models.device.generateDeviceKey(this.device.id); + const key = await balena.models.device.generateDeviceKey( + this.device.id, + new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), + ); await balena.auth.logout(); await balena.auth.loginWithToken(key); });