From ea8e8d2f5fb94d7457ef9fed31108f3c881e33c9 Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Fri, 11 May 2018 10:29:15 +0100 Subject: [PATCH] Don't generate config fields in offline mode Change-type: patch Closes: #648 Signed-off-by: Cameron Diver --- src/config.coffee | 18 +++++++---- test/11-api-binder.spec.coffee | 34 +++++++++++++++++--- test/data/testconfig-apibinder-offline.json | 1 + test/data/testconfig-apibinder-offline2.json | 1 + test/lib/prepare.coffee | 9 ++++++ 5 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 test/data/testconfig-apibinder-offline.json create mode 100644 test/data/testconfig-apibinder-offline2.json diff --git a/src/config.coffee b/src/config.coffee index f5a23ab7..1a6d4163 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -241,14 +241,20 @@ module.exports = class Config extends EventEmitter deviceRegister.generateUniqueKey() generateRequiredFields: => - @getMany([ 'uuid', 'deviceApiKey', 'apiSecret', 'logsChannelSecret' ]) - .then ({ uuid, deviceApiKey, apiSecret, logsChannelSecret }) => - if !uuid? or !deviceApiKey? or !apiSecret? or !logsChannelSecret? + @getMany([ 'uuid', 'deviceApiKey', 'apiSecret', 'logsChannelSecret', 'offlineMode' ]) + .then ({ uuid, deviceApiKey, apiSecret, logsChannelSecret, offlineMode }) => + # These fields need to be set regardless + if !uuid? or !apiSecret? uuid ?= @newUniqueKey() - deviceApiKey ?= @newUniqueKey() apiSecret ?= @newUniqueKey() - logsChannelSecret ?= @newUniqueKey() - @set({ uuid, deviceApiKey, apiSecret, logsChannelSecret }) + @set({ uuid, apiSecret }) + .then => + # These fields only need set when we're not in offlineMode + return if offlineMode + if !deviceApiKey? or !logsChannelSecret? + deviceApiKey ?= @newUniqueKey() + logsChannelSecret ?= @newUniqueKey() + @set({ deviceApiKey, logsChannelSecret }) regenerateRegistrationFields: => uuid = deviceRegister.generateUniqueKey() diff --git a/test/11-api-binder.spec.coffee b/test/11-api-binder.spec.coffee index ac9da4f2..7baba496 100644 --- a/test/11-api-binder.spec.coffee +++ b/test/11-api-binder.spec.coffee @@ -12,11 +12,11 @@ Config = require('../src/config') DeviceState = require('../src/device-state') APIBinder = require('../src/api-binder') -initModels = -> +initModels = (filename) -> @timeout(5000) prepare() @db = new DB() - @config = new Config({ @db, configPath: '/config-apibinder.json' }) + @config = new Config({ @db, configPath: filename }) @eventTracker = { track: stub().callsFake (ev, props) -> console.log(ev, props) @@ -49,7 +49,7 @@ describe 'APIBinder', -> # We do not support older OS versions anymore, so we only test this case describe 'on an OS with deviceApiKey support', -> before -> - initModels.call(this) + initModels.call(this, '/config-apibinder.json') it 'provisions a device', -> promise = @apiBinder.provisionDevice() @@ -79,7 +79,7 @@ describe 'APIBinder', -> describe 'fetchDevice', -> before -> - initModels.call(this) + initModels.call(this, '/config-apibinder.json') it 'gets a device by its uuid from the Resin API', -> # Manually add a device to the mocked API @@ -97,7 +97,7 @@ describe 'APIBinder', -> describe '_exchangeKeyAndGetDevice', -> before -> - initModels.call(this) + initModels.call(this, '/config-apibinder.json') it 'returns the device if it can fetch it with the deviceApiKey', -> spy(resinAPI.resinBackend, 'deviceKeyHandler') @@ -135,3 +135,27 @@ describe 'APIBinder', -> expect(@apiBinder.fetchDevice).to.be.calledTwice @apiBinder.fetchDevice.restore() resinAPI.resinBackend.deviceKeyHandler.restore() + + describe 'offline mode', -> + before -> + initModels.call(this, '/config-apibinder-offline.json') + + it 'does not generate a key if the device is in offline mode', -> + @config.get('offlineMode').then (mode) => + # Ensure offline mode is set + 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['uuid']).to.not.be.undefined + + describe 'Minimal config offline mode', -> + before -> + initModels.call(this, '/config-apibinder-offline2.json') + + it 'does not generate a key with the minimal config', -> + @config.get('offlineMode').then (mode) => + expect(mode).to.equal(true) + @config.getMany([ 'deviceApiKey', 'uuid' ]).then (conf) -> + expect(conf['deviceApiKey']).to.be.undefined + expect(conf['uuid']).to.not.be.undefined diff --git a/test/data/testconfig-apibinder-offline.json b/test/data/testconfig-apibinder-offline.json new file mode 100644 index 00000000..37052ce4 --- /dev/null +++ b/test/data/testconfig-apibinder-offline.json @@ -0,0 +1 @@ +{"applicationName":"supertestrpi3","applicationId":78373,"deviceType":"raspberrypi3","userId":1001,"username":"someone","appUpdatePollInterval":3000,"listenPort":2345,"vpnPort":443,"apiEndpoint":"http://0.0.0.0:3000","vpnEndpoint":"vpn.resin.io","registryEndpoint":"registry2.resin.io","deltaEndpoint":"https://delta.resin.io","pubnubSubscribeKey":"foo","pubnubPublishKey":"bar","mixpanelToken":"baz","apiKey":"boo","version":"2.0.6+rev3.prod","supervisorOfflineMode":true} diff --git a/test/data/testconfig-apibinder-offline2.json b/test/data/testconfig-apibinder-offline2.json new file mode 100644 index 00000000..56ca7eb5 --- /dev/null +++ b/test/data/testconfig-apibinder-offline2.json @@ -0,0 +1 @@ +{"deviceType": "raspberrypi3"} diff --git a/test/lib/prepare.coffee b/test/lib/prepare.coffee index 4b057938..5d806f78 100644 --- a/test/lib/prepare.coffee +++ b/test/lib/prepare.coffee @@ -16,3 +16,12 @@ module.exports = -> try fs.writeFileSync('./test/data/config.json', fs.readFileSync('./test/data/testconfig.json')) fs.writeFileSync('./test/data/config-apibinder.json', fs.readFileSync('./test/data/testconfig-apibinder.json')) + + fs.writeFileSync( + './test/data/config-apibinder-offline.json', + fs.readFileSync('./test/data/testconfig-apibinder-offline.json') + ) + fs.writeFileSync( + './test/data/config-apibinder-offline2.json', + fs.readFileSync('./test/data/testconfig-apibinder-offline2.json') + )