Skip to content

Commit a6552ee

Browse files
kevin-liiKevin Li
andauthored
Allow users to send requests from CMK using POST requests. (#161)
* Adding changes to reflect changes to Cloudstack that enforce POST and timestamps * Making some changes * Fixing some errors based off PR * Changing postRequest from true to false * Adding config * Fixing confusion --------- Co-authored-by: Kevin Li <[email protected]>
1 parent 91f5600 commit a6552ee

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

cmd/network.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
203203
params.Add(key, value)
204204
}
205205
}
206+
signatureversion := "3"
207+
expiresKey := "expires"
206208
params.Add("response", "json")
209+
params.Add("signatureversion", signatureversion)
210+
params.Add(expiresKey, time.Now().UTC().Add(15 * time.Minute).Format(time.RFC3339))
207211

208212
var encodedParams string
209213
var err error
@@ -220,8 +224,13 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
220224
mac := hmac.New(sha1.New, []byte(secretKey))
221225
mac.Write([]byte(strings.ToLower(encodedParams)))
222226
signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
223-
encodedParams = encodedParams + fmt.Sprintf("&signature=%s", url.QueryEscape(signature))
224-
params = nil
227+
if r.Config.Core.PostRequest {
228+
params.Add("signature", signature)
229+
} else {
230+
encodedParams = encodedParams + fmt.Sprintf("&signature=%s", url.QueryEscape(signature))
231+
params = nil
232+
}
233+
225234
} else if len(r.Config.ActiveProfile.Username) > 0 && len(r.Config.ActiveProfile.Password) > 0 {
226235
sessionKey, err := Login(r)
227236
if err != nil {
@@ -287,7 +296,7 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
287296
// we can implement further conditions to do POST or GET (or other http commands) here
288297
func executeRequest(r *Request, requestURL string, params url.Values) (*http.Response, error) {
289298
config.SetupContext(r.Config)
290-
if params.Has("password") || params.Has("userdata") {
299+
if params.Has("password") || params.Has("userdata") || r.Config.Core.PostRequest {
291300
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
292301
return r.Client().PostForm(requestURL, params)
293302
} else {

config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Core struct {
6767
VerifyCert bool `ini:"verifycert"`
6868
ProfileName string `ini:"profile"`
6969
AutoComplete bool `ini:"autocomplete"`
70+
PostRequest bool `ini:postrequest`
7071
}
7172

7273
// Config describes CLI config file and default options
@@ -151,6 +152,7 @@ func defaultCoreConfig() Core {
151152
VerifyCert: true,
152153
ProfileName: "localcloud",
153154
AutoComplete: true,
155+
PostRequest: true,
154156
}
155157
}
156158

@@ -282,6 +284,9 @@ func saveConfig(cfg *Config) *Config {
282284
core.AutoComplete = true
283285
core.Output = JSON
284286
}
287+
if !conf.Section(ini.DEFAULT_SECTION).HasKey("postrequest") {
288+
core.PostRequest = true
289+
}
285290
cfg.Core = core
286291
}
287292

0 commit comments

Comments
 (0)