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

feat: gRPC gateway #1238

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3cafe32
WIP: gRPC gateway
hperl Nov 23, 2022
866380c
WIP: generate complete OpenAPI spec
hperl Dec 2, 2022
79a45a1
WIP
hperl Dec 6, 2022
a86c910
WIP: grpc gateway
hperl Jan 2, 2023
8fd10fa
WIP
hperl Feb 13, 2023
57a26a5
chore: update sdk
hperl Feb 13, 2023
c20f53a
WIP: E2E tests missing
hperl Feb 20, 2023
2f11642
feat: switch to gRPC gateway
hperl Feb 21, 2023
3b006fa
Merge remote-tracking branch 'origin/master' into hperl/exp/grpc-gateway
hperl Feb 22, 2023
31e931c
fix: docs examples
hperl Feb 22, 2023
7ec7e6b
chore: format
hperl Feb 22, 2023
600be5f
fix: remove well-known pb for Go
hperl Feb 22, 2023
4883c7c
fix: block on dial
hperl Feb 22, 2023
36d77f8
chore: remove http handlers
hperl Feb 22, 2023
7a8c33f
refactor: add validation middleware
hperl Feb 23, 2023
f9e5646
chore: clean up swagger comments
hperl Feb 23, 2023
a769059
fix: re-add ParseErrors field
hperl Feb 23, 2023
147b988
Merge remote-tracking branch 'origin/master' into hperl/exp/grpc-gateway
hperl Feb 23, 2023
4136a0b
chore: make sdk
hperl Feb 23, 2023
e588023
chore: fix makefile
hperl Feb 23, 2023
1452688
Merge remote-tracking branch 'origin/master' into hperl/exp/grpc-gateway
hperl Feb 23, 2023
619a306
fix: null tree
hperl Feb 28, 2023
a99a212
fix: add internal gRPC server
hperl Mar 10, 2023
af1c06f
feat: configure gRPC interceptors per type
hperl Mar 13, 2023
1e7458b
Merge branch 'master' into hperl/exp/grpc-gateway
hperl Mar 13, 2023
149c9cd
chore: sdk
hperl Mar 28, 2023
8bb9ab6
Merge remote-tracking branch 'origin/master' into hperl/exp/grpc-gateway
hperl Feb 15, 2024
d2d14c8
chore: update deps
hperl Feb 15, 2024
f2eea82
chore: bump deps
hperl Feb 15, 2024
628979f
fix buf
hperl Feb 15, 2024
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
22 changes: 22 additions & 0 deletions .schema/openapi/patches/checkServices.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- op: add
path: /paths/~1relation-tuples~1check/get/operationId
value: checkPermissionOrError
- op: add
path: /paths/~1relation-tuples~1check/post/operationId
value: postCheckPermissionOrError
- op: add
path: /paths/~1relation-tuples~1check~1openapi/get/operationId
value: checkPermission
- op: add
path: /paths/~1relation-tuples~1check~1openapi/post/operationId
value: postCheckPermission

