-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroom.go
179 lines (158 loc) · 4.59 KB
/
broom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2021 Bojan Zivanovic and contributors
// SPDX-License-Identifier: Apache-2.0
package broom
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"os"
"os/exec"
"slices"
"strings"
"github.com/fatih/color"
strip "github.com/grokify/html-strip-tags-go"
"github.com/tidwall/pretty"
"golang.org/x/net/http/httpguts"
)
// Version is the current version, replaced at build time.
var Version = "dev"
// Result represents the result of executing an HTTP request.
type Result struct {
StatusCode int
Output string
}
// Authenticate authenticates the given request.
func Authenticate(req *http.Request, cfg AuthConfig) error {
if cfg.Credentials == "" && cfg.Command == "" {
return nil
}
credentials := cfg.Credentials
if cfg.Command != "" {
var err error
credentials, err = RunCommand(cfg.Command)
if err != nil {
return fmt.Errorf("run command: %w", err)
}
if credentials == "" {
return fmt.Errorf("run command: no credentials received")
}
}
switch cfg.Type {
case "bearer":
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", credentials))
case "basic":
credentials = base64.StdEncoding.EncodeToString([]byte(credentials))
req.Header.Set("Authorization", fmt.Sprintf("Basic %v", credentials))
case "api-key":
key := cfg.APIKeyHeader
if key == "" {
key = "X-API-Key"
}
req.Header.Set(key, credentials)
case "":
return errors.New("auth type not specified")
default:
return fmt.Errorf("unrecognized auth type %q", cfg.Type)
}
return nil
}
// AuthTypes returns a list of supported authentication types.
func AuthTypes() []string {
return []string{"bearer", "basic", "api-key"}
}
// Execute performs the given HTTP request and returns the result.
//
// The output consists of the request body (pretty-printed if JSON),
// and optionally the status code and headers (when "verbose" is true).
func Execute(req *http.Request, verbose bool) (Result, error) {
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return Result{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Result{}, err
}
sb := strings.Builder{}
if verbose {
sb.WriteString(resp.Status)
sb.WriteByte('\n')
writeHeaders(&sb, resp.Header, nil)
sb.WriteByte('\n')
}
if IsJSON(resp.Header.Get("Content-Type")) {
body = PrettyJSON(body)
}
sb.Write(body)
return Result{resp.StatusCode, sb.String()}, nil
}
// IsJSON checks whether the given media type matches a JSON format.
func IsJSON(mediaType string) bool {
// Needs to match not just application/json, but also variants
// such as application/vnd.api+json and application/hal+json,
// with or without a charset suffix.
return strings.Contains(mediaType, "json")
}
// PrettyJSON pretty-prints the given JSON.
func PrettyJSON(json []byte) []byte {
// Many web stacks (Go, Ruby on Rails, Symfony) escape the &, <, >
// HTML characters for safety reasons. Unescape them for readability.
json = bytes.ReplaceAll(json, []byte("\\u0026"), []byte("&"))
json = bytes.ReplaceAll(json, []byte("\\u003c"), []byte("<"))
json = bytes.ReplaceAll(json, []byte("\\u003e"), []byte(">"))
json = pretty.Pretty(json)
if !color.NoColor {
json = pretty.Color(json, nil)
}
return json
}
// RunCommand runs the given command and returns its output.
//
// The command has access to environment variables.
func RunCommand(command string) (string, error) {
errBuf := &bytes.Buffer{}
cmd := exec.Command("sh", "-c", command)
cmd.Env = os.Environ()
cmd.Stderr = errBuf
b, err := cmd.Output()
if err != nil {
// The error is just a return code, which isn't useful.
return "", errors.New(errBuf.String())
}
output := bytes.TrimSpace(b)
return string(output), nil
}
// Sanitize sanitizes the given string, stripping HTML and trailing newlines.
func Sanitize(s string) string {
return strings.Trim(strip.StripTags(s), "\n")
}
// writeHeaders writes sorted, colored headers to the given writer.
func writeHeaders(w io.StringWriter, headers http.Header, exclude []string) {
keys := make([]string, 0, len(headers))
for key := range headers {
if !slices.Contains(exclude, key) {
keys = append(keys, key)
}
}
slices.Sort(keys)
headerNewlineToSpace := strings.NewReplacer("\n", " ", "\r", " ")
for _, key := range keys {
if !httpguts.ValidHeaderFieldName(key) {
// Drop invalid headers the same way http.Header.WriteSubset() does.
continue
}
for _, v := range headers[key] {
v = headerNewlineToSpace.Replace(v)
v = textproto.TrimString(v)
w.WriteString(color.YellowString("%s: ", key))
w.WriteString(v)
w.WriteString("\n")
}
}
}