Skip to content

Commit

Permalink
oVirt provider: Fix the POST auth request
Browse files Browse the repository at this point in the history
The oVirt auth request needs to be Content-type
application/x-www-form-urlencoded, which requires the URL encoding
inside the POST data request.

For this was necessary to update the Post method in Client.
If the posted body is type string, it will pass it as it
is. If the body is any other type, it uses the json.Marshal for
json formatting, which the oVirt auth API does not support.

Signed-off-by: Martin Necas <[email protected]>
  • Loading branch information
mnecas authored and ahadas committed Jun 26, 2023
1 parent 9f76037 commit 7c20047
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 3 additions & 7 deletions pkg/controller/provider/container/ovirt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,11 @@ func (r *Client) connect() (status int, err error) {
values.Add("scope", "ovirt-app-api")

client.Header = http.Header{
"Accept": []string{"application/json"},
"Accept": []string{"application/json"},
"Content-Type": []string{"application/x-www-form-urlencoded"},
}
request := &struct {
Input interface{} `json:"input"`
}{}
request.Input = values
response := &ovirtTokenResponse{}
url.RawQuery = values.Encode()
status, err = client.Post(url.String(), request, response)
status, err = client.Post(url.String(), values.Encode(), response)
if err != nil {
return
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/lib/inventory/web/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ func (r *Client) Get(url string, out interface{}, params ...Param) (status int,
return
}

// HTTP POST (method).
/*
HTTP POST (method).
When the `in interface{}` is a string, it is passed as raw bytes otherwise it uses the json.Marshal on it.
*/
func (r *Client) Post(url string, in interface{}, out interface{}) (status int, err error) {
parsedURL, err := liburl.Parse(url)
if err != nil {
Expand All @@ -172,8 +175,14 @@ func (r *Client) Post(url string, in interface{}, out interface{}) (status int,
url)
return
}
body, _ := json.Marshal(in)
reader := bytes.NewReader(body)
var reader *bytes.Reader
switch v := in.(type) {
case string:
reader = bytes.NewReader([]byte(v))
default:
body, _ := json.Marshal(in)
reader = bytes.NewReader(body)
}
request := &http.Request{
Header: r.Header,
Method: http.MethodPost,
Expand Down

0 comments on commit 7c20047

Please sign in to comment.