fix(core): update

This commit is contained in:
2018-09-17 22:32:31 +02:00
parent 0454eef45f
commit c032ff69d0
29 changed files with 1311 additions and 1487 deletions

View File

@ -1,7 +1,6 @@
import 'typings-global'
import * as plugins from './smartssh.plugins'
import * as plugins from './smartssh.plugins';
export {SshInstance} from './smartssh.classes.sshinstance'
export {SshKey} from './smartssh.classes.sshkey'
export {SshDir} from './smartssh.classes.sshdir'
export {SshConfig} from './smartssh.classes.sshconfig'
export { SshInstance } from './smartssh.classes.sshinstance';
export { SshKey } from './smartssh.classes.sshkey';
export { SshDir } from './smartssh.classes.sshdir';
export { SshConfig } from './smartssh.classes.sshconfig';

View File

@ -1,8 +1,7 @@
import 'typings-global'
import * as plugins from './smartssh.plugins'
import {SshKey} from './smartssh.classes.sshkey'
import * as plugins from './smartssh.plugins';
import { SshKey } from './smartssh.classes.sshkey';
export let sshKeyArrayFromDir = function(dirArg: string): SshKey[]{
let sshKeyArray = [] // TODO
return sshKeyArray
}
export let sshKeyArrayFromDir = function(dirArg: string): SshKey[] {
let sshKeyArray = []; // TODO
return sshKeyArray;
};

View File

@ -1,54 +1,60 @@
import 'typings-global'
import * as plugins from './smartssh.plugins'
import * as helpers from './smartssh.classes.helpers'
import {SshKey} from './smartssh.classes.sshkey'
import * as plugins from './smartssh.plugins';
import * as helpers from './smartssh.classes.helpers';
import { SshKey } from './smartssh.classes.sshkey';
export class SshConfig {
private _sshKeyArray: SshKey[]
constructor(sshKeyArrayArg: SshKey[]) {
this._sshKeyArray = sshKeyArrayArg
}
private _sshKeyArray: SshKey[];
constructor(sshKeyArrayArg: SshKey[]) {
this._sshKeyArray = sshKeyArrayArg;
}
/**
* stores a config file
*/
store(dirPathArg: string) {
let done = plugins.q.defer()
let configArray: configObject[] = []
let configString
for (let key in this._sshKeyArray) {
let sshKey = this._sshKeyArray[key]
if (sshKey.host) {
configString = 'Host ' + sshKey.host + '\n' +
' HostName ' + sshKey.host + '\n' +
' IdentityFile ~/.ssh/' + sshKey.host + '\n' +
' StrictHostKeyChecking no' + '\n'
}
configArray.push({
configString: configString,
authorized: sshKey.authorized,
sshKey: sshKey
})
}
let configFile: string = ''
for (let key in configArray) {
configFile = configFile + configArray[key].configString + '\n'
};
plugins.smartfile.memory.toFsSync(configFile,plugins.path.join(dirPathArg,'config'))
return done.promise
/**
* stores a config file
*/
store(dirPathArg: string) {
let done = plugins.q.defer();
let configArray: configObject[] = [];
let configString;
for (let key in this._sshKeyArray) {
let sshKey = this._sshKeyArray[key];
if (sshKey.host) {
configString =
'Host ' +
sshKey.host +
'\n' +
' HostName ' +
sshKey.host +
'\n' +
' IdentityFile ~/.ssh/' +
sshKey.host +
'\n' +
' StrictHostKeyChecking no' +
'\n';
}
configArray.push({
configString: configString,
authorized: sshKey.authorized,
sshKey: sshKey
});
}
read(dirPathArg) {
let done = plugins.q.defer()
let configArray: configObject[]
plugins.smartfile.fs.toStringSync(plugins.path.join(dirPathArg,'config'))
return done.promise
let configFile: string = '';
for (let key in configArray) {
configFile = configFile + configArray[key].configString + '\n';
}
};
plugins.smartfile.memory.toFsSync(configFile, plugins.path.join(dirPathArg, 'config'));
return done.promise;
}
read(dirPathArg) {
let done = plugins.q.defer();
let configArray: configObject[];
plugins.smartfile.fs.toStringSync(plugins.path.join(dirPathArg, 'config'));
return done.promise;
}
}
export interface configObject {
configString: string
authorized: boolean
sshKey: SshKey
};
configString: string;
authorized: boolean;
sshKey: SshKey;
}

