config: Set default apiKey of empty string to avoid undefined keys

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2018-11-28 14:19:25 +00:00
parent 142d2a7c6f
commit b37ce5e3d9
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
3 changed files with 8 additions and 5 deletions

View File

@ -9,6 +9,7 @@ import supervisorVersion = require('../lib/supervisor-version');
import * as constants from '../lib/constants';
import * as osRelease from '../lib/os-release';
import { ConfigValue } from '../lib/types';
import { checkTruthy } from '../lib/validation';
// A provider for schema entries with source 'func'
type ConfigProviderFunctionGetter = () => Bluebird<any>;
@ -51,7 +52,9 @@ export function createProviderFunctions(
return config
.getMany(['apiEndpoint', 'supervisorOfflineMode'])
.then(({ apiEndpoint, supervisorOfflineMode }) => {
return Boolean(supervisorOfflineMode) || !Boolean(apiEndpoint);
return (
checkTruthy(supervisorOfflineMode as boolean) || !apiEndpoint
);
});
},
},

View File

@ -29,7 +29,7 @@ class Config extends EventEmitter {
deltaEndpoint: { source: 'config.json', default: 'https://delta.resin.io' },
uuid: { source: 'config.json', mutable: true },
apiKey: { source: 'config.json', mutable: true, removeIfNull: true },
deviceApiKey: { source: 'config.json', mutable: true },
deviceApiKey: { source: 'config.json', mutable: true, default: '' },
deviceType: { source: 'config.json', default: 'unknown' },
username: { source: 'config.json' },
userId: { source: 'config.json' },
@ -292,7 +292,7 @@ class Config extends EventEmitter {
if (offlineMode) {
return;
}
if (deviceApiKey == null) {
if (!deviceApiKey) {
return this.set({ deviceApiKey: this.newUniqueKey() });
}
});

View File

@ -145,7 +145,7 @@ describe 'APIBinder', ->
expect(mode).to.equal(true)
# Check that there is no deviceApiKey
@config.getMany([ 'deviceApiKey', 'uuid' ]).then (conf) ->
expect(conf['deviceApiKey']).to.be.undefined
expect(conf['deviceApiKey']).to.be.empty
expect(conf['uuid']).to.not.be.undefined
describe 'Minimal config offline mode', ->
@ -156,5 +156,5 @@ describe 'APIBinder', ->
@config.get('offlineMode').then (mode) =>
expect(mode).to.equal(true)
@config.getMany([ 'deviceApiKey', 'uuid' ]).then (conf) ->
expect(conf['deviceApiKey']).to.be.undefined
expect(conf['deviceApiKey']).to.be.empty
expect(conf['uuid']).to.not.be.undefined