# Remove schema from empty response.
- op: replace
path: /paths/~1admin~1relation-tuples/delete/responses/204
value:
description: An empty response.
- op: replace
path: /paths/~1admin~1relation-tuples/patch/responses/204
value:
description: An empty response.
19 changes: 15 additions & 4 deletions .schema/openapi/patches/health.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- op: replace
- op: add
path: /paths/~1health~1alive
value:
get:
Expand All @@ -17,17 +17,22 @@
content:
application/json:
schema:
"$ref": "#/components/schemas/healthStatus"
required:
- status
type: object
properties:
status:
type: string
description: {{.ProjectHumanName}} is ready to accept connections.
'500':
content:
application/json:
schema:
"$ref": "#/components/schemas/genericError"
"$ref": "#/definitions/errorGeneric"
description: genericError
summary: Check HTTP Server Status
tags: {{ .HealthPathTags | toJson }}
- op: replace
- op: add
path: /paths/~1health~1ready
value:
get:
Expand All @@ -46,6 +51,8 @@
content:
application/json:
schema:
required:
- status
type: object
properties:
status:
Expand All @@ -56,6 +63,8 @@
content:
application/json:
schema:
required:
- errors
properties:
errors:
additionalProperties:
Expand Down Expand Up @@ -85,6 +94,8 @@
application/json:
schema:
type: object
required:
- version
properties:
version:
description: The version of {{.ProjectHumanName}}.
Expand Down
2 changes: 1 addition & 1 deletion .schema/openapi/patches/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- op: replace
- op: add
path: /info
value:
title: Ory Keto API
Expand Down
11 changes: 11 additions & 0 deletions .schema/openapi/patches/replacements.sed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
s/ory.keto.relation_tuples.v1alpha2.ErrorResponse/errorGeneric/g
s/ory.keto.relation_tuples.v1alpha2.ErrorResponse.Error/genericError/g
s/ory.keto.relation_tuples.v1alpha2.CreateRelationTupleRequest.Relationship/createRelationshipBody/g
s/ory.keto.relation_tuples.v1alpha2.SubjectSet/subjectSet/g
s/ory.keto.relation_tuples.v1alpha2.SubjectTree/expandedPermissionTree/g
s/ory.keto.relation_tuples.v1alpha2.RelationTuple/relationship/g
s/ory.keto.relation_tuples.v1alpha2.ListRelationTuplesResponse/relationships/g
s/ory.keto.relation_tuples.v1alpha2.ListNamespacesResponse/relationshipNamespaces/g
s/ory.keto.relation_tuples.v1alpha2.CheckRequest/postCheckPermissionBody/g
s/ory.keto.relation_tuples.v1alpha2.CheckResponse/checkPermissionResult/g
s/ory.keto.opl.v1alpha1.CheckResponse/checkOplSyntaxResult/g
24 changes: 11 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ GO_DEPENDENCIES = golang.org/x/tools/cmd/goimports \
github.com/mattn/goveralls \
github.com/ory/go-acc \
github.com/bufbuild/buf/cmd/buf \
github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc \
github.com/josephburnett/jd \
github.com/mikefarah/yq/v4 \
golang.org/x/tools/cmd/stringer \
Expand Down Expand Up @@ -45,10 +44,11 @@ authors: # updates the AUTHORS file
curl https://raw.githubusercontent.com/ory/ci/master/authors/authors.sh | env PRODUCT="Ory Keto" bash

.PHONY: format
format: .bin/ory .bin/goimports node_modules
format: .bin/buf .bin/ory .bin/goimports node_modules
.bin/ory dev headers copyright --type=open-source --exclude=.bin --exclude=internal/httpclient --exclude=proto
.bin/goimports -w -local github.com/ory/keto *.go internal cmd contrib ketoctx ketoapi embedx
npm exec -- prettier --write .
buf format -w

.PHONY: install
install:
Expand All @@ -60,21 +60,18 @@ docker:

# Generates the SDKs
.PHONY: sdk
sdk: .bin/swagger .bin/ory node_modules
sdk: buf .bin/swagger .bin/ory node_modules
rm -rf internal/httpclient
swagger generate spec -m -o spec/swagger.json \
-c github.com/ory/keto \
-c github.com/ory/x/healthx \
-x internal/httpclient \
-x internal/e2e
.bin/ory dev swagger sanitize ./spec/swagger.json
swagger validate ./spec/swagger.json
.bin/ory dev swagger sanitize ./spec/api.swagger.json
sed -i -f ./.schema/openapi/patches/replacements.sed ./spec/api.swagger.json
swagger validate ./spec/api.swagger.json
CIRCLE_PROJECT_USERNAME=ory CIRCLE_PROJECT_REPONAME=keto \
.bin/ory dev openapi migrate \
--health-path-tags metadata \
-p https://raw.githubusercontent.com/ory/x/master/healthx/openapi/patch.yaml \
-p file://.schema/openapi/patches/health.yaml \
-p file://.schema/openapi/patches/meta.yaml \
spec/swagger.json spec/api.json
-p file://.schema/openapi/patches/checkServices.yaml \
spec/api.swagger.json spec/api.json

