From 0e8de013785352ccf4cb6cab06189d3214ee1753 Mon Sep 17 00:00:00 2001 From: Luca Burgazzoli Date: Tue, 29 Nov 2022 17:41:18 +0100 Subject: [PATCH] chore(rhoc): add some config inspection commands --- rhoc/go.mod | 9 ++++++--- rhoc/go.sum | 10 ++++++---- rhoc/pkg/cmd/config/config.go | 2 ++ rhoc/pkg/cmd/config/reset/reset.go | 27 +++++++++++++++++++++++++++ rhoc/pkg/cmd/config/show/show.go | 13 +++++++++++++ rhoc/pkg/cmd/whoami/whoami.go | 29 +++++++++++++++++++++++++---- 6 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 rhoc/pkg/cmd/config/reset/reset.go diff --git a/rhoc/go.mod b/rhoc/go.mod index 52f9664..f511e03 100644 --- a/rhoc/go.mod +++ b/rhoc/go.mod @@ -12,7 +12,7 @@ require ( github.com/openshift-online/ocm-cli v0.1.60 github.com/openshift-online/ocm-sdk-go v0.1.287 github.com/pkg/errors v0.9.1 - github.com/redhat-developer/app-services-cli v0.51.3 + github.com/redhat-developer/app-services-cli v0.51.7 github.com/spf13/cobra v1.5.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.8.1 @@ -29,7 +29,10 @@ require ( github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4 ) -require github.com/golang-jwt/jwt v3.2.2+incompatible // indirect +require ( + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0 // indirect +) require ( github.com/BurntSushi/toml v1.2.0 // indirect @@ -108,7 +111,7 @@ require ( github.com/redhat-developer/app-services-sdk-go/connectormgmt v0.8.0 // indirect github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.9.0 // indirect github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.13.0 // indirect - github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.1 // indirect + github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2 // indirect github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/segmentio/ksuid v1.0.3 // indirect diff --git a/rhoc/go.sum b/rhoc/go.sum index 97927a2..00ff7b0 100644 --- a/rhoc/go.sum +++ b/rhoc/go.sum @@ -766,8 +766,8 @@ github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0 github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= -github.com/redhat-developer/app-services-cli v0.51.3 h1:Lcyp8XCQou7y+ehH430sPYM0iHMEkgJDocl0+2dAT5g= -github.com/redhat-developer/app-services-cli v0.51.3/go.mod h1:ymaEMnece1pgH8dW5iWtcaXvjcFSOogT6lTjJbWNRbs= +github.com/redhat-developer/app-services-cli v0.51.7 h1:iWorpI9P8l7NpjRrY4nno/sPO6M48hNUsFKV0zwmCuk= +github.com/redhat-developer/app-services-cli v0.51.7/go.mod h1:gDm1pe3MnII8woC8k0Zmy5Wb/TSLcTLVZELTrhp86PM= github.com/redhat-developer/app-services-sdk-go v0.10.0/go.mod h1:enn8Zz6IT0HZYzS6LSttiME2apwnvfVWZnGRS81A4rk= github.com/redhat-developer/app-services-sdk-go v0.11.0 h1:4r5YKDW2LN2oa51PXrYidByiWfTXB9Z+1YqjtnPaBZ0= github.com/redhat-developer/app-services-sdk-go v0.11.0/go.mod h1:XAuy1xpY6OxnZ2VhJFTM0HM0WMJ0R31AFRFDvYcvkTE= @@ -779,10 +779,12 @@ github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.9.0 h1:AEjtq7k3 github.com/redhat-developer/app-services-sdk-go/kafkainstance v0.9.0/go.mod h1:yazwUm4IHuIWrQ0CCsqN0h7rHZx51nlFbYWKnUn7B84= github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.13.0 h1:aSuONBf3znnotUX7ywLh6xvOVVFsPRIUCfsEBWTWEM0= github.com/redhat-developer/app-services-sdk-go/kafkamgmt v0.13.0/go.mod h1:ILvcakLEXMLZyRdO//WJZNk9fdFbnU+cM3XrBvubE64= -github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.1 h1:S2nGyknlqS1vibgI8ya5KWch/bbxm5QkwWasVSrQhz0= -github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.1/go.mod h1:HkNzOWHTW/SomobQ4343+yR4oTmiyvm85BIWlsh0qbA= +github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2 h1:U2je87d/DIeOaQIycg2Y7TLiESmGu0/0rQC5n64Od0Y= +github.com/redhat-developer/app-services-sdk-go/registryinstance v0.8.2/go.mod h1:HkNzOWHTW/SomobQ4343+yR4oTmiyvm85BIWlsh0qbA= github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0 h1:9kqTCGoz5jR6VE+cgz8fl5Zt9Ctctg9GN1XlkzheVQw= github.com/redhat-developer/app-services-sdk-go/registrymgmt v0.10.0/go.mod h1:UoxuqkUN+g5Ni8zgsCA7zidR5s774m9fqhZ5o4eOSIM= +github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0 h1:kMvH66RXnxrF7FKraWu7n1BnaWrCUchw2unYa9rl/IM= +github.com/redhat-developer/app-services-sdk-go/serviceaccountmgmt v0.9.0/go.mod h1:kpEKXWqyD6GUiQjBHCGzp/LIbCBfkTWpPo4VqkQ9zq4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= diff --git a/rhoc/pkg/cmd/config/config.go b/rhoc/pkg/cmd/config/config.go index b0820c0..f940f5a 100644 --- a/rhoc/pkg/cmd/config/config.go +++ b/rhoc/pkg/cmd/config/config.go @@ -2,6 +2,7 @@ package config import ( "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/cmd/config/context" + "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/cmd/config/reset" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/cmd/config/show" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/cmd/config/token" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" @@ -24,6 +25,7 @@ func NewConfigCommand(f *factory.Factory) *cobra.Command { } cmd.AddCommand(show.NewConfigShowCommand(f)) + cmd.AddCommand(reset.NewConfigResetCommand(f)) cmd.AddCommand(token.NewConfigTokenCommand(f)) cmd.AddCommand(context.NewContextCommand(f)) diff --git a/rhoc/pkg/cmd/config/reset/reset.go b/rhoc/pkg/cmd/config/reset/reset.go new file mode 100644 index 0000000..6a26a95 --- /dev/null +++ b/rhoc/pkg/cmd/config/reset/reset.go @@ -0,0 +1,27 @@ +package reset + +import ( + "github.com/redhat-developer/app-services-cli/pkg/shared/factory" + "github.com/spf13/cobra" +) + +func NewConfigResetCommand(f *factory.Factory) *cobra.Command { + + cmd := &cobra.Command{ + Use: "reset", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + if err := f.Config.Remove(); err != nil { + return err + } + + if err := f.ServiceContext.Remove(); err != nil { + return err + } + + return nil + }, + } + + return cmd +} diff --git a/rhoc/pkg/cmd/config/show/show.go b/rhoc/pkg/cmd/config/show/show.go index 8d15779..bdffa7a 100644 --- a/rhoc/pkg/cmd/config/show/show.go +++ b/rhoc/pkg/cmd/config/show/show.go @@ -12,6 +12,7 @@ type keyVal struct { } func NewConfigShowCommand(f *factory.Factory) *cobra.Command { + cmd := &cobra.Command{ Use: "show", Args: cobra.NoArgs, @@ -40,7 +41,19 @@ func NewConfigShowCommand(f *factory.Factory) *cobra.Command { }, } + configLocation, err := f.Config.Location() + if err != nil { + return err + } + + contextLocation, err := f.ServiceContext.Location() + if err != nil { + return err + } + dumper.DumpTable(config, f.IOStreams.Out, []keyVal{ + {key: "Config Path", val: configLocation}, + {key: "Context Path", val: contextLocation}, {key: "API URL", val: c.APIUrl}, {key: "Auth URL", val: c.AuthURL}, }) diff --git a/rhoc/pkg/cmd/whoami/whoami.go b/rhoc/pkg/cmd/whoami/whoami.go index 24db5e4..1551cb2 100644 --- a/rhoc/pkg/cmd/whoami/whoami.go +++ b/rhoc/pkg/cmd/whoami/whoami.go @@ -2,6 +2,7 @@ package whoami import ( "fmt" + "strings" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/service" "github.com/redhat-developer/app-services-cli/pkg/core/auth/token" @@ -21,13 +22,33 @@ func NewWhoAmICommand(f *factory.Factory) *cobra.Command { return err } - userName, ok := token.GetUsername(a.GetConfig().AccessToken) + if a.GetConfig().AccessToken == "" { + return nil + } + + accessTkn, err := token.Parse(a.GetConfig().AccessToken) + if err != nil { + return err + } + + tknClaims, _ := token.MapClaims(accessTkn) + + realms, ok := tknClaims["realm_access"].(map[string]interface{}) if !ok { - userName = "unknown" + return nil } + roles, ok := realms["roles"].([]interface{}) + if !ok { + return nil + } + + _, _ = fmt.Fprintln(f.IOStreams.Out, "roles:") + for i := range roles { + role := roles[i].(string) - if ok { - _, _ = fmt.Fprintf(f.IOStreams.Out, "%s@%s\n", userName, a.GetConfig().ApiURL.String()) + if strings.HasPrefix(role, "cos-fleet-manager-") { + _, _ = fmt.Fprintf(f.IOStreams.Out, " %s\n", role) + } } return nil