From b86232b587dafc69352c1e5bae9569e9a1206564 Mon Sep 17 00:00:00 2001 From: Dan Keohane Date: Wed, 16 Nov 2022 19:25:31 +0100 Subject: [PATCH] Adding minor feedback to cluster context command (#281) * Adding minor feedback to cluster context command * Add usertoken auth --- cmd/cluster/context.go | 46 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/cmd/cluster/context.go b/cmd/cluster/context.go index de7f9c2d..627b8713 100644 --- a/cmd/cluster/context.go +++ b/cmd/cluster/context.go @@ -31,7 +31,9 @@ type contextOptions struct { clusterID string baseDomain string days int + pages int oauthtoken string + usertoken string externalID string infraID string awsProfile string @@ -39,6 +41,7 @@ type contextOptions struct { const ( PagerDutyOauthTokenConfigKey = "pd_oauth_token" + PagerDutyUserTokenConfigKey = "pd_user_token" ) // newCmdContext implements the context command to show the current context of a cluster @@ -58,9 +61,11 @@ func newCmdContext() *cobra.Command { contextCmd.Flags().StringVarP(&ops.clusterID, "cluster-id", "C", "", "Cluster ID") contextCmd.Flags().StringVarP(&ops.awsProfile, "profile", "p", "", "AWS Profile") contextCmd.Flags().BoolVarP(&ops.verbose, "verbose", "", false, "Verbose output") - contextCmd.Flags().BoolVarP(&ops.full, "full", "", false, "Run full suite of checks.") - contextCmd.Flags().IntVarP(&ops.days, "days", "z", 30, "Command will display X days of Error SLs sent to the cluster. Days is set to 30 by default") - contextCmd.Flags().StringVarP(&ops.oauthtoken, "oauthtoken", "t", "", "Pass in PD oauthtoken directly. If not passed in, by default will read token from ~/.config/pagerduty-cli/config.json") + contextCmd.Flags().BoolVar(&ops.full, "full", false, "Run full suite of checks.") + contextCmd.Flags().IntVarP(&ops.days, "days", "d", 30, "Command will display X days of Error SLs sent to the cluster. Days is set to 30 by default") + contextCmd.Flags().IntVar(&ops.pages, "pages", 40, "Command will display X pages of Cloud Trail logs for the cluster. Pages is set to 40 by default") + contextCmd.Flags().StringVar(&ops.oauthtoken, "oauthtoken", "", "Pass in PD oauthtoken directly. If not passed in, by default will read `pd_oauth_token` from ~/.config/osdctl") + contextCmd.Flags().StringVar(&ops.usertoken, "usertoken", "", "Pass in PD usertoken directly. If not passed in, by default will read `pd_user_token` from ~/.config/osdctl") return contextCmd } @@ -133,7 +138,8 @@ func (o *contextOptions) run() error { err = o.printPDAlerts() if err != nil { fmt.Fprintf(os.Stderr, "Can't print pagerduty alerts: %v\n", err) - os.Exit(1) + // Here we don't actually want to error out, this is to ensure that even if we don't have the + // pd auth setup, we can still get the rest of the output. } // Print other helpful links @@ -250,7 +256,17 @@ func (o *contextOptions) printServiceLogs() error { return nil } -func GetPagerdutyClient(oauthtoken string) (*pd.Client, error) { +func getPDUserClient(usertoken string) (*pd.Client, error) { + if usertoken == "" { + if !viper.IsSet(PagerDutyUserTokenConfigKey) { + return nil, fmt.Errorf("key %s is not set in config file", PagerDutyUserTokenConfigKey) + } + usertoken = viper.GetString(PagerDutyUserTokenConfigKey) + } + return pd.NewClient(usertoken), nil +} + +func getPDOauthClient(oauthtoken string) (*pd.Client, error) { if oauthtoken == "" { if !viper.IsSet(PagerDutyOauthTokenConfigKey) { return nil, fmt.Errorf("key %s is not set in config file", PagerDutyOauthTokenConfigKey) @@ -260,6 +276,20 @@ func GetPagerdutyClient(oauthtoken string) (*pd.Client, error) { return pd.NewOAuthClient(oauthtoken), nil } +func GetPagerdutyClient(usertoken string, oauthtoken string) (*pd.Client, error) { + client, err := getPDUserClient(usertoken) + if client != nil { + fmt.Print("Usering User Client") + return client, err + } + client, err = getPDOauthClient(oauthtoken) + if err != nil { + return nil, fmt.Errorf("Failed to create both user and oauth clients for pd") + } + fmt.Print("Usering Oauth Client") + return client, err +} + func getPDSeviceID(pdClient *pd.Client, ctx context.Context, baseDomain string) (string, error) { lsResponse, err := pdClient.ListServicesWithContext(ctx, pd.ListServiceOptions{Query: baseDomain}) @@ -426,7 +456,7 @@ func printHistoricalPDAlertSummary(pdClient *pd.Client, ctx context.Context, ser func (o *contextOptions) printPDAlerts() error { - pdClient, err := GetPagerdutyClient(o.oauthtoken) + pdClient, err := GetPagerdutyClient(o.usertoken, o.oauthtoken) if err != nil { fmt.Println("error getting pd client: ", err.Error()) return err @@ -475,8 +505,8 @@ func (o *contextOptions) printCloudTrailLogs() error { foundEvents := []*cloudtrail.Event{} var eventSearchInput = cloudtrail.LookupEventsInput{} - fmt.Println("Pulling and filtering the past 40 pages of Cloudtrail data") - for counter := 0; counter <= 40; counter++ { + fmt.Println("Pulling and filtering the past", o.pages, "pages of Cloudtrail data") + for counter := 0; counter <= o.pages; counter++ { print(".") cloudTrailEvents, err := awsJumpClient.LookupEvents(&eventSearchInput) if err != nil {