View File

@ -1,42 +1,47 @@
import 'typings-global'
import * as plugins from './smartssh.plugins'
import * as helpers from './smartssh.classes.helpers'
import { SshInstance } from './smartssh.classes.sshinstance'
import { SshKey } from './smartssh.classes.sshkey'
import { SshConfig } from './smartssh.classes.sshconfig'
import * as plugins from './smartssh.plugins';
import * as helpers from './smartssh.classes.helpers';
import { SshInstance } from './smartssh.classes.sshinstance';
import { SshKey } from './smartssh.classes.sshkey';
import { SshConfig } from './smartssh.classes.sshconfig';
export class SshDir { // sshDir class -> NOT EXPORTED, ONLY FOR INTERNAL USE
private _path: string // the path of the ssh directory
private _sshKeyArray: SshKey[]
private _sshConfig: SshConfig
constructor (sshKeyArray: SshKey[], sshConfig: SshConfig, sshDirPathArg?: string) {
this._sshKeyArray = sshKeyArray
this._sshConfig = sshConfig
export class SshDir {
// sshDir class -> NOT EXPORTED, ONLY FOR INTERNAL USE
private _path: string; // the path of the ssh directory
private _sshKeyArray: SshKey[];
private _sshConfig: SshConfig;
constructor(sshKeyArray: SshKey[], sshConfig: SshConfig, sshDirPathArg?: string) {
this._sshKeyArray = sshKeyArray;
this._sshConfig = sshConfig;
if (sshDirPathArg) {
this._path = sshDirPathArg
this._path = sshDirPathArg;
} else {
this._path = plugins.path.join(plugins.smartpath.get.home(), '.ssh/')
this._path = plugins.path.join(plugins.smartpath.get.home(), '.ssh/');
}
}
writeToDir (dirPathArg?: string) { // syncs sshInstance to directory
let path = this._path
if (dirPathArg) path = dirPathArg
this._sshKeyArray.forEach((sshKeyArg) => {
sshKeyArg.store(path)
})
this._sshConfig.store(path)
writeToDir(dirPathArg?: string) {
// syncs sshInstance to directory
let path = this._path;
if (dirPathArg) path = dirPathArg;
this._sshKeyArray.forEach(sshKeyArg => {
sshKeyArg.store(path);
});
this._sshConfig.store(path);
}
readFromDir (dirPathArg?: string) { // syncs sshInstance from directory
let path = this._path
if (dirPathArg) path = dirPathArg
/**
* TODO: implement reading of directories
*/
readFromDir(dirPathArg?: string) {
// syncs sshInstance from directory
let path = this._path;
if (dirPathArg) path = dirPathArg;
}
updateDirPath (dirPathArg: string) {
this._path = dirPathArg
updateDirPath(dirPathArg: string) {
this._path = dirPathArg;
}
getKeys () {
return helpers.sshKeyArrayFromDir(this._path)
getKeys() {
return helpers.sshKeyArrayFromDir(this._path);
}
}

View File

@ -1,103 +1,102 @@
import 'typings-global'
import * as plugins from './smartssh.plugins'
import * as helpers from './smartssh.classes.helpers'
import * as plugins from './smartssh.plugins';
import * as helpers from './smartssh.classes.helpers';
import { SshDir } from './smartssh.classes.sshdir'
import { SshConfig } from './smartssh.classes.sshconfig'
import { SshKey } from './smartssh.classes.sshkey'
import { SshDir } from './smartssh.classes.sshdir';
import { SshConfig } from './smartssh.classes.sshconfig';
import { SshKey } from './smartssh.classes.sshkey';
/**
* SshInstance is the main class dealing with ssh management
*/
export class SshInstance {
private _sshKeyArray: SshKey[] // holds all ssh keys
private _sshConfig: SshConfig // sshConfig (e.g. represents ~/.ssh/config)
private _sshDir: SshDir // points to sshDir class instance.
private _sshSync: boolean // if set to true, the ssh dir will be kept in sync automatically
constructor(optionsArg: { sshDirPath?: string, sshSync?: boolean } = {}) {
optionsArg ? void (0) : optionsArg = {}
this._sshKeyArray = []
this._sshConfig = new SshConfig(this._sshKeyArray)
this._sshDir = new SshDir(this._sshKeyArray, this._sshConfig, optionsArg.sshDirPath)
this._sshSync = optionsArg.sshSync
};
private _sshKeyArray: SshKey[]; // holds all ssh keys
private _sshConfig: SshConfig; // sshConfig (e.g. represents ~/.ssh/config)
private _sshDir: SshDir; // points to sshDir class instance.
private _sshSync: boolean; // if set to true, the ssh dir will be kept in sync automatically
constructor(optionsArg: { sshDirPath?: string; sshSync?: boolean } = {}) {
optionsArg ? void 0 : (optionsArg = {});
this._sshKeyArray = [];
this._sshConfig = new SshConfig(this._sshKeyArray);
this._sshDir = new SshDir(this._sshKeyArray, this._sshConfig, optionsArg.sshDirPath);
this._sshSync = optionsArg.sshSync;
}
// altering methods
addKey (sshKeyArg: SshKey) {
this._syncAuto('from')
this._sshKeyArray.push(sshKeyArg)
this._syncAuto('to')
};
removeKey (sshKeyArg: SshKey) {
this._syncAuto('from')
addKey(sshKeyArg: SshKey) {
this._syncAuto('from');
this._sshKeyArray.push(sshKeyArg);
this._syncAuto('to');
}
removeKey(sshKeyArg: SshKey) {
this._syncAuto('from');
let filteredArray = this._sshKeyArray.filter((sshKeyArg2: SshKey) => {
return (sshKeyArg != sshKeyArg2)
})
this._sshKeyArray = filteredArray
this._syncAuto('to')
};
replaceKey (sshKeyOldArg: SshKey, sshKeyNewArg: SshKey) {
this._syncAuto('from')
this.removeKey(sshKeyOldArg)
this.addKey(sshKeyNewArg)
this._syncAuto('to')
};
return sshKeyArg != sshKeyArg2;
});
this._sshKeyArray = filteredArray;
this._syncAuto('to');
}
replaceKey(sshKeyOldArg: SshKey, sshKeyNewArg: SshKey) {
this._syncAuto('from');
this.removeKey(sshKeyOldArg);
this.addKey(sshKeyNewArg);
this._syncAuto('to');
}
//
getKey (hostArg: string): SshKey {
this._syncAuto('from')
let filteredArray = this._sshKeyArray.filter(function (keyArg) {
return (keyArg.host === hostArg)
})
// non altering methods
getKey(hostArg: string): SshKey {
this._syncAuto('from');
let filteredArray = this._sshKeyArray.filter(function(keyArg) {
return keyArg.host === hostArg;
});
if (filteredArray.length > 0) {
return filteredArray[ 0 ]
return filteredArray[0];
} else {
return undefined
return undefined;
}
};
get sshKeys (): SshKey[] {
this._syncAuto('from')
return this._sshKeyArray
};
}
get sshKeys(): SshKey[] {
this._syncAuto('from');
return this._sshKeyArray;
}
// FS methods
/**
* write SshInstance to disk
*/
writeToDisk (dirPathArg?: string) {
this._sync('to', dirPathArg)
writeToDisk(dirPathArg?: string) {
this._sync('to', dirPathArg);
}
/**
* read ab SshInstance from disk
*/
readFromDisk (dirPathArg?: string) {
this._sync('from', dirPathArg)
readFromDisk(dirPathArg?: string) {
this._sync('from', dirPathArg);
}
/* ===============================================================
========================= Private Methods ========================
================================================================*/
private _makeConfig () {
}
/**
* method to invoke SshInstance _sync automatically when sshSync is true
*/
private _syncAuto (directionArg) {
if (this._sshSync) this._sync(directionArg)
private _syncAuto(directionArg: 'from' | 'to') {
if (this._sshSync) {
// if auto sync is not enabled, do nothing
this._sync(directionArg);
}
}
/**
* private method to sync SshInstance
*/
private _sync (directionArg: string, dirPathArg?: string) {
private _sync(directionArg: 'from' | 'to', dirPathArg?: string) {
if (directionArg === 'from') {
this._sshDir.readFromDir(dirPathArg) // call sync method of sshDir class;
this._sshDir.readFromDir(dirPathArg); // call sync method of sshDir class;
} else if (directionArg === 'to') {
this._sshDir.writeToDir(dirPathArg)
} else {
throw new Error("directionArg not recognised. Must be 'to' or 'from'")
this._sshDir.writeToDir(dirPathArg);
}
};
}
}

View File

@ -1,99 +1,106 @@
import 'typings-global'
import * as plugins from './smartssh.plugins'
import * as helpers from './smartssh.classes.helpers'
import * as plugins from './smartssh.plugins';
import * as helpers from './smartssh.classes.helpers';
export class SshKey {
private _privKey: string
private _pubKey: string
private _hostVar: string
private _authorized: boolean
private _privKey: string;
private _pubKey: string;
private _hostVar: string;
private _authorized: boolean;
/**
* the constructor for class SshKey
*/
constructor(optionsArg: {private?: string,public?: string,host?: string,authorized?: boolean}= {}) {
this._privKey = optionsArg.private
this._pubKey = optionsArg.public
this._hostVar = optionsArg.host
this._authorized = optionsArg.authorized
};
private _smarthshellInstance = new plugins.shelljs.Smartshell({
executor: 'bash'
});
// this.host
get host(){
return this._hostVar
};
set host(hostArg: string){
this._hostVar = hostArg
};
/**
* the constructor for class SshKey
*/
constructor(
optionsArg: { private?: string; public?: string; host?: string; authorized?: boolean } = {}
) {
this._privKey = optionsArg.private;
this._pubKey = optionsArg.public;
this._hostVar = optionsArg.host;
this._authorized = optionsArg.authorized;
}
// this.privKey
get privKey(){
return this._privKey
};
set privKey(privateKeyArg: string){
this._privKey = privateKeyArg
};
// this.host
get host() {
return this._hostVar;
}
set host(hostArg: string) {
this._hostVar = hostArg;
}
// this.privKeyBase64
get privKeyBase64(){
return plugins.smartstring.base64.encode(this._privKey)
}
set privKeyBase64(privateKeyArg: string) {
this._privKey = plugins.smartstring.base64.decode(privateKeyArg)
}
// this.privKey
get privKey() {
return this._privKey;
}
set privKey(privateKeyArg: string) {
this._privKey = privateKeyArg;
}
// this.pubKey
get pubKey(){
return this._pubKey
}
set pubKey(publicKeyArg: string){
this._pubKey = publicKeyArg
};
// this.privKeyBase64
get privKeyBase64() {
return plugins.smartstring.base64.encode(this._privKey);
}
set privKeyBase64(privateKeyArg: string) {
this._privKey = plugins.smartstring.base64.decode(privateKeyArg);
}
// this.pubKeyBase64
get pubKeyBase64(){
return plugins.smartstring.base64.encode(this._pubKey)
}
set pubKeyBase64(publicKeyArg: string) {
this._pubKey = plugins.smartstring.base64.decode(publicKeyArg)
}
// this.pubKey
get pubKey() {
return this._pubKey;
}
set pubKey(publicKeyArg: string) {
this._pubKey = publicKeyArg;
}
get authorized(){
return this._authorized
}
set authorized(authorizedArg: boolean){
this._authorized = authorizedArg
}
get type(){
if (this._privKey && this._pubKey) {
return 'duplex'
} else if (this._privKey) {
return 'private'
} else if (this._pubKey) {
return 'public'
}
};
set type(someVlueArg: any){
console.log('the type of an SshKey connot be set. This value is autpcomputed.')
}
// this.pubKeyBase64
get pubKeyBase64() {
return plugins.smartstring.base64.encode(this._pubKey);
}
set pubKeyBase64(publicKeyArg: string) {
this._pubKey = plugins.smartstring.base64.decode(publicKeyArg);
}
// methods
read(filePathArg) {
get authorized() {
return this._authorized;
}
set authorized(authorizedArg: boolean) {
this._authorized = authorizedArg;
}
/**
* returns wether there is a private, a public or both keys
*/
get type() {
if (this._privKey && this._pubKey) {
return 'duplex';
} else if (this._privKey) {
return 'private';
} else if (this._pubKey) {
return 'public';
}
}
set type(someVlueArg: any) {
console.log('the type of an SshKey connot be set. This value is autocomputed.');
}
store(dirPathArg: string) {
plugins.fs.ensureDirSync(dirPathArg)
let fileNameBase = this.host
if (this._privKey) {
let filePath = plugins.path.join(dirPathArg,fileNameBase)
plugins.smartfile.memory.toFsSync(this._privKey,filePath)
plugins.shelljs.chmod(600,filePath)
};
if (this._pubKey) {
let filePath = plugins.path.join(dirPathArg,fileNameBase + '.pub')
plugins.smartfile.memory.toFsSync(this._pubKey,filePath)
plugins.shelljs.chmod(600,filePath)
}
// methods
read(filePathArg) {}
async store(dirPathArg: string) {
plugins.fs.ensureDirSync(dirPathArg);
let fileNameBase = this.host;
if (this._privKey) {
let filePath = plugins.path.join(dirPathArg, fileNameBase);
plugins.smartfile.memory.toFsSync(this._privKey, filePath);
await this._smarthshellInstance.exec(`chmod 0600 ${filePath}`);
}
if (this._pubKey) {
let filePath = plugins.path.join(dirPathArg, fileNameBase + '.pub');
plugins.smartfile.memory.toFsSync(this._pubKey, filePath);
await this._smarthshellInstance.exec(`chmod 0600 ${filePath}`);
}
}
}

View File

@ -1,22 +1,10 @@
import 'typings-global'
import * as beautylog from 'beautylog'
import * as fs from 'fs-extra'
import * as minimatch from 'minimatch'
import * as path from 'path'
import * as q from 'q'
import * as shelljs from 'shelljs'
import * as smartfile from 'smartfile'
import * as smartpath from 'smartpath'
import * as smartstring from 'smartstring'
import * as fs from 'fs-extra';
import * as minimatch from 'minimatch';
import * as path from 'path';
import * as q from '@pushrocks/smartpromise';
import * as shelljs from '@pushrocks/smartshell';
import * as smartfile from '@pushrocks/smartfile';
import * as smartpath from '@pushrocks/smartpath';
import * as smartstring from '@pushrocks/smartstring';
export {
beautylog,
fs,
minimatch,
path,
q,
shelljs,
smartfile,
smartpath,
smartstring
}
export { fs, minimatch, path, q, shelljs, smartfile, smartpath, smartstring };