From d2a4b8ddd74fd2e9c5b7d77d25786518b3c25792 Mon Sep 17 00:00:00 2001 From: btoews Date: Fri, 23 Aug 2024 07:42:03 -0600 Subject: [PATCH 1/2] give json encoding for nonce --- macaroon_test.go | 22 ++++++++++++++++++++++ nonce.go | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/macaroon_test.go b/macaroon_test.go index 575e478..fba6bce 100644 --- a/macaroon_test.go +++ b/macaroon_test.go @@ -2,6 +2,7 @@ package macaroon import ( "bytes" + "encoding/json" "errors" "fmt" "math/rand" @@ -683,6 +684,27 @@ func TestDecodeNonce(t *testing.T) { assert.Equal(t, m.Nonce, n) } +func TestNonceJSON(t *testing.T) { + n1 := newNonce([]byte{1, 2, 3}, false) + n2 := newNonce([]byte{1, 2, 3}, true) + n3 := n1 + n3.version = nonceV0 + n4 := n1 + n4.version = nonceV0 + + for _, n := range []Nonce{n1, n2, n3, n4} { + assert.NoError(t, msgpack.Unmarshal(n.MustEncode(), &n)) + + j, err := n.MarshalJSON() + assert.NoError(t, err) + + var d Nonce + assert.NoError(t, json.Unmarshal(j, &d)) + + assert.Equal(t, n, d) + } +} + func dischargeMacaroon(ka EncryptionKey, location string, encodedMacaroon []byte) (bool, []Caveat, *Macaroon, error) { tickets, err := TicketsForThirdParty(encodedMacaroon, location) if err != nil { diff --git a/nonce.go b/nonce.go index 79c09bb..cef24e3 100644 --- a/nonce.go +++ b/nonce.go @@ -1,6 +1,7 @@ package macaroon import ( + "encoding/json" "fmt" "github.com/google/uuid" @@ -112,6 +113,24 @@ func (n Nonce) MustEncode() []byte { return b } +func (n Nonce) MarshalJSON() ([]byte, error) { + raw, err := msgpack.Marshal(&n) + if err != nil { + return nil, err + } + + return json.Marshal(raw) +} + +func (c *Nonce) UnmarshalJSON(b []byte) error { + var raw []byte + if err := json.Unmarshal(b, &raw); err != nil { + return err + } + + return msgpack.Unmarshal(raw, c) +} + // newNonce creates a nonce from a key-id, where the key-id // is any opaque string; the resulting nonce has a bunch of random // goo in it to goo up the nonce. A sane key-id might be a user-id From 6f1c90daad42f0023d778f9591e4c1c8b71fb387 Mon Sep 17 00:00:00 2001 From: btoews Date: Fri, 23 Aug 2024 07:45:24 -0600 Subject: [PATCH 2/2] fix flaky test --- flyio/caveats.go | 1 + flyio/caveats_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/flyio/caveats.go b/flyio/caveats.go index 390f9e2..16f9bdd 100644 --- a/flyio/caveats.go +++ b/flyio/caveats.go @@ -286,6 +286,7 @@ func (r Role) String() string { combined |= namedRole if combined == r { + slices.Sort(names) // for consistency in tests return strings.Join(names, "+") } } diff --git a/flyio/caveats_test.go b/flyio/caveats_test.go index 244b883..cb8f38d 100644 --- a/flyio/caveats_test.go +++ b/flyio/caveats_test.go @@ -136,7 +136,7 @@ func TestAllowedRoles(t *testing.T) { func TestRole(t *testing.T) { assert.Equal(t, "admin", RoleAdmin.String()) assert.Equal(t, "member", RoleMember.String()) - assert.Equal(t, "member+billing_manager", (RoleMember | RoleBillingManager).String()) + assert.Equal(t, "billing_manager+member", (RoleMember | RoleBillingManager).String()) assert.True(t, RoleAdmin.HasAllRoles(RoleAdmin)) assert.True(t, RoleAdmin.HasAllRoles(RoleMember))