diff --git a/cache.go b/cache.go index ba94afe..57f10a3 100644 --- a/cache.go +++ b/cache.go @@ -1,6 +1,7 @@ package sshvault import ( + "crypto/md5" "fmt" "io/ioutil" "log" @@ -25,12 +26,24 @@ func Cache() *cache { // Get return ssh-key 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) { keys, err := GetKey(u) if err != nil { return "", err } + if isURL.MatchString(u) { + u = hash + } for k, v := range keys { err = ioutil.WriteFile(fmt.Sprintf("%s/%s.key-%d", c.dir, u, k+1), []byte(v), diff --git a/cmd/ssh-vault/main.go b/cmd/ssh-vault/main.go index e0a15ee..6c48a4e 100644 --- a/cmd/ssh-vault/main.go +++ b/cmd/ssh-vault/main.go @@ -20,7 +20,7 @@ func exit1(err error) { func main() { var ( 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`") options = []string{"create", "edit", "view"} v = flag.Bool("v", false, fmt.Sprintf("Print version: %s", version)) diff --git a/getkey.go b/getkey.go index 55a644e..35d96a1 100644 --- a/getkey.go +++ b/getkey.go @@ -14,12 +14,13 @@ const GITHUB = "https://github.com" // GetKey fetches ssh-key from url func GetKey(u string) ([]string, error) { + url := u + if !isURL.MatchString(u) { + url = fmt.Sprintf("%s/%s.keys", GITHUB, u) + } client := &http.Client{} // create a new request - req, _ := http.NewRequest("GET", fmt.Sprintf("%s/%s.keys", - GITHUB, - u), - nil) + req, _ := http.NewRequest("GET", url, nil) req.Header.Set("User-Agent", "ssh-vault") res, err := client.Do(req) if err != nil { diff --git a/vault.go b/vault.go index cbf0aa6..9fcf4d0 100644 --- a/vault.go +++ b/vault.go @@ -9,6 +9,7 @@ import ( "encoding/pem" "fmt" "os/exec" + "regexp" "strconv" "strings" ) @@ -22,6 +23,8 @@ type vault struct { password []byte } +var isURL = regexp.MustCompile(`^https?://`) + // New initialize vault parameters func New(k, u, o, v string) (*vault, error) { var (