-
Notifications
You must be signed in to change notification settings - Fork 4
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
Introduce cobra CLI and basic authorization functionality
- Loading branch information
Showing
29 changed files
with
818 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// adminCmd represents the admin command | ||
var adminCmd = &cobra.Command{ | ||
Use: "admin", | ||
Short: "Manage the Admin users", | ||
Long: `Manage the Admin users.`, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(adminCmd) | ||
} |
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,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// createCmd represents the create command | ||
var createCmd = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create an admin user", | ||
Long: `Create an admin user.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("TODO") | ||
}, | ||
} | ||
|
||
func init() { | ||
adminCmd.AddCommand(createCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// createCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
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,69 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/canonical/identity-platform-admin-ui/internal/authorization" | ||
"github.com/canonical/identity-platform-admin-ui/internal/logging" | ||
"github.com/canonical/identity-platform-admin-ui/internal/monitoring" | ||
fga "github.com/canonical/identity-platform-admin-ui/internal/openfga" | ||
"github.com/canonical/identity-platform-admin-ui/internal/tracing" | ||
"github.com/openfga/go-sdk/client" | ||
"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("fga-store-id") | ||
createModel(apiUrl, apiToken, storeId) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(createFgaModelCmd) | ||
|
||
createFgaModelCmd.Flags().String("fga-api-url", "", "The openfga API URL") | ||
createFgaModelCmd.Flags().String("fga-api-token", "", "The openfga API token") | ||
createFgaModelCmd.Flags().String("fga-store-id", "", "The openfga store to create the model in") | ||
createFgaModelCmd.MarkFlagRequired("fga-api-url") | ||
createFgaModelCmd.MarkFlagRequired("fga-api-token") | ||
createFgaModelCmd.MarkFlagRequired("fga-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) | ||
authModelReq := client.ClientWriteAuthorizationModelRequest{ | ||
TypeDefinitions: authorization.AuthModel.TypeDefinitions, | ||
SchemaVersion: authorization.AuthModel.SchemaVersion, | ||
Conditions: authorization.AuthModel.Conditions, | ||
} | ||
modelId, err := fgaClient.WriteModel(context.Background(), &authModelReq) | ||
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 Admin UI", | ||
Long: `An admin UI for the Canonical Identity Platform`, | ||
} | ||
|
||
// 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-admin-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
datastore: | ||
engine: postgres | ||
uri: "postgres://iam:[email protected]/openfga?sslmode=disable" | ||
authn: | ||
preshared: | ||
keys: | ||
- "42" |
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
Oops, something went wrong.