1
0
mirror of https://github.com/ssh-vault/ssh-vault.git synced 2025-08-07 13:02:55 +03:00

Implemented option to select keys -k N

This commit is contained in:
nbari
2016-10-08 15:40:06 +02:00
parent d5e47eb16e
commit e5be707960
4 changed files with 44 additions and 18 deletions

View File

@@ -24,16 +24,23 @@ func Cache() *cache {
} }
// Get return ssh-key // Get return ssh-key
func (c *cache) Get(u string) (string, error) { func (c *cache) Get(u string, k int) (string, error) {
uKey := fmt.Sprintf("%s/%s.key", c.dir, u) uKey := fmt.Sprintf("%s/%s.key-%d", c.dir, u, k)
if !c.IsFile(uKey) { if !c.IsFile(uKey) {
key, err := GetKey(u) keys, err := GetKey(u)
if err != nil { if err != nil {
return "", err return "", err
} }
err = ioutil.WriteFile(uKey, []byte(key), 0644) for k, v := range keys {
if err != nil { err = ioutil.WriteFile(fmt.Sprintf("%s/%s.key-%d", c.dir, u, k+1),
log.Println(err) []byte(v),
0644)
if err != nil {
log.Println(err)
}
}
if !c.IsFile(uKey) {
return "", fmt.Errorf("key index not found, try -k with a value between 1 and %d", len(keys))
} }
return uKey, nil return uKey, nil
} }

View File

@@ -44,8 +44,10 @@ func main() {
} }
usr, _ := user.Current() usr, _ := user.Current()
if (*k)[:2] == "~/" { if len(*k) > 2 {
*k = filepath.Join(usr.HomeDir, (*k)[2:]) if (*k)[:2] == "~/" {
*k = filepath.Join(usr.HomeDir, (*k)[2:])
}
} }
vault, err := sv.New(*k, *u, flag.Arg(0), flag.Arg(1)) vault, err := sv.New(*k, *u, flag.Arg(0), flag.Arg(1))

View File

@@ -13,7 +13,7 @@ import (
const GITHUB = "https://github.com" const GITHUB = "https://github.com"
// GetKey fetches ssh-key from url // GetKey fetches ssh-key from url
func GetKey(u string) (string, error) { func GetKey(u string) ([]string, error) {
client := &http.Client{} client := &http.Client{}
// create a new request // create a new request
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/%s.keys", req, _ := http.NewRequest("GET", fmt.Sprintf("%s/%s.keys",
@@ -23,19 +23,23 @@ func GetKey(u string) (string, error) {
req.Header.Set("User-Agent", "ssh-vault") req.Header.Set("User-Agent", "ssh-vault")
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
return "", err return nil, err
} }
defer res.Body.Close() defer res.Body.Close()
reader := bufio.NewReader(res.Body) reader := bufio.NewReader(res.Body)
tp := textproto.NewReader(reader) tp := textproto.NewReader(reader)
keys := []string{}
for { for {
if line, err := tp.ReadLine(); err != nil { if line, err := tp.ReadLine(); err != nil {
if err == io.EOF { if err == io.EOF {
return "", fmt.Errorf("key %q not found", u) if len(keys) == 0 {
return nil, fmt.Errorf("key %q not found", u)
}
return keys, nil
} }
return "", err return nil, err
} else if strings.HasPrefix(line, "ssh-rsa") { } else if strings.HasPrefix(line, "ssh-rsa") {
return line, nil keys = append(keys, line)
} }
} }
} }

View File

@@ -9,13 +9,13 @@ import (
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
) )
// Vault structure // Vault structure
type vault struct { type vault struct {
key string key string
option string
vault string vault string
PublicKey *rsa.PublicKey PublicKey *rsa.PublicKey
Fingerprint string Fingerprint string
@@ -30,17 +30,30 @@ func New(k, u, o, v string) (*vault, error) {
) )
cache := Cache() cache := Cache()
if u != "" { if u != "" {
keyPath, err = cache.Get(u) // use -k N where N is the index to use when multiple keys
// are available
var ki int
if ki, err = strconv.Atoi(k); err != nil {
ki = 1
}
if ki <= 1 {
ki = 1
}
keyPath, err = cache.Get(u, ki)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else if !cache.IsFile(keyPath) { } else if !cache.IsFile(keyPath) {
return nil, fmt.Errorf("key not found or unable to read") return nil, fmt.Errorf("key not found or unable to read")
} }
if o == "create" {
if cache.IsFile(v) {
return nil, fmt.Errorf("File already exists: %q", v)
}
}
return &vault{ return &vault{
key: keyPath, key: keyPath,
option: o, vault: v,
vault: v,
}, nil }, nil
} }