Skip to content

Allow users to send requests from CMK using POST requests. #161

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 12 additions & 3 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ 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", signatureversion)
params.Add(expiresKey, time.Now().UTC().Add(15 * time.Minute).Format(time.RFC3339))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build failing here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed.


var encodedParams string
var err error
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
VerifyCert bool `ini:"verifycert"`
ProfileName string `ini:"profile"`
AutoComplete bool `ini:"autocomplete"`
PostRequest bool `ini:postrequest`
}

// Config describes CLI config file and default options
Expand Down Expand Up @@ -151,6 +152,7 @@
VerifyCert: true,
ProfileName: "localcloud",
AutoComplete: true,
PostRequest: true,
}
}

Expand Down Expand Up @@ -282,6 +284,9 @@
core.AutoComplete = true
core.Output = JSON
}
if !conf.Section(ini.DEFAULT_SECTION).HasKey("postrequest") { {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !conf.Section(ini.DEFAULT_SECTION).HasKey("postrequest") { {
if !conf.Section(ini.DEFAULT_SECTION).HasKey("postrequest") {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevin-lii check this, build fails

core.PostRequest = false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevin-lii seems some confusion here, core here is not reflected from the defaultCoreConfig(), and core.PostRequest is false by default. possible to check the conf section has postrequest key and it's value is true, to set core.PostRequest = true? Are you able to verify the GET & POST calls with this change?

cfg.Core = core
}

Expand Down Expand Up @@ -317,7 +322,7 @@
}

// LoadProfile loads an existing profile
func (c *Config) LoadProfile(name string) {

Check failure on line 325 in config/config.go

View workflow job for this annotation

GitHub Actions / build

missing ',' in argument list
Debug("Trying to load profile: " + name)
conf := readConfig(c)
section, err := conf.GetSection(name)
Expand Down
Loading