2021-03-23 23:14:49 +00:00
|
|
|
package stepcas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-08-11 21:47:11 +00:00
|
|
|
"github.com/google/uuid"
|
2021-03-25 02:05:56 +00:00
|
|
|
"github.com/smallstep/certificates/ca"
|
2021-03-23 23:14:49 +00:00
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
2021-03-25 02:05:56 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2021-03-23 23:14:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockErrIssuer struct{}
|
|
|
|
|
2022-08-03 18:57:42 +00:00
|
|
|
func (m mockErrIssuer) SignToken(subject string, sans []string, info *raInfo) (string, error) {
|
2022-09-20 23:32:49 +00:00
|
|
|
return "", apiv1.NotImplementedError{}
|
2021-03-23 23:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockErrIssuer) RevokeToken(subject string) (string, error) {
|
2022-09-20 23:32:49 +00:00
|
|
|
return "", apiv1.NotImplementedError{}
|
2021-03-23 23:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockErrIssuer) Lifetime(d time.Duration) time.Duration {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2021-03-25 02:05:56 +00:00
|
|
|
type mockErrSigner struct{}
|
|
|
|
|
|
|
|
func (s *mockErrSigner) Sign(payload []byte) (*jose.JSONWebSignature, error) {
|
2022-09-20 23:32:49 +00:00
|
|
|
return nil, apiv1.NotImplementedError{}
|
2021-03-25 02:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockErrSigner) Options() jose.SignerOptions {
|
|
|
|
return jose.SignerOptions{}
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:47:11 +00:00
|
|
|
func Test_newServerEndpointID(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []byte
|
|
|
|
}{
|
|
|
|
{"ok", args{"foo"}, []byte{
|
|
|
|
0x8f, 0x63, 0x69, 0x20, 0x8a, 0x7a, 0x57, 0x0c, 0xbe, 0x4c, 0x46, 0x66, 0x77, 0xf8, 0x54, 0xe7,
|
|
|
|
}},
|
|
|
|
{"ok uuid", args{"e4fa6d2d-fa9c-4fdc-913e-7484cc9516e4"}, []byte{
|
|
|
|
0x8d, 0x8d, 0x7f, 0x04, 0x73, 0xd4, 0x5f, 0x2f, 0xa8, 0xe1, 0x28, 0x9a, 0xd1, 0xa8, 0xcf, 0x7e,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var want uuid.UUID
|
|
|
|
copy(want[:], tt.want)
|
|
|
|
got := newServerEndpointID(tt.args.name)
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("newServerEndpointID() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
// Check version
|
|
|
|
if v := (got[6] & 0xf0) >> 4; v != 5 {
|
|
|
|
t.Errorf("newServerEndpointID() version = %d, want 5", v)
|
|
|
|
}
|
|
|
|
// Check variant
|
|
|
|
if v := (got[8] & 0x80) >> 6; v != 2 {
|
|
|
|
t.Errorf("newServerEndpointID() variant = %d, want 2", v)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:14:49 +00:00
|
|
|
func Test_newStepIssuer(t *testing.T) {
|
2021-03-25 02:05:56 +00:00
|
|
|
caURL, client := testCAHelper(t)
|
|
|
|
signer, err := newJWKSignerFromEncryptedKey(testKeyID, testEncryptedJWKKey, testPassword)
|
2021-03-23 23:14:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
2021-03-25 02:05:56 +00:00
|
|
|
caURL *url.URL
|
|
|
|
client *ca.Client
|
|
|
|
iss *apiv1.CertificateIssuer
|
2021-03-23 23:14:49 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want stepIssuer
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2021-03-25 02:05:56 +00:00
|
|
|
{"x5c", args{caURL, client, &apiv1.CertificateIssuer{
|
2021-03-23 23:14:49 +00:00
|
|
|
Type: "x5c",
|
|
|
|
Provisioner: "X5C",
|
|
|
|
Certificate: testX5CPath,
|
|
|
|
Key: testX5CKeyPath,
|
|
|
|
}}, &x5cIssuer{
|
|
|
|
caURL: caURL,
|
|
|
|
certFile: testX5CPath,
|
|
|
|
keyFile: testX5CKeyPath,
|
|
|
|
issuer: "X5C",
|
|
|
|
}, false},
|
2021-03-25 02:05:56 +00:00
|
|
|
{"jwk", args{caURL, client, &apiv1.CertificateIssuer{
|
2021-03-23 23:14:49 +00:00
|
|
|
Type: "jwk",
|
|
|
|
Provisioner: "ra@doe.org",
|
|
|
|
Key: testX5CKeyPath,
|
|
|
|
}}, &jwkIssuer{
|
2021-03-25 02:05:56 +00:00
|
|
|
caURL: caURL,
|
|
|
|
issuer: "ra@doe.org",
|
|
|
|
signer: signer,
|
2021-03-23 23:14:49 +00:00
|
|
|
}, false},
|
2021-03-25 02:05:56 +00:00
|
|
|
{"fail", args{caURL, client, &apiv1.CertificateIssuer{
|
2021-03-23 23:14:49 +00:00
|
|
|
Type: "unknown",
|
|
|
|
Provisioner: "ra@doe.org",
|
|
|
|
Key: testX5CKeyPath,
|
|
|
|
}}, nil, true},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-03-25 02:05:56 +00:00
|
|
|
got, err := newStepIssuer(tt.args.caURL, tt.args.client, tt.args.iss)
|
2021-03-23 23:14:49 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("newStepIssuer() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2021-03-25 02:05:56 +00:00
|
|
|
if tt.args.iss.Type == "jwk" && got != nil && tt.want != nil {
|
|
|
|
got.(*jwkIssuer).signer = tt.want.(*jwkIssuer).signer
|
|
|
|
}
|
2021-03-23 23:14:49 +00:00
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("newStepIssuer() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|