-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #534 from smallstep/mariano/validity
Add support validities in templates
- Loading branch information
Showing
10 changed files
with
232 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,9 @@ import ( | |
"io" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
|
@@ -71,6 +73,7 @@ func mustGeneratePublicKey(t *testing.T) ssh.PublicKey { | |
} | ||
|
||
func TestNewCertificate(t *testing.T) { | ||
now := time.Now().Truncate(time.Second) | ||
key := mustGeneratePublicKey(t) | ||
cr := CertificateRequest{ | ||
Key: key, | ||
|
@@ -100,8 +103,8 @@ func TestNewCertificate(t *testing.T) { | |
Type: UserCert, | ||
KeyID: "[email protected]", | ||
Principals: []string{"jane"}, | ||
ValidAfter: 0, | ||
ValidBefore: 0, | ||
ValidAfter: time.Time{}, | ||
ValidBefore: time.Time{}, | ||
CriticalOptions: nil, | ||
Extensions: map[string]string{ | ||
"permit-X11-forwarding": "", | ||
|
@@ -121,8 +124,8 @@ func TestNewCertificate(t *testing.T) { | |
Type: HostCert, | ||
KeyID: "foobar", | ||
Principals: []string{"foo.internal", "bar.internal"}, | ||
ValidAfter: 0, | ||
ValidBefore: 0, | ||
ValidAfter: time.Time{}, | ||
ValidBefore: time.Time{}, | ||
CriticalOptions: nil, | ||
Extensions: nil, | ||
Reserved: nil, | ||
|
@@ -136,8 +139,8 @@ func TestNewCertificate(t *testing.T) { | |
Type: HostCert, | ||
KeyID: `foobar", "criticalOptions": {"foo": "bar"},"foo":"`, | ||
Principals: []string{"foo.internal", "bar.internal"}, | ||
ValidAfter: 0, | ||
ValidBefore: 0, | ||
ValidAfter: time.Time{}, | ||
ValidBefore: time.Time{}, | ||
CriticalOptions: nil, | ||
Extensions: nil, | ||
Reserved: nil, | ||
|
@@ -159,8 +162,8 @@ func TestNewCertificate(t *testing.T) { | |
Type: UserCert, | ||
KeyID: "[email protected]", | ||
Principals: []string{"john", "[email protected]"}, | ||
ValidAfter: 0, | ||
ValidBefore: 0, | ||
ValidAfter: time.Time{}, | ||
ValidBefore: time.Time{}, | ||
CriticalOptions: nil, | ||
Extensions: map[string]string{ | ||
"[email protected]": "john", | ||
|
@@ -174,15 +177,47 @@ func TestNewCertificate(t *testing.T) { | |
SignatureKey: nil, | ||
Signature: nil, | ||
}, false}, | ||
{"file with dates", args{cr, []Option{WithTemplateFile("./testdata/date.tpl", TemplateData{ | ||
TypeKey: UserCert, | ||
KeyIDKey: "[email protected]", | ||
PrincipalsKey: []string{"john", "[email protected]"}, | ||
ExtensionsKey: DefaultExtensions(UserCert), | ||
InsecureKey: TemplateData{ | ||
"User": map[string]interface{}{"username": "john"}, | ||
}, | ||
WebhooksKey: TemplateData{ | ||
"Test": map[string]interface{}{"validity": "16h"}, | ||
}, | ||
})}}, &Certificate{ | ||
Nonce: nil, | ||
Key: key, | ||
Serial: 0, | ||
Type: UserCert, | ||
KeyID: "[email protected]", | ||
Principals: []string{"john", "[email protected]"}, | ||
ValidAfter: now, | ||
ValidBefore: now.Add(16 * time.Hour), | ||
CriticalOptions: nil, | ||
Extensions: map[string]string{ | ||
"permit-X11-forwarding": "", | ||
"permit-agent-forwarding": "", | ||
"permit-port-forwarding": "", | ||
"permit-pty": "", | ||
"permit-user-rc": "", | ||
}, | ||
Reserved: nil, | ||
SignatureKey: nil, | ||
Signature: nil, | ||
}, false}, | ||
{"base64", args{cr, []Option{WithTemplateBase64(base64.StdEncoding.EncodeToString([]byte(DefaultTemplate)), CreateTemplateData(HostCert, "foo.internal", nil))}}, &Certificate{ | ||
Nonce: nil, | ||
Key: key, | ||
Serial: 0, | ||
Type: HostCert, | ||
KeyID: "foo.internal", | ||
Principals: nil, | ||
ValidAfter: 0, | ||
ValidBefore: 0, | ||
ValidAfter: time.Time{}, | ||
ValidBefore: time.Time{}, | ||
CriticalOptions: nil, | ||
Extensions: nil, | ||
Reserved: nil, | ||
|
@@ -203,6 +238,15 @@ func TestNewCertificate(t *testing.T) { | |
t.Errorf("NewCertificate() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != nil && tt.want != nil { | ||
if assert.WithinDuration(t, tt.want.ValidAfter, got.ValidAfter, 2*time.Second) { | ||
tt.want.ValidAfter = got.ValidAfter | ||
} | ||
if assert.WithinDuration(t, tt.want.ValidBefore, got.ValidBefore, 2*time.Second) { | ||
tt.want.ValidBefore = got.ValidBefore | ||
} | ||
|
||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewCertificate() = %v, want %v", got, tt.want) | ||
} | ||
|
@@ -212,6 +256,7 @@ func TestNewCertificate(t *testing.T) { | |
|
||
func TestCertificate_GetCertificate(t *testing.T) { | ||
key := mustGeneratePublicKey(t) | ||
now := time.Now() | ||
|
||
type fields struct { | ||
Nonce []byte | ||
|
@@ -220,8 +265,8 @@ func TestCertificate_GetCertificate(t *testing.T) { | |
Type CertType | ||
KeyID string | ||
Principals []string | ||
ValidAfter uint64 | ||
ValidBefore uint64 | ||
ValidAfter time.Time | ||
ValidBefore time.Time | ||
CriticalOptions map[string]string | ||
Extensions map[string]string | ||
Reserved []byte | ||
|
@@ -240,8 +285,8 @@ func TestCertificate_GetCertificate(t *testing.T) { | |
Type: UserCert, | ||
KeyID: "key-id", | ||
Principals: []string{"john"}, | ||
ValidAfter: 1111, | ||
ValidBefore: 2222, | ||
ValidAfter: now, | ||
ValidBefore: now.Add(time.Hour), | ||
CriticalOptions: map[string]string{"foo": "bar"}, | ||
Extensions: map[string]string{"[email protected]": "john"}, | ||
Reserved: []byte("reserved"), | ||
|
@@ -254,8 +299,8 @@ func TestCertificate_GetCertificate(t *testing.T) { | |
CertType: ssh.UserCert, | ||
KeyId: "key-id", | ||
ValidPrincipals: []string{"john"}, | ||
ValidAfter: 1111, | ||
ValidBefore: 2222, | ||
ValidAfter: uint64(now.Unix()), | ||
ValidBefore: uint64(now.Add(time.Hour).Unix()), | ||
Permissions: ssh.Permissions{ | ||
CriticalOptions: map[string]string{"foo": "bar"}, | ||
Extensions: map[string]string{"[email protected]": "john"}, | ||
|
@@ -269,8 +314,8 @@ func TestCertificate_GetCertificate(t *testing.T) { | |
Type: HostCert, | ||
KeyID: "key-id", | ||
Principals: []string{"foo.internal", "bar.internal"}, | ||
ValidAfter: 1111, | ||
ValidBefore: 2222, | ||
ValidAfter: time.Time{}, | ||
ValidBefore: time.Time{}, | ||
CriticalOptions: map[string]string{"foo": "bar"}, | ||
Extensions: nil, | ||
Reserved: []byte("reserved"), | ||
|
@@ -283,8 +328,8 @@ func TestCertificate_GetCertificate(t *testing.T) { | |
CertType: ssh.HostCert, | ||
KeyId: "key-id", | ||
ValidPrincipals: []string{"foo.internal", "bar.internal"}, | ||
ValidAfter: 1111, | ||
ValidBefore: 2222, | ||
ValidAfter: 0, | ||
ValidBefore: 0, | ||
Permissions: ssh.Permissions{ | ||
CriticalOptions: map[string]string{"foo": "bar"}, | ||
Extensions: nil, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"type": "{{ .Type }}", | ||
"keyId": "{{ .KeyID }}", | ||
"principals": {{ toJson .Principals }}, | ||
"extensions": {{ toJson .Extensions }}, | ||
"validAfter": {{ now | toJson }}, | ||
"validBefore": {{ now | dateModify .Webhooks.Test.validity | toJson }} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.