2014-11-20 18:00:39 +00:00
|
|
|
_ = require('lodash')
|
2014-12-09 18:35:45 +00:00
|
|
|
_.str = require('underscore.string')
|
2014-12-19 17:06:58 +00:00
|
|
|
async = require('async')
|
|
|
|
fs = require('fs')
|
2014-11-26 16:07:10 +00:00
|
|
|
resin = require('../resin')
|
2014-11-20 18:00:39 +00:00
|
|
|
helpers = require('../helpers/helpers')
|
2014-12-05 16:00:15 +00:00
|
|
|
ui = require('../ui')
|
2014-12-12 21:20:29 +00:00
|
|
|
permissions = require('../permissions/permissions')
|
2014-11-20 17:02:29 +00:00
|
|
|
|
2014-12-12 21:20:29 +00:00
|
|
|
exports.list = permissions.user ->
|
2014-12-05 14:53:59 +00:00
|
|
|
resin.server.get resin.settings.get('urls.keys'), (error, response, keys) ->
|
2014-11-26 16:38:02 +00:00
|
|
|
resin.errors.handle(error) if error?
|
2014-12-05 16:00:15 +00:00
|
|
|
resin.log.out ui.widgets.table.horizontal keys, (key) ->
|
2014-11-20 17:02:29 +00:00
|
|
|
delete key.public_key
|
|
|
|
return key
|
|
|
|
, [ 'ID', 'Title' ]
|
2014-11-20 18:00:39 +00:00
|
|
|
|
2014-12-12 21:20:29 +00:00
|
|
|
exports.info = permissions.user (params) ->
|
2014-11-20 18:00:39 +00:00
|
|
|
|
|
|
|
# TODO: We don't have a way to query a single ssh key yet.
|
|
|
|
# As a workaround, we request all of them, and filter
|
|
|
|
# the one we need. Fix once we have a better way.
|
2014-12-05 14:53:59 +00:00
|
|
|
resin.server.get resin.settings.get('urls.keys'), (error, response, keys) ->
|
2014-11-26 16:38:02 +00:00
|
|
|
resin.errors.handle(error) if error?
|
2014-12-12 21:20:29 +00:00
|
|
|
key = _.findWhere(keys, id: params.id)
|
2014-11-20 18:00:39 +00:00
|
|
|
if not key?
|
2014-12-12 21:20:29 +00:00
|
|
|
resin.errors.handle(new resin.errors.NotFound("key #{params.id}"))
|
2014-11-20 18:00:39 +00:00
|
|
|
|
2014-12-12 21:20:29 +00:00
|
|
|
key.public_key = '\n' + _.str.chop(key.public_key, resin.settings.get('sshKeyWidth')).join('\n')
|
2014-12-05 16:00:15 +00:00
|
|
|
resin.log.out(ui.widgets.table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ]))
|
2014-11-21 17:56:11 +00:00
|
|
|
|
2014-12-12 21:20:29 +00:00
|
|
|
exports.remove = permissions.user (params, options) ->
|
|
|
|
ui.patterns.remove 'key', options.yes, (callback) ->
|
|
|
|
url = _.template(resin.settings.get('urls.sshKey'), id: params.id)
|
2014-12-01 14:06:03 +00:00
|
|
|
resin.server.delete(url, callback)
|
2014-11-26 16:38:02 +00:00
|
|
|
, resin.errors.handle
|
2014-12-19 17:06:58 +00:00
|
|
|
|
|
|
|
exports.add = permissions.user (params) ->
|
|
|
|
async.waterfall [
|
|
|
|
|
|
|
|
(callback) ->
|
2014-12-22 14:30:21 +00:00
|
|
|
if params.path?
|
|
|
|
fs.readFile(params.path, encoding: 'utf8', callback)
|
|
|
|
else
|
|
|
|
helpers.readStdin(callback)
|
2014-12-19 17:06:58 +00:00
|
|
|
|
2014-12-22 14:30:21 +00:00
|
|
|
(key, callback) ->
|
|
|
|
key = key.trim()
|
2014-12-19 17:06:58 +00:00
|
|
|
|
|
|
|
url = resin.settings.get('urls.keys')
|
|
|
|
data =
|
|
|
|
title: params.name
|
2014-12-22 14:30:21 +00:00
|
|
|
key: key
|
2014-12-19 17:06:58 +00:00
|
|
|
resin.server.post(url, data, callback)
|
|
|
|
|
|
|
|
], (error) ->
|
|
|
|
return if not error?
|
|
|
|
|
|
|
|
# TODO: Make handle() check the error type
|
|
|
|
# and accomodate as most as possible to prevent
|
|
|
|
# this types of checks in client code.
|
|
|
|
if error.code is 'EISDIR'
|
|
|
|
error.message = "File is a directory: #{params.path}"
|
|
|
|
|
|
|
|
if error.code is 'ENOENT'
|
|
|
|
error = new resin.errors.FileNotFound(params.path)
|
|
|
|
|
|
|
|
resin.errors.handle(error)
|