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..')
utils.connectivityCheck()
bootstrap.startBootstrapping()
.then (uuid) ->
Promise.join bootstrap.startBootstrapping(), utils.getOrGenerateApiSecret(), (uuid, secret) ->
# Persist the uuid in subsequent metrics
utils.mixpanelProperties.uuid = uuid
api = require './api'
application = require('./application')(uuid)
device = require './device'
randomHexString = require './lib/random-hex-string'
bootstrap.done
.then ->
return config.forceApiSecret ? randomHexString.generate()
.then (secret) ->
console.log('Starting API server..')
api(secret, application).listen(config.listenPort)
# Let API know what version we are, and our api connection info.

View File

@ -349,25 +349,23 @@ application.update = update = (force) ->
customOptions:
apikey: apiKey
remoteAppEnvs = {}
Promise.join deviceId, remoteApps, (deviceId, remoteApps) ->
return Promise.map remoteApps, (remoteApp) ->
getEnvironment(remoteApp.id, deviceId, apiKey)
return Promise.map remoteApps, (app) ->
getEnvironment(app.id, deviceId, apiKey)
.then (environment) ->
remoteApp.environment_variable = environment
return remoteApp
app.environment_variable = environment
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) ->
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')
remoteAppIds = _.keys(remoteApps)

View File

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

View File

@ -2,10 +2,12 @@ Promise = require 'bluebird'
_ = require 'lodash'
fs = Promise.promisifyAll require 'fs'
config = require './config'
knex = require './db'
mixpanel = require 'mixpanel'
networkCheck = require 'network-checker'
blink = require('blinking')(config.ledFile)
url = require 'url'
randomHexString = require './lib/random-hex-string'
utils = exports
@ -97,13 +99,30 @@ exports.connectivityCheck = _.once ->
console.log('Waiting for connectivity...')
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) ->
host = '127.0.0.1'
newEnv =
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_API_KEY: exports.getOrGenerateApiSecret()
RESIN: '1'
USER: 'root'
if env?
_.extend(newEnv, env)
return newEnv
return Promise.props(newEnv)