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 {
case "alice":
return []string{"ssh-rsa ABC"}, nil
case "bob":
return nil, nil
default:
return nil, fmt.Errorf("Error")
}
@ -44,6 +46,7 @@ func TestCacheGet(t *testing.T) {
{"alice", 0, "alice.key-1", false},
{"alice", 1, "alice.key-1", false},
{"alice", 2, "", true},
{"bob", 1, "", true},
}
cache := &cache{dir}
gk := mockSchlosser{}

View File

@ -9,22 +9,21 @@ import (
"strings"
)
// GITHUB https://github.com/<username>.keys
const GITHUB = "https://github.com"
// Schlosser interface
type Schlosser interface {
GetKey(string) ([]string, error)
}
// Locksmith implements Schlosser
type Locksmith struct{}
type Locksmith struct {
Github string
}
// GetKey fetches ssh-key from url
func (l Locksmith) GetKey(u string) ([]string, error) {
url := 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{}
// create a new request

View File

@ -7,7 +7,7 @@ import (
"testing"
)
func TestGetKeyFound(t *testing.T) {
func TestGetKeyFoundURL(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")
@ -22,6 +22,21 @@ func TestGetKeyFound(t *testing.T) {
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) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expect(t, "ssh-vault", r.Header.Get("User-agent"))

View File

@ -22,6 +22,10 @@ type vault struct {
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?://`)
// New initialize vault parameters
@ -31,7 +35,7 @@ func New(k, u, o, v string) (*vault, error) {
keyPath string = k
)
cache := Cache()
s := Locksmith{}
s := Locksmith{GITHUB}
if u != "" {
// use -k N where N is the index to use when multiple keys
// are available