From 9123d2f2e014c2213b5ffa79aafc2f6334b0e820 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Tue, 11 Mar 2025 15:35:35 -0400 Subject: [PATCH] Add ability to post bodys to HTTP requests --- confluent_osdeploy/utils/confusebox/apiclient.go | 15 ++++++++------- confluent_osdeploy/utils/confusebox/main.go | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/confluent_osdeploy/utils/confusebox/apiclient.go b/confluent_osdeploy/utils/confusebox/apiclient.go index 81360a0c..bbbab0e5 100644 --- a/confluent_osdeploy/utils/confusebox/apiclient.go +++ b/confluent_osdeploy/utils/confusebox/apiclient.go @@ -72,22 +72,23 @@ func NewApiClient(cafile string, keyfile string, nodename string, server string) func (apiclient *ApiClient) RegisterKey(crypted string, hmac string) (error) { cryptbytes := []byte(crypted) - _, err := apiclient.request("/confluent-api/self/registerapikey", "", &cryptbytes, "", hmac) + cryptbuffer := bytes.NewBuffer(cryptbytes) + _, err := apiclient.request("/confluent-api/self/registerapikey", "", cryptbuffer, "", hmac) return err } -func (apiclient *ApiClient) Fetch(url string, outputfile string, mime string) (error) { +func (apiclient *ApiClient) Fetch(url string, outputfile string, mime string, body io.Reader) (error) { outp, err := os.Create(outputfile) if err != nil { return err } defer outp.Close() - rsp, err := apiclient.request(url, mime, nil, "", "") + rsp, err := apiclient.request(url, mime, body, "", "") if err != nil { return err } _, err = io.Copy(outp, rsp) return err } -func (apiclient *ApiClient) GrabText(url string, mime string) (string, error){ - rsp, err := apiclient.request(url, mime, nil, "", "") +func (apiclient *ApiClient) GrabText(url string, mime string, body io.Reader) (string, error){ + rsp, err := apiclient.request(url, mime, body, "", "") if err != nil { return "", err } rspdata, err := io.ReadAll(rsp) if err != nil { return "", err } @@ -95,7 +96,7 @@ func (apiclient *ApiClient) GrabText(url string, mime string) (string, error){ return rsptxt, nil } -func (apiclient *ApiClient) request(url string, mime string, body *[]byte, method string, hmac string) (io.ReadCloser, error) { +func (apiclient *ApiClient) request(url string, mime string, body io.Reader, method string, hmac string) (io.ReadCloser, error) { if ! strings.Contains(url, "https://") { url = fmt.Sprintf("https://%s%s", apiclient.urlserver, url) } @@ -111,7 +112,7 @@ func (apiclient *ApiClient) request(url string, mime string, body *[]byte, metho if body == nil { rq, err = http.NewRequest(method, url, nil) } else { - rq, err = http.NewRequest(method, url, bytes.NewBuffer(*body)) + rq, err = http.NewRequest(method, url, body) } if err != nil { return nil, err } if (mime != "") { rq.Header.Set("Accept", mime) } diff --git a/confluent_osdeploy/utils/confusebox/main.go b/confluent_osdeploy/utils/confusebox/main.go index 3ff86563..9ae23959 100644 --- a/confluent_osdeploy/utils/confusebox/main.go +++ b/confluent_osdeploy/utils/confusebox/main.go @@ -4,6 +4,7 @@ import ( "bytes" "flag" "os" + "io" "fmt" ) @@ -48,6 +49,8 @@ func main() { invokeapi.BoolVar(&usejson, "j", false, "Request JSON formatted reply") outputfile := invokeapi.String("o", "", "Filename to store download to") invokeapi.StringVar(&confluentsrv, "s", "", "Confluent server to request from") + invokedata := invokeapi.String("d", "", "Data to submit") + invokedatafile := invokeapi.String("i", "", "File containing data to submit") @@ -74,7 +77,16 @@ func main() { outp.Write([]byte(password)) case "invoke": var err error + var body io.Reader + body = nil invokeapi.Parse(os.Args[2:]) + if *invokedata != "" { + body = bytes.NewBuffer([]byte(*invokedata)) + } + if *invokedatafile != "" { + body, err = os.Open(*invokedatafile) + if err != nil { panic(err) } + } if confluentsrv == "" { confluentsrv, err = get_confluent_server() } @@ -85,9 +97,9 @@ func main() { mime = "application/json" } if *outputfile != "" { - apiclient.Fetch(invokeapi.Arg(0), *outputfile, mime) + apiclient.Fetch(invokeapi.Arg(0), *outputfile, mime, body) } - rsp, err := apiclient.GrabText(invokeapi.Arg(0), mime) + rsp, err := apiclient.GrabText(invokeapi.Arg(0), mime, body) if err != nil { panic(err) } fmt.Println(rsp) default: