Fix 764 (#2093)
* Fix KeyExpiration when a zero time value has a timezone When a zero time value is loaded from JSON or a DB in a way that assigns it the local timezone, it does not roudtrip in JSON as a value for which IsZero returns true. This causes KeyExpiry to be treated as a far past value instead of a nilish value. See https://github.com/golang/go/issues/57040 * Fix whitespace * Ensure that postgresql is used for all tests when env var is set * Pass through value of HEADSCALE_INTEGRATION_POSTGRES env var * Add option to set timezone on headscale container * Add test for registration with auth key in alternate timezone
This commit is contained in:
parent
aa0f3d43cc
commit
3101f895a7
8 changed files with 91 additions and 7 deletions
|
@ -93,7 +93,7 @@ func tailNode(
|
|||
User: tailcfg.UserID(node.UserID),
|
||||
|
||||
Key: node.NodeKey,
|
||||
KeyExpiry: keyExpiry,
|
||||
KeyExpiry: keyExpiry.UTC(),
|
||||
|
||||
Machine: node.MachineKey,
|
||||
DiscoKey: node.DiscoKey,
|
||||
|
@ -102,7 +102,7 @@ func tailNode(
|
|||
Endpoints: node.Endpoints,
|
||||
DERP: derp,
|
||||
Hostinfo: node.Hostinfo.View(),
|
||||
Created: node.CreatedAt,
|
||||
Created: node.CreatedAt.UTC(),
|
||||
|
||||
Online: node.IsOnline,
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package mapper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -205,3 +206,68 @@ func TestTailNode(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeExpiry(t *testing.T) {
|
||||
tp := func(t time.Time) *time.Time {
|
||||
return &t
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
exp *time.Time
|
||||
wantTime time.Time
|
||||
wantTimeZero bool
|
||||
}{
|
||||
{
|
||||
name: "no-expiry",
|
||||
exp: nil,
|
||||
wantTimeZero: true,
|
||||
},
|
||||
{
|
||||
name: "zero-expiry",
|
||||
exp: &time.Time{},
|
||||
wantTimeZero: true,
|
||||
},
|
||||
{
|
||||
name: "localtime",
|
||||
exp: tp(time.Time{}.Local()),
|
||||
wantTimeZero: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
node := &types.Node{
|
||||
GivenName: "test",
|
||||
Expiry: tt.exp,
|
||||
}
|
||||
tn, err := tailNode(
|
||||
node,
|
||||
0,
|
||||
&policy.ACLPolicy{},
|
||||
&types.Config{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("nodeExpiry() error = %v", err)
|
||||
}
|
||||
|
||||
// Round trip the node through JSON to ensure the time is serialized correctly
|
||||
seri, err := json.Marshal(tn)
|
||||
if err != nil {
|
||||
t.Fatalf("nodeExpiry() error = %v", err)
|
||||
}
|
||||
var deseri tailcfg.Node
|
||||
err = json.Unmarshal(seri, &deseri)
|
||||
if err != nil {
|
||||
t.Fatalf("nodeExpiry() error = %v", err)
|
||||
}
|
||||
|
||||
if tt.wantTimeZero {
|
||||
if !deseri.KeyExpiry.IsZero() {
|
||||
t.Errorf("nodeExpiry() = %v, want zero", deseri.KeyExpiry)
|
||||
}
|
||||
} else if deseri.KeyExpiry != tt.wantTime {
|
||||
t.Errorf("nodeExpiry() = %v, want %v", deseri.KeyExpiry, tt.wantTime)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue