Use random secret for logs channel

This commit is contained in:
Pablo Carranza Vélez 2015-10-28 20:02:00 -03:00
parent 605d72cbdc
commit b08a028d69
5 changed files with 26 additions and 22 deletions

View File

@ -1,3 +1,5 @@
* Use random name for PubNub channel and report to API [Pablo]
# v1.2.0
* Don't bind mount (the sometimes non-existent) docker.sock [Pablo]

View File

@ -13,7 +13,7 @@ module.exports = (application) ->
api = express()
api.use(bodyParser())
api.use (req, res, next) ->
utils.getOrGenerateApiSecret()
utils.getOrGenerateSecret('apiSecret')
.then (secret) ->
if req.query.apikey is secret
next()
@ -129,9 +129,9 @@ module.exports = (application) ->
# Expires the supervisor's API key and generates a new one.
# It also communicates the new key to the Resin API.
api.post '/v1/regenerate-api-key', (req, res) ->
utils.newApiSecret()
utils.newSecret('apiSecret')
.then (secret) ->
device.updateState(apikey: secret)
device.updateState(api_secret: secret)
res.status(200).send(secret)
.catch (err) ->
res.status(503).send(err?.message or err or 'Unknown error')

View File

@ -14,12 +14,12 @@ knex.init.then ->
console.log('Starting connectivity check..')
utils.connectivityCheck()
Promise.join bootstrap.startBootstrapping(), utils.getOrGenerateApiSecret(), (uuid, secret) ->
Promise.join bootstrap.startBootstrapping(), utils.getOrGenerateSecret('apiSecret'), utils.getOrGenerateSecret('logsChannel'), (uuid, secret, logsChannel) ->
# Persist the uuid in subsequent metrics
utils.mixpanelProperties.uuid = uuid
api = require './api'
application = require('./application')(uuid)
application = require('./application')(logsChannel)
device = require './device'
bootstrap.done
@ -35,6 +35,7 @@ knex.init.then ->
provisioning_progress: null
provisioning_state: ''
download_progress: null
logs_channel: logsChannel
)
console.log('Starting Apps..')

View File

@ -508,10 +508,10 @@ application.initialize = ->
application.poll()
application.update()
module.exports = (uuid) ->
module.exports = (logsChannel) ->
logger.init(
dockerSocket: config.dockerSocket
pubnub: config.pubnub
channel: "device-#{uuid}-logs"
channel: "device-#{logsChannel}-logs"
)
return application

View File

@ -102,27 +102,28 @@ exports.connectivityCheck = _.once ->
blink.pattern.start(networkPattern)
apiSecretPromise = null
generateApiSecret = ->
secretPromises = {}
generateSecret = (name) ->
Promise.try ->
return config.forceApiSecret ? randomHexString.generate()
return config.forceApiSecret if name == 'apiSecret' && config.forceApiSecret?
return randomHexString.generate()
.then (newSecret) ->
secretInDB = { key: 'apiSecret', value: newSecret }
knex('config').update(secretInDB).where(key: 'apiSecret')
secretInDB = { key: name, value: newSecret }
knex('config').update(secretInDB).where(key: name)
.then (affectedRows) ->
knex('config').insert(secretInDB) if affectedRows == 0
.return(newSecret)
exports.newApiSecret = newApiSecret = ->
apiSecretPromise ?= Promise.resolve()
apiSecretPromise = apiSecretPromise.then ->
generateApiSecret()
exports.newSecret = newSecret = (name) ->
secretPromises[name] ?= Promise.resolve()
secretPromises[name] = secretPromises[name].then ->
generateSecret(name)
exports.getOrGenerateApiSecret = ->
apiSecretPromise ?= knex('config').select('value').where(key: 'apiSecret').then ([ apiSecret ]) ->
return apiSecret.value if apiSecret?
generateApiSecret()
return apiSecretPromise
exports.getOrGenerateSecret = (name) ->
secretPromises[name] ?= knex('config').select('value').where(key: name).then ([ secret ]) ->
return secret.value if secret?
generateSecret(name)
return secretPromises[name]
exports.extendEnvVars = (env, uuid) ->
host = '127.0.0.1'
@ -131,7 +132,7 @@ exports.extendEnvVars = (env, uuid) ->
RESIN_SUPERVISOR_ADDRESS: "http://#{host}:#{config.listenPort}"
RESIN_SUPERVISOR_HOST: host
RESIN_SUPERVISOR_PORT: config.listenPort
RESIN_SUPERVISOR_API_KEY: exports.getOrGenerateApiSecret()
RESIN_SUPERVISOR_API_KEY: exports.getOrGenerateSecret('apiSecret')
RESIN_SUPERVISOR_VERSION: exports.supervisorVersion
RESIN: '1'
USER: 'root'