Files
threshold/api/output_string.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.4 KiB
Go
Raw Normal View History

// Copyright IBM Corp. 2020, 2026
2023-02-02 17:15:17 -08:00
// SPDX-License-Identifier: MPL-2.0
2020-05-04 10:58:03 -04:00
package api
import (
"fmt"
"strings"
retryablehttp "github.com/hashicorp/go-retryablehttp"
)
const (
ErrOutputStringRequest = "output a string, please"
)
var LastOutputStringError *OutputStringError
2020-05-04 10:58:03 -04:00
type OutputStringError struct {
*retryablehttp.Request
unixSocket string
2020-05-04 10:58:03 -04:00
parsingError error
parsedCurlString string
}
func NewOutputDomainSocketCurlStringError(req *retryablehttp.Request, socketAddr string) *OutputStringError {
return &OutputStringError{
Request: req,
unixSocket: socketAddr,
}
}
2020-05-04 10:58:03 -04:00
func (d *OutputStringError) Error() string {
if d.parsedCurlString == "" {
d.parseRequest()
if d.parsingError != nil {
return d.parsingError.Error()
}
}
return ErrOutputStringRequest
}
func (d *OutputStringError) parseRequest() {
body, err := d.Request.BodyBytes()
if err != nil {
d.parsingError = err
return
}
// Build cURL string
2021-04-13 15:08:51 -05:00
d.parsedCurlString = "curl"
2020-05-04 10:58:03 -04:00
if d.Request.Method != "GET" {
2021-04-13 15:08:51 -05:00
d.parsedCurlString = fmt.Sprintf("%s -X %s", d.parsedCurlString, d.Request.Method)
2020-05-04 10:58:03 -04:00
}
if d.unixSocket != "" {
d.parsedCurlString = fmt.Sprintf("%s --unix-socket %s", d.parsedCurlString, d.unixSocket)
}
2020-05-04 10:58:03 -04:00
for k, v := range d.Request.Header {
for _, h := range v {
if strings.ToLower(k) == "authorization" {
tokenName := Getenv("BOUNDARY_TOKEN_NAME")
keyringType := Getenv("BOUNDARY_KEYRING_TYPE")
2020-10-21 09:00:17 -04:00
switch {
case tokenName == "none" || keyringType == "none":
2020-09-25 17:06:53 -04:00
h = `Bearer <token>`
2020-10-21 09:00:17 -04:00
case tokenName == "" && keyringType == "":
2020-09-25 17:06:53 -04:00
h = `Bearer $(boundary config get-token)`
default:
2020-10-21 09:00:17 -04:00
h = fmt.Sprintf("Bearer $(boundary config get-token -keyring-type %s -token-name %s)", keyringType, tokenName)
2020-09-25 17:06:53 -04:00
}
2020-05-04 10:58:03 -04:00
}
2021-04-13 15:08:51 -05:00
d.parsedCurlString = fmt.Sprintf("%s -H \"%s: %s\"", d.parsedCurlString, k, h)
2020-05-04 10:58:03 -04:00
}
}
if len(body) > 0 {
// We need to escape single quotes since that's what we're using to
// quote the body
escapedBody := strings.Replace(string(body), "'", "'\"'\"'", -1)
2021-04-13 15:08:51 -05:00
d.parsedCurlString = fmt.Sprintf("%s -d '%s'", d.parsedCurlString, escapedBody)
2020-05-04 10:58:03 -04:00
}
// Filters can have shell characters so we use single quotes to surround the URL
d.parsedCurlString = fmt.Sprintf("%s '%s'", d.parsedCurlString, d.Request.URL.String())
2020-05-04 10:58:03 -04:00
}
func (d *OutputStringError) CurlString() string {
if d.parsedCurlString == "" {
d.parseRequest()
}
return d.parsedCurlString
}