Pass api key to app, and keep it saved in the DB (do not regenerate on startup)

This commit is contained in:
Pablo Carranza Vélez 2015-10-02 17:14:47 +00:00
parent 3f3a26965e
commit ec6fd6eb03
4 changed files with 40 additions and 25 deletions

View File

@ -14,20 +14,16 @@ knex.init.then ->
console.log('Starting connectivity check..') console.log('Starting connectivity check..')
utils.connectivityCheck() utils.connectivityCheck()
bootstrap.startBootstrapping() Promise.join bootstrap.startBootstrapping(), utils.getOrGenerateApiSecret(), (uuid, secret) ->
.then (uuid) ->
# Persist the uuid in subsequent metrics # Persist the uuid in subsequent metrics
utils.mixpanelProperties.uuid = uuid utils.mixpanelProperties.uuid = uuid
api = require './api' api = require './api'
application = require('./application')(uuid) application = require('./application')(uuid)
device = require './device' device = require './device'
randomHexString = require './lib/random-hex-string'
bootstrap.done bootstrap.done
.then -> .then ->
return config.forceApiSecret ? randomHexString.generate()
.then (secret) ->
console.log('Starting API server..') console.log('Starting API server..')
api(secret, application).listen(config.listenPort) api(secret, application).listen(config.listenPort)
# Let API know what version we are, and our api connection info. # Let API know what version we are, and our api connection info.

View File

@ -349,25 +349,23 @@ application.update = update = (force) ->
customOptions: customOptions:
apikey: apiKey apikey: apiKey
remoteAppEnvs = {}
Promise.join deviceId, remoteApps, (deviceId, remoteApps) -> Promise.join deviceId, remoteApps, (deviceId, remoteApps) ->
return Promise.map remoteApps, (remoteApp) -> return Promise.map remoteApps, (app) ->
getEnvironment(remoteApp.id, deviceId, apiKey) getEnvironment(app.id, deviceId, apiKey)
.then (environment) -> .then (environment) ->
remoteApp.environment_variable = environment app.environment_variable = environment
return remoteApp utils.extendEnvVars(app.environment_variable, uuid)
.then (env) ->
remoteAppEnvs[app.id] = env
env = _.omit(env, _.keys(specialActionEnvVars))
return {
appId: '' + app.id
commit: app.commit
imageId: "#{config.registryEndpoint}/#{path.basename(app.git_repository, '.git')}/#{app.commit}"
env: JSON.stringify(env) # The env has to be stored as a JSON string for knex
}
.then (remoteApps) -> .then (remoteApps) ->
remoteAppEnvs = {}
remoteApps = _.map remoteApps, (app) ->
env = utils.extendEnvVars(app.environment_variable, uuid)
remoteAppEnvs[app.id] = env
env = _.omit(env, _.keys(specialActionEnvVars))
return {
appId: '' + app.id
commit: app.commit
imageId: "#{config.registryEndpoint}/#{path.basename(app.git_repository, '.git')}/#{app.commit}"
env: JSON.stringify(env) # The env has to be stored as a JSON string for knex
}
remoteApps = _.indexBy(remoteApps, 'appId') remoteApps = _.indexBy(remoteApps, 'appId')
remoteAppIds = _.keys(remoteApps) remoteAppIds = _.keys(remoteApps)

View File

@ -22,8 +22,10 @@ loadPreloadedApps = ->
fs.readFileAsync(appsPath, 'utf8') fs.readFileAsync(appsPath, 'utf8')
.then(JSON.parse) .then(JSON.parse)
.map (app) -> .map (app) ->
app.env = JSON.stringify(utils.extendEnvVars(app.env, userConfig.uuid)) utils.extendEnvVars(app.env, userConfig.uuid)
knex('app').insert(app) .then (extendedEnv) ->
app.env = JSON.stringify(extendedEnv)
knex('app').insert(app)
.catch (err) -> .catch (err) ->
utils.mixpanelTrack('Loading preloaded apps failed', {error: err}) utils.mixpanelTrack('Loading preloaded apps failed', {error: err})

View File

@ -2,10 +2,12 @@ Promise = require 'bluebird'
_ = require 'lodash' _ = require 'lodash'
fs = Promise.promisifyAll require 'fs' fs = Promise.promisifyAll require 'fs'
config = require './config' config = require './config'
knex = require './db'
mixpanel = require 'mixpanel' mixpanel = require 'mixpanel'
networkCheck = require 'network-checker' networkCheck = require 'network-checker'
blink = require('blinking')(config.ledFile) blink = require('blinking')(config.ledFile)
url = require 'url' url = require 'url'
randomHexString = require './lib/random-hex-string'
utils = exports utils = exports
@ -97,13 +99,30 @@ exports.connectivityCheck = _.once ->
console.log('Waiting for connectivity...') console.log('Waiting for connectivity...')
blink.pattern.start(networkPattern) blink.pattern.start(networkPattern)
exports.getOrGenerateApiSecret = do ->
apiSecretPromise = null
return ->
apiSecretPromise ?= Promise.rejected()
apiSecretPromise = apiSecretPromise.catch ->
knex('config').select('value').where(key: 'apiSecret')
.then ([ apiSecret ]) ->
return apiSecret if apiSecret?
Promise.try ->
return config.forceApiSecret ? randomHexString.generate()
.then (newSecret) ->
knex('config').insert([{ key: 'apiSecret', value: newSecret }])
.return(newSecret)
exports.extendEnvVars = (env, uuid) -> exports.extendEnvVars = (env, uuid) ->
host = '127.0.0.1'
newEnv = newEnv =
RESIN_DEVICE_UUID: uuid RESIN_DEVICE_UUID: uuid
RESIN_SUPERVISOR_ADDRESS: 'http://127.0.0.1:' + config.listenPort RESIN_SUPERVISOR_ADDRESS: "http://#{host}:#{config.listenPort}"
RESIN_SUPERVISOR_HOST: host
RESIN_SUPERVISOR_PORT: config.listenPort RESIN_SUPERVISOR_PORT: config.listenPort
RESIN_SUPERVISOR_API_KEY: exports.getOrGenerateApiSecret()
RESIN: '1' RESIN: '1'
USER: 'root' USER: 'root'
if env? if env?
_.extend(newEnv, env) _.extend(newEnv, env)
return newEnv return Promise.props(newEnv)