Skip to content

adding materialize demo #31

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 32 additions & 0 deletions code-examples/go/materialize-demo/authzed-download-fbc717.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
schema: |-
definition user {}

definition organization {
relation member: user
}

definition folder {
relation viewer: user
relation organization: organization

permission view_doc = viewer & organization->member
}

definition document {
relation folder: folder
permission view = folder->view_doc
}

relationships: |-
document:123#folder@folder:abc
folder:abc#organization@organization:acme
organization:acme#member@user:tim
folder:abc#viewer@user:tim
document:456#folder@folder:abc

assertions:
assertTrue: []
assertFalse: []

validation: {}
17 changes: 17 additions & 0 deletions code-examples/go/materialize-demo/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

#to do: handle this in the app logic

# Find the process ID (PID) using lsof to identify processes using port 5432
PID=$(lsof -ti :5432)

# Check if PID is not empty
if [ -z "$PID" ]; then
echo "No process is running on port 5432."
else
# Kill the process
kill -9 $PID
echo "Killed process with PID $PID running on port 5432."
fi

zed relationship delete document:789 folder folder:abc --endpoint=materialize-demo-epic-herring-5707.us-east-1.demo.aws.authzed.net:443 --token=sdbst_h256_e4b91fcb2d3eb39d75c507b5fc4595494aa36f70db44f39a8a0ab5c3ba6eb836be7d8b225a0cd1bd8a036aed5e461dd0ab044dca0dbc525a2026638e2aa8dd3521bbeb74fdfc752f440e56010c48a42208fbd2b198f59e46030ef7e33db40c4e
45 changes: 45 additions & 0 deletions code-examples/go/materialize-demo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module materialize

go 1.22.5

