mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-06-01 23:30:48 +00:00
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:
parent
2710a05d18
commit
03ec97ab8d
@ -37,7 +37,7 @@
|
|||||||
"pubnub": "^3.7.13",
|
"pubnub": "^3.7.13",
|
||||||
"request": "^2.51.0",
|
"request": "^2.51.0",
|
||||||
"request-progress": "^2.0.1",
|
"request-progress": "^2.0.1",
|
||||||
"resin-register-device": "^2.0.0",
|
"resin-register-device": "^3.0.0",
|
||||||
"rimraf": "^2.5.4",
|
"rimraf": "^2.5.4",
|
||||||
"rwlock": "^5.0.0",
|
"rwlock": "^5.0.0",
|
||||||
"sqlite3": "^3.1.0",
|
"sqlite3": "^3.1.0",
|
||||||
|
@ -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..')
|
console.log('Starting Apps..')
|
||||||
application.initialize()
|
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()
|
|
||||||
|
@ -2,7 +2,6 @@ Promise = require 'bluebird'
|
|||||||
knex = require './db'
|
knex = require './db'
|
||||||
utils = require './utils'
|
utils = require './utils'
|
||||||
deviceRegister = require 'resin-register-device'
|
deviceRegister = require 'resin-register-device'
|
||||||
{ resinApi } = require './request'
|
|
||||||
fs = Promise.promisifyAll(require('fs'))
|
fs = Promise.promisifyAll(require('fs'))
|
||||||
config = require './config'
|
config = require './config'
|
||||||
configPath = '/boot/config.json'
|
configPath = '/boot/config.json'
|
||||||
@ -11,9 +10,6 @@ _ = require 'lodash'
|
|||||||
deviceConfig = require './device-config'
|
deviceConfig = require './device-config'
|
||||||
userConfig = {}
|
userConfig = {}
|
||||||
|
|
||||||
DuplicateUuidError = (err) ->
|
|
||||||
return err.message == '"uuid" must be unique.'
|
|
||||||
|
|
||||||
bootstrapper = {}
|
bootstrapper = {}
|
||||||
|
|
||||||
loadPreloadedApps = ->
|
loadPreloadedApps = ->
|
||||||
@ -39,22 +35,22 @@ bootstrap = ->
|
|||||||
userConfig.deviceType ?= 'raspberry-pi'
|
userConfig.deviceType ?= 'raspberry-pi'
|
||||||
if userConfig.registered_at?
|
if userConfig.registered_at?
|
||||||
return userConfig
|
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)
|
.timeout(config.apiTimeout)
|
||||||
.catch DuplicateUuidError, ->
|
.then ({ id }) ->
|
||||||
resinApi.get
|
|
||||||
resource: 'device'
|
|
||||||
options:
|
|
||||||
filter:
|
|
||||||
uuid: userConfig.uuid
|
|
||||||
customOptions:
|
|
||||||
apikey: userConfig.apiKey
|
|
||||||
.timeout(config.apiTimeout)
|
|
||||||
.then ([ device ]) ->
|
|
||||||
return device
|
|
||||||
.then (device) ->
|
|
||||||
userConfig.registered_at = Date.now()
|
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))
|
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
|
||||||
.return(userConfig)
|
.return(userConfig)
|
||||||
.then (userConfig) ->
|
.then (userConfig) ->
|
||||||
@ -63,7 +59,7 @@ bootstrap = ->
|
|||||||
.then ->
|
.then ->
|
||||||
knex('config').insert([
|
knex('config').insert([
|
||||||
{ key: 'uuid', value: userConfig.uuid }
|
{ key: 'uuid', value: userConfig.uuid }
|
||||||
{ key: 'apiKey', value: userConfig.apiKey }
|
{ key: 'apiKey', value: userConfig.deviceApiKey }
|
||||||
{ key: 'username', value: userConfig.username }
|
{ key: 'username', value: userConfig.username }
|
||||||
{ key: 'userId', value: userConfig.userId }
|
{ key: 'userId', value: userConfig.userId }
|
||||||
{ key: 'version', value: utils.supervisorVersion }
|
{ key: 'version', value: utils.supervisorVersion }
|
||||||
@ -77,12 +73,11 @@ readConfig = ->
|
|||||||
|
|
||||||
readConfigAndEnsureUUID = ->
|
readConfigAndEnsureUUID = ->
|
||||||
Promise.try ->
|
Promise.try ->
|
||||||
return userConfig.uuid if userConfig.uuid?
|
return userConfig.uuid if userConfig.uuid? and userConfig.deviceApiKey?
|
||||||
deviceRegister.generateUUID()
|
userConfig.uuid ?= deviceRegister.generateUniqueKey()
|
||||||
.then (uuid) ->
|
userConfig.deviceApiKey ?= deviceRegister.generateUniqueKey()
|
||||||
userConfig.uuid = uuid
|
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
|
||||||
fs.writeFileAsync(configPath, JSON.stringify(userConfig))
|
.return(userConfig.uuid)
|
||||||
.return(uuid)
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
console.log('Error generating and saving UUID: ', err)
|
console.log('Error generating and saving UUID: ', err)
|
||||||
Promise.delay(config.bootstrapRetryDelay)
|
Promise.delay(config.bootstrapRetryDelay)
|
||||||
@ -115,6 +110,7 @@ bootstrapper.startBootstrapping = ->
|
|||||||
bootstrapper.doneBootstrapping() if !bootstrapper.offlineMode
|
bootstrapper.doneBootstrapping() if !bootstrapper.offlineMode
|
||||||
return uuid.value
|
return uuid.value
|
||||||
console.log('New device detected. Bootstrapping..')
|
console.log('New device detected. Bootstrapping..')
|
||||||
|
|
||||||
readConfigAndEnsureUUID()
|
readConfigAndEnsureUUID()
|
||||||
.tap ->
|
.tap ->
|
||||||
loadPreloadedApps()
|
loadPreloadedApps()
|
||||||
@ -122,6 +118,8 @@ bootstrapper.startBootstrapping = ->
|
|||||||
if bootstrapper.offlineMode
|
if bootstrapper.offlineMode
|
||||||
return knex('config').insert({ key: 'uuid', value: uuid })
|
return knex('config').insert({ key: 'uuid', value: uuid })
|
||||||
else
|
else
|
||||||
return bootstrapOrRetry()
|
bootstrapOrRetry()
|
||||||
|
# Don't wait on bootstrapping here, bootstrapper.done is for that.
|
||||||
|
return
|
||||||
|
|
||||||
module.exports = bootstrapper
|
module.exports = bootstrapper
|
||||||
|
@ -50,9 +50,9 @@ router.post '/v1/devices', (req, res) ->
|
|||||||
utils.getConfig('apiKey')
|
utils.getConfig('apiKey')
|
||||||
utils.getConfig('userId')
|
utils.getConfig('userId')
|
||||||
device.getID()
|
device.getID()
|
||||||
deviceRegister.generateUUID()
|
|
||||||
randomHexString.generate()
|
randomHexString.generate()
|
||||||
(apiKey, userId, deviceId, uuid, logsChannel) ->
|
(apiKey, userId, deviceId, logsChannel) ->
|
||||||
|
uuid = deviceRegister.generateUniqueKey()
|
||||||
d =
|
d =
|
||||||
user: userId
|
user: userId
|
||||||
application: req.body.appId
|
application: req.body.appId
|
||||||
|
@ -7,7 +7,7 @@ while true; do
|
|||||||
echo "UUID missing from config file, VPN cannot connect"
|
echo "UUID missing from config file, VPN cannot connect"
|
||||||
sleep 2
|
sleep 2
|
||||||
else
|
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/
|
mkdir -p /var/volatile/
|
||||||
echo $uuid > /var/volatile/vpnfile
|
echo $uuid > /var/volatile/vpnfile
|
||||||
echo $api_key >> /var/volatile/vpnfile
|
echo $api_key >> /var/volatile/vpnfile
|
||||||
|
Loading…
x
Reference in New Issue
Block a user