2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-02-01 22:13:51 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-03-24 17:58:03 +00:00
|
|
|
"net/http"
|
2019-02-01 22:13:51 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
retryablehttp "github.com/hashicorp/go-retryablehttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrOutputStringRequest = "output a string, please"
|
|
|
|
)
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
var LastOutputStringError *OutputStringError
|
2019-02-01 22:13:51 +00:00
|
|
|
|
|
|
|
type OutputStringError struct {
|
|
|
|
*retryablehttp.Request
|
2022-01-20 18:25:26 +00:00
|
|
|
TLSSkipVerify bool
|
|
|
|
ClientCACert, ClientCAPath string
|
|
|
|
ClientCert, ClientKey string
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString string
|
2019-02-01 22:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *OutputStringError) Error() string {
|
2022-04-27 23:35:18 +00:00
|
|
|
if d.finalCurlString == "" {
|
|
|
|
cs, err := d.buildCurlString()
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
2019-02-01 22:13:51 +00:00
|
|
|
}
|
2022-04-27 23:35:18 +00:00
|
|
|
d.finalCurlString = cs
|
2019-02-01 22:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ErrOutputStringRequest
|
|
|
|
}
|
|
|
|
|
2022-04-27 23:35:18 +00:00
|
|
|
func (d *OutputStringError) CurlString() (string, error) {
|
|
|
|
if d.finalCurlString == "" {
|
|
|
|
cs, err := d.buildCurlString()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
d.finalCurlString = cs
|
|
|
|
}
|
|
|
|
return d.finalCurlString, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *OutputStringError) buildCurlString() (string, error) {
|
2019-02-01 22:13:51 +00:00
|
|
|
body, err := d.Request.BodyBytes()
|
|
|
|
if err != nil {
|
2022-04-27 23:35:18 +00:00
|
|
|
return "", err
|
2019-02-01 22:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build cURL string
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString := "curl "
|
2021-06-14 15:09:29 +00:00
|
|
|
if d.TLSSkipVerify {
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString += "--insecure "
|
2021-06-14 15:09:29 +00:00
|
|
|
}
|
2022-03-24 17:58:03 +00:00
|
|
|
if d.Request.Method != http.MethodGet {
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString = fmt.Sprintf("%s-X %s ", finalCurlString, d.Request.Method)
|
2019-03-08 19:14:50 +00:00
|
|
|
}
|
2022-01-20 18:25:26 +00:00
|
|
|
if d.ClientCACert != "" {
|
2022-08-03 19:22:48 +00:00
|
|
|
clientCACert := strings.ReplaceAll(d.ClientCACert, "'", "'\"'\"'")
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString = fmt.Sprintf("%s--cacert '%s' ", finalCurlString, clientCACert)
|
2022-01-20 18:25:26 +00:00
|
|
|
}
|
|
|
|
if d.ClientCAPath != "" {
|
2022-08-03 19:22:48 +00:00
|
|
|
clientCAPath := strings.ReplaceAll(d.ClientCAPath, "'", "'\"'\"'")
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString = fmt.Sprintf("%s--capath '%s' ", finalCurlString, clientCAPath)
|
2022-01-20 18:25:26 +00:00
|
|
|
}
|
|
|
|
if d.ClientCert != "" {
|
2022-08-03 19:22:48 +00:00
|
|
|
clientCert := strings.ReplaceAll(d.ClientCert, "'", "'\"'\"'")
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString = fmt.Sprintf("%s--cert '%s' ", finalCurlString, clientCert)
|
2022-01-20 18:25:26 +00:00
|
|
|
}
|
|
|
|
if d.ClientKey != "" {
|
2022-08-03 19:22:48 +00:00
|
|
|
clientKey := strings.ReplaceAll(d.ClientKey, "'", "'\"'\"'")
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString = fmt.Sprintf("%s--key '%s' ", finalCurlString, clientKey)
|
2022-01-20 18:25:26 +00:00
|
|
|
}
|
2019-02-01 22:13:51 +00:00
|
|
|
for k, v := range d.Request.Header {
|
|
|
|
for _, h := range v {
|
|
|
|
if strings.ToLower(k) == "x-vault-token" {
|
|
|
|
h = `$(vault print token)`
|
|
|
|
}
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString = fmt.Sprintf("%s-H \"%s: %s\" ", finalCurlString, k, h)
|
2019-02-01 22:13:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(body) > 0 {
|
|
|
|
// We need to escape single quotes since that's what we're using to
|
|
|
|
// quote the body
|
2022-08-03 19:22:48 +00:00
|
|
|
escapedBody := strings.ReplaceAll(string(body), "'", "'\"'\"'")
|
2022-04-27 23:35:18 +00:00
|
|
|
finalCurlString = fmt.Sprintf("%s-d '%s' ", finalCurlString, escapedBody)
|
2019-02-01 22:13:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 23:35:18 +00:00
|
|
|
return fmt.Sprintf("%s%s", finalCurlString, d.Request.URL.String()), nil
|
2019-02-01 22:13:51 +00:00
|
|
|
}
|