require (
github.com/authzed/authzed-go v0.14.0
github.com/authzed/grpcutil v0.0.0-20240123194739-2ea1e3d2d98b
github.com/fatih/color v1.18.0
github.com/fergusstrange/embedded-postgres v1.28.0
github.com/jackc/pgx/v5 v5.6.0
github.com/jzelinskie/stringz v0.0.3
github.com/kataras/golog v0.1.12
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

require (
cloud.google.com/go/compute/metadata v0.3.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/kataras/pio v0.0.13 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/planetscale/vtprotobuf v0.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/samber/lo v1.46.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
198 changes: 198 additions & 0 deletions code-examples/go/materialize-demo/go.sum

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions code-examples/go/materialize-demo/internal/data/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package data

import (
"context"
"fmt"
"log"

v0 "github.com/authzed/authzed-go/proto/authzed/api/materialize/v0"
"github.com/fatih/color"
"github.com/kataras/golog"
"google.golang.org/protobuf/encoding/prototext"
)

func Write(change *v0.PermissionSetChange) {

switch oneOf := change.GetChild().(type) {
//s2s
case *v0.PermissionSetChange_ChildSet:
s2s(change, oneOf)
color.Blue(prototext.Format(change))
fmt.Println("------------------------------------------------")
//m2s
case *v0.PermissionSetChange_ChildMember:
m2s(change, oneOf)
color.Green(prototext.Format(change))
fmt.Println("------------------------------------------------")
default:
log.Fatalf("Unknown child type")
}

}

func s2s(change *v0.PermissionSetChange, childSet *v0.PermissionSetChange_ChildSet) {
//see comment above
conn := connInit("sets")
defer conn.Close(context.Background())

query := `INSERT INTO set_to_set (child_type, child_id, child_relation, parent_type, parent_id, parent_relation)
VALUES ($1, $2, $3, $4, $5, $6)`
_, err := conn.Exec(context.Background(), query,
childSet.ChildSet.ObjectType,
childSet.ChildSet.ObjectId,
childSet.ChildSet.PermissionOrRelation,
change.ParentSet.ObjectType,
change.ParentSet.ObjectId,
change.ParentSet.PermissionOrRelation)
if err != nil {
golog.Errorf("unable to insert %v into set_to_set table: %v", change, err)
}

}

func m2s(change *v0.PermissionSetChange, childMember *v0.PermissionSetChange_ChildMember) {
//I wonder if this is the most effecient way to init the conn
conn := connInit("sets")
defer conn.Close(context.Background())

query := `INSERT INTO member_to_set (member_type, member_id, member_relation, set_type, set_id, set_relation)
VALUES ($1, $2, $3, $4, $5, $6)`
_, err := conn.Exec(context.Background(), query,
childMember.ChildMember.ObjectType,
childMember.ChildMember.ObjectId,
childMember.ChildMember.OptionalPermissionOrRelation,
change.ParentSet.ObjectType,
change.ParentSet.ObjectId,
change.ParentSet.PermissionOrRelation)
if err != nil {
golog.Errorf("unable to insert %v into member_to_set table: %v", change, err)
}

}
153 changes: 153 additions & 0 deletions code-examples/go/materialize-demo/internal/data/postgres_init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package data

import (
"fmt"
"log"

"github.com/fatih/color"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/kataras/golog"

"context"
"io"

"github.com/jackc/pgx/v5"

"strings"
)

func DbStart() (*embeddedpostgres.EmbeddedPostgres, error) {
golog.Info("Starting embedded postgres")

// Redirect the embedded postgres logs to ioutil.Discard to suppress them
postgres := embeddedpostgres.NewDatabase(embeddedpostgres.DefaultConfig().
Logger(io.Discard))
err := postgres.Start()
if err != nil {
return postgres, err
}

golog.Info("Started embedded postgres")

return postgres, nil
}

// to do: stop the db in the event of a fatalf
func DbStop(postgres *embeddedpostgres.EmbeddedPostgres) error {
golog.Info("Stopping embedded postgres")
err := postgres.Stop()
if err != nil {
return err
}
golog.Info("Stopped embedded postgres")

return nil
}

// connInit lets you specify the logical database you want to connect to
func connInit(db string) *pgx.Conn {
conn, err := pgx.Connect(context.Background(), "postgres://postgres:postgres@localhost:5432/"+db)
if err != nil {
log.Fatalf("unable to connect to database: %v", err)
}

return conn
}

func createDatabase() error {

golog.Info("Creating logical database called 'sets'")

conn := connInit("postgres")
defer conn.Close(context.Background())

// Create the database if it doesn't exist
_, err := conn.Exec(context.Background(), "CREATE DATABASE sets")

if err != nil {
if strings.Contains(err.Error(), "SQLSTATE 42P04") {
golog.Info("Database called 'sets' already exists. Continuing...")
return nil
}
return err
}

return nil
}

func truncateTable(tableName string) error {
conn := connInit("sets")
defer conn.Close(context.Background())

_, err := conn.Exec(context.Background(), "TRUNCATE TABLE "+tableName+";")
if err != nil {
golog.Error(err)
return err
}

return nil
}

func createTables() error {
conn := connInit("sets")
defer conn.Close(context.Background())

m2sTable := `CREATE TABLE member_to_set (
member_type varchar(100),
member_id varchar(100),
member_relation varchar(100),
set_type varchar(100),
set_id varchar(100),
set_relation varchar(100)
);`
_, err := conn.Exec(context.Background(), m2sTable)

if err != nil {
if strings.Contains(err.Error(), "SQLSTATE 42P07") {
truncateTable("member_to_set")
golog.Info("Table member_to_set already exists. Table truncated and continuing...")
} else {
return err
}
} else {
color.Set(color.FgGreen)
fmt.Printf("Created member_to_set table: %v\n", m2sTable)
color.Unset()
}

s2sTable := `CREATE TABLE set_to_set (
child_type varchar(100),
child_id varchar(100),
child_relation varchar(100),
parent_type varchar(100),
parent_id varchar(100),
parent_relation varchar(100)
);`
_, err = conn.Exec(context.Background(), s2sTable)
if err != nil {
if strings.Contains(err.Error(), "SQLSTATE 42P07") {
truncateTable("set_to_set")
golog.Info("Table set_to_set already exists. Table truncated and continuing...")
return nil
}
return err
} else {
color.Set(color.FgBlue)
fmt.Printf("Created set_to_set table: %v\n", s2sTable)
color.Unset()
}
return nil
}

func PrepareDatabase() error {
err := createDatabase()
if err != nil {
return err
}
err = createTables()
if err != nil {
return err
}

return nil
}
Loading