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 12, 2024
1 parent 2a14629 commit ffdf44e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 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 @@ -617,7 +617,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 @@ -869,7 +869,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 @@ -1880,7 +1880,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 @@ -1889,25 +1889,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
10 changes: 5 additions & 5 deletions src/models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,32 +826,32 @@ 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, {
Expand Down
4 changes: 4 additions & 0 deletions src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,10 @@ const getDeviceModel = function (
sdkInstance.auth.getUserInfo(),
sdkInstance.models.application.generateProvisioningKey(
applicationSlugOrUuidOrId,
// Use 10 minute expiry date as we will immediately use the provisioning key to create a device and then not need it
new Date(Date.now() + 1000 * 60 * 10).toISOString(),
'SDK register device',
'Created by SDK to register a device',
),
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
21 changes: 16 additions & 5 deletions tests/integration/models/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,10 @@ 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],
new Date(Date.now() + 1000 * 60 * 60).toISOString(),
)
.then(function (key) {
expect(_.isString(key)).to.be.true;
return expect(key).to.have.length(32);
Expand All @@ -625,6 +628,7 @@ describe('Application Model', function () {

const key = await balena.models.application.generateProvisioningKey(
this.application[prop],
new Date(Date.now() + 1000 * 60 * 60).toISOString(),
`key_${prop}`,
);

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

const key = await balena.models.application.generateProvisioningKey(
this.application[prop],
new Date(Date.now() + 1000 * 60 * 60).toISOString(),
`key_${prop}`,
`Provisioning key generated with name key_${prop}`,
);
Expand Down Expand Up @@ -682,11 +687,14 @@ describe('Application Model', function () {
this.application[prop],
);

const oneHourDate = new Date(
Date.now() + 1000 * 60 * 60,
).toISOString();
const key = await balena.models.application.generateProvisioningKey(
this.application[prop],
oneHourDate,
`key_${prop}`,
`Provisioning key generated with name key_${prop}`,
'2030-01-01',
);

expect(key).to.be.a('string');
Expand All @@ -705,22 +713,25 @@ describe('Application Model', function () {
expect(provisionKeys[0]).to.have.property('expiry_date');
expect(provisionKeys[0])
.to.have.property('expiry_date')
.to.be.equal('2030-01-01T00:00:00.000Z');
.to.be.equal(oneHourDate);
});
});

it('should be rejected if the application slug does not exist', function () {
const promise = balena.models.application.generateProvisioningKey(
`${this.initialOrg.handle}/helloworldapp`,
new Date(Date.now() + 1000 * 60 * 60).toISOString(),
);
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,
new Date(Date.now() + 1000 * 60 * 60).toISOString(),
);
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,
new Date(Date.now() + 1000 * 60 * 60).toISOString(),
);
await balena.auth.logout();
await balena.auth.loginWithToken(key);
Expand Down

0 comments on commit ffdf44e

Please sign in to comment.