mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 19:17:58 +00:00
Input validations, help strings, default_user support
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -16,6 +15,11 @@ import (
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
/*
|
||||
Executes the command represented by the input.
|
||||
Multiple commands can be run by concatinating strings with ';'.
|
||||
Currently, it is supported only for linux platforms and user bash shell.
|
||||
*/
|
||||
func exec_command(cmdString string) error {
|
||||
cmd := exec.Command("/bin/bash", "-c", cmdString)
|
||||
if _, err := cmd.Output(); err != nil {
|
||||
@@ -24,11 +28,16 @@ func exec_command(cmdString string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
Transfers the file to the target machine by establishing an SSH session with the target.
|
||||
Uses the public key authentication method and hence the parameter 'key' takes in the private key.
|
||||
The fileName parameter takes an absolute path.
|
||||
*/
|
||||
func uploadFileScp(fileName, username, ip, key string) error {
|
||||
nameBase := filepath.Base(fileName)
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to open file")
|
||||
return err
|
||||
}
|
||||
stat, err := file.Stat()
|
||||
if os.IsNotExist(err) {
|
||||
@@ -36,7 +45,7 @@ func uploadFileScp(fileName, username, ip, key string) error {
|
||||
}
|
||||
session, err := createSSHPublicKeysSession(username, ip, key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to create SSH Session using public keys: %s", err)
|
||||
return err
|
||||
}
|
||||
if session == nil {
|
||||
return fmt.Errorf("Invalid session object")
|
||||
@@ -50,12 +59,19 @@ func uploadFileScp(fileName, username, ip, key string) error {
|
||||
w.Close()
|
||||
}()
|
||||
if err := session.Run(fmt.Sprintf("scp -vt %s", nameBase)); err != nil {
|
||||
return fmt.Errorf("Failed to run: %s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func createSSHPublicKeysSession(username string, ipAddr string, hostKey string) (*ssh.Session, error) {
|
||||
/*
|
||||
Creates a SSH session object which can be used to run commands in the target machine.
|
||||
The session will use public key authentication method with port 22.
|
||||
*/
|
||||
func createSSHPublicKeysSession(username, ipAddr, hostKey string) (*ssh.Session, error) {
|
||||
if username == "" || ipAddr == "" || hostKey == "" {
|
||||
return nil, fmt.Errorf("Invalid parameters")
|
||||
}
|
||||
signer, err := ssh.ParsePrivateKey([]byte(hostKey))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Parsing Private Key failed: %s", err)
|
||||
@@ -70,7 +86,7 @@ func createSSHPublicKeysSession(username string, ipAddr string, hostKey string)
|
||||
|
||||
client, err := ssh.Dial("tcp", ipAddr+":22", config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Dial Failed: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
if client == nil {
|
||||
return nil, fmt.Errorf("Invalid client object: %s", err)
|
||||
@@ -78,35 +94,45 @@ func createSSHPublicKeysSession(username string, ipAddr string, hostKey string)
|
||||
|
||||
session, err := client.NewSession()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Creating new client session failed: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func removeFile(fileName string) {
|
||||
/*
|
||||
Deletes the file in the current directory.
|
||||
The parameter is just the name of the file and not a path.
|
||||
*/
|
||||
func removeFile(fileName string) error {
|
||||
if fileName == "" {
|
||||
return fmt.Errorf("Invalid file name")
|
||||
}
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Printf("Error fetching working directory:%s", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
absFileName := wd + "/" + fileName
|
||||
|
||||
if _, err := os.Stat(absFileName); err == nil {
|
||||
err := os.Remove(absFileName)
|
||||
if err != nil {
|
||||
log.Printf(fmt.Sprintf("Failed: %s", err))
|
||||
return
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateRSAKeys() (string, string, error) {
|
||||
/*
|
||||
Creates a new RSA key pair with key length of 2048.
|
||||
The private key will be of pem format and the public key will be of OpenSSH format.
|
||||
*/
|
||||
func generateRSAKeys() (publicKeyRsa string, privateKeyRsa string, err error) {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("error generating RSA key-pair: %s", err)
|
||||
}
|
||||
|
||||
privateKeyRsa := string(pem.EncodeToMemory(&pem.Block{
|
||||
privateKeyRsa = string(pem.EncodeToMemory(&pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
||||
}))
|
||||
@@ -115,10 +141,6 @@ func generateRSAKeys() (string, string, error) {
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("error generating RSA key-pair: %s", err)
|
||||
}
|
||||
publicKeyRsa := "ssh-rsa " + base64.StdEncoding.EncodeToString(sshPublicKey.Marshal())
|
||||
|
||||
//ioutil.WriteFile("testkey.pem", []byte(privateKeyRsa), 0600)
|
||||
//ioutil.WriteFile("testkey.pub", []byte(publicKeyRsa), 0600)
|
||||
|
||||
return publicKeyRsa, privateKeyRsa, nil
|
||||
publicKeyRsa = "ssh-rsa " + base64.StdEncoding.EncodeToString(sshPublicKey.Marshal())
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user