-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add create-fga-model command
- Loading branch information
Showing
29 changed files
with
334 additions
and
92 deletions.
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
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,66 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
authz "github.com/canonical/identity-platform-login-ui/internal/authorization" | ||
logging "github.com/canonical/identity-platform-login-ui/internal/logging" | ||
monitoring "github.com/canonical/identity-platform-login-ui/internal/monitoring" | ||
fga "github.com/canonical/identity-platform-login-ui/internal/openfga" | ||
tracing "github.com/canonical/identity-platform-login-ui/internal/tracing" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// createFgaModelCmd represents the createFgaModel command | ||
var createFgaModelCmd = &cobra.Command{ | ||
Use: "create-fga-model", | ||
Short: "Creates an openfga model", | ||
Long: `Creates an openfga model`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
apiUrl, _ := cmd.Flags().GetString("fga-api-url") | ||
apiToken, _ := cmd.Flags().GetString("fga-api-token") | ||
storeId, _ := cmd.Flags().GetString("store-id") | ||
createModel(apiUrl, apiToken, storeId) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(createFgaModelCmd) | ||
|
||
createFgaModelCmd.PersistentFlags().String("fga-api-url", "", "The openfga API URL") | ||
createFgaModelCmd.PersistentFlags().String("fga-api-token", "", "The openfga API token") | ||
createFgaModelCmd.PersistentFlags().String("store-id", "", "The openfga store to create the model in") | ||
createFgaModelCmd.MarkPersistentFlagRequired("api-url") | ||
createFgaModelCmd.MarkPersistentFlagRequired("api-token") | ||
createFgaModelCmd.MarkPersistentFlagRequired("store-id") | ||
} | ||
|
||
func createModel(apiUrl, apiToken, storeId string) { | ||
logger := logging.NewNoopLogger() | ||
tracer := tracing.NewNoopTracer() | ||
monitor := monitoring.NewNoopMonitor("", logger) | ||
scheme, host, err := parseURL(apiUrl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
cfg := fga.NewConfig(scheme, host, storeId, apiToken, "", false, tracer, monitor, logger) | ||
fgaClient := fga.NewClient(cfg) | ||
modelId, err := fgaClient.WriteModel(context.Background(), []byte(authz.AuthModel)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("Created model: %s\n", modelId) | ||
} | ||
|
||
func parseURL(s string) (string, string, error) { | ||
u, err := url.Parse(s) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return u.Scheme, u.Host, nil | ||
} |
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,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "", | ||
Short: "Identity Platform Login UI", | ||
Long: `A login UI for the Ory stack`, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
} |
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,22 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/canonical/identity-platform-login-ui/internal/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// versionCmd represents the version command | ||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Get the application's version", | ||
Long: `Get the application's version`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Printf("App Version: %s\n", version.Version) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(versionCmd) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.