Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ It connects to `localhost:8080` by default. Each setting comes from the first of
| `--output`, `-o` | `GFSCTL_OUTPUT` | `text` (or `json`) |
| `--timeout` | `GFSCTL_TIMEOUT` | `30s` |

Use `GFSCTL_TOKEN` or the config file for the token: other local users can read `--token` with `ps`. Statistics answer 503 until mount feeds them to the API, and `gfsctl` prints that error.
Use `GFSCTL_TOKEN` or the config file for the token: other local users can read `--token` with `ps`. gfsctl and the API speak plain HTTP, so for a remote mount don't send the token across the network: tunnel over SSH (`ssh -L 8080:127.0.0.1:8080 node`, then `gfsctl status`) or put a TLS proxy in front of the API. Statistics answer 503 until mount feeds them to the API, and `gfsctl` prints that error.

## Multi-Node Setup

Expand Down
9 changes: 8 additions & 1 deletion cmd/gfsctl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"time"
)

// MaxResponseSize bounds how much of one response the client reads.
const MaxResponseSize = 16 << 20

// Client is an HTTP client for the GoClusterFS API.
type Client struct {
host string
Expand Down Expand Up @@ -94,11 +97,15 @@ func (c *Client) doRequest(ctx context.Context, method, path string) ([]byte, er
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
body, err := io.ReadAll(io.LimitReader(resp.Body, MaxResponseSize+1))
if err != nil {
return nil, fmt.Errorf("read response: %w", err)
}

if len(body) > MaxResponseSize {
return nil, fmt.Errorf("response from %s is larger than %d bytes", path, MaxResponseSize)
}

if resp.StatusCode >= 400 {
var errResp ErrorResponse
if json.Unmarshal(body, &errResp) == nil && errResp.Error != "" {
Expand Down
17 changes: 17 additions & 0 deletions cmd/gfsctl/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

Expand Down Expand Up @@ -50,3 +51,19 @@ func TestAPIErrorCarriesStatusAndDetails(t *testing.T) {
t.Fatalf("non-JSON 503: %v", err)
}
}

func TestOversizedResponseIsRefused(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
chunk := make([]byte, 1<<20)
for written := 0; written <= MaxResponseSize; written += len(chunk) {
if _, err := w.Write(chunk); err != nil {
return
}
}
}))
defer srv.Close()
c := &Client{baseURL: srv.URL, httpClient: srv.Client()}
if _, err := c.GetRaw(context.Background(), "/metrics"); err == nil || !strings.Contains(err.Error(), "larger than") {
t.Fatalf("oversized response: %v", err)
}
}
16 changes: 16 additions & 0 deletions cmd/gfsctl/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,19 @@ func TestVersionNeedsNoServer(t *testing.T) {
t.Fatalf("err=%v output=%q", err, out)
}
}

func TestConfigShowHidesToken(t *testing.T) {
const token = "abcd1234wxyz"
out, _, err := run(t, "--token", token, "config", "show")
if err != nil {
t.Fatal(err)
}
for _, part := range []string{"abcd", "wxyz", token} {
if strings.Contains(out, part) {
t.Errorf("config show reveals %q:\n%s", part, out)
}
}
if !strings.Contains(out, "set (12 chars)") {
t.Errorf("config show does not say the token is set:\n%s", out)
}
}
5 changes: 1 addition & 4 deletions cmd/gfsctl/commands/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,5 @@ func maskToken(token string) string {
if token == "" {
return "(not set)"
}
if len(token) <= 8 {
return "****"
}
return token[:4] + "..." + token[len(token)-4:]
return fmt.Sprintf("set (%d chars)", len(token))
}
Loading