Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle TokenType serialized as string or as uint8. #7233

Merged
merged 3 commits into from
Aug 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion builtin/credential/approle/path_role_test.go
Original file line number Diff line number Diff line change
@@ -1864,7 +1864,7 @@ func TestAppRole_TokenutilUpgrade(t *testing.T) {
// Hand craft JSON because there is overlap between fields
if err := s.Put(ctx, &logical.StorageEntry{
Key: "role/foo",
Value: []byte(`{"policies": ["foo"], "period": 300000000000, "token_bound_cidrs": ["127.0.0.1", "10.10.10.10/24"]}`),
Value: []byte(`{"policies": ["foo"], "period": 300000000000, "token_bound_cidrs": ["127.0.0.1", "10.10.10.10/24"], "token_type": "service"}`),
}); err != nil {
t.Fatal(err)
}
@@ -1882,6 +1882,7 @@ func TestAppRole_TokenutilUpgrade(t *testing.T) {
TokenPolicies: []string{"foo"},
TokenPeriod: 300 * time.Second,
TokenBoundCIDRs: []*sockaddr.SockAddrMarshaler{&sockaddr.SockAddrMarshaler{SockAddr: sockaddr.MustIPAddr("127.0.0.1")}, &sockaddr.SockAddrMarshaler{SockAddr: sockaddr.MustIPAddr("10.10.10.10/24")}},
TokenType: logical.TokenTypeService,
},
}
if diff := deep.Equal(fooEntry, exp); diff != nil {
26 changes: 26 additions & 0 deletions sdk/logical/token.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logical

import (
"fmt"
"time"

sockaddr "github.com/hashicorp/go-sockaddr"
@@ -28,6 +29,31 @@ const (
TokenTypeDefaultBatch
)

func (t *TokenType) UnmarshalJSON(b []byte) error {
if len(b) == 1 {
*t = TokenType(b[0] - '0')
return nil
}

// Handle upgrade from pre-1.2 where we were serialized as string:
s := string(b)
switch s {
case `"default"`:
*t = TokenTypeDefault
case `"service"`:
*t = TokenTypeService
case `"batch"`:
*t = TokenTypeBatch
case `"default-service"`:
*t = TokenTypeDefaultService
case `"default-batch"`:
*t = TokenTypeDefaultBatch
default:
return fmt.Errorf("unknown token type %q", s)
}
return nil
}

func (t TokenType) String() string {
switch t {
case TokenTypeDefault:
33 changes: 33 additions & 0 deletions sdk/logical/token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package logical

import (
"encoding/json"
"testing"
)

func TestJSONSerialization(t *testing.T) {
tt := TokenTypeDefaultBatch
s, err := json.Marshal(tt)
if err != nil {
t.Fatal(err)
}

var utt TokenType
err = json.Unmarshal(s, &utt)
if err != nil {
t.Fatal(err)
}

if tt != utt {
t.Fatalf("expected %v, got %v", tt, utt)
}

utt = TokenTypeDefault
err = json.Unmarshal([]byte(`"default-batch"`), &utt)
if err != nil {
t.Fatal(err)
}
if tt != utt {
t.Fatalf("expected %v, got %v", tt, utt)
}
}