-
Notifications
You must be signed in to change notification settings - Fork 12
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
Introduce query engine and schema generation #55
Merged
jnschaeffer
merged 5 commits into
infratographer:main
from
jnschaeffer:schema-generation
Mar 29, 2023
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f6fca06
Add support for schema generation
jnschaeffer 3015762
Add query.Engine, introduce internal resource types
jnschaeffer 6c61524
Drop pubsubx package and worker command
jnschaeffer 4518ff5
Change spicedbx prefix to namespace, add schema generation tests
jnschaeffer 44c32f0
Apply suggestions from code review
jnschaeffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Emacs stuff | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ package cmd | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" | ||
"github.com/spf13/cobra" | ||
|
@@ -26,19 +27,32 @@ import ( | |
"go.infratographer.com/permissions-api/internal/spicedbx" | ||
) | ||
|
||
var schemaCmd = &cobra.Command{ | ||
Use: "schema", | ||
Short: "write the schema into SpiceDB", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
writeSchema(cmd.Context(), globalCfg) | ||
}, | ||
} | ||
var ( | ||
schemaCmd = &cobra.Command{ | ||
Use: "schema", | ||
Short: "write the schema into SpiceDB", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
writeSchema(cmd.Context(), dryRun, globalCfg) | ||
}, | ||
} | ||
|
||
dryRun bool | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(schemaCmd) | ||
|
||
schemaCmd.Flags().BoolVar(&dryRun, "dry-run", false, "dry run: print the schema instead of applying it") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
} | ||
|
||
func writeSchema(ctx context.Context, cfg *config.AppConfig) { | ||
func writeSchema(ctx context.Context, dryRun bool, cfg *config.AppConfig) { | ||
schemaStr := spicedbx.GeneratedSchema("infratographer") | ||
|
||
if dryRun { | ||
fmt.Printf("%s", schemaStr) | ||
return | ||
} | ||
|
||
err := otelx.InitTracer(cfg.Tracing, appName, logger) | ||
if err != nil { | ||
logger.Fatalw("unable to initialize tracing system", "error", err) | ||
|
@@ -49,11 +63,9 @@ func writeSchema(ctx context.Context, cfg *config.AppConfig) { | |
logger.Fatalw("unable to initialize spicedb client", "error", err) | ||
} | ||
|
||
schema := spicedbx.GeneratedSchema(cfg.SpiceDB.Prefix) | ||
|
||
logger.Debugw("Writing schema to DB", "schema", schema) | ||
logger.Debugw("Writing schema to DB", "schema", schemaStr) | ||
|
||
_, err = client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{Schema: schema}) | ||
_, err = client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{Schema: schemaStr}) | ||
if err != nil { | ||
logger.Fatalw("error writing schema to SpiceDB", "error", err) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package query | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"go.infratographer.com/permissions-api/internal/types" | ||
) | ||
|
||
func newRoleFromTemplate(t types.RoleTemplate) types.Role { | ||
out := types.Role{ | ||
ID: uuid.New(), | ||
Actions: t.Actions, | ||
} | ||
|
||
return out | ||
jnschaeffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package query | ||
|
||
import "github.com/authzed/authzed-go/v1" | ||
|
||
// Engine represents a client for making permissions queries. | ||
type Engine struct { | ||
namespace string | ||
client *authzed.Client | ||
} | ||
|
||
func NewEngine(namespace string, client *authzed.Client) *Engine { | ||
jnschaeffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &Engine{ | ||
namespace: namespace, | ||
client: client, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
total nit but I prefer
RunE
over a bunch oflogger.Fatal
- not sure how you feel about that (yes, i know you didn't really change this 😆 )There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a good suggestion for a subsequent PR. Can you open a GH issue? Would be a nice low hanging fruit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! See #57.