Don't generate config fields in offline mode

Change-type: patch
Closes: #648
Signed-off-by: Cameron Diver <cameron@resin.io>
This commit is contained in:
Cameron Diver 2018-05-11 10:29:15 +01:00
parent 37267f62c8
commit ea8e8d2f5f
No known key found for this signature in database
GPG Key ID: 69264F9C923F55C1
5 changed files with 52 additions and 11 deletions

View File

@ -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()

View File

@ -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

View File

@ -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}

View File

@ -0,0 +1 @@
{"deviceType": "raspberrypi3"}

View File

@ -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')
)