mkdir -p internal/httpclient

Expand All @@ -99,7 +96,8 @@ build:
# Generate APIs and client stubs from the definitions
#
.PHONY: buf-gen
buf-gen: .bin/buf .bin/protoc-gen-doc node_modules
buf-gen: .bin/buf node_modules
buf format -w
buf generate proto
make format
@echo "All code was generated successfully!"
Expand Down
22 changes: 20 additions & 2 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ plugins:
out: proto
opt: paths=source_relative,require_unimplemented_servers=false

- plugin: buf.build/bufbuild/validate-go
out: proto
opt: paths=source_relative

- plugin: buf.build/protocolbuffers/js
out: proto
opt: import_style=commonjs,binary
Expand All @@ -22,7 +26,21 @@ plugins:
opt: ts_out=proto
path: node_modules/.bin/protoc-gen-ts

- name: doc
- remote: buf.build/grpc-ecosystem/plugins/grpc-gateway:v2.14.0-1
out: proto
opt: paths=source_relative

- remote: buf.build/grpc-ecosystem/plugins/openapiv2:v2.14.0-1
opt:
- allow_merge=true
- merge_file_name=api
- openapi_naming_strategy=fqn
- visibility_restriction_selectors=PUBLIC
- disable_service_tags=true
- disable_default_errors=true
- disable_default_responses=true
out: spec

- remote: buf.build/sawadashota/plugins/protoc-gen-doc:v1.5.1
out: proto
opt: markdown,proto/buf.md
strategy: all
6 changes: 3 additions & 3 deletions cmd/check/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"github.com/ory/keto/ketoapi"
rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"

"github.com/ory/keto/internal/check"

"github.com/ory/x/cmdx"
"github.com/spf13/cobra"

"github.com/ory/keto/cmd/client"
)

type checkOutput check.CheckPermissionResult
type checkOutput struct {
Allowed bool `json:"allowed"`
}

func (o *checkOutput) String() string {
if o.Allowed {
Expand Down
18 changes: 17 additions & 1 deletion cmd/expand/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package expand
import (
"fmt"

"google.golang.org/protobuf/encoding/protojson"

"github.com/ory/keto/ketoapi"

rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"
Expand Down Expand Up @@ -53,7 +55,7 @@ func NewExpandCmd() *cobra.Command {
tree = ketoapi.TreeFromProto[*ketoapi.RelationTuple](resp.Tree)
}

cmdx.PrintJSONAble(cmd, tree)
cmdx.PrintJSONAble(cmd, &pbJSONValue{resp})
switch flagx.MustGetString(cmd, cmdx.FlagFormat) {
case string(cmdx.FormatDefault), "":
if tree == nil && !flagx.MustGetBool(cmd, cmdx.FlagQuiet) {
Expand All @@ -76,3 +78,17 @@ func NewExpandCmd() *cobra.Command {
func RegisterCommandsRecursive(parent *cobra.Command) {
parent.AddCommand(NewExpandCmd())
}

type pbJSONValue struct{ *rts.ExpandResponse }

func (v *pbJSONValue) MarshalJSON() ([]byte, error) {
marshaler := &protojson.MarshalOptions{EmitUnpopulated: true}
return marshaler.Marshal(v.Tree)
}
func (v *pbJSONValue) String() string {
var tree *ketoapi.Tree[*ketoapi.RelationTuple]
if v.Tree != nil {
tree = ketoapi.TreeFromProto[*ketoapi.RelationTuple](v.Tree)
}
return tree.String()
}
2 changes: 1 addition & 1 deletion cmd/expand/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestExpandCommand(t *testing.T) {
"--"+cmdx.FlagFormat, string(cmdx.FormatJSON),
"--insecure-skip-hostname-verification=true",
)
assert.Equal(t, "null\n", stdOut)
assert.Equal(t, "{\"type\":\"unspecified\",\"subject\":null,\"tuple\":null,\"children\":[]}\n", stdOut)
hperl marked this conversation as resolved.
Show resolved Hide resolved
})

t.Run("format=default", func(t *testing.T) {
Expand Down
Loading