From 952a9e0b842c99404bb9d156698186096fd91ace Mon Sep 17 00:00:00 2001 From: Philip Zingmark Date: Thu, 19 Sep 2024 20:38:28 +0200 Subject: [PATCH] added a command to test sm authentication --- cmd/cli_compose.go | 11 ++++++++ pkg/v1/commands/compose/storage/check.go | 33 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 pkg/v1/commands/compose/storage/check.go diff --git a/cmd/cli_compose.go b/cmd/cli_compose.go index 9ae284f..d1e16a4 100644 --- a/cmd/cli_compose.go +++ b/cmd/cli_compose.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/Phillezi/kthcloud-cli/pkg/v1/commands/compose" + "github.com/Phillezi/kthcloud-cli/pkg/v1/commands/compose/storage" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -35,6 +36,15 @@ var composeDownCmd = &cobra.Command{ }, } +var testSMAuthCmd = &cobra.Command{ + Use: "sm check", + Short: "Test authentication against storage manager", + Hidden: true, + Run: func(cmd *cobra.Command, args []string) { + storage.Check() + }, +} + func init() { composeUpCmd.Flags().BoolP("try-volumes", "", false, "Try uploading local files and dirs that should be mounted on the deployment.\nIf enabled it will \"steal\" cookies from your browser to authenticate.") composeUpCmd.Flags().BoolP("detached", "d", false, "doesn't do anything, just here for parity with Docker Compose up") @@ -44,6 +54,7 @@ func init() { composeCmd.AddCommand(composeParseCmd) composeCmd.AddCommand(composeUpCmd) composeCmd.AddCommand(composeDownCmd) + composeCmd.AddCommand(testSMAuthCmd) // Register the compose command in root rootCmd.AddCommand(composeCmd) diff --git a/pkg/v1/commands/compose/storage/check.go b/pkg/v1/commands/compose/storage/check.go new file mode 100644 index 0000000..4c5c6d6 --- /dev/null +++ b/pkg/v1/commands/compose/storage/check.go @@ -0,0 +1,33 @@ +package storage + +import ( + "github.com/Phillezi/kthcloud-cli/pkg/v1/auth/client" + storageclient "github.com/Phillezi/kthcloud-cli/pkg/v1/auth/storage-client" + "github.com/sirupsen/logrus" + "github.com/spf13/viper" +) + +func Check() { + c := client.Get() + if !c.HasValidSession() { + logrus.Fatal("not logged in") + } + user, err := c.User() + if err != nil { + logrus.Fatal(err) + } + if user.StorageURL == nil { + logrus.Fatal("user doesnt have storageurl") + } + if c.StorageClient == nil { + c.StorageClient = storageclient.GetInstance(*user.StorageURL, viper.GetString("keycloak-host")) + } + isAuth, err := c.StorageClient.Auth() + if err != nil { + logrus.Fatal(err) + } + if !isAuth { + logrus.Fatal("not authenticated on storage url" + *user.StorageURL) + } + logrus.Infoln("Passed :)") +}