From c88639f5a46451fd36177b8f5a23aa196867be3f Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Sat, 2 Dec 2023 11:16:16 -0800 Subject: [PATCH 1/5] bug: fixes missing expiry during update #341 --- internal/apiclient/httpclient.go | 2 +- internal/client/apps/keys.go | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/apiclient/httpclient.go b/internal/apiclient/httpclient.go index d9cea7771..19e73d8c7 100644 --- a/internal/apiclient/httpclient.go +++ b/internal/apiclient/httpclient.go @@ -451,7 +451,7 @@ func handleResponse(resp *http.Response) (respBody []byte, err error) { clilog.HttpError.Println(string(respBody)) return nil, errors.New(getErrorMessage(resp.StatusCode)) } - + clilog.Debug.Println("Response: ", string(respBody)) return respBody, PrettyPrint(resp.Header.Get("Content-Type"), respBody) } diff --git a/internal/client/apps/keys.go b/internal/client/apps/keys.go index 4ab8d6f98..d000bb9d6 100644 --- a/internal/client/apps/keys.go +++ b/internal/client/apps/keys.go @@ -63,7 +63,7 @@ func CreateKey(developerEmail string, appID string, consumerKey string, consumer // since the API does not support adding products when creating a key, use a second API call to add products if len(apiProducts) > 0 { apiclient.ClientPrintHttpResponse.Set(false) - respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts) + respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts, expires) apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) } @@ -87,7 +87,8 @@ func GetKey(developerEmail string, appID string, key string) (respBody []byte, e } // UpdateKey -func UpdateKey(developerEmail string, appID string, consumerKey string, consumerSecret string, apiProducts []string, scopes []string, expires string, attrs map[string]string) (respBody []byte, err error) { +func UpdateKey(developerEmail string, appID string, consumerKey string, consumerSecret string, + apiProducts []string, scopes []string, expires string, attrs map[string]string) (respBody []byte, err error) { u, _ := url.Parse(apiclient.BaseURL) key := []string{} @@ -127,11 +128,14 @@ func UpdateKey(developerEmail string, appID string, consumerKey string, consumer return respBody, err } -func UpdateKeyProducts(developerEmail string, appID string, consumerKey string, apiProducts []string) (respBody []byte, err error) { +func UpdateKeyProducts(developerEmail string, appID string, consumerKey string, apiProducts []string, expires string) (respBody []byte, err error) { u, _ := url.Parse(apiclient.BaseURL) key := []string{} key = append(key, "\"apiProducts\":[\""+getArrayStr(apiProducts)+"\"]") + if expires != "" { + key = append(key, "\"expiresAt\":\""+expires+"\"") + } payload := "{" + strings.Join(key, ",") + "}" From 685dd23c5b31fe0ba17199c3e4c5243f83c239dc Mon Sep 17 00:00:00 2001 From: Kurt Kanaskie Date: Wed, 13 Dec 2023 17:16:37 -0500 Subject: [PATCH 2/5] expires in seconds consistently and for keys create and apps genkey only --- cmd/apps/createkey.go | 8 +++++--- cmd/apps/genkey.go | 13 +++++++++++-- cmd/apps/updatekey.go | 13 +------------ internal/client/apps/keys.go | 21 ++++++++------------- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/cmd/apps/createkey.go b/cmd/apps/createkey.go index 2de6e0d13..281fdee4c 100644 --- a/cmd/apps/createkey.go +++ b/cmd/apps/createkey.go @@ -34,8 +34,10 @@ var CreateKeyCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { - if _, err = strconv.Atoi(expires); err != nil { - return fmt.Errorf("expires must be an integer: %v", err) + if expires != "" { + if _, err = strconv.Atoi(expires); err != nil { + return fmt.Errorf("expires must be an integer: %v", err) + } } _, err = apps.CreateKey(developerEmail, name, key, secret, apiProducts, scopes, expires, attrs) return @@ -52,7 +54,7 @@ func init() { CreateKeyCmd.Flags().StringArrayVarP(&scopes, "scopes", "s", []string{}, "OAuth scopes") CreateKeyCmd.Flags().StringVarP(&expires, "expires", "x", - "", "A setting, in milliseconds, for the lifetime of the consumer key") + "", "A setting, in seconds, for the lifetime of the consumer key") CreateKeyCmd.Flags().StringToStringVar(&attrs, "attrs", nil, "Custom attributes") diff --git a/cmd/apps/genkey.go b/cmd/apps/genkey.go index 6574fb672..924d4a271 100644 --- a/cmd/apps/genkey.go +++ b/cmd/apps/genkey.go @@ -15,6 +15,9 @@ package apps import ( + "fmt" + "strconv" + "internal/apiclient" "internal/client/apps" @@ -31,6 +34,12 @@ var GenKeyCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { + if expires != "" { + if _, err = strconv.Atoi(expires); err != nil { + return fmt.Errorf("expires must be an integer: %v", err) + } + expires += "000" + } _, err = apps.GenerateKey(name, devID, apiProducts, callback, expires, scopes) return }, @@ -42,9 +51,9 @@ func init() { GenKeyCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the developer app") GenKeyCmd.Flags().StringVarP(&devID, "devid", "d", - "", "Developer Id") + "", "The developer's id or email") GenKeyCmd.Flags().StringVarP(&expires, "expires", "x", - "", "A setting, in milliseconds, for the lifetime of the consumer key") + "", "A setting, in seconds, for the lifetime of the consumer key") GenKeyCmd.Flags().StringVarP(&callback, "callback", "c", "", "The callbackUrl is used by OAuth") GenKeyCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p", diff --git a/cmd/apps/updatekey.go b/cmd/apps/updatekey.go index 4e377b2d1..110f4b8e3 100644 --- a/cmd/apps/updatekey.go +++ b/cmd/apps/updatekey.go @@ -15,9 +15,6 @@ package apps import ( - "fmt" - "strconv" - "internal/apiclient" "internal/client/apps" @@ -34,10 +31,7 @@ var UpdateKeyCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { - if _, err = strconv.Atoi(expires); err != nil { - return fmt.Errorf("expires must be an integer: %v", err) - } - _, err = apps.UpdateKey(developerEmail, name, key, secret, apiProducts, scopes, expires, attrs) + _, err = apps.UpdateKey(developerEmail, name, key, secret, apiProducts, scopes, attrs) return }, } @@ -45,19 +39,14 @@ var UpdateKeyCmd = &cobra.Command{ func init() { UpdateKeyCmd.Flags().StringVarP(&key, "key", "k", "", "Developer app consumer key") - UpdateKeyCmd.Flags().StringVarP(&secret, "secret", "r", - "", "Developer app consumer secret") UpdateKeyCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p", []string{}, "A list of api products") UpdateKeyCmd.Flags().StringArrayVarP(&scopes, "scopes", "s", []string{}, "OAuth scopes") - UpdateKeyCmd.Flags().StringVarP(&expires, "expires", "x", - "", "A setting, in milliseconds, for the lifetime of the consumer key") UpdateKeyCmd.Flags().StringToStringVar(&attrs, "attrs", nil, "Custom attributes") _ = UpdateKeyCmd.MarkFlagRequired("name") _ = UpdateKeyCmd.MarkFlagRequired("key") - _ = UpdateKeyCmd.MarkFlagRequired("secret") _ = UpdateKeyCmd.MarkFlagRequired("prods") } diff --git a/internal/client/apps/keys.go b/internal/client/apps/keys.go index d000bb9d6..8427d116a 100644 --- a/internal/client/apps/keys.go +++ b/internal/client/apps/keys.go @@ -44,11 +44,10 @@ func CreateKey(developerEmail string, appID string, consumerKey string, consumer key = append(key, "\"consumerSecret\":\""+consumerSecret+"\"") if expires != "" { - key = append(key, "\"expiresAt\":\""+expires+"\"") + key = append(key, "\"expiresInSeconds\":\""+expires+"\"") } payload := "{" + strings.Join(key, ",") + "}" - u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", developerEmail, "apps", appID, "keys") if len(apiProducts) > 0 { @@ -62,9 +61,8 @@ func CreateKey(developerEmail string, appID string, consumerKey string, consumer // since the API does not support adding products when creating a key, use a second API call to add products if len(apiProducts) > 0 { - apiclient.ClientPrintHttpResponse.Set(false) - respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts, expires) - apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) + apiclient.ClientPrintHttpResponse.Set(true) + respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts, scopes) } return respBody, err @@ -88,7 +86,7 @@ func GetKey(developerEmail string, appID string, key string) (respBody []byte, e // UpdateKey func UpdateKey(developerEmail string, appID string, consumerKey string, consumerSecret string, - apiProducts []string, scopes []string, expires string, attrs map[string]string) (respBody []byte, err error) { + apiProducts []string, scopes []string, attrs map[string]string) (respBody []byte, err error) { u, _ := url.Parse(apiclient.BaseURL) key := []string{} @@ -101,10 +99,6 @@ func UpdateKey(developerEmail string, appID string, consumerKey string, consumer key = append(key, "\"scopes\":[\""+getArrayStr(scopes)+"\"]") } - if expires != "" { - key = append(key, "\"expiresAt\":\""+expires+"\"") - } - if len(attrs) > 0 { attributes := []string{} for keyattr, value := range attrs { @@ -128,13 +122,14 @@ func UpdateKey(developerEmail string, appID string, consumerKey string, consumer return respBody, err } -func UpdateKeyProducts(developerEmail string, appID string, consumerKey string, apiProducts []string, expires string) (respBody []byte, err error) { +func UpdateKeyProducts(developerEmail string, appID string, consumerKey string, apiProducts []string, scopes []string) (respBody []byte, err error) { u, _ := url.Parse(apiclient.BaseURL) key := []string{} key = append(key, "\"apiProducts\":[\""+getArrayStr(apiProducts)+"\"]") - if expires != "" { - key = append(key, "\"expiresAt\":\""+expires+"\"") + + if len(scopes) > 0 { + key = append(key, "\"scopes\":[\""+getArrayStr(scopes)+"\"]") } payload := "{" + strings.Join(key, ",") + "}" From 46bad6d97d26060bce555a54cb1facf88b8bc604 Mon Sep 17 00:00:00 2001 From: Kurt Kanaskie Date: Thu, 14 Dec 2023 11:59:58 -0500 Subject: [PATCH 3/5] Setting expires consistently for seconds or Apps and AppGroup Apps --- cmd/appgroups/createkey.go | 15 ++++++++++----- cmd/appgroups/crtapp.go | 12 +++++++++++- cmd/appgroups/updateapp.go | 11 ++++++++++- cmd/apps/crtapp.go | 11 ++++++++++- cmd/apps/updateapp.go | 9 +++++++++ cmd/apps/updatekey.go | 2 +- internal/client/appgroups/keys.go | 7 +++++-- internal/client/apps/keys.go | 6 +----- 8 files changed, 57 insertions(+), 16 deletions(-) diff --git a/cmd/appgroups/createkey.go b/cmd/appgroups/createkey.go index ebd66ab7e..c4b1b6aff 100644 --- a/cmd/appgroups/createkey.go +++ b/cmd/appgroups/createkey.go @@ -36,14 +36,18 @@ var CreateKeyCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { - _, err = appgroups.CreateKey(name, appName, key, secret, strconv.Itoa(expiry), apiProducts, scopes, attrs) + if expires != "" { + if _, err = strconv.Atoi(expires); err != nil { + return fmt.Errorf("expires must be an integer: %v", err) + } + } + _, err = appgroups.CreateKey(name, appName, key, secret, expires, apiProducts, scopes, attrs) return }, } var ( - secret string - expiry int + secret string ) func init() { @@ -55,8 +59,8 @@ func init() { "", "Import an existing AppGroup app consumer key") CreateKeyCmd.Flags().StringVarP(&secret, "secret", "r", "", "Import an existing AppGroup app consumer secret") - CreateKeyCmd.Flags().IntVarP(&expiry, "expiry", "x", - -1, "Expiration time, in seconds, for the consumer key") + CreateKeyCmd.Flags().StringVarP(&expires, "expires", "x", + "", "A setting, in seconds, for the lifetime of the consumer key") CreateKeyCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p", []string{}, "A list of api products") CreateKeyCmd.Flags().StringArrayVarP(&scopes, "scopes", "s", @@ -66,4 +70,5 @@ func init() { _ = CreateKeyCmd.MarkFlagRequired("name") _ = CreateKeyCmd.MarkFlagRequired("app-name") + _ = CreateKeyCmd.MarkFlagRequired("prods") } diff --git a/cmd/appgroups/crtapp.go b/cmd/appgroups/crtapp.go index 724617d14..87c87ae2a 100644 --- a/cmd/appgroups/crtapp.go +++ b/cmd/appgroups/crtapp.go @@ -15,6 +15,9 @@ package appgroups import ( + "fmt" + "strconv" + "internal/apiclient" "internal/client/appgroups" @@ -31,6 +34,12 @@ var CreateAppCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { + if expires != "" { + if _, err = strconv.Atoi(expires); err != nil { + return fmt.Errorf("expires must be an integer: %v", err) + } + expires += "000" + } _, err = appgroups.CreateApp(name, appName, expires, callback, apiProducts, scopes, attrs) return }, @@ -47,7 +56,7 @@ func init() { CreateAppCmd.Flags().StringVarP(&appName, "app-name", "", "", "Name of the app") CreateAppCmd.Flags().StringVarP(&expires, "expires", "x", - "", "A setting, in milliseconds, for the lifetime of the consumer key") + "", "A setting, in seconds, for the lifetime of the consumer key") CreateAppCmd.Flags().StringVarP(&callback, "callback", "c", "", "The callbackUrl is used by OAuth") CreateAppCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p", @@ -59,4 +68,5 @@ func init() { _ = CreateAppCmd.MarkFlagRequired("name") _ = CreateAppCmd.MarkFlagRequired("app-name") + _ = CreateAppCmd.MarkFlagRequired("prods") } diff --git a/cmd/appgroups/updateapp.go b/cmd/appgroups/updateapp.go index f393bf2e4..8083f78d7 100644 --- a/cmd/appgroups/updateapp.go +++ b/cmd/appgroups/updateapp.go @@ -15,6 +15,9 @@ package appgroups import ( + "fmt" + "strconv" + "internal/apiclient" "internal/client/appgroups" @@ -31,6 +34,12 @@ var UpdateAppCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { + if expires != "" { + if _, err = strconv.Atoi(expires); err != nil { + return fmt.Errorf("expires must be an integer: %v", err) + } + expires += "000" + } _, err = appgroups.UpdateApp(name, appName, expires, callback, apiProducts, scopes, attrs) return }, @@ -42,7 +51,7 @@ func init() { UpdateAppCmd.Flags().StringVarP(&appName, "app-name", "", "", "Name of the app") UpdateAppCmd.Flags().StringVarP(&expires, "expires", "x", - "", "A setting, in milliseconds, for the lifetime of the consumer key") + "", "A setting, in seconds, for the lifetime of the consumer key") UpdateAppCmd.Flags().StringVarP(&callback, "callback", "c", "", "The callbackUrl is used by OAuth") UpdateAppCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p", diff --git a/cmd/apps/crtapp.go b/cmd/apps/crtapp.go index f3f71bd99..e6761db8d 100644 --- a/cmd/apps/crtapp.go +++ b/cmd/apps/crtapp.go @@ -15,6 +15,9 @@ package apps import ( + "fmt" + "strconv" + "internal/apiclient" "internal/client/apps" @@ -31,6 +34,12 @@ var CreateCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { + if expires != "" { + if _, err = strconv.Atoi(expires); err != nil { + return fmt.Errorf("expires must be an integer: %v", err) + } + expires += "000" + } _, err = apps.Create(name, email, expires, callback, apiProducts, scopes, attrs) return }, @@ -48,7 +57,7 @@ func init() { CreateCmd.Flags().StringVarP(&email, "email", "e", "", "The developer's email or id") CreateCmd.Flags().StringVarP(&expires, "expires", "x", - "", "A setting, in milliseconds, for the lifetime of the consumer key") + "", "A setting, in seconds, for the lifetime of the consumer key") CreateCmd.Flags().StringVarP(&callback, "callback", "c", "", "The callbackUrl is used by OAuth") CreateCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p", diff --git a/cmd/apps/updateapp.go b/cmd/apps/updateapp.go index f8cc750b2..75e68eb21 100644 --- a/cmd/apps/updateapp.go +++ b/cmd/apps/updateapp.go @@ -15,6 +15,9 @@ package apps import ( + "fmt" + "strconv" + "internal/apiclient" "internal/client/apps" @@ -31,6 +34,12 @@ var UpdateCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { + if expires != "" { + if _, err = strconv.Atoi(expires); err != nil { + return fmt.Errorf("expires must be an integer: %v", err) + } + expires += "000" + } _, err = apps.Update(name, email, expires, callback, apiProducts, scopes, attrs) return }, diff --git a/cmd/apps/updatekey.go b/cmd/apps/updatekey.go index 110f4b8e3..791563fd6 100644 --- a/cmd/apps/updatekey.go +++ b/cmd/apps/updatekey.go @@ -31,7 +31,7 @@ var UpdateKeyCmd = &cobra.Command{ return apiclient.SetApigeeOrg(org) }, RunE: func(cmd *cobra.Command, args []string) (err error) { - _, err = apps.UpdateKey(developerEmail, name, key, secret, apiProducts, scopes, attrs) + _, err = apps.UpdateKey(developerEmail, name, key, apiProducts, scopes, attrs) return }, } diff --git a/internal/client/appgroups/keys.go b/internal/client/appgroups/keys.go index cfd2004ed..228f04cd1 100644 --- a/internal/client/appgroups/keys.go +++ b/internal/client/appgroups/keys.go @@ -45,6 +45,10 @@ func CreateKey(name string, appName string, consumerKey string, consumerSecret s key = append(key, "\"consumerSecret\":\""+consumerSecret+"\"") } + if expiresInSeconds != "" { + key = append(key, "\"expiresInSeconds\":\""+expiresInSeconds+"\"") + } + payload := "{" + strings.Join(key, ",") + "}" u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "appgroups", name, "apps", appName, "keys") @@ -60,9 +64,8 @@ func CreateKey(name string, appName string, consumerKey string, consumerSecret s // since the API does not support adding products when creating a key, use a second API call to add products if len(apiProducts) > 0 { - apiclient.ClientPrintHttpResponse.Set(false) + apiclient.ClientPrintHttpResponse.Set(true) respBody, err = UpdateKeyProducts(name, appName, consumerKey, apiProducts) - apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) } return respBody, err diff --git a/internal/client/apps/keys.go b/internal/client/apps/keys.go index 8427d116a..4d1ee4341 100644 --- a/internal/client/apps/keys.go +++ b/internal/client/apps/keys.go @@ -85,7 +85,7 @@ func GetKey(developerEmail string, appID string, key string) (respBody []byte, e } // UpdateKey -func UpdateKey(developerEmail string, appID string, consumerKey string, consumerSecret string, +func UpdateKey(developerEmail string, appID string, consumerKey string, apiProducts []string, scopes []string, attrs map[string]string) (respBody []byte, err error) { u, _ := url.Parse(apiclient.BaseURL) @@ -110,10 +110,6 @@ func UpdateKey(developerEmail string, appID string, consumerKey string, consumer key = append(key, "\"consumerKey\":\""+consumerKey+"\"") - if consumerSecret != "" { - key = append(key, "\"consumerSecret\":\""+consumerSecret+"\"") - } - payload := "{" + strings.Join(key, ",") + "}" u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", developerEmail, "apps", appID, "keys", consumerKey) From 73f9d08d74dae396ca98ba5ec762a2850c49e325 Mon Sep 17 00:00:00 2001 From: Kurt Kanaskie Date: Thu, 14 Dec 2023 12:04:21 -0500 Subject: [PATCH 4/5] minor linting fix --- cmd/appgroups/createkey.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/appgroups/createkey.go b/cmd/appgroups/createkey.go index c4b1b6aff..47b561716 100644 --- a/cmd/appgroups/createkey.go +++ b/cmd/appgroups/createkey.go @@ -46,9 +46,7 @@ var CreateKeyCmd = &cobra.Command{ }, } -var ( - secret string -) +var secret string func init() { CreateKeyCmd.Flags().StringVarP(&name, "name", "n", From 8af20cc4bbbd450862ff830137581113df7fbbb2 Mon Sep 17 00:00:00 2001 From: Kurt Kanaskie Date: Thu, 14 Dec 2023 14:39:31 -0500 Subject: [PATCH 5/5] Properly reset client response setting --- internal/client/appgroups/keys.go | 3 ++- internal/client/apps/keys.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/client/appgroups/keys.go b/internal/client/appgroups/keys.go index 228f04cd1..99a29f9b4 100644 --- a/internal/client/appgroups/keys.go +++ b/internal/client/appgroups/keys.go @@ -64,7 +64,8 @@ func CreateKey(name string, appName string, consumerKey string, consumerSecret s // since the API does not support adding products when creating a key, use a second API call to add products if len(apiProducts) > 0 { - apiclient.ClientPrintHttpResponse.Set(true) + // restore client output setting + apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) respBody, err = UpdateKeyProducts(name, appName, consumerKey, apiProducts) } diff --git a/internal/client/apps/keys.go b/internal/client/apps/keys.go index 4d1ee4341..b63e7a852 100644 --- a/internal/client/apps/keys.go +++ b/internal/client/apps/keys.go @@ -61,7 +61,8 @@ func CreateKey(developerEmail string, appID string, consumerKey string, consumer // since the API does not support adding products when creating a key, use a second API call to add products if len(apiProducts) > 0 { - apiclient.ClientPrintHttpResponse.Set(true) + // restore client output setting + apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts, scopes) }