Skip to content

Commit

Permalink
application.generateProvisioningKey: Require keyExpiryDate parame…
Browse files Browse the repository at this point in the history
…ter and require it before the optional description

Change-type: major
  • Loading branch information
myarmolinsky committed Dec 10, 2024
1 parent 04306f7 commit 7b3f502
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 68 deletions.
16 changes: 8 additions & 8 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const sdk = fromSharedOptions();
* [.remove(slugOrUuidOrIdOrIds)](#balena.models.application.remove) ⇒ <code>Promise</code>
* [.rename(slugOrUuidOrId, newName)](#balena.models.application.rename) ⇒ <code>Promise</code>
* [.restart(slugOrUuidOrId)](#balena.models.application.restart) ⇒ <code>Promise</code>
* [.generateProvisioningKey(slugOrUuidOrId, [keyName], [keyDescription], [keyExpiryDate])](#balena.models.application.generateProvisioningKey) ⇒ <code>Promise</code>
* [.generateProvisioningKey(slugOrUuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.application.generateProvisioningKey) ⇒ <code>Promise</code>
* [.purge(appId)](#balena.models.application.purge) ⇒ <code>Promise</code>
* [.shutdown(appId, [options])](#balena.models.application.shutdown) ⇒ <code>Promise</code>
* [.reboot(appId, [options])](#balena.models.application.reboot) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -616,7 +616,7 @@ balena.models.device.get(123).catch(function (error) {
* [.remove(slugOrUuidOrIdOrIds)](#balena.models.application.remove) ⇒ <code>Promise</code>
* [.rename(slugOrUuidOrId, newName)](#balena.models.application.rename) ⇒ <code>Promise</code>
* [.restart(slugOrUuidOrId)](#balena.models.application.restart) ⇒ <code>Promise</code>
* [.generateProvisioningKey(slugOrUuidOrId, [keyName], [keyDescription], [keyExpiryDate])](#balena.models.application.generateProvisioningKey) ⇒ <code>Promise</code>
* [.generateProvisioningKey(slugOrUuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.application.generateProvisioningKey) ⇒ <code>Promise</code>
* [.purge(appId)](#balena.models.application.purge) ⇒ <code>Promise</code>
* [.shutdown(appId, [options])](#balena.models.application.shutdown) ⇒ <code>Promise</code>
* [.reboot(appId, [options])](#balena.models.application.reboot) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -867,7 +867,7 @@ balena.models.device.get(123).catch(function (error) {
* [.remove(slugOrUuidOrIdOrIds)](#balena.models.application.remove) ⇒ <code>Promise</code>
* [.rename(slugOrUuidOrId, newName)](#balena.models.application.rename) ⇒ <code>Promise</code>
* [.restart(slugOrUuidOrId)](#balena.models.application.restart) ⇒ <code>Promise</code>
* [.generateProvisioningKey(slugOrUuidOrId, [keyName], [keyDescription], [keyExpiryDate])](#balena.models.application.generateProvisioningKey) ⇒ <code>Promise</code>
* [.generateProvisioningKey(slugOrUuidOrId, keyExpiryDate, [keyName], [keyDescription])](#balena.models.application.generateProvisioningKey) ⇒ <code>Promise</code>
* [.purge(appId)](#balena.models.application.purge) ⇒ <code>Promise</code>
* [.shutdown(appId, [options])](#balena.models.application.shutdown) ⇒ <code>Promise</code>
* [.reboot(appId, [options])](#balena.models.application.reboot) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -1878,7 +1878,7 @@ balena.models.application.restart(123);
```
<a name="balena.models.application.generateProvisioningKey"></a>

##### application.generateProvisioningKey(slugOrUuidOrId, [keyName], [keyDescription], [keyExpiryDate]) ⇒ <code>Promise</code>
##### application.generateProvisioningKey(slugOrUuidOrId, keyExpiryDate, [keyName], [keyDescription]) ⇒ <code>Promise</code>
**Kind**: static method of [<code>application</code>](#balena.models.application)
**Summary**: Generate a device provisioning key for a specific application
**Access**: public
Expand All @@ -1887,25 +1887,25 @@ balena.models.application.restart(123);
| Param | Type | Description |
| --- | --- | --- |
| slugOrUuidOrId | <code>String</code> \| <code>Number</code> | application slug (string), uuid (string) or id (number) |
| keyExpiryDate | <code>String</code> | Expiry Date for provisioning key |
| [keyName] | <code>String</code> | Provisioning key name |
| [keyDescription] | <code>String</code> | Description for provisioning key |
| [keyExpiryDate] | <code>String</code> | Expiry Date for provisioning key |

**Example**
```js
balena.models.application.generateProvisioningKey('myorganization/myapp').then(function(key) {
balena.models.application.generateProvisioningKey('myorganization/myapp', '2030-10-12').then(function(key) {
console.log(key);
});
```
**Example**
```js
balena.models.application.generateProvisioningKey(123).then(function(key) {
balena.models.application.generateProvisioningKey(123, '2030-10-12').then(function(key) {
console.log(key);
});
```
**Example**
```js
balena.models.application.generateProvisioningKey(123, 'api key name', 'api key long description', '2030-01-01T00:00:00Z').then(function(key) {
balena.models.application.generateProvisioningKey(123, '2030-10-12', 'api key name', 'api key long description', '2030-01-01T00:00:00Z').then(function(key) {
console.log(key);
});
```
Expand Down
120 changes: 64 additions & 56 deletions src/models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ import * as url from 'url';
import once from 'lodash/once';
import * as errors from 'balena-errors';

import {
isId,
mergePineOptions,
withSupervisorLockedError,
} from '../util';
import { isId, mergePineOptions, withSupervisorLockedError } from '../util';

import {
getCurrentServiceDetailsPineExpand,
Expand Down Expand Up @@ -729,11 +725,13 @@ const getApplicationModel = function (
slugOrUuidOrIdOrIds: string | number | number[],
): Promise<void> => {
if (typeof slugOrUuidOrIdOrIds === 'string') {
const applicationId = (await sdkInstance.models.application.get(slugOrUuidOrIdOrIds)).id;
await pine.delete({
resource: 'application',
id: applicationId,
});
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrIdOrIds)
).id;
await pine.delete({
resource: 'application',
id: applicationId,
});
return;
}
await batchApplicationOperation()({
Expand Down Expand Up @@ -773,14 +771,16 @@ const getApplicationModel = function (
slugOrUuidOrId: string | number,
newAppName: string,
): Promise<void> => {
const applicationId = (await sdkInstance.models.application.get(slugOrUuidOrId)).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: {
app_name: newAppName,
},
});
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId)
).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: {
app_name: newAppName,
},
});
},

/**
Expand All @@ -801,13 +801,15 @@ const getApplicationModel = function (
*/
restart: (slugOrUuidOrId: string | number): Promise<void> =>
withSupervisorLockedError(async () => {
const applicationId = (await sdkInstance.models.application.get(slugOrUuidOrId)).id;
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId)
).id;

await request.send({
method: 'POST',
url: `/application/${applicationId}/restart`,
baseUrl: apiUrl,
});
await request.send({
method: 'POST',
url: `/application/${applicationId}/restart`,
baseUrl: apiUrl,
});
}),

/**
Expand All @@ -818,48 +820,50 @@ const getApplicationModel = function (
* @memberof balena.models.application
*
* @param {String|Number} slugOrUuidOrId - application slug (string), uuid (string) or id (number)
* @param {String} keyExpiryDate - Expiry Date for provisioning key
* @param {String} [keyName] - Provisioning key name
* @param {String} [keyDescription] - Description for provisioning key
* @param {String} [keyExpiryDate] - Expiry Date for provisioning key
* @fulfil {String} - device provisioning key
* @returns {Promise}
*
* @example
* balena.models.application.generateProvisioningKey('myorganization/myapp').then(function(key) {
* balena.models.application.generateProvisioningKey('myorganization/myapp', '2030-10-12').then(function(key) {
* console.log(key);
* });
*
* @example
* balena.models.application.generateProvisioningKey(123).then(function(key) {
* balena.models.application.generateProvisioningKey(123, '2030-10-12').then(function(key) {
* console.log(key);
* });
*
* @example
* balena.models.application.generateProvisioningKey(123, 'api key name', 'api key long description', '2030-01-01T00:00:00Z').then(function(key) {
* balena.models.application.generateProvisioningKey(123, '2030-10-12', 'api key name', 'api key long description', '2030-01-01T00:00:00Z').then(function(key) {
* console.log(key);
* });
*/
generateProvisioningKey: async (
slugOrUuidOrId: string | number,
keyExpiryDate: string | null,
keyName?: string,
keyDescription?: string,
keyExpiryDate?: string,
): Promise<string> => {
const applicationId = (await sdkInstance.models.application.get(slugOrUuidOrId)).id;
const { body } = await request.send({
method: 'POST',
url: '/api-key/v1/',
baseUrl: apiUrl,
body: {
actorType: 'application',
actorTypeId: applicationId,
roles: ['provisioning-api-key'],
name: keyName,
description: keyDescription,
expiryDate: keyExpiryDate,
},
});
return body;
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId)
).id;
const { body } = await request.send({
method: 'POST',
url: '/api-key/v1/',
baseUrl: apiUrl,
body: {
actorType: 'application',
actorTypeId: applicationId,
roles: ['provisioning-api-key'],
name: keyName,
description: keyDescription,
expiryDate: keyExpiryDate,
},
});
return body;
},

/**
Expand Down Expand Up @@ -1287,12 +1291,14 @@ const getApplicationModel = function (
);
}

const applicationId = (await sdkInstance.models.application.get(slugOrUuidOrId)).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_accessible_by_support_until__date: expiryTimestamp },
});
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId)
).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_accessible_by_support_until__date: expiryTimestamp },
});
},

/**
Expand All @@ -1314,12 +1320,14 @@ const getApplicationModel = function (
revokeSupportAccess: async (
slugOrUuidOrId: string | number,
): Promise<void> => {
const applicationId = (await sdkInstance.models.application.get(slugOrUuidOrId)).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_accessible_by_support_until__date: null },
});
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId)
).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_accessible_by_support_until__date: null },
});
},

/**
Expand Down
1 change: 1 addition & 0 deletions src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,7 @@ const getDeviceModel = function (
sdkInstance.auth.getUserInfo(),
sdkInstance.models.application.generateProvisioningKey(
applicationSlugOrUuidOrId,
new Date(Date.now() + 1000 * 60 * 10).toISOString(),
),
sdkInstance.models.application.get(
applicationSlugOrUuidOrId,
Expand Down
1 change: 1 addition & 0 deletions tests/integration/models/api-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ describe('API Key model', function () {

await balena.models.application.generateProvisioningKey(
this.application.id,
null,
);

await balena.models.device.generateDeviceKey(this.device.id);
Expand Down
13 changes: 9 additions & 4 deletions tests/integration/models/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ describe('Application Model', function () {
applicationRetrievalFields.forEach((prop) => {
it(`should be able to generate a provisioning key by ${prop}`, function () {
return balena.models.application
.generateProvisioningKey(this.application[prop])
.generateProvisioningKey(this.application[prop], null)
.then(function (key) {
expect(_.isString(key)).to.be.true;
return expect(key).to.have.length(32);
Expand All @@ -625,6 +625,7 @@ describe('Application Model', function () {

const key = await balena.models.application.generateProvisioningKey(
this.application[prop],
null,
`key_${prop}`,
);

Expand Down Expand Up @@ -654,6 +655,7 @@ describe('Application Model', function () {

const key = await balena.models.application.generateProvisioningKey(
this.application[prop],
null,
`key_${prop}`,
`Provisioning key generated with name key_${prop}`,
);
Expand Down Expand Up @@ -684,9 +686,9 @@ describe('Application Model', function () {

const key = await balena.models.application.generateProvisioningKey(
this.application[prop],
'2030-01-01',
`key_${prop}`,
`Provisioning key generated with name key_${prop}`,
'2030-01-01',
);

expect(key).to.be.a('string');
Expand All @@ -712,15 +714,18 @@ describe('Application Model', function () {
it('should be rejected if the application slug does not exist', function () {
const promise = balena.models.application.generateProvisioningKey(
`${this.initialOrg.handle}/helloworldapp`,
null,
);
return expect(promise).to.be.rejectedWith(
`Application not found: ${this.initialOrg.handle}/helloworldapp`,
);
});

it('should be rejected if the application id does not exist', function () {
const promise =
balena.models.application.generateProvisioningKey(999999);
const promise = balena.models.application.generateProvisioningKey(
999999,
null,
);
return expect(promise).to.be.rejectedWith(
'Application not found: 999999',
);
Expand Down
1 change: 1 addition & 0 deletions tests/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ export function givenLoggedInWithAnApplicationApiKey(
beforeFn(async function () {
const key = await balena.models.application.generateProvisioningKey(
this.application.slug,
null,
);
await balena.auth.logout();
await balena.auth.loginWithToken(key);
Expand Down

0 comments on commit 7b3f502

Please sign in to comment.