mirror of
https://github.com/smallstep/certificates.git
synced 2024-10-31 03:20:16 +00:00
don't wrap time.Duration
This commit is contained in:
parent
4b742042ee
commit
0615f7eb11
@ -25,10 +25,13 @@ var (
|
||||
Renegotiation: false,
|
||||
}
|
||||
defaultDisableRenewal = false
|
||||
minTLSDur = 5 * time.Minute
|
||||
maxTLSDur = 24 * time.Hour
|
||||
defaultTLSDur = 24 * time.Hour
|
||||
globalProvisionerClaims = ProvisionerClaims{
|
||||
MinTLSDur: &Duration{5 * time.Minute},
|
||||
MaxTLSDur: &Duration{24 * time.Hour},
|
||||
DefaultTLSDur: &Duration{24 * time.Hour},
|
||||
MinTLSDur: (*duration)(&minTLSDur),
|
||||
MaxTLSDur: (*duration)(&maxTLSDur),
|
||||
DefaultTLSDur: (*duration)(&defaultTLSDur),
|
||||
DisableRenewal: &defaultDisableRenewal,
|
||||
}
|
||||
)
|
||||
|
@ -12,9 +12,9 @@ import (
|
||||
// ProvisionerClaims so that individual provisioners can override global claims.
|
||||
type ProvisionerClaims struct {
|
||||
globalClaims *ProvisionerClaims
|
||||
MinTLSDur *Duration `json:"minTLSCertDuration,omitempty"`
|
||||
MaxTLSDur *Duration `json:"maxTLSCertDuration,omitempty"`
|
||||
DefaultTLSDur *Duration `json:"defaultTLSCertDuration,omitempty"`
|
||||
MinTLSDur *duration `json:"minTLSCertDuration,omitempty"`
|
||||
MaxTLSDur *duration `json:"maxTLSCertDuration,omitempty"`
|
||||
DefaultTLSDur *duration `json:"defaultTLSCertDuration,omitempty"`
|
||||
DisableRenewal *bool `json:"disableRenewal,omitempty"`
|
||||
}
|
||||
|
||||
@ -32,30 +32,30 @@ func (pc *ProvisionerClaims) Init(global *ProvisionerClaims) (*ProvisionerClaims
|
||||
// provisioner. If the default is not set within the provisioner, then the global
|
||||
// default from the authority configuration will be used.
|
||||
func (pc *ProvisionerClaims) DefaultTLSCertDuration() time.Duration {
|
||||
if pc.DefaultTLSDur == nil || pc.DefaultTLSDur.Duration == 0 {
|
||||
if pc.DefaultTLSDur == nil || *pc.DefaultTLSDur == 0 {
|
||||
return pc.globalClaims.DefaultTLSCertDuration()
|
||||
}
|
||||
return pc.DefaultTLSDur.Duration
|
||||
return time.Duration(*pc.DefaultTLSDur)
|
||||
}
|
||||
|
||||
// MinTLSCertDuration returns the minimum TLS cert duration for the provisioner.
|
||||
// If the minimum is not set within the provisioner, then the global
|
||||
// minimum from the authority configuration will be used.
|
||||
func (pc *ProvisionerClaims) MinTLSCertDuration() time.Duration {
|
||||
if pc.MinTLSDur == nil || pc.MinTLSDur.Duration == 0 {
|
||||
if pc.MinTLSDur == nil || *pc.MinTLSDur == 0 {
|
||||
return pc.globalClaims.MinTLSCertDuration()
|
||||
}
|
||||
return pc.MinTLSDur.Duration
|
||||
return time.Duration(*pc.MinTLSDur)
|
||||
}
|
||||
|
||||
// MaxTLSCertDuration returns the maximum TLS cert duration for the provisioner.
|
||||
// If the maximum is not set within the provisioner, then the global
|
||||
// maximum from the authority configuration will be used.
|
||||
func (pc *ProvisionerClaims) MaxTLSCertDuration() time.Duration {
|
||||
if pc.MaxTLSDur == nil || pc.MaxTLSDur.Duration == 0 {
|
||||
if pc.MaxTLSDur == nil || *pc.MaxTLSDur == 0 {
|
||||
return pc.globalClaims.MaxTLSCertDuration()
|
||||
}
|
||||
return pc.MaxTLSDur.Duration
|
||||
return time.Duration(*pc.MaxTLSDur)
|
||||
}
|
||||
|
||||
// IsDisableRenewal returns if the renewal flow is disabled for the
|
||||
|
@ -8,32 +8,37 @@ import (
|
||||
)
|
||||
|
||||
// Duration is a wrapper around Time.Duration to aid with marshal/unmarshal.
|
||||
type Duration struct {
|
||||
time.Duration
|
||||
}
|
||||
type duration time.Duration
|
||||
|
||||
// MarshalJSON parses a Duration string and sets it to the duration.
|
||||
// MarshalJSON parses a duration string and sets it to the duration.
|
||||
//
|
||||
// A duration string is a possibly signed sequence of decimal numbers, each with
|
||||
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
||||
func (d *Duration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(d.String())
|
||||
func (d *duration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal((*time.Duration)(d).String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON parses a Duration string and sets it to the duration.
|
||||
// UnmarshalJSON parses a duration string and sets it to the duration.
|
||||
//
|
||||
// A Duration string is a possibly signed sequence of decimal numbers, each with
|
||||
// A duration string is a possibly signed sequence of decimal numbers, each with
|
||||
// optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
|
||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
||||
func (d *Duration) UnmarshalJSON(data []byte) (err error) {
|
||||
var s string
|
||||
func (d *duration) UnmarshalJSON(data []byte) (err error) {
|
||||
var (
|
||||
s string
|
||||
_d time.Duration
|
||||
)
|
||||
if d == nil {
|
||||
return errors.New("duration cannot be nil")
|
||||
}
|
||||
if err = json.Unmarshal(data, &s); err != nil {
|
||||
return errors.Wrapf(err, "error unmarshalling %s", data)
|
||||
}
|
||||
if d.Duration, err = time.ParseDuration(s); err != nil {
|
||||
if _d, err = time.ParseDuration(s); err != nil {
|
||||
return errors.Wrapf(err, "error parsing %s as duration", s)
|
||||
}
|
||||
*d = duration(_d)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package authority
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_multiString_First(t *testing.T) {
|
||||
@ -71,7 +72,6 @@ func Test_multiString_MarshalJSON(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_multiString_UnmarshalJSON(t *testing.T) {
|
||||
|
||||
type args struct {
|
||||
data []byte
|
||||
}
|
||||
@ -101,3 +101,63 @@ func Test_multiString_UnmarshalJSON(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func durPtr(_d time.Duration) *duration {
|
||||
d := new(duration)
|
||||
*d = duration(_d)
|
||||
return d
|
||||
}
|
||||
|
||||
func Test_duration_UnmarshalJSON(t *testing.T) {
|
||||
type args struct {
|
||||
data []byte
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
d *duration
|
||||
args args
|
||||
want *duration
|
||||
wantErr bool
|
||||
}{
|
||||
{"empty", new(duration), args{[]byte{}}, new(duration), true},
|
||||
{"bad type", new(duration), args{[]byte(`15`)}, new(duration), true},
|
||||
{"empty string", new(duration), args{[]byte(`""`)}, new(duration), true},
|
||||
{"non duration", new(duration), args{[]byte(`"15"`)}, new(duration), true},
|
||||
{"duration", new(duration), args{[]byte(`"15m30s"`)}, durPtr(15*time.Minute + 30*time.Second), false},
|
||||
{"nil", nil, args{nil}, nil, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.d.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
|
||||
t.Errorf("multiString.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(tt.d, tt.want) {
|
||||
t.Errorf("multiString.UnmarshalJSON() = %v, want %v", tt.d, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_duration_MarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
d *duration
|
||||
want []byte
|
||||
wantErr bool
|
||||
}{
|
||||
{"string", durPtr(15*time.Minute + 30*time.Second), []byte(`"15m30s"`), false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := tt.d.MarshalJSON()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("duration.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("duration.MarshalJSON() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user