Change to the new device registration method to exchange our provisioning key with a dedicated api key for the device.

Change-Type: minor
This commit is contained in:
Pagan Gazzard 2016-07-29 17:46:10 -07:00 committed by Pagan Gazzard
parent 2710a05d18
commit 03ec97ab8d
5 changed files with 40 additions and 42 deletions

View File

@ -37,7 +37,7 @@
"pubnub": "^3.7.13",
"request": "^2.51.0",
"request-progress": "^2.0.1",
"resin-register-device": "^2.0.0",
"resin-register-device": "^3.0.0",
"rimraf": "^2.5.4",
"rwlock": "^5.0.0",
"sqlite3": "^3.1.0",

View File

@ -51,17 +51,17 @@ knex.init.then ->
)
)
updateIpAddr = ->
utils.gosuper.getAsync('/v1/ipaddr', { json: true })
.spread (response, body) ->
if response.statusCode == 200 && body.Data.IPAddresses?
device.updateState(
ip_address: body.Data.IPAddresses.join(' ')
)
.catch(_.noop)
console.log('Starting periodic check for IP addresses..')
setInterval(updateIpAddr, 30 * 1000) # Every 30s
updateIpAddr()
console.log('Starting Apps..')
application.initialize()
updateIpAddr = ->
utils.gosuper.getAsync('/v1/ipaddr', { json: true })
.spread (response, body) ->
if response.statusCode == 200 && body.Data.IPAddresses?
device.updateState(
ip_address: body.Data.IPAddresses.join(' ')
)
.catch(_.noop)
console.log('Starting periodic check for IP addresses..')
setInterval(updateIpAddr, 30 * 1000) # Every 30s
updateIpAddr()

View File

@ -2,7 +2,6 @@ Promise = require 'bluebird'
knex = require './db'
utils = require './utils'
deviceRegister = require 'resin-register-device'
{ resinApi } = require './request'
fs = Promise.promisifyAll(require('fs'))
config = require './config'
configPath = '/boot/config.json'
@ -11,9 +10,6 @@ _ = require 'lodash'
deviceConfig = require './device-config'
userConfig = {}
DuplicateUuidError = (err) ->
return err.message == '"uuid" must be unique.'
bootstrapper = {}
loadPreloadedApps = ->
@ -39,22 +35,22 @@ bootstrap = ->
userConfig.deviceType ?= 'raspberry-pi'
if userConfig.registered_at?
return userConfig
deviceRegister.register(resinApi, userConfig)
deviceRegister.register(
userId: userConfig.userId
applicationId: userConfig.applicationId
uuid: userConfig.uuid
deviceType: userConfig.deviceType
deviceApiKey: userConfig.deviceApiKey
provisioningApiKey: userConfig.apiKey
apiEndpoint: config.apiEndpoint
)
.timeout(config.apiTimeout)
.catch DuplicateUuidError, ->
resinApi.get
resource: 'device'
options:
filter:
uuid: userConfig.uuid
customOptions:
apikey: userConfig.apiKey
.timeout(config.apiTimeout)
.then ([ device ]) ->
return device
.then (device) ->
.then ({ id }) ->
userConfig.registered_at = Date.now()
userConfig.deviceId = device.id
userConfig.deviceId = id
# Delete the provisioning key now.
delete userConfig.apiKey
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
.return(userConfig)
.then (userConfig) ->
@ -63,7 +59,7 @@ bootstrap = ->
.then ->
knex('config').insert([
{ key: 'uuid', value: userConfig.uuid }
{ key: 'apiKey', value: userConfig.apiKey }
{ key: 'apiKey', value: userConfig.deviceApiKey }
{ key: 'username', value: userConfig.username }
{ key: 'userId', value: userConfig.userId }
{ key: 'version', value: utils.supervisorVersion }
@ -77,12 +73,11 @@ readConfig = ->
readConfigAndEnsureUUID = ->
Promise.try ->
return userConfig.uuid if userConfig.uuid?
deviceRegister.generateUUID()
.then (uuid) ->
userConfig.uuid = uuid
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
.return(uuid)
return userConfig.uuid if userConfig.uuid? and userConfig.deviceApiKey?
userConfig.uuid ?= deviceRegister.generateUniqueKey()
userConfig.deviceApiKey ?= deviceRegister.generateUniqueKey()
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
.return(userConfig.uuid)
.catch (err) ->
console.log('Error generating and saving UUID: ', err)
Promise.delay(config.bootstrapRetryDelay)
@ -115,6 +110,7 @@ bootstrapper.startBootstrapping = ->
bootstrapper.doneBootstrapping() if !bootstrapper.offlineMode
return uuid.value
console.log('New device detected. Bootstrapping..')
readConfigAndEnsureUUID()
.tap ->
loadPreloadedApps()
@ -122,6 +118,8 @@ bootstrapper.startBootstrapping = ->
if bootstrapper.offlineMode
return knex('config').insert({ key: 'uuid', value: uuid })
else
return bootstrapOrRetry()
bootstrapOrRetry()
# Don't wait on bootstrapping here, bootstrapper.done is for that.
return
module.exports = bootstrapper

View File

@ -50,9 +50,9 @@ router.post '/v1/devices', (req, res) ->
utils.getConfig('apiKey')
utils.getConfig('userId')
device.getID()
deviceRegister.generateUUID()
randomHexString.generate()
(apiKey, userId, deviceId, uuid, logsChannel) ->
(apiKey, userId, deviceId, logsChannel) ->
uuid = deviceRegister.generateUniqueKey()
d =
user: userId
application: req.body.appId

View File

@ -7,7 +7,7 @@ while true; do
echo "UUID missing from config file, VPN cannot connect"
sleep 2
else
read uuid api_key <<<$(jq -r '.uuid,.apiKey' $CONFIG_PATH)
read uuid api_key <<<$(jq -r '.uuid,.deviceApiKey // .apiKey' $CONFIG_PATH)
mkdir -p /var/volatile/
echo $uuid > /var/volatile/vpnfile
echo $api_key >> /var/volatile/vpnfile