From e056395bc944cdb9a9823cc38bd76a247cec07ee Mon Sep 17 00:00:00 2001 From: Kevin Li Date: Fri, 28 Feb 2025 16:17:11 -0800 Subject: [PATCH 1/4] Adding changes to reflect changes to Cloudstack that enforce POST and timestamps --- cmd/network.go | 15 ++++++++++++--- config/config.go | 5 +++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cmd/network.go b/cmd/network.go index b5f4bef..ff61bff 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -204,6 +204,10 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str } } params.Add("response", "json") + params.Add("signatureversion", "3") + expirationTime := time.Now().UTC().Add(15 * time.Minute) + expirationStr := expirationTime.Format("2006-01-02T15:04:05Z") + params.Add("expires", expirationStr) var encodedParams string var err error @@ -220,8 +224,13 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str mac := hmac.New(sha1.New, []byte(secretKey)) mac.Write([]byte(strings.ToLower(encodedParams))) signature := base64.StdEncoding.EncodeToString(mac.Sum(nil)) - encodedParams = encodedParams + fmt.Sprintf("&signature=%s", url.QueryEscape(signature)) - params = nil + if r.Config.Core.PostRequest { + params.Add("signature", signature) + } else { + encodedParams = encodedParams + fmt.Sprintf("&signature=%s", url.QueryEscape(signature)) + params = nil + } + } else if len(r.Config.ActiveProfile.Username) > 0 && len(r.Config.ActiveProfile.Password) > 0 { sessionKey, err := Login(r) if err != nil { @@ -287,7 +296,7 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str // we can implement further conditions to do POST or GET (or other http commands) here func executeRequest(r *Request, requestURL string, params url.Values) (*http.Response, error) { config.SetupContext(r.Config) - if params.Has("password") || params.Has("userdata") { + if params.Has("password") || params.Has("userdata") || r.Config.Core.PostRequest { requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL) return r.Client().PostForm(requestURL, params) } else { diff --git a/config/config.go b/config/config.go index e390c22..52ee9f0 100644 --- a/config/config.go +++ b/config/config.go @@ -67,6 +67,7 @@ type Core struct { VerifyCert bool `ini:"verifycert"` ProfileName string `ini:"profile"` AutoComplete bool `ini:"autocomplete"` + PostRequest bool `ini:postrequest` } // Config describes CLI config file and default options @@ -151,6 +152,7 @@ func defaultCoreConfig() Core { VerifyCert: true, ProfileName: "localcloud", AutoComplete: true, + PostRequest: true, } } @@ -282,6 +284,9 @@ func saveConfig(cfg *Config) *Config { core.AutoComplete = true core.Output = JSON } + if !conf.Section(ini.DEFAULT_SECTION).HasKey("postrequest") { + core.PostRequest = true + } cfg.Core = core } From 0b342b283439c0952617588a412b189ed0442fce Mon Sep 17 00:00:00 2001 From: Kevin Li Date: Mon, 31 Mar 2025 15:52:41 -0700 Subject: [PATCH 2/4] Making some changes --- cmd/network.go | 4 +--- config/config.go | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/network.go b/cmd/network.go index ff61bff..4d3e1e3 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -205,9 +205,7 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str } params.Add("response", "json") params.Add("signatureversion", "3") - expirationTime := time.Now().UTC().Add(15 * time.Minute) - expirationStr := expirationTime.Format("2006-01-02T15:04:05Z") - params.Add("expires", expirationStr) + params.Add(expiresKey, time.Now().UTC().Add(15 * time.Minute).Format(time.RFC3339)) var encodedParams string var err error diff --git a/config/config.go b/config/config.go index 52ee9f0..b6e4fe7 100644 --- a/config/config.go +++ b/config/config.go @@ -284,7 +284,7 @@ func saveConfig(cfg *Config) *Config { core.AutoComplete = true core.Output = JSON } - if !conf.Section(ini.DEFAULT_SECTION).HasKey("postrequest") { + if conf.Section(ini.DEFAULT_SECTION).HasKey("postrequest") { core.PostRequest = true } cfg.Core = core From 4a7d83367a80f5815e5aa5cc3ed0f3f0ec6643dd Mon Sep 17 00:00:00 2001 From: Kevin Li Date: Tue, 8 Apr 2025 10:45:08 -0700 Subject: [PATCH 3/4] Fixing some errors based off PR --- cmd/network.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/network.go b/cmd/network.go index 4d3e1e3..0dae39f 100644 --- a/cmd/network.go +++ b/cmd/network.go @@ -203,8 +203,10 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str params.Add(key, value) } } + signatureversion := "3" + expiresKey := "expires" params.Add("response", "json") - params.Add("signatureversion", "3") + params.Add("signatureversion", signatureversion) params.Add(expiresKey, time.Now().UTC().Add(15 * time.Minute).Format(time.RFC3339)) var encodedParams string From 5dc7dd9944c76b7b1b946e4818272cb8c9b05f3c Mon Sep 17 00:00:00 2001 From: Kevin Li Date: Wed, 23 Apr 2025 11:49:21 -0700 Subject: [PATCH 4/4] Changing postRequest from true to false --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index b6e4fe7..a9176d7 100644 --- a/config/config.go +++ b/config/config.go @@ -285,7 +285,7 @@ func saveConfig(cfg *Config) *Config { core.Output = JSON } if conf.Section(ini.DEFAULT_SECTION).HasKey("postrequest") { - core.PostRequest = true + core.PostRequest = false } cfg.Core = core }