2020-07-25 00:08:32 +00:00
|
|
|
package sshutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_getFuncMap_fail(t *testing.T) {
|
|
|
|
var failMesage string
|
|
|
|
fns := getFuncMap(&failMesage)
|
|
|
|
fail := fns["fail"].(func(s string) (string, error))
|
|
|
|
s, err := fail("the fail message")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("fail() error = %v, wantErr %v", err, errors.New("the fail message"))
|
|
|
|
}
|
|
|
|
if s != "" {
|
|
|
|
t.Errorf("fail() = \"%s\", want \"the fail message\"", s)
|
|
|
|
}
|
|
|
|
if failMesage != "the fail message" {
|
|
|
|
t.Errorf("fail() message = \"%s\", want \"the fail message\"", failMesage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithTemplate(t *testing.T) {
|
|
|
|
key := mustGeneratePublicKey(t)
|
2020-07-29 21:29:12 +00:00
|
|
|
cr := CertificateRequest{
|
|
|
|
Key: key,
|
|
|
|
}
|
2020-07-25 00:08:32 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
text string
|
|
|
|
data TemplateData
|
2020-07-29 21:29:12 +00:00
|
|
|
cr CertificateRequest
|
2020-07-25 00:08:32 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want Options
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"user", args{DefaultCertificate, TemplateData{
|
|
|
|
TypeKey: "user",
|
|
|
|
KeyIDKey: "jane@doe.com",
|
|
|
|
PrincipalsKey: []string{"jane", "jane@doe.com"},
|
|
|
|
ExtensionsKey: DefaultExtensions(UserCert),
|
2020-07-29 21:29:12 +00:00
|
|
|
}, cr}, Options{
|
2020-07-25 00:08:32 +00:00
|
|
|
CertBuffer: bytes.NewBufferString(`{
|
|
|
|
"type": "user",
|
|
|
|
"keyId": "jane@doe.com",
|
|
|
|
"principals": ["jane","jane@doe.com"],
|
2020-07-31 01:04:39 +00:00
|
|
|
"extensions": {"permit-X11-forwarding":"","permit-agent-forwarding":"","permit-port-forwarding":"","permit-pty":"","permit-user-rc":""},
|
|
|
|
"criticalOptions": null
|
2020-07-25 00:08:32 +00:00
|
|
|
}`)}, false},
|
|
|
|
{"host", args{DefaultCertificate, TemplateData{
|
2020-07-31 01:04:39 +00:00
|
|
|
TypeKey: "host",
|
|
|
|
KeyIDKey: "foo",
|
|
|
|
PrincipalsKey: []string{"foo.internal"},
|
|
|
|
CriticalOptionsKey: map[string]string{"foo": "bar"},
|
2020-07-29 21:29:12 +00:00
|
|
|
}, cr}, Options{
|
2020-07-25 00:08:32 +00:00
|
|
|
CertBuffer: bytes.NewBufferString(`{
|
|
|
|
"type": "host",
|
|
|
|
"keyId": "foo",
|
|
|
|
"principals": ["foo.internal"],
|
2020-07-31 01:04:39 +00:00
|
|
|
"extensions": null,
|
|
|
|
"criticalOptions": {"foo":"bar"}
|
2020-07-25 00:08:32 +00:00
|
|
|
}`)}, false},
|
2020-07-29 21:29:12 +00:00
|
|
|
{"fail", args{`{{ fail "a message" }}`, TemplateData{}, cr}, Options{}, true},
|
|
|
|
{"failTemplate", args{`{{ fail "fatal error }}`, TemplateData{}, cr}, Options{}, true},
|
2020-07-25 00:08:32 +00:00
|
|
|
{"error", args{`{{ mustHas 3 .Data }}`, TemplateData{
|
|
|
|
"Data": 3,
|
2020-07-29 21:29:12 +00:00
|
|
|
}, cr}, Options{}, true},
|
2020-07-25 00:08:32 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var got Options
|
|
|
|
fn := WithTemplate(tt.args.text, tt.args.data)
|
2020-07-29 21:29:12 +00:00
|
|
|
if err := fn(tt.args.cr, &got); (err != nil) != tt.wantErr {
|
2020-07-25 00:08:32 +00:00
|
|
|
t.Errorf("WithTemplate() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("WithTemplate() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithTemplateBase64(t *testing.T) {
|
|
|
|
key := mustGeneratePublicKey(t)
|
2020-07-29 21:29:12 +00:00
|
|
|
cr := CertificateRequest{
|
|
|
|
Key: key,
|
|
|
|
}
|
2020-07-25 00:08:32 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
s string
|
|
|
|
data TemplateData
|
2020-07-29 21:29:12 +00:00
|
|
|
cr CertificateRequest
|
2020-07-25 00:08:32 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want Options
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"host", args{base64.StdEncoding.EncodeToString([]byte(DefaultCertificate)), TemplateData{
|
2020-07-31 01:04:39 +00:00
|
|
|
TypeKey: "host",
|
|
|
|
KeyIDKey: "foo.internal",
|
|
|
|
PrincipalsKey: []string{"foo.internal", "bar.internal"},
|
|
|
|
ExtensionsKey: map[string]interface{}{"foo": "bar"},
|
|
|
|
CriticalOptionsKey: map[string]interface{}{"bar": "foo"},
|
2020-07-29 21:29:12 +00:00
|
|
|
}, cr}, Options{
|
2020-07-25 00:08:32 +00:00
|
|
|
CertBuffer: bytes.NewBufferString(`{
|
|
|
|
"type": "host",
|
|
|
|
"keyId": "foo.internal",
|
|
|
|
"principals": ["foo.internal","bar.internal"],
|
2020-07-31 01:04:39 +00:00
|
|
|
"extensions": {"foo":"bar"},
|
|
|
|
"criticalOptions": {"bar":"foo"}
|
2020-07-25 00:08:32 +00:00
|
|
|
}`)}, false},
|
2020-07-29 21:29:12 +00:00
|
|
|
{"badBase64", args{"foobar", TemplateData{}, cr}, Options{}, true},
|
2020-07-25 00:08:32 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var got Options
|
|
|
|
fn := WithTemplateBase64(tt.args.s, tt.args.data)
|
2020-07-29 21:29:12 +00:00
|
|
|
if err := fn(tt.args.cr, &got); (err != nil) != tt.wantErr {
|
2020-07-25 00:08:32 +00:00
|
|
|
t.Errorf("WithTemplateBase64() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("WithTemplateBase64() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithTemplateFile(t *testing.T) {
|
|
|
|
key := mustGeneratePublicKey(t)
|
2020-07-29 21:29:12 +00:00
|
|
|
cr := CertificateRequest{
|
|
|
|
Key: key,
|
|
|
|
}
|
2020-07-25 00:08:32 +00:00
|
|
|
|
|
|
|
data := TemplateData{
|
|
|
|
TypeKey: "user",
|
|
|
|
KeyIDKey: "jane@doe.com",
|
|
|
|
PrincipalsKey: []string{"jane", "jane@doe.com"},
|
|
|
|
ExtensionsKey: DefaultExtensions(UserCert),
|
2020-07-29 21:29:12 +00:00
|
|
|
InsecureKey: TemplateData{
|
2020-07-25 00:08:32 +00:00
|
|
|
UserKey: map[string]interface{}{
|
|
|
|
"username": "jane",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
path string
|
|
|
|
data TemplateData
|
2020-07-29 21:29:12 +00:00
|
|
|
cr CertificateRequest
|
2020-07-25 00:08:32 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want Options
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2020-07-29 21:29:12 +00:00
|
|
|
{"github.com", args{"./testdata/github.tpl", data, cr}, Options{
|
2020-07-25 00:08:32 +00:00
|
|
|
CertBuffer: bytes.NewBufferString(`{
|
|
|
|
"type": "user",
|
|
|
|
"keyId": "jane@doe.com",
|
|
|
|
"principals": ["jane","jane@doe.com"],
|
|
|
|
"extensions": {"login@github.com":"jane","permit-X11-forwarding":"","permit-agent-forwarding":"","permit-port-forwarding":"","permit-pty":"","permit-user-rc":""}
|
|
|
|
}`),
|
|
|
|
}, false},
|
2020-07-29 21:29:12 +00:00
|
|
|
{"missing", args{"./testdata/missing.tpl", data, cr}, Options{}, true},
|
2020-07-25 00:08:32 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var got Options
|
|
|
|
fn := WithTemplateFile(tt.args.path, tt.args.data)
|
2020-07-29 21:29:12 +00:00
|
|
|
if err := fn(tt.args.cr, &got); (err != nil) != tt.wantErr {
|
2020-07-25 00:08:32 +00:00
|
|
|
t.Errorf("WithTemplateFile() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("WithTemplateFile() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|