Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: fixes missing expiry during update #341 #343

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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, secret, apiProducts, scopes, attrs)
srinandan marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -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)
}

Expand Down
21 changes: 10 additions & 11 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,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)
apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting())
apiclient.ClientPrintHttpResponse.Set(true)
srinandan marked this conversation as resolved.
Show resolved Hide resolved
respBody, err = UpdateKeyProducts(developerEmail, appID, consumerKey, apiProducts, scopes)
}

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

key := []string{}
Expand All @@ -100,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 {
Expand All @@ -127,12 +122,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