-
Notifications
You must be signed in to change notification settings - Fork 183
ROX-28780: Unify Sensor endpoint env vars #20751
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package env | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| const ( | ||
| sensorEndpointEnvVar = "ROX_SENSOR_ENDPOINT" | ||
| legacyAdvertisedSensorEndpoint = "ROX_ADVERTISED_ENDPOINT" | ||
| defaultSensorEndpoint = "sensor.stackrox.svc:443" | ||
| ) | ||
|
|
||
| // SensorEndpointSetting returns the effective in-cluster Sensor host:port endpoint by checking: | ||
| // 1. ROX_SENSOR_ENDPOINT (canonical) | ||
| // 2. ROX_ADVERTISED_ENDPOINT (legacy fallback) | ||
| // 3. sensor.{POD_NAMESPACE}.svc:443 (runtime derivation) | ||
| // 4. sensor.stackrox.svc:443 (hard default) | ||
| func SensorEndpointSetting() string { | ||
| if endpoint := sensorEndpointFromEnv(sensorEndpointEnvVar); endpoint != "" { | ||
| return endpoint | ||
| } | ||
| if endpoint := sensorEndpointFromEnv(legacyAdvertisedSensorEndpoint); endpoint != "" { | ||
| return endpoint | ||
| } | ||
| if ns := Namespace.Setting(); ns != "" { | ||
| return fmt.Sprintf("sensor.%s.svc:443", ns) | ||
| } | ||
| return defaultSensorEndpoint | ||
| } | ||
|
|
||
| func sensorEndpointFromEnv(envVar string) string { | ||
| val, ok := os.LookupEnv(envVar) | ||
| if !ok { | ||
| return "" | ||
| } | ||
| val = strings.TrimSpace(val) | ||
| if val == "" { | ||
| return "" | ||
| } | ||
| return stripSchemePrefix(val) | ||
| } | ||
|
|
||
| func stripSchemePrefix(val string) string { | ||
| for _, prefix := range []string{"https://", "http://"} { | ||
| prev := val | ||
| val = strings.TrimPrefix(val, prefix) | ||
| if prev != val { | ||
| break | ||
| } | ||
| } | ||
| return val | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package env | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestSensorEndpointSetting(t *testing.T) { | ||
| cases := map[string]struct { | ||
| sensorEndpoint string | ||
| advertisedEndpoint string | ||
| namespace string | ||
| setSensor bool | ||
| setAdvertised bool | ||
| setNamespace bool | ||
| expected string | ||
| }{ | ||
| "canonical endpoint": { | ||
| sensorEndpoint: "sensor.custom.svc:8443", | ||
| setSensor: true, | ||
| expected: "sensor.custom.svc:8443", | ||
| }, | ||
| "canonical strips https prefix": { | ||
| sensorEndpoint: "https://sensor.custom.svc:8443", | ||
| setSensor: true, | ||
| expected: "sensor.custom.svc:8443", | ||
| }, | ||
| "canonical trims surrounding whitespace before stripping prefix": { | ||
| sensorEndpoint: " https://sensor.custom.svc:8443\t", | ||
| setSensor: true, | ||
| expected: "sensor.custom.svc:8443", | ||
| }, | ||
| "legacy advertised endpoint only": { | ||
| advertisedEndpoint: "sensor.legacy.svc:443", | ||
| setAdvertised: true, | ||
| expected: "sensor.legacy.svc:443", | ||
| }, | ||
| "canonical wins over legacy": { | ||
| sensorEndpoint: "sensor.canonical.svc:443", | ||
| advertisedEndpoint: "sensor.legacy.svc:443", | ||
| setSensor: true, | ||
| setAdvertised: true, | ||
| expected: "sensor.canonical.svc:443", | ||
| }, | ||
| "derive from namespace when unset": { | ||
| namespace: "rhacs", | ||
| setNamespace: true, | ||
| expected: "sensor.rhacs.svc:443", | ||
| }, | ||
| "empty canonical falls through to legacy": { | ||
| sensorEndpoint: "", | ||
| advertisedEndpoint: "sensor.legacy.svc:443", | ||
| setSensor: true, | ||
| setAdvertised: true, | ||
| expected: "sensor.legacy.svc:443", | ||
| }, | ||
| "whitespace-only canonical falls through to legacy": { | ||
| sensorEndpoint: " \t ", | ||
| advertisedEndpoint: "sensor.legacy.svc:443", | ||
| setSensor: true, | ||
| setAdvertised: true, | ||
| expected: "sensor.legacy.svc:443", | ||
| }, | ||
| "namespace default stackrox when unset": { | ||
| expected: defaultSensorEndpoint, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Setenv(sensorEndpointEnvVar, "") | ||
| t.Setenv(legacyAdvertisedSensorEndpoint, "") | ||
| t.Setenv(Namespace.EnvVar(), "") | ||
|
|
||
| if tc.setSensor { | ||
| t.Setenv(sensorEndpointEnvVar, tc.sensorEndpoint) | ||
| } | ||
| if tc.setAdvertised { | ||
| t.Setenv(legacyAdvertisedSensorEndpoint, tc.advertisedEndpoint) | ||
| } | ||
| if tc.setNamespace { | ||
| t.Setenv(Namespace.EnvVar(), tc.namespace) | ||
| } | ||
|
|
||
| assert.Equal(t, tc.expected, SensorEndpointSetting()) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.