Skip to content

Commit

Permalink
bug: fixes missing expiry during update #341 (#343)
Browse files Browse the repository at this point in the history
* bug: fixes missing expiry during update #341

* expires in seconds consistently and for keys create and apps genkey only

* Setting expires consistently for seconds or Apps and AppGroup Apps

* minor linting fix

* Properly reset client response setting

---------

Co-authored-by: Kurt Kanaskie <[email protected]>
  • Loading branch information
srinandan and kurtkanaskie authored Dec 14, 2023
1 parent 2c7312c commit 4ab7176
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 44 deletions.
17 changes: 10 additions & 7 deletions cmd/appgroups/createkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ 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
)
var secret string

func init() {
CreateKeyCmd.Flags().StringVarP(&name, "name", "n",
Expand All @@ -55,8 +57,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",
Expand All @@ -66,4 +68,5 @@ func init() {

_ = CreateKeyCmd.MarkFlagRequired("name")
_ = CreateKeyCmd.MarkFlagRequired("app-name")
_ = CreateKeyCmd.MarkFlagRequired("prods")
}
12 changes: 11 additions & 1 deletion cmd/appgroups/crtapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package appgroups

import (
"fmt"
"strconv"

"internal/apiclient"

"internal/client/appgroups"
Expand All @@ -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
},
Expand All @@ -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",
Expand All @@ -59,4 +68,5 @@ func init() {

_ = CreateAppCmd.MarkFlagRequired("name")
_ = CreateAppCmd.MarkFlagRequired("app-name")
_ = CreateAppCmd.MarkFlagRequired("prods")
}
11 changes: 10 additions & 1 deletion cmd/appgroups/updateapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package appgroups

import (
"fmt"
"strconv"

"internal/apiclient"

"internal/client/appgroups"
Expand All @@ -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
},
Expand All @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions cmd/apps/createkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down
11 changes: 10 additions & 1 deletion cmd/apps/crtapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package apps

import (
"fmt"
"strconv"

"internal/apiclient"

"internal/client/apps"
Expand All @@ -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
},
Expand All @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions cmd/apps/genkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package apps

import (
"fmt"
"strconv"

"internal/apiclient"

"internal/client/apps"
Expand All @@ -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
},
Expand All @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions cmd/apps/updateapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package apps

import (
"fmt"
"strconv"

"internal/apiclient"

"internal/client/apps"
Expand All @@ -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
},
Expand Down
13 changes: 1 addition & 12 deletions cmd/apps/updatekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package apps

import (
"fmt"
"strconv"

"internal/apiclient"

"internal/client/apps"
Expand All @@ -34,30 +31,22 @@ 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, apiProducts, scopes, attrs)
return
},
}

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")
}
2 changes: 1 addition & 1 deletion internal/apiclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,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)
}

Expand Down
8 changes: 6 additions & 2 deletions internal/client/appgroups/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -60,9 +64,9 @@ 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)
respBody, err = UpdateKeyProducts(name, appName, consumerKey, apiProducts)
// restore client output setting
apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
respBody, err = UpdateKeyProducts(name, appName, consumerKey, apiProducts)
}

return respBody, err
Expand Down
24 changes: 10 additions & 14 deletions internal/client/apps/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -62,9 +61,9 @@ 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)
// restore client output setting
apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts, scopes)
}

return respBody, err
Expand All @@ -87,7 +86,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,
apiProducts []string, scopes []string, attrs map[string]string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)

key := []string{}
Expand All @@ -100,10 +100,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 {
Expand All @@ -115,10 +111,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)
Expand All @@ -127,12 +119,16 @@ 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, scopes []string) (respBody []byte, err error) {
u, _ := url.Parse(apiclient.BaseURL)

key := []string{}
key = append(key, "\"apiProducts\":[\""+getArrayStr(apiProducts)+"\"]")

if len(scopes) > 0 {
key = append(key, "\"scopes\":[\""+getArrayStr(scopes)+"\"]")
}

payload := "{" + strings.Join(key, ",") + "}"

u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", developerEmail, "apps", appID, "keys", consumerKey)
Expand Down

0 comments on commit 4ab7176

Please sign in to comment.