Skip to content

Commit e667765

Browse files
authored
[spicedb] generate ts definitions (#18308)
1 parent be3d71d commit e667765

File tree

8 files changed

+652
-27
lines changed

8 files changed

+652
-27
lines changed

components/server/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"start": "node ./dist/main.js",
88
"start-inspect": "node --inspect=0.0.0.0:9229 ./dist/main.js",
9-
"build": "yarn lint && npx tsc",
9+
"generate": "leeway run components/spicedb:generate-ts > src/authorization/definitions.ts && npx prettier --write src/authorization/definitions.ts",
10+
"build": "yarn generate && yarn lint && npx tsc",
1011
"lint": "yarn eslint src/*.ts src/**/*.ts",
1112
"lint:fix": "yarn eslint src/*.ts src/**/*.ts --fix",
1213
"build:clean": "yarn clean && yarn build",
@@ -90,7 +91,6 @@
9091
"redlock": "^5.0.0-beta.2",
9192
"reflect-metadata": "^0.1.10",
9293
"stripe": "^9.0.0",
93-
9494
"twilio": "^3.78.0",
9595
"uuid": "^8.3.2",
9696
"vscode-ws-jsonrpc": "^0.2.0",
@@ -132,6 +132,7 @@
132132
"eslint": "^7.11.0",
133133
"expect": "^1.20.2",
134134
"mocha": "^5.0.0",
135+
"prettier": "^3.0.0",
135136
"rimraf": "^3.0.2",
136137
"source-map-loader": "^0.2.3",
137138
"standard": "^16.0.3",

components/server/src/authorization/definitions.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,44 @@
55
*/
66

77
export const InstallationID = "1";
8-
export const InstallationResourceType = "installation";
9-
export const OrganizationResourceType = "organization";
10-
export const ProjectResourceType = "project";
11-
export const UserResourceType = "user";
12-
export type ResourceType =
13-
| typeof InstallationResourceType
14-
| typeof OrganizationResourceType
15-
| typeof ProjectResourceType
16-
| typeof UserResourceType;
17-
18-
export type InstallationRelation = "user" | "admin";
19-
export type OrganizationRelation = "installation" | "owner" | "member";
20-
export type ProjectRelation = "org" | "editor" | "viewer";
8+
export type ResourceType = UserResourceType | InstallationResourceType | OrganizationResourceType | ProjectResourceType;
9+
2110
export type Relation = InstallationRelation | OrganizationRelation | ProjectRelation;
2211

12+
export type Permission = InstallationPermission | OrganizationPermission | ProjectPermission;
13+
14+
export type UserResourceType = "user";
15+
16+
export type InstallationResourceType = "installation";
17+
18+
export type InstallationRelation = "member" | "admin";
19+
2320
export type InstallationPermission = "create_organization";
21+
22+
export type OrganizationResourceType = "organization";
23+
24+
export type OrganizationRelation = "installation" | "member" | "owner";
25+
2426
export type OrganizationPermission =
27+
| "installation_admin"
2528
| "read_info"
2629
| "write_info"
30+
| "delete"
31+
| "read_settings"
32+
| "write_settings"
2733
| "read_members"
2834
| "invite_members"
2935
| "write_members"
3036
| "leave"
31-
| "delete"
3237
| "create_project"
33-
| "read_settings"
34-
| "write_settings"
3538
| "read_git_provider"
3639
| "write_git_provider"
3740
| "read_billing"
3841
| "write_billing"
3942
| "write_billing_admin";
40-
export type ProjectPermission = "write_info" | "read_info" | "delete";
41-
export type Permission = OrganizationPermission;
43+
44+
export type ProjectResourceType = "project";
45+
46+
export type ProjectRelation = "org" | "editor" | "viewer";
47+
48+
export type ProjectPermission = "read_info" | "write_info" | "delete";

components/spicedb/BUILD.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ packages:
99
srcs:
1010
- go.mod
1111
- go.sum
12-
- "**/*.go"
12+
- "*.go"
1313
- "schema/*.yaml"
1414
deps:
1515
- components/spicedb:schema
@@ -52,8 +52,8 @@ scripts:
5252
echo "Stopped spicedb."
5353
fi
5454
55-
- name: report-spicedb
56-
description: "Reports spicedb's current state."
55+
- name: generate-ts
56+
description: "Generate definition in typescript."
5757
deps: []
5858
script: |
59-
spicedn
59+
(cd codegen && go run .)
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package main
6+
7+
import (
8+
"context"
9+
"errors"
10+
"fmt"
11+
"os"
12+
"strings"
13+
14+
"github.com/authzed/spicedb/pkg/development"
15+
"github.com/authzed/spicedb/pkg/namespace"
16+
dev_v1 "github.com/authzed/spicedb/pkg/proto/developer/v1"
17+
implv1 "github.com/authzed/spicedb/pkg/proto/impl/v1"
18+
"github.com/authzed/spicedb/pkg/schemadsl/compiler"
19+
"gopkg.in/yaml.v2"
20+
)
21+
22+
type Schema struct {
23+
Schema string `yaml:"schema"`
24+
}
25+
26+
func main() {
27+
schema := GetCompiledSchema()
28+
fmt.Print(GenerateDefinition(schema))
29+
}
30+
31+
func GetCompiledSchema() *compiler.CompiledSchema {
32+
// read schemaFile
33+
b, err := os.ReadFile("../schema/schema.yaml")
34+
if err != nil {
35+
panic(err)
36+
}
37+
38+
var schema Schema
39+
err = yaml.Unmarshal(b, &schema)
40+
if err != nil {
41+
panic(err)
42+
}
43+
44+
devCtx, devErrs, err := development.NewDevContext(context.Background(), &dev_v1.RequestContext{
45+
Schema: schema.Schema,
46+
Relationships: nil,
47+
})
48+
if err != nil {
49+
panic(err)
50+
}
51+
if devErrs != nil {
52+
panic(errors.New(devErrs.InputErrors[0].Message))
53+
}
54+
return devCtx.CompiledSchema
55+
}
56+
57+
func GenerateDefinition(schema *compiler.CompiledSchema) string {
58+
59+
resource := "export type ResourceType ="
60+
relation := "export type Relation ="
61+
permission := "export type Permission ="
62+
other := ""
63+
64+
// list definitions
65+
for _, def := range schema.ObjectDefinitions {
66+
// make sure the first character is upper case
67+
simpleName := strings.ToUpper(string(def.Name[0])) + def.Name[1:]
68+
resourceTypeName := simpleName + "ResourceType"
69+
resource += "\n | " + resourceTypeName + ""
70+
other += "\nexport type " + resourceTypeName + " = \"" + def.Name + "\";\n"
71+
// check if relations exists
72+
hasRelations := false
73+
for _, rel := range def.Relation {
74+
if namespace.GetRelationKind(rel) == implv1.RelationMetadata_RELATION {
75+
hasRelations = true
76+
break
77+
}
78+
}
79+
if hasRelations {
80+
relation += "\n | " + simpleName + "Relation"
81+
other += "\nexport type " + simpleName + "Relation ="
82+
for _, rel := range def.Relation {
83+
if namespace.GetRelationKind(rel) == implv1.RelationMetadata_RELATION {
84+
other += "\n | \"" + rel.Name + "\""
85+
}
86+
}
87+
other += ";\n"
88+
}
89+
// check if permissions exists
90+
hasPermissions := false
91+
for _, rel := range def.Relation {
92+
if namespace.GetRelationKind(rel) == implv1.RelationMetadata_PERMISSION {
93+
hasPermissions = true
94+
break
95+
}
96+
}
97+
if hasPermissions {
98+
permission += "\n | " + simpleName + "Permission"
99+
// permissions
100+
other += "\nexport type " + simpleName + "Permission ="
101+
for _, rel := range def.Relation {
102+
if namespace.GetRelationKind(rel) == implv1.RelationMetadata_PERMISSION {
103+
other += "\n | \"" + rel.Name + "\""
104+
}
105+
}
106+
other += ";\n"
107+
}
108+
}
109+
110+
return `/**
111+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
112+
* Licensed under the GNU Affero General Public License (AGPL).
113+
* See License.AGPL.txt in the project root for license information.
114+
*/
115+
116+
export const InstallationID = "1";
117+
` +
118+
resource + ";\n\n" + relation + ";\n\n" + permission + ";\n\n" + other
119+
}

components/spicedb/codegen/go.mod

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module github.com/gitpod-io/gitpod/components/spicedb/codegen
2+
3+
go 1.20
4+
5+
require (
6+
github.com/authzed/spicedb v1.23.1
7+
gopkg.in/yaml.v2 v2.4.0
8+
)
9+
10+
require (
11+
github.com/Masterminds/squirrel v1.5.4 // indirect
12+
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
13+
github.com/authzed/authzed-go v0.8.1-0.20230620170737-8257e7bd388e // indirect
14+
github.com/authzed/grpcutil v0.0.0-20230703173955-bdd0ac3f16a5 // indirect
15+
github.com/beorn7/perks v1.0.1 // indirect
16+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
17+
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
18+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
19+
github.com/creasty/defaults v1.7.0 // indirect
20+
github.com/dalzilio/rudd v1.1.1-0.20220422201445-0a0cd32c7df9 // indirect
21+
github.com/davecgh/go-spew v1.1.1 // indirect
22+
github.com/dustin/go-humanize v1.0.1 // indirect
23+
github.com/ecordell/optgen v0.0.10-0.20230609182709-018141bf9698 // indirect
24+
github.com/emirpasic/gods v1.18.1 // indirect
25+
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
26+
github.com/go-logr/logr v1.2.4 // indirect
27+
github.com/go-logr/stdr v1.2.2 // indirect
28+
github.com/golang/protobuf v1.5.3 // indirect
29+
github.com/google/cel-go v0.12.5 // indirect
30+
github.com/google/go-cmp v0.5.9 // indirect
31+
github.com/google/uuid v1.3.0 // indirect
32+
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
33+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.5 // indirect
34+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
35+
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
36+
github.com/hashicorp/go-memdb v1.3.4 // indirect
37+
github.com/hashicorp/golang-lru v0.5.4 // indirect
38+
github.com/jzelinskie/stringz v0.0.1 // indirect
39+
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
40+
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
41+
github.com/mattn/go-colorable v0.1.13 // indirect
42+
github.com/mattn/go-isatty v0.0.19 // indirect
43+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
44+
github.com/pmezard/go-difflib v1.0.0 // indirect
45+
github.com/prometheus/client_golang v1.15.1 // indirect
46+
github.com/prometheus/client_model v0.4.0 // indirect
47+
github.com/prometheus/common v0.44.0 // indirect
48+
github.com/prometheus/procfs v0.10.0 // indirect
49+
github.com/rs/zerolog v1.29.0 // indirect
50+
github.com/samber/lo v1.38.1 // indirect
51+
github.com/scylladb/go-set v1.0.2 // indirect
52+
github.com/shopspring/decimal v1.3.1 // indirect
53+
github.com/stoewer/go-strcase v1.2.0 // indirect
54+
github.com/stretchr/testify v1.8.4 // indirect
55+
go.opentelemetry.io/otel v1.16.0 // indirect
56+
go.opentelemetry.io/otel/metric v1.16.0 // indirect
57+
go.opentelemetry.io/otel/trace v1.16.0 // indirect
58+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
59+
golang.org/x/net v0.11.0 // indirect
60+
golang.org/x/sync v0.3.0 // indirect
61+
golang.org/x/sys v0.9.0 // indirect
62+
golang.org/x/text v0.10.0 // indirect
63+
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
64+
google.golang.org/grpc v1.56.1 // indirect
65+
google.golang.org/protobuf v1.31.0 // indirect
66+
gopkg.in/yaml.v3 v3.0.1 // indirect
67+
)

0 commit comments

Comments
 (0)