2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-02-02 01:13:51 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-03-24 20:58:03 +03:00
|
|
|
"net/http"
|
2019-02-02 01:13:51 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
retryablehttp "github.com/hashicorp/go-retryablehttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrOutputStringRequest = "output a string, please"
|
|
|
|
)
|
|
|
|
|
2021-04-08 19:43:39 +03:00
|
|
|
var LastOutputStringError *OutputStringError
|
2019-02-02 01:13:51 +03:00
|
|
|
|
|
|
|
type OutputStringError struct {
|
|
|
|
*retryablehttp.Request
|
2022-01-20 21:25:26 +03:00
|
|
|
TLSSkipVerify bool
|
|
|
|
ClientCACert, ClientCAPath string
|
|
|
|
ClientCert, ClientKey string
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString string
|
2019-02-02 01:13:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *OutputStringError) Error() string {
|
2022-04-28 02:35:18 +03:00
|
|
|
if d.finalCurlString == "" {
|
|
|
|
cs, err := d.buildCurlString()
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
2019-02-02 01:13:51 +03:00
|
|
|
}
|
2022-04-28 02:35:18 +03:00
|
|
|
d.finalCurlString = cs
|
2019-02-02 01:13:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ErrOutputStringRequest
|
|
|
|
}
|
|
|
|
|
2022-04-28 02:35:18 +03: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-02 01:13:51 +03:00
|
|
|
body, err := d.Request.BodyBytes()
|
|
|
|
if err != nil {
|
2022-04-28 02:35:18 +03:00
|
|
|
return "", err
|
2019-02-02 01:13:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build cURL string
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString := "curl "
|
2021-06-14 18:09:29 +03:00
|
|
|
if d.TLSSkipVerify {
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString += "--insecure "
|
2021-06-14 18:09:29 +03:00
|
|
|
}
|
2022-03-24 20:58:03 +03:00
|
|
|
if d.Request.Method != http.MethodGet {
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString = fmt.Sprintf("%s-X %s ", finalCurlString, d.Request.Method)
|
2019-03-08 22:14:50 +03:00
|
|
|
}
|
2022-01-20 21:25:26 +03:00
|
|
|
if d.ClientCACert != "" {
|
2022-08-03 22:22:48 +03:00
|
|
|
clientCACert := strings.ReplaceAll(d.ClientCACert, "'", "'\"'\"'")
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString = fmt.Sprintf("%s--cacert '%s' ", finalCurlString, clientCACert)
|
2022-01-20 21:25:26 +03:00
|
|
|
}
|
|
|
|
if d.ClientCAPath != "" {
|
2022-08-03 22:22:48 +03:00
|
|
|
clientCAPath := strings.ReplaceAll(d.ClientCAPath, "'", "'\"'\"'")
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString = fmt.Sprintf("%s--capath '%s' ", finalCurlString, clientCAPath)
|
2022-01-20 21:25:26 +03:00
|
|
|
}
|
|
|
|
if d.ClientCert != "" {
|
2022-08-03 22:22:48 +03:00
|
|
|
clientCert := strings.ReplaceAll(d.ClientCert, "'", "'\"'\"'")
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString = fmt.Sprintf("%s--cert '%s' ", finalCurlString, clientCert)
|
2022-01-20 21:25:26 +03:00
|
|
|
}
|
|
|
|
if d.ClientKey != "" {
|
2022-08-03 22:22:48 +03:00
|
|
|
clientKey := strings.ReplaceAll(d.ClientKey, "'", "'\"'\"'")
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString = fmt.Sprintf("%s--key '%s' ", finalCurlString, clientKey)
|
2022-01-20 21:25:26 +03:00
|
|
|
}
|
2019-02-02 01:13:51 +03: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-28 02:35:18 +03:00
|
|
|
finalCurlString = fmt.Sprintf("%s-H \"%s: %s\" ", finalCurlString, k, h)
|
2019-02-02 01:13:51 +03: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 22:22:48 +03:00
|
|
|
escapedBody := strings.ReplaceAll(string(body), "'", "'\"'\"'")
|
2022-04-28 02:35:18 +03:00
|
|
|
finalCurlString = fmt.Sprintf("%s-d '%s' ", finalCurlString, escapedBody)
|
2019-02-02 01:13:51 +03:00
|
|
|
}
|
|
|
|
|
2022-04-28 02:35:18 +03:00
|
|
|
return fmt.Sprintf("%s%s", finalCurlString, d.Request.URL.String()), nil
|
2019-02-02 01:13:51 +03:00
|
|
|
}
|