|
| 1 | +//go:build unit || !integration |
| 2 | + |
| 3 | +package agent |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/labstack/echo/v4" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/bacalhau-project/bacalhau/pkg/config/types" |
| 16 | + "github.com/bacalhau-project/bacalhau/pkg/publicapi/apimodels" |
| 17 | +) |
| 18 | + |
| 19 | +// TestEndpointConfigRedactFields asserts that auth tokens in the config are redacted. |
| 20 | +func TestEndpointConfigRedactFields(t *testing.T) { |
| 21 | + router := echo.New() |
| 22 | + |
| 23 | + // populate the fields that should be redacted with "secret" values. |
| 24 | + NewEndpoint(EndpointParams{ |
| 25 | + Router: router, |
| 26 | + BacalhauConfig: types.Bacalhau{ |
| 27 | + Orchestrator: types.Orchestrator{ |
| 28 | + Auth: types.OrchestratorAuth{ |
| 29 | + Token: "super-secret-orchestrator-token", |
| 30 | + }, |
| 31 | + }, |
| 32 | + Compute: types.Compute{ |
| 33 | + Auth: types.ComputeAuth{ |
| 34 | + Token: "super-secret-orchestrator-token", |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }) |
| 39 | + |
| 40 | + req := httptest.NewRequest(http.MethodGet, "/api/v1/agent/config", nil) |
| 41 | + rr := httptest.NewRecorder() |
| 42 | + router.ServeHTTP(rr, req) |
| 43 | + |
| 44 | + require.Equal(t, http.StatusOK, rr.Code) |
| 45 | + |
| 46 | + // assert the secret values are not present. |
| 47 | + var payload apimodels.GetAgentConfigResponse |
| 48 | + err := json.NewDecoder(rr.Body).Decode(&payload) |
| 49 | + require.NoError(t, err) |
| 50 | + assert.Equal(t, payload.Config.Orchestrator.Auth.Token, "<redacted>") |
| 51 | + assert.Equal(t, payload.Config.Compute.Auth.Token, "<redacted>") |
| 52 | +} |
0 commit comments