From 03ec97ab8d788cc7f73ff8753054174cd17daa32 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Fri, 29 Jul 2016 17:46:10 -0700 Subject: [PATCH] Change to the new device registration method to exchange our provisioning key with a dedicated api key for the device. Change-Type: minor --- package.json | 2 +- src/app.coffee | 24 ++++++++++----------- src/bootstrap.coffee | 50 +++++++++++++++++++++---------------------- src/proxyvisor.coffee | 4 ++-- tools/dind/vpn-init | 2 +- 5 files changed, 40 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index ddd08d81..475dbf78 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app.coffee b/src/app.coffee index 05da7010..847d813d 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -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() diff --git a/src/bootstrap.coffee b/src/bootstrap.coffee index e9a451a1..549abee8 100644 --- a/src/bootstrap.coffee +++ b/src/bootstrap.coffee @@ -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 diff --git a/src/proxyvisor.coffee b/src/proxyvisor.coffee index 72b3fb7c..111fed5c 100644 --- a/src/proxyvisor.coffee +++ b/src/proxyvisor.coffee @@ -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 diff --git a/tools/dind/vpn-init b/tools/dind/vpn-init index 394bcd0c..5452807b 100755 --- a/tools/dind/vpn-init +++ b/tools/dind/vpn-init @@ -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