feat: add validation to vault address - #1186
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1186 +/- ##
==========================================
+ Coverage 49.72% 49.87% +0.14%
==========================================
Files 146 146
Lines 13455 13495 +40
==========================================
+ Hits 6691 6730 +39
- Misses 6184 6185 +1
Partials 580 580 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
05e042a to
325d3d4
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds stricter validation for the SECRETS_ADDR (Vault address) configuration, aiming to prevent insecure (non-HTTPS) remote Vault endpoints and to provide clearer error reporting, along with expanded unit test coverage for these cases.
Changes:
- Added
Config.Validate()and Vault address validation that enforces HTTPS for non-localhost endpoints. - Introduced dedicated error values for common Vault address misconfiguration scenarios.
- Added unit tests covering Vault address parsing/validation behavior and a
NewConfiginvalid-env-var failure case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
config/config.go |
Adds Vault address validation (including localhost detection) and new config validation errors. |
config/config_test.go |
Adds tests for the new Vault address validation behavior and NewConfig env-var error handling. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
@nmgaston : Could you please add some evidence with your testing to this PR like when the vault address is misconfigure how does it get reported when console gets executed. Looking for how this error could be observed in the logs or the execution output for the following scenarios ErrSecretsAddrEmpty = errors.New("SECRETS_ADDR is empty") |
d260c3f to
84e79a6
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Suppressed comments (9)
config/config.go:385
- More
//nolint:staticcheckannotations intended for QF1008 should target the linter that emits QF1008 (gocritic), otherwise they may be ineffective and/or flagged as unused bynolintlint.
// Check for valid scheme (must be http or https)
if parsed.Scheme != "http" && parsed.Scheme != "https" {
if !strings.Contains(c.Secrets.Address, "://") { //nolint:staticcheck // QF1008: explicit field reference is clearer
return fmt.Errorf("%w: %q", ErrSecretsAddrMissingScheme, c.Secrets.Address) //nolint:staticcheck // QF1008: explicit field reference is clearer
}
return fmt.Errorf("%w: unsupported scheme %q in %q", ErrSecretsAddrInvalid, parsed.Scheme, c.Secrets.Address) //nolint:staticcheck // QF1008: explicit field reference is clearer
}
config/config_test.go:194
- These subtests call
t.Parallel()and close over the range variableaddr. Captureaddrinside the loop (pattern used elsewhere in the repo, e.g.internal/controller/httpapi/v1/server_test.go:50-55) to satisfyparalleltestand avoid accidental variable capture issues.
for _, addr := range testCases {
t.Run(addr, func(t *testing.T) {
t.Parallel()
config/config_test.go:231
- These parallel subtests close over
addrfrom the surrounding range loop. Capture the variable inside the loop (addr := addr) to match the repo’sparalleltestexpectations (seeinternal/controller/httpapi/v1/server_test.go:50-55).
for _, addr := range testCases {
t.Run(addr, func(t *testing.T) {
t.Parallel()
config/config_test.go:256
- These parallel subtests close over the loop variable
addr. Capture it inside the loop (addr := addr) to match the repo’s table-test pattern fort.Parallel()subtests.
for _, addr := range testCases {
t.Run(addr, func(t *testing.T) {
t.Parallel()
config/config_test.go:364
- These parallel subtests close over
addrfrom the range loop. Captureaddrinside the loop (addr := addr) to match the repo’s parallel table-test pattern and avoidparalleltestviolations.
for _, addr := range testCases {
t.Run(addr, func(t *testing.T) {
t.Parallel()
config/config_test.go:415
- These parallel subtests close over
tcfrom a range loop over a map. Capturetcinside the loop (tc := tc) before callingt.Run(..., func(t *testing.T) { t.Parallel(); ... })to satisfyparalleltestexpectations.
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
config/config.go:376
- These
//nolint:staticcheckannotations appear to be intended to suppress gocritic QF1008 (per the comment), but they currently target the wrong linter. This can fail CI ifnolintlintis enabled (unusednolint) and it also won't suppress QF1008 if emitted by gocritic.
This issue also appears on line 378 of the same file.
func (c *Config) Validate() error {
// Ensure non-localhost Vault addresses use HTTPS
if c.Secrets.Address != "" { //nolint:staticcheck // QF1008: explicit field reference is clearer
if err := c.validateSecretsAddr(); err != nil {
return err
config/config_test.go:172
- These subtests call
t.Parallel()and close over the range variableaddr. This repo enforces capturing the loop variable for parallel subtests (seeinternal/controller/httpapi/v1/server_test.go:50-55/CLAUDE.md), so addaddr := addrinside the loop.
This issue also appears in the following locations of the same file:
- line 192
- line 229
- line 254
- line 362
for _, addr := range testCases {
t.Run(addr, func(t *testing.T) {
t.Parallel()
config/config_test.go:305
- These parallel subtests close over the range variable
tc. Capture it inside the loop (tc := tc) to satisfy the repo’sparalleltestexpectations for table-driven tests witht.Parallel()subtests.
This issue also appears on line 413 of the same file.
for _, tc := range testCases {
t.Run(tc.host, func(t *testing.T) {
t.Parallel()
Runtime behavior when console starts with invalid SECRETS_ADDR Observed output: Command used: Observed output: Command used: Observed output: Command used: Observed output: Command used: Observed output: |
84e79a6 to
5ef57a4
Compare
I removed this one, since it can be empty. It will skip the validation if it is empty. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (1)
config/config.go:27
- ErrSecretsAddrMissingScheme currently suggests using either http:// or https://, but the validation only permits HTTP for localhost/loopback. Updating the error text will make the remediation clearer and avoid users switching to insecure HTTP on remote hosts and getting a second error.
ErrSecretsAddrMissingScheme = errors.New("SECRETS_ADDR missing scheme (use http:// or https://)")
This pull request strengthens the validation logic for the
SECRETS_ADDRconfiguration, ensuring improved security and correctness when specifying the Vault server address. It enforces that non-localhost addresses use HTTPS, adds granular error handling, and introduces comprehensive tests for these scenarios.Security and Validation Improvements
SECRETS_ADDRconfig: non-localhost addresses must use HTTPS, and various error cases (missing scheme, invalid URL, missing host) are now handled with specific error messages. (config/config.go)isLocalhostandstripPortto reliably detect localhost addresses and handle IPv4/IPv6/hostname formats. (config/config.go)Error Handling
config/config.go)Testing
config/config_test.go)SECRETS_ADDRenvironment variable before and after tests. (config/config_test.go)NewConfig. (config/config_test.go)These changes make it much harder to accidentally misconfigure the Vault address in an insecure way and ensure that configuration errors are caught early with clear error messages.