Skip to content

Commit 5a29a38

Browse files
authored
upgraded cedar-go to v0.3.2 (#200)
Signed-off-by: mqf20 <mingqingfoo@gmail.com>
1 parent 851135f commit 5a29a38

32 files changed

+600
-532
lines changed

tinytodo-go/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ You need Python3 and Go (1.22 or later).
2222

2323
See [TinyTodo's README](../tinytodo/README.md) for more information.
2424

25-
## Comparison with TinyTodo
25+
## Comparison with [TinyTodo](../tinytodo)
2626

27-
TinyTodo-Go is constrained by the features of [`cedar-go`](https://github.com/cedar-policy/cedar-go). Refer to [this README](https://github.com/cedar-policy/cedar-go?tab=readme-ov-file#comparison-to-the-rust-implementation) to learn about the missing features.
27+
TinyTodo-Go relies on [v0.3.2 of `cedar-go`](https://github.com/cedar-policy/cedar-go/releases/tag/v0.3.2). Refer to [this README](https://github.com/cedar-policy/cedar-go/tree/v0.3.2?tab=readme-ov-file#comparison-to-the-rust-implementation) to learn about the features that `cedar-go` is missing in comparison to [`cedar`](https://github.com/cedar-policy/cedar).

tinytodo-go/cmd/server/authorization.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const (
1212
DefaultEntitiesFileName = "entities.json" // this is not in the Cedar entity schema, conversion required
1313
)
1414

15-
func prepareCedarPolicyEntities() (*entitystore.EntityStore, cedar.PolicySet, error) {
15+
func prepareCedarPolicyEntities() (*entitystore.EntityStore, *cedar.PolicySet, error) {
1616

1717
entitiesFile, err := os.ReadFile(DefaultEntitiesFileName)
1818
if err != nil {
@@ -29,7 +29,7 @@ func prepareCedarPolicyEntities() (*entitystore.EntityStore, cedar.PolicySet, er
2929
return nil, nil, fmt.Errorf("failed to read Cedar policy file: %w", err)
3030
}
3131

32-
ps, err := cedar.NewPolicySet(DefaultCedarPolicyFileName, psFile)
32+
ps, err := cedar.NewPolicySetFromBytes(DefaultCedarPolicyFileName, psFile)
3333
if err != nil {
3434
return nil, nil, fmt.Errorf("failed to create Cedar policy set: %w", err)
3535
}

tinytodo-go/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/cedar-policy/cedar-examples/tinytodo-go
33
go 1.22
44

55
require (
6-
github.com/cedar-policy/cedar-go v0.0.0-20240715162045-a71e93ee6ae7 // pins the cedar-go commit
6+
github.com/cedar-policy/cedar-go v0.3.2
77
github.com/go-chi/chi/v5 v5.1.0
88
github.com/stretchr/testify v1.9.0
99
)

tinytodo-go/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/cedar-policy/cedar-go v0.0.0-20240715162045-a71e93ee6ae7 h1:3WPOmm5kgn8q5kbQc2kG97RK//GTQAp79AW7pV3pa8M=
2-
github.com/cedar-policy/cedar-go v0.0.0-20240715162045-a71e93ee6ae7/go.mod h1:pEgiK479O5dJfzXnTguOMm+bCplzy5rEEFPGdZKPWz4=
1+
github.com/cedar-policy/cedar-go v0.3.2 h1:WKE8sW/RsnTp9hkAHSf3oGspcEoIOGCPPz1GDF3dgFc=
2+
github.com/cedar-policy/cedar-go v0.3.2/go.mod h1:pEgiK479O5dJfzXnTguOMm+bCplzy5rEEFPGdZKPWz4=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=

tinytodo-go/internal/app/server/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package server
33
import (
44
"context"
55
"fmt"
6-
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore"
76
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/action"
7+
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entityuid"
88
"github.com/cedar-policy/cedar-go"
99
"log/slog"
1010
)
@@ -16,9 +16,9 @@ import (
1616
// Non-existent entities (resources) will result in an error. (TODO: we may not want this behaviour)
1717
func (s *Server) isAuthorized(
1818
ctx context.Context,
19-
principal entitystore.EntityUID,
19+
principal entityuid.EntityUID,
2020
action action.Action,
21-
resource entitystore.EntityUID,
21+
resource entityuid.EntityUID,
2222
) (bool, cedar.Diagnostic, error) {
2323

2424
// we have to generate entities every time, because the entities may have been updated

tinytodo-go/internal/app/server/context_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package server
33
import (
44
"context"
55
"encoding/json"
6+
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entity/list"
7+
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entity/team"
8+
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entity/user"
9+
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entityuid"
610
"os"
711
"path"
812
"testing"
@@ -27,7 +31,7 @@ func TestServer_isAuthorized(t *testing.T) {
2731
// read policies
2832

2933
psFile := readFile(t, path.Join("../../../", "policies.cedar"))
30-
ps, err := cedar.NewPolicySet("policies.cedar", psFile)
34+
ps, err := cedar.NewPolicySetFromBytes("policies.cedar", psFile)
3135
require.NoError(t, err)
3236

3337
// read entities (will be modified later)
@@ -44,25 +48,25 @@ func TestServer_isAuthorized(t *testing.T) {
4448

4549
// extract users
4650

47-
userAndrew, ok := es.Users[entitystore.UserUID{
48-
EntityUID: entitystore.NewEntityUID(entitytype.User, "andrew"),
51+
userAndrew, ok := es.Users[user.UserUID{
52+
EntityUID: entityuid.New(entitytype.User, "andrew"),
4953
}]
5054
require.True(t, ok)
5155

52-
userAaron, ok := es.Users[entitystore.UserUID{
53-
EntityUID: entitystore.NewEntityUID(entitytype.User, "aaron"),
56+
userAaron, ok := es.Users[user.UserUID{
57+
EntityUID: entityuid.New(entitytype.User, "aaron"),
5458
}]
5559
require.True(t, ok)
5660

57-
userKesha, ok := es.Users[entitystore.UserUID{
58-
EntityUID: entitystore.NewEntityUID(entitytype.User, "kesha"),
61+
userKesha, ok := es.Users[user.UserUID{
62+
EntityUID: entityuid.New(entitytype.User, "kesha"),
5963
}]
6064
require.True(t, ok)
6165

6266
// extract teams
6367

64-
teamInterns, ok := es.Teams[entitystore.TeamUID{
65-
EntityUID: entitystore.NewEntityUID(entitytype.Team, "interns"),
68+
teamInterns, ok := es.Teams[team.TeamUID{
69+
EntityUID: entityuid.New(entitytype.Team, "interns"),
6670
}]
6771
require.True(t, ok)
6872

@@ -100,7 +104,7 @@ func TestServer_isAuthorized(t *testing.T) {
100104
list0Readers := es.InsertNextTeam() // readers for list0
101105
list0Editors := es.InsertNextTeam() // editors for list0
102106

103-
list0 := entitystore.NewList(
107+
list0 := list.New(
104108
list0UID,
105109
"Cedar blog post",
106110
userAndrew.EUID,
@@ -157,7 +161,7 @@ func TestServer_isAuthorized(t *testing.T) {
157161
context.Background(),
158162
userAaron.EUID.EntityUID,
159163
action.GetList,
160-
entitystore.NewEntityUID(entitytype.List, "non-existent"),
164+
entityuid.New(entitytype.List, "non-existent"),
161165
)
162166
require.NoError(t, err)
163167
assert.False(t, decision)

tinytodo-go/internal/app/server/entitystore/action/action.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
// Package action contains the enum Action that represents the different actions supported by entitystore.EntityStore.
1+
// Package action contains the enum Action that represents the different actions supported by TinyTodo.
22
package action
33

44
import (
5-
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore"
65
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entitytype"
6+
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entityuid"
7+
"github.com/cedar-policy/cedar-go/types"
78
"strings"
89
)
910

10-
// Action is an enum that represents the different entity types supported by Cedar.
11+
// Action is an enum that represents the different entity types supported by TinyTodo.
1112
type Action int
1213

1314
const (
@@ -37,17 +38,17 @@ var (
3738
DeleteList: "Action::\"DeleteList\"",
3839
}
3940

40-
EntityUID = map[Action]entitystore.EntityUID{}
41+
EntityUID = map[Action]entityuid.EntityUID{}
4142
)
4243

4344
func init() {
4445
// verify that all Actions are valid EUIDs
4546
for k, act := range Name {
46-
euid, err := entitystore.ParseEntityUID(act)
47+
euid, err := entityuid.Parse(act)
4748
if err != nil {
4849
panic(err)
4950
}
50-
if euid.Type != entitytype.Action.String() {
51+
if euid.Type != types.EntityType(entitytype.Action.String()) {
5152
panic(err)
5253
}
5354
EntityUID[k] = euid
@@ -68,6 +69,6 @@ func Parse(act string) Action {
6869
return Unknown
6970
}
7071

71-
func (a Action) GetEUID() entitystore.EntityUID {
72+
func (a Action) GetEUID() entityuid.EntityUID {
7273
return EntityUID[a]
7374
}

tinytodo-go/internal/app/server/entitystore/app.go

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
package entitystore
22

33
import (
4-
"github.com/cedar-policy/cedar-go"
4+
"github.com/cedar-policy/cedar-go/types"
55
)
66

7-
// AsEntities converts EntityStore's native objects into cedar.Entities, to be passed to the Cedar authorization engine
7+
// AsEntities converts EntityStore's native objects into types.Entities, to be passed to the Cedar authorization engine
88
// when it evaluates a request.
9-
func (e *EntityStore) AsEntities() (cedar.Entities, error) {
9+
func (e *EntityStore) AsEntities() (types.Entities, error) {
1010

11-
es := make(cedar.Entities)
11+
es := make(types.Entities)
1212

1313
// process users
1414

1515
for _, user := range e.Users {
16-
es[user.EUID.EntityUID.EntityUID] = *user.AsCedarEntity()
16+
es[user.EUID.EntityUID.EntityUID] = user.AsCedarEntity()
1717
}
1818

1919
// process teams
2020

2121
for _, team := range e.Teams {
22-
es[team.UID.EntityUID.EntityUID] = *team.AsCedarEntity()
22+
es[team.UID.EntityUID.EntityUID] = team.AsCedarEntity()
2323
}
2424

2525
// process lists
2626

2727
for _, list := range e.Lists {
28-
es[list.UID.EntityUID.EntityUID] = *list.AsCedarEntity()
28+
es[list.UID.EntityUID.EntityUID] = list.AsCedarEntity()
2929
}
3030

3131
// process application
3232

33-
es[e.App.EUID.EntityUID] = *e.App.AsCedarEntity()
33+
es[e.App.EUID.EntityUID] = e.App.AsCedarEntity()
3434

3535
return es, nil
3636
}

tinytodo-go/internal/app/server/entitystore/convert_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package entitystore
33
import (
44
"encoding/json"
55
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entitytype"
6-
"github.com/cedar-policy/cedar-go"
6+
"github.com/cedar-policy/cedar-examples/tinytodo-go/internal/app/server/entitystore/entityuid"
7+
"github.com/cedar-policy/cedar-go/types"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
"testing"
@@ -16,7 +17,7 @@ func TestEntityStore_AsEntities(t *testing.T) {
1617
require.NoError(t, json.Unmarshal(f, &es))
1718
assert.Equal(
1819
t,
19-
NewEntityUID(entitytype.Application, "TinyTodo"),
20+
entityuid.New(entitytype.Application, "TinyTodo"),
2021
es.App.EUID,
2122
)
2223

@@ -26,19 +27,28 @@ func TestEntityStore_AsEntities(t *testing.T) {
2627
assert.Contains(
2728
t,
2829
entities,
29-
cedar.NewEntityUID(entitytype.Application.String(), "TinyTodo"),
30+
types.NewEntityUID(
31+
types.EntityType(entitytype.Application.String()),
32+
"TinyTodo",
33+
),
3034
)
3135

3236
assert.Contains(
3337
t,
3438
entities,
35-
cedar.NewEntityUID(entitytype.User.String(), "kesha"),
39+
types.NewEntityUID(
40+
types.EntityType(entitytype.User.String()),
41+
"kesha",
42+
),
3643
)
3744

3845
assert.Contains(
3946
t,
4047
entities,
41-
cedar.NewEntityUID(entitytype.Team.String(), "temp"),
48+
types.NewEntityUID(
49+
types.EntityType(entitytype.Team.String()),
50+
"temp",
51+
),
4252
)
4353
})
4454
}

0 commit comments

Comments
 (0)