mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
acl: fix a bug in token creation when parsing expiration TTLs. (#15999)
The ACL token decoding was not correctly handling time duration syntax such as "1h" which forced people to use the nanosecond representation via the HTTP API. The change adds an unmarshal function which allows this syntax to be used, along with other styles correctly.
This commit is contained in:
47
api/acl.go
47
api/acl.go
@@ -545,6 +545,53 @@ type ACLTokenRoleLink struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaler interface and allows
|
||||
// ACLToken.ExpirationTTL to be marshaled correctly.
|
||||
func (a *ACLToken) MarshalJSON() ([]byte, error) {
|
||||
type Alias ACLToken
|
||||
exported := &struct {
|
||||
ExpirationTTL string
|
||||
*Alias
|
||||
}{
|
||||
ExpirationTTL: a.ExpirationTTL.String(),
|
||||
Alias: (*Alias)(a),
|
||||
}
|
||||
if a.ExpirationTTL == 0 {
|
||||
exported.ExpirationTTL = ""
|
||||
}
|
||||
return json.Marshal(exported)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface and allows
|
||||
// ACLToken.ExpirationTTL to be unmarshalled correctly.
|
||||
func (a *ACLToken) UnmarshalJSON(data []byte) (err error) {
|
||||
type Alias ACLToken
|
||||
aux := &struct {
|
||||
ExpirationTTL any
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(a),
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(data, &aux); err != nil {
|
||||
return err
|
||||
}
|
||||
if aux.ExpirationTTL != nil {
|
||||
switch v := aux.ExpirationTTL.(type) {
|
||||
case string:
|
||||
if v != "" {
|
||||
if a.ExpirationTTL, err = time.ParseDuration(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case float64:
|
||||
a.ExpirationTTL = time.Duration(v)
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type ACLTokenListStub struct {
|
||||
AccessorID string
|
||||
Name string
|
||||
|
||||
Reference in New Issue
Block a user