Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include crypto diagnostics in /debug/vars output #23948

Merged
merged 5 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 70 additions & 1 deletion services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,6 @@ func (h *Handler) async(q *influxql.Query, results <-chan *query.Result) {
// in the database URL query value. It is encoded using a forward slash like
// "database/retentionpolicy" and we should be able to simply split that string
// on the forward slash.
//
func bucket2dbrp(bucket string) (string, string, error) {
// test for a slash in our bucket name.
switch idx := strings.IndexByte(bucket, '/'); idx {
Expand Down Expand Up @@ -2250,6 +2249,35 @@ func (h *Handler) serveExpvar(w http.ResponseWriter, r *http.Request) {
first = false
fmt.Fprintf(w, "\"cmdline\": %s", val)
}

// We're going to print some kind of crypto data, we just
// need to find the proper source for it.
{
var jv map[string]interface{}
val := diags["crypto"]
if val != nil {
jv, err = parseCryptoDiagnostics(val)
if err != nil {
h.httpError(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
jv = ossCryptoDiagnostics()
}

data, err := json.Marshal(jv)
if err != nil {
h.httpError(w, err.Error(), http.StatusInternalServerError)
return
}

if !first {
fmt.Fprintln(w, ",")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still in favor of an h.Logger.Error(err) if Fprintln fails here.

}
first = false
fmt.Fprintf(w, "\"crypto\": %s", data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, let's log Fprintf errors, as unlikely as they may be.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll go a step further and say the fact there are fmt.Printf calls to check when generating JSON is problematic. I don't understand why we don't build the sections into a map[string]interface{} and then serialize that. There's a lot more fmt.Printf's in there than just the ones I added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely agree. But I didn't want to generate more work for you rewriting everything.

}

if val := expvar.Get("memstats"); val != nil {
if !first {
fmt.Fprintln(w, ",")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, let's log Fprintln errors.

Expand Down Expand Up @@ -2434,6 +2462,47 @@ func parseBuildInfo(d *diagnostics.Diagnostics) (map[string]interface{}, error)
return m, nil
}

// ossCryptoDiagnostics creates a default crypto diagnostics map that
// can be marshaled into JSON for /debug/vars.
func ossCryptoDiagnostics() map[string]interface{} {
return map[string]interface{}{
"ensureFIPS": false,
"FIPS": false,
"implementation": "Go",
"passwordHash": "bcrypt",
}
}

// parseCryptoDiagnostics converts the crypto diagnostics into an appropriate
// format for marshaling to JSON in the /debug/vars format.
func parseCryptoDiagnostics(d *diagnostics.Diagnostics) (map[string]interface{}, error) {
// No defaults (eg ossCryptoDiagnostics) to avoid lying if values are missing
m := make(map[string]interface{})

for key := range m {
// Find the associated column.
ci := -1
for i, col := range d.Columns {
if col == key {
ci = i
break
}
}

// Don't error out if we can't find the column or cell for a given key.
// There could still be useful information we gather.
if ci == -1 { // column not found
continue
}
if len(d.Rows) < 1 || len(d.Rows[0]) <= ci { // data cell not found
continue
}

m[key] = d.Rows[0][ci]
}
return m, nil
}

// httpError writes an error to the client in a standard format.
func (h *Handler) httpError(w http.ResponseWriter, errmsg string, code int) {
if code == http.StatusUnauthorized {
Expand Down
9 changes: 8 additions & 1 deletion services/httpd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,7 @@ func TestHandlerDebugVars(t *testing.T) {
h := NewHandler(false)
h.Monitor.StatisticsFn = func(_ map[string]string) ([]*monitor.Statistic, error) {
return stats(
stat("crypto", tags("FIPS", "ensureFIPS", "passwordHash", "implementation"), nil),
stat("database", tags("database", "foo"), nil),
stat("hh", tags("path", "/mnt/foo/bar"), nil),
stat("httpd", tags("bind", "127.0.0.1:8088", "proto", "https"), nil),
Expand All @@ -2656,7 +2657,7 @@ func TestHandlerDebugVars(t *testing.T) {
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
got := keys(read(t, w.Body, Ignored...))
exp := []string{"database:foo", "hh:/mnt/foo/bar", "httpd:https:127.0.0.1:8088", "other", "shard:/mnt/foo:111"}
exp := []string{"crypto", "database:foo", "hh:/mnt/foo/bar", "httpd:https:127.0.0.1:8088", "other", "shard:/mnt/foo:111"}
if !cmp.Equal(got, exp) {
t.Errorf("unexpected keys; -got/+exp\n%s", cmp.Diff(got, exp))
}
Expand All @@ -2677,6 +2678,12 @@ func TestHandlerDebugVars(t *testing.T) {
h.ServeHTTP(w, req)
got := read(t, w.Body, Ignored...)
exp := map[string]interface{}{
"crypto": map[string]interface{}{
"FIPS": false,
"ensureFIPS": false,
"passwordHash": "bcrypt",
"implementation": "Go",
},
"hh_processor": map[string]interface{}{
"name": "hh_processor",
"tags": map[string]interface{}{"db": "foo", "shardID": "10"},
Expand Down