Skip to content

Commit db994d7

Browse files
committed
migrate tokens
1 parent 7dc33fc commit db994d7

File tree

9 files changed

+162
-39
lines changed

9 files changed

+162
-39
lines changed

pkg/roles/api/auth/api_tokens.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ type APITokensPutOutput struct {
6262

6363
func (ap *AuthProvider) APITokensPut() usecase.Interactor {
6464
u := usecase.NewInteractor(func(ctx context.Context, input APITokensPutInput, output *APITokensPutOutput) error {
65-
token := &Token{
65+
token := &types.Token{
6666
Key: base64.RawStdEncoding.EncodeToString(securecookie.GenerateRandomKey(64)),
6767
Username: input.Username,
68-
ap: ap,
6968
}
70-
err := token.put(ctx)
69+
err := ap.putToken(token, ctx)
7170
if err != nil {
7271
return status.Wrap(err, status.Internal)
7372
}

pkg/roles/api/auth/api_tokens_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ func TestAPITokensGet(t *testing.T) {
2020
defer role.Stop()
2121
prov := auth.NewAuthProvider(role, inst)
2222

23-
tests.PanicIfError(inst.KV().Put(
23+
tests.PanicIfError(inst.KV().PutObj(
2424
ctx,
2525
inst.KV().Key(
2626
types.KeyRole,
2727
types.KeyTokens,
2828
tests.RandomString(),
2929
).String(),
30-
tests.MustJSON(auth.Token{}),
30+
&types.Token{},
3131
))
3232

3333
var output auth.APITokensGetOutput
@@ -58,7 +58,7 @@ func TestAPITokensPut(t *testing.T) {
5858
types.KeyTokens,
5959
output.Key,
6060
),
61-
auth.Token{
61+
&types.Token{
6262
Username: name,
6363
},
6464
)
@@ -75,14 +75,14 @@ func TestAPITokensDelete(t *testing.T) {
7575

7676
name := tests.RandomString()
7777

78-
tests.PanicIfError(inst.KV().Put(
78+
tests.PanicIfError(inst.KV().PutObj(
7979
ctx,
8080
inst.KV().Key(
8181
types.KeyRole,
8282
types.KeyTokens,
8383
name,
8484
).String(),
85-
tests.MustJSON(auth.Token{}),
85+
&types.Token{},
8686
))
8787

8888
assert.NoError(t, prov.APITokensDelete().Interact(ctx, auth.APITokensDeleteInput{

pkg/roles/api/auth/first_start.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"beryju.io/gravity/pkg/extconfig"
99
"beryju.io/gravity/pkg/roles"
10+
"beryju.io/gravity/pkg/roles/api/types"
1011
"github.com/gorilla/securecookie"
1112
"go.uber.org/zap"
1213
)
@@ -36,12 +37,11 @@ func (ap *AuthProvider) FirstStart(ev *roles.Event) {
3637

3738
token := os.Getenv("ADMIN_TOKEN")
3839
if token != "" {
39-
t := Token{
40+
t := &types.Token{
4041
Key: token,
4142
Username: username,
42-
ap: ap,
4343
}
44-
err := t.put(ev.Context)
44+
err := ap.putToken(t, ev.Context)
4545
if err != nil {
4646
ap.log.Warn("failed to create bootstrap token", zap.Error(err))
4747
return

pkg/roles/api/auth/token.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package auth
22

33
import (
44
"context"
5-
"encoding/json"
65
"strings"
76

87
"beryju.io/gravity/pkg/roles/api/types"
@@ -15,39 +14,26 @@ const (
1514
BearerType = "bearer"
1615
)
1716

18-
type Token struct {
19-
ap *AuthProvider
20-
Key string `json:"-"`
21-
22-
Username string `json:"username"`
23-
}
24-
25-
func (token *Token) put(ctx context.Context, opts ...clientv3.OpOption) error {
26-
raw, err := json.Marshal(&token)
27-
if err != nil {
28-
return err
29-
}
30-
fullKey := token.ap.inst.KV().Key(
17+
func (ap *AuthProvider) putToken(t *types.Token, ctx context.Context, opts ...clientv3.OpOption) error {
18+
fullKey := ap.inst.KV().Key(
3119
types.KeyRole,
3220
types.KeyTokens,
33-
token.Key,
21+
t.Key,
3422
).String()
35-
_, err = token.ap.inst.KV().Put(ctx, fullKey, string(raw), opts...)
23+
_, err := ap.inst.KV().PutObj(ctx, fullKey, t, opts...)
3624
return err
3725
}
3826

39-
func (ap *AuthProvider) tokenFromKV(raw *mvccpb.KeyValue) (*Token, error) {
40-
token := &Token{
41-
ap: ap,
42-
}
27+
func (ap *AuthProvider) tokenFromKV(raw *mvccpb.KeyValue) (*types.Token, error) {
28+
token := &types.Token{}
4329
prefix := ap.inst.KV().Key(
4430
types.KeyRole,
4531
types.KeyTokens,
4632
).Prefix(true).String()
47-
token.Key = strings.TrimPrefix(string(raw.Key), prefix)
48-
err := json.Unmarshal(raw.Value, &token)
33+
err := ap.inst.KV().Unmarshal(raw.Value, &token)
4934
if err != nil {
5035
return token, err
5136
}
37+
token.Key = strings.TrimPrefix(string(raw.Key), prefix)
5238
return token, nil
5339
}

pkg/roles/api/role_migrations.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api
22

33
import (
44
"context"
5-
"encoding/json"
65
"net/http"
76
"strings"
87

@@ -30,15 +29,15 @@ func (r *Role) RegisterMigrations() {
3029
shouldIntercept := res != nil && len(res.Kvs) > 0 && strings.HasPrefix(key, userPrefix)
3130
// If we're fetching a user, intercept the response
3231
if shouldIntercept {
33-
u := map[string]interface{}{}
34-
err := json.Unmarshal(res.Kvs[0].Value, &u)
32+
u := map[string]any{}
33+
err := r.i.KV().Unmarshal(res.Kvs[0].Value, &u)
3534
if err != nil {
3635
return res, nil
3736
}
3837
if _, set := u["permissions"]; !set {
3938
u["permissions"] = defaultPerms
4039
}
41-
v, err := json.Marshal(u)
40+
v, err := r.i.KV().Marshal(u)
4241
if err != nil {
4342
return res, nil
4443
}

pkg/roles/api/types/role_api_token.pb.go

Lines changed: 131 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/roles/api/types/role_api_user.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/roles/tsdb/types/role_tsdb_record.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protobuf/role_api_token.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
3+
option go_package = "pkg/roles/api/types";
4+
5+
message Token {
6+
string key = 1;
7+
string username = 2;
8+
}

0 commit comments

Comments
 (0)