mirror of
https://github.com/ssh-vault/ssh-vault.git
synced 2025-07-31 05:24:22 +03:00
add tests for getkey Schlosser
This commit is contained in:
15
a_test.go
Normal file
15
a_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package sshvault
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Test Helpers */
|
||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
||||
_, fn, line, _ := runtime.Caller(1)
|
||||
if a != b {
|
||||
t.Errorf("Expected: %v (type %v) Got: %v (type %v) in %s:%d", a, reflect.TypeOf(a), b, reflect.TypeOf(b), fn, line)
|
||||
}
|
||||
}
|
4
cache.go
4
cache.go
@ -25,7 +25,7 @@ func Cache() *cache {
|
||||
}
|
||||
|
||||
// Get return ssh-key
|
||||
func (c *cache) Get(u string, k int) (string, error) {
|
||||
func (c *cache) Get(s Schlosser, u string, k int) (string, error) {
|
||||
var (
|
||||
uKey string
|
||||
hash string
|
||||
@ -37,7 +37,7 @@ func (c *cache) Get(u string, k int) (string, error) {
|
||||
uKey = fmt.Sprintf("%s/%s.key-%d", c.dir, hash, k)
|
||||
}
|
||||
if !c.IsFile(uKey) {
|
||||
keys, err := GetKey(u)
|
||||
keys, err := s.GetKey(u)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
10
getkey.go
10
getkey.go
@ -12,8 +12,16 @@ import (
|
||||
// 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{}
|
||||
|
||||
// GetKey fetches ssh-key from url
|
||||
func GetKey(u string) ([]string, error) {
|
||||
func (l Locksmith) GetKey(u string) ([]string, error) {
|
||||
url := u
|
||||
if !isURL.MatchString(u) {
|
||||
url = fmt.Sprintf("%s/%s.keys", GITHUB, u)
|
||||
|
58
getkey_test.go
Normal file
58
getkey_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package sshvault
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetKeyFound(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{}
|
||||
s, err := l.GetKey(ts.URL)
|
||||
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"))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
l := Locksmith{}
|
||||
s, err := l.GetKey(ts.URL)
|
||||
if err == nil {
|
||||
t.Errorf("Expecting error")
|
||||
}
|
||||
expect(t, 0, len(s))
|
||||
}
|
||||
|
||||
func TestGetKeyMultipleKeys(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.Fprintf(w, "%s\n%s\n%s\n%s\n%s\n\n\n",
|
||||
"ssh-rsa ABC",
|
||||
"no key",
|
||||
"ssh-rsa ABC",
|
||||
"ssh-foo ABC",
|
||||
"ssh-rsa end",
|
||||
)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
l := Locksmith{}
|
||||
s, err := l.GetKey(ts.URL)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
expect(t, 3, len(s))
|
||||
}
|
3
vault.go
3
vault.go
@ -31,6 +31,7 @@ func New(k, u, o, v string) (*vault, error) {
|
||||
keyPath string = k
|
||||
)
|
||||
cache := Cache()
|
||||
s := Locksmith{}
|
||||
if u != "" {
|
||||
// use -k N where N is the index to use when multiple keys
|
||||
// are available
|
||||
@ -41,7 +42,7 @@ func New(k, u, o, v string) (*vault, error) {
|
||||
if ki <= 1 {
|
||||
ki = 1
|
||||
}
|
||||
keyPath, err = cache.Get(u, ki)
|
||||
keyPath, err = cache.Get(s, u, ki)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user