-
Notifications
You must be signed in to change notification settings - Fork 138
Add container-aware Vault OIDC authentication and browser handling #861
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
openshift-merge-bot
merged 7 commits into
openshift:master
from
clcollins:container-vault-auth-fix
Mar 10, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7c7061
Add container-aware Vault OIDC authentication and browser handling
clcollins 40afe67
Phase 1: Fix code formatting
clcollins b200500
Phase 2: Add IsContainerEnvironment helper function
clcollins a3aa366
Phase 3: Refactor vault.go to use helper and fix docs
clcollins caeb779
Phase 4: Improve vault_test.go test coverage and reliability
clcollins fb2553f
Phase 5: Update dashboardCmd.go to use helper and add user feedback
clcollins 71808cd
Use ocm-container's IsRunningInOcmContainer() helper
clcollins 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| package dynatrace | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "testing" | ||
|
|
||
| ocmutils "github.com/openshift/ocm-container/pkg/utils" | ||
| ) | ||
|
|
||
| // TestSetupVaultToken_ContainerEnvironment tests that the vault login command | ||
| // uses the correct flags when running inside a container (IO_OPENSHIFT_MANAGED_NAME env var set) | ||
| func TestSetupVaultToken_ContainerEnvironment(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| containerEnvValue string | ||
| expectNoBrowser bool | ||
| }{ | ||
| { | ||
| name: "Container environment with IO_OPENSHIFT_MANAGED_NAME=ocm-container", | ||
| containerEnvValue: "ocm-container", | ||
| expectNoBrowser: true, | ||
| }, | ||
| { | ||
| name: "Non-container environment (empty)", | ||
| containerEnvValue: "", | ||
| expectNoBrowser: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Set environment using t.Setenv - automatically cleaned up after test | ||
| // Always call t.Setenv to ensure clean environment, even for empty case | ||
| t.Setenv("IO_OPENSHIFT_MANAGED_NAME", tt.containerEnvValue) | ||
|
|
||
| // Build the command args as the code does | ||
| loginArgs := []string{"login", "-method=oidc", "-no-print"} | ||
| if ocmutils.IsRunningInOcmContainer() { | ||
| loginArgs = []string{"login", "-method=oidc", "skip_browser=true", "listenaddress=0.0.0.0"} | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // Verify the correct parameter is used | ||
| cmd := exec.Command("vault", loginArgs...) | ||
| cmdArgs := cmd.Args[1:] // Skip the "vault" binary name | ||
|
|
||
| if tt.expectNoBrowser { | ||
| // Should have skip_browser=true and listenaddress=0.0.0.0 parameters | ||
| hasSkipBrowser := false | ||
| hasNoPrint := false | ||
| hasListenAddress := false | ||
| for _, arg := range cmdArgs { | ||
| if arg == "skip_browser=true" { | ||
| hasSkipBrowser = true | ||
| } | ||
| if arg == "-no-print" { | ||
| hasNoPrint = true | ||
| } | ||
| if arg == "listenaddress=0.0.0.0" { | ||
| hasListenAddress = true | ||
| } | ||
| } | ||
|
|
||
| if !hasSkipBrowser { | ||
| t.Errorf("Expected skip_browser=true parameter in container environment, got args: %v", cmdArgs) | ||
| } | ||
| if hasNoPrint { | ||
| t.Errorf("Did not expect -no-print flag in container environment, got args: %v", cmdArgs) | ||
| } | ||
| if !hasListenAddress { | ||
| t.Errorf("Expected listenaddress=0.0.0.0 parameter in container environment, got args: %v", cmdArgs) | ||
| } | ||
| } else { | ||
| // Should have -no-print flag | ||
| hasSkipBrowser := false | ||
| hasNoPrint := false | ||
| for _, arg := range cmdArgs { | ||
| if arg == "skip_browser=true" { | ||
| hasSkipBrowser = true | ||
| } | ||
| if arg == "-no-print" { | ||
| hasNoPrint = true | ||
| } | ||
| } | ||
|
|
||
| if hasSkipBrowser { | ||
| t.Errorf("Did not expect skip_browser=true parameter in non-container environment, got args: %v", cmdArgs) | ||
| } | ||
| if !hasNoPrint { | ||
| t.Errorf("Expected -no-print flag in non-container environment, got args: %v", cmdArgs) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestSetupVaultToken_OutputRedirection tests that stdout/stderr are properly | ||
| // redirected based on the environment | ||
| func TestSetupVaultToken_OutputRedirection(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| containerEnvValue string | ||
| expectOutput bool | ||
| }{ | ||
| { | ||
| name: "Container environment shows output", | ||
| containerEnvValue: "ocm-container", | ||
| expectOutput: true, | ||
| }, | ||
| { | ||
| name: "Non-container environment hides output", | ||
| containerEnvValue: "", | ||
| expectOutput: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Set environment using t.Setenv - automatically cleaned up after test | ||
| // Always call t.Setenv to ensure clean environment, even for empty case | ||
| t.Setenv("IO_OPENSHIFT_MANAGED_NAME", tt.containerEnvValue) | ||
|
|
||
| // Build the command as the code does | ||
| loginArgs := []string{"login", "-method=oidc", "-no-print"} | ||
| if ocmutils.IsRunningInOcmContainer() { | ||
| loginArgs = []string{"login", "-method=oidc", "skip_browser=true", "listenaddress=0.0.0.0"} | ||
| } | ||
| loginCmd := exec.Command("vault", loginArgs...) | ||
|
|
||
| // Set output redirection as the code does | ||
| if ocmutils.IsRunningInOcmContainer() { | ||
| loginCmd.Stdout = os.Stdout | ||
| loginCmd.Stderr = os.Stderr | ||
| } else { | ||
| loginCmd.Stdout = nil | ||
| loginCmd.Stderr = nil | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // Verify output redirection is correct | ||
| if tt.expectOutput { | ||
| if loginCmd.Stdout != os.Stdout { | ||
| t.Error("Expected Stdout to be os.Stdout in container environment") | ||
| } | ||
| if loginCmd.Stderr != os.Stderr { | ||
| t.Error("Expected Stderr to be os.Stderr in container environment") | ||
| } | ||
| } else { | ||
| if loginCmd.Stdout != nil { | ||
| t.Error("Expected Stdout to be nil in non-container environment") | ||
| } | ||
| if loginCmd.Stderr != nil { | ||
| t.Error("Expected Stderr to be nil in non-container environment") | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.