Skip to content

Commit

Permalink
device.generateDeviceKey: Require expiryDate parameter and requir…
Browse files Browse the repository at this point in the history
…e it before the optional description

Change-type: major
  • Loading branch information
myarmolinsky committed Dec 2, 2024
1 parent c0367f7 commit d444ac6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
13 changes: 7 additions & 6 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ const sdk = fromSharedOptions();
* [.getSupervisorTargetStateForApp(uuidOrId, release)](#balena.models.device.getSupervisorTargetStateForApp) ⇒ <code>Promise</code>
* [.generateUniqueKey()](#balena.models.device.generateUniqueKey) ⇒ <code>String</code>
* [.register(applicationSlugOrUuidOrId, uuid, [deviceTypeSlug])](#balena.models.device.register) ⇒ <code>Promise</code>
* [.generateDeviceKey(uuidOrId, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ <code>Promise</code>
* [.generateDeviceKey(uuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ <code>Promise</code>
* [.hasDeviceUrl(uuidOrId)](#balena.models.device.hasDeviceUrl) ⇒ <code>Promise</code>
* [.getDeviceUrl(uuidOrId)](#balena.models.device.getDeviceUrl) ⇒ <code>Promise</code>
* [.enableDeviceUrl(uuidOrIdOrArray)](#balena.models.device.enableDeviceUrl) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -680,7 +680,7 @@ balena.models.device.get(123).catch(function (error) {
* [.getSupervisorTargetStateForApp(uuidOrId, release)](#balena.models.device.getSupervisorTargetStateForApp) ⇒ <code>Promise</code>
* [.generateUniqueKey()](#balena.models.device.generateUniqueKey) ⇒ <code>String</code>
* [.register(applicationSlugOrUuidOrId, uuid, [deviceTypeSlug])](#balena.models.device.register) ⇒ <code>Promise</code>
* [.generateDeviceKey(uuidOrId, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ <code>Promise</code>
* [.generateDeviceKey(uuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ <code>Promise</code>
* [.hasDeviceUrl(uuidOrId)](#balena.models.device.hasDeviceUrl) ⇒ <code>Promise</code>
* [.getDeviceUrl(uuidOrId)](#balena.models.device.getDeviceUrl) ⇒ <code>Promise</code>
* [.enableDeviceUrl(uuidOrIdOrArray)](#balena.models.device.enableDeviceUrl) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -2221,7 +2221,7 @@ balena.models.application.revokeSupportAccess(123);
* [.getSupervisorTargetStateForApp(uuidOrId, release)](#balena.models.device.getSupervisorTargetStateForApp) ⇒ <code>Promise</code>
* [.generateUniqueKey()](#balena.models.device.generateUniqueKey) ⇒ <code>String</code>
* [.register(applicationSlugOrUuidOrId, uuid, [deviceTypeSlug])](#balena.models.device.register) ⇒ <code>Promise</code>
* [.generateDeviceKey(uuidOrId, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ <code>Promise</code>
* [.generateDeviceKey(uuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.device.generateDeviceKey) ⇒ <code>Promise</code>
* [.hasDeviceUrl(uuidOrId)](#balena.models.device.hasDeviceUrl) ⇒ <code>Promise</code>
* [.getDeviceUrl(uuidOrId)](#balena.models.device.getDeviceUrl) ⇒ <code>Promise</code>
* [.enableDeviceUrl(uuidOrIdOrArray)](#balena.models.device.enableDeviceUrl) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -3469,26 +3469,27 @@ balena.models.device.register(123, uuid).then(function(registrationInfo) {
```
<a name="balena.models.device.generateDeviceKey"></a>

##### device.generateDeviceKey(uuidOrId, [keyName], [keyDescription]) ⇒ <code>Promise</code>
##### device.generateDeviceKey(uuidOrId, keyExpiryDate, [keyName], [keyDescription]) ⇒ <code>Promise</code>
**Kind**: static method of [<code>device</code>](#balena.models.device)
**Summary**: Generate a device key
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| uuidOrId | <code>String</code> \| <code>Number</code> | device uuid (string) or id (number) |
| keyExpiryDate | <code>String</code> | Device key expiry date |
| [keyName] | <code>String</code> | Device key name |
| [keyDescription] | <code>String</code> | 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);
});
```
Expand Down
7 changes: 4 additions & 3 deletions src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
const deviceId = (await sdkInstance.models.device.get(uuidOrId)).id;
const { body } = await request.send({
Expand Down
10 changes: 8 additions & 2 deletions tests/integration/models/api-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 () {
Expand Down
16 changes: 13 additions & 3 deletions tests/integration/models/device.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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}`,
);
Expand Down Expand Up @@ -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');
Expand All @@ -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',
);
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit d444ac6

Please sign in to comment.