1
0
mirror of https://github.com/ssh-vault/ssh-vault.git synced 2025-07-04 14:22:28 +03:00

modified: cache_test.go

modified:   getkey.go
	modified:   getkey_test.go
	modified:   vault.go
This commit is contained in:
nbari
2016-10-24 12:23:58 +02:00
parent cc008b463c
commit 83692b27d1
4 changed files with 28 additions and 7 deletions

View File

@ -24,6 +24,8 @@ func (m mockSchlosser) GetKey(u string) ([]string, error) {
switch u { switch u {
case "alice": case "alice":
return []string{"ssh-rsa ABC"}, nil return []string{"ssh-rsa ABC"}, nil
case "bob":
return nil, nil
default: default:
return nil, fmt.Errorf("Error") return nil, fmt.Errorf("Error")
} }
@ -44,6 +46,7 @@ func TestCacheGet(t *testing.T) {
{"alice", 0, "alice.key-1", false}, {"alice", 0, "alice.key-1", false},
{"alice", 1, "alice.key-1", false}, {"alice", 1, "alice.key-1", false},
{"alice", 2, "", true}, {"alice", 2, "", true},
{"bob", 1, "", true},
} }
cache := &cache{dir} cache := &cache{dir}
gk := mockSchlosser{} gk := mockSchlosser{}

View File

@ -9,22 +9,21 @@ import (
"strings" "strings"
) )
// GITHUB https://github.com/<username>.keys
const GITHUB = "https://github.com"
// Schlosser interface // Schlosser interface
type Schlosser interface { type Schlosser interface {
GetKey(string) ([]string, error) GetKey(string) ([]string, error)
} }
// Locksmith implements Schlosser // Locksmith implements Schlosser
type Locksmith struct{} type Locksmith struct {
Github string
}
// GetKey fetches ssh-key from url // GetKey fetches ssh-key from url
func (l Locksmith) GetKey(u string) ([]string, error) { func (l Locksmith) GetKey(u string) ([]string, error) {
url := u url := u
if !isURL.MatchString(u) { if !isURL.MatchString(u) {
url = fmt.Sprintf("%s/%s.keys", GITHUB, u) url = fmt.Sprintf("%s/%s.keys", l.Github, u)
} }
client := &http.Client{} client := &http.Client{}
// create a new request // create a new request

View File

@ -7,7 +7,7 @@ import (
"testing" "testing"
) )
func TestGetKeyFound(t *testing.T) { func TestGetKeyFoundURL(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expect(t, "ssh-vault", r.Header.Get("User-agent")) expect(t, "ssh-vault", r.Header.Get("User-agent"))
fmt.Fprintln(w, "ssh-rsa ABC") fmt.Fprintln(w, "ssh-rsa ABC")
@ -22,6 +22,21 @@ func TestGetKeyFound(t *testing.T) {
expect(t, 1, len(s)) expect(t, 1, len(s))
} }
func TestGetKeyFoundUser(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expect(t, "ssh-vault", r.Header.Get("User-agent"))
fmt.Fprintln(w, "ssh-rsa ABC")
}))
defer ts.Close()
l := Locksmith{ts.URL}
s, err := l.GetKey("bob")
if err != nil {
t.Error(err)
}
expect(t, 1, len(s))
}
func TestGetKeyNotFound(t *testing.T) { func TestGetKeyNotFound(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expect(t, "ssh-vault", r.Header.Get("User-agent")) expect(t, "ssh-vault", r.Header.Get("User-agent"))

View File

@ -22,6 +22,10 @@ type vault struct {
Password []byte Password []byte
} }
// GITHUB https://github.com/<username>.keys
const GITHUB = "https://github.com"
// isURL regex to match if user is an URL
var isURL = regexp.MustCompile(`^https?://`) var isURL = regexp.MustCompile(`^https?://`)
// New initialize vault parameters // New initialize vault parameters
@ -31,7 +35,7 @@ func New(k, u, o, v string) (*vault, error) {
keyPath string = k keyPath string = k
) )
cache := Cache() cache := Cache()
s := Locksmith{} s := Locksmith{GITHUB}
if u != "" { if u != "" {
// use -k N where N is the index to use when multiple keys // use -k N where N is the index to use when multiple keys
// are available // are available