1
0
mirror of https://github.com/ssh-vault/ssh-vault.git synced 2025-07-31 05:24:22 +03:00

-u also accepts an URL from where to fetch the ssh-keys

This commit is contained in:
nbari
2016-10-19 20:35:15 +02:00
parent 8c022e453c
commit cedfc81a0e
4 changed files with 23 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package sshvault package sshvault
import ( import (
"crypto/md5"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -25,12 +26,24 @@ func Cache() *cache {
// Get return ssh-key // Get return ssh-key
func (c *cache) Get(u string, k int) (string, error) { func (c *cache) Get(u string, k int) (string, error) {
uKey := fmt.Sprintf("%s/%s.key-%d", c.dir, u, k) var (
uKey string
hash string
)
if !isURL.MatchString(u) {
uKey = fmt.Sprintf("%s/%s.key-%d", c.dir, u, k)
} else {
hash = fmt.Sprintf("%x", md5.Sum([]byte(u)))
uKey = fmt.Sprintf("%s/%s.key-%d", c.dir, hash, k)
}
if !c.IsFile(uKey) { if !c.IsFile(uKey) {
keys, err := GetKey(u) keys, err := GetKey(u)
if err != nil { if err != nil {
return "", err return "", err
} }
if isURL.MatchString(u) {
u = hash
}
for k, v := range keys { for k, v := range keys {
err = ioutil.WriteFile(fmt.Sprintf("%s/%s.key-%d", c.dir, u, k+1), err = ioutil.WriteFile(fmt.Sprintf("%s/%s.key-%d", c.dir, u, k+1),
[]byte(v), []byte(v),

View File

@ -20,7 +20,7 @@ func exit1(err error) {
func main() { func main() {
var ( var (
k = flag.String("k", "~/.ssh/id_rsa.pub", "public `ssh key or index` when using option -u") k = flag.String("k", "~/.ssh/id_rsa.pub", "public `ssh key or index` when using option -u")
u = flag.String("u", "", "GitHub `username`, optional [-k N] where N is the key index to use") u = flag.String("u", "", "GitHub `username or URL`, optional [-k N] where N is the key index to use")
f = flag.Bool("f", false, "Print ssh key `fingerprint`") f = flag.Bool("f", false, "Print ssh key `fingerprint`")
options = []string{"create", "edit", "view"} options = []string{"create", "edit", "view"}
v = flag.Bool("v", false, fmt.Sprintf("Print version: %s", version)) v = flag.Bool("v", false, fmt.Sprintf("Print version: %s", version))

View File

@ -14,12 +14,13 @@ 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) {
url := u
if !isURL.MatchString(u) {
url = fmt.Sprintf("%s/%s.keys", GITHUB, u)
}
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", url, nil)
GITHUB,
u),
nil)
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 {

View File

@ -9,6 +9,7 @@ import (
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"os/exec" "os/exec"
"regexp"
"strconv" "strconv"
"strings" "strings"
) )
@ -22,6 +23,8 @@ type vault struct {
password []byte password []byte
} }
var isURL = regexp.MustCompile(`^https?://`)
// New initialize vault parameters // New initialize vault parameters
func New(k, u, o, v string) (*vault, error) { func New(k, u, o, v string) (*vault, error) {
var ( var (