-
Notifications
You must be signed in to change notification settings - Fork 136
feat(cmd): add kong-admin-token flag #2123
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
+189
−6
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5a32b35
feat(cmd): add kong-admin-token flag
VarunAthreya 9c4a99f
feat: add support for RBAC admin token in integration tests
VarunAthreya 78d8db1
fix: give --kong-admin-token precedence over colliding headers
VarunAthreya 1097eb2
fix: add comment to skip headers set by dedicated flags to avoid coll…
VarunAthreya 8bbf38f
fix: remove non lts Kong gateway versions from integration tests
VarunAthreya 33af6ff
fix: replace assert with require for authentication error validation …
VarunAthreya d496202
Merge branch 'main' into feat/admin-token-flag
VarunAthreya 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| //go:build integration | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // Test_RBAC_AdminToken exercises the --kong-admin-token flag (added to | ||
| // authenticate against an RBAC-enabled Kong Admin API). | ||
| // | ||
| // It only runs against an Enterprise Kong instance with RBAC enforcement | ||
| // enabled, which is provided by the dedicated `integration-rbac` job in | ||
| // .github/workflows/integration-enterprise.yaml. When RBAC is enforced, a | ||
| // command that talks to the Admin API must fail without a valid admin token | ||
| // and succeed once the token is supplied. | ||
| func Test_RBAC_AdminToken(t *testing.T) { | ||
| runWhenRBAC(t, ">=2.8.0") | ||
|
|
||
| // disable analytics for integration tests | ||
| t.Setenv("DECK_ANALYTICS", "off") | ||
|
|
||
| // The CI job seeds the kong_admin token in KONG_ADMIN_TOKEN; fall back to | ||
| // the decK CLI variable for local runs. | ||
| adminToken := os.Getenv("KONG_ADMIN_TOKEN") | ||
| if adminToken == "" { | ||
| adminToken = os.Getenv("DECK_KONG_ADMIN_TOKEN") | ||
| } | ||
| require.NotEmpty(t, adminToken, | ||
| "KONG_ADMIN_TOKEN or DECK_KONG_ADMIN_TOKEN must be set when running RBAC tests") | ||
|
|
||
| // online validation hits the Admin API but does not mutate state, so no | ||
| // reset/cleanup (which would itself require the token) is needed. | ||
| const stateFile = "testdata/validate/kong-ee.yaml" | ||
|
|
||
| t.Run("fails when kong-admin-token is not passed", func(t *testing.T) { | ||
| // make sure the CLI cannot pick the token up from the environment, so | ||
| // the request reaches Kong unauthenticated. | ||
| t.Setenv("DECK_KONG_ADMIN_TOKEN", "") | ||
|
|
||
| err := validate(ONLINE, stateFile) | ||
| require.Error(t, err, | ||
| "online validate should fail against an RBAC-enabled Kong without an admin token") | ||
| // Assert it fails *specifically* because of authentication (HTTP 401) | ||
| // rather than some unrelated error (bad file, gateway down, etc.). | ||
| // go-kong formats API errors as `HTTP status 401 (message: ...)`. | ||
| assert.Contains(t, err.Error(), "401", | ||
|
VarunAthreya marked this conversation as resolved.
Outdated
|
||
| "expected an authentication failure (HTTP 401), got: %v", err) | ||
| }) | ||
|
|
||
| t.Run("succeeds when kong-admin-token is passed", func(t *testing.T) { | ||
| // scrub the env so the token can only come from the flag, proving the | ||
| // flag is what authenticates the request. | ||
| t.Setenv("DECK_KONG_ADMIN_TOKEN", "") | ||
|
|
||
| err := validate(ONLINE, stateFile, "--kong-admin-token", adminToken) | ||
| require.NoError(t, err, | ||
| "online validate should succeed against an RBAC-enabled Kong with a valid admin token") | ||
| }) | ||
|
|
||
| t.Run("kong-admin-token takes precedence over a colliding --headers value", func(t *testing.T) { | ||
| // scrub the env so the token can only come from the flags under test. | ||
| t.Setenv("DECK_KONG_ADMIN_TOKEN", "") | ||
|
|
||
| // Supply an invalid Kong-Admin-Token via --headers alongside the valid | ||
| // token via --kong-admin-token. The explicit flag must win, so the | ||
| // invalid header value is dropped and the request authenticates. | ||
| err := validate(ONLINE, stateFile, | ||
| "--headers", "Kong-Admin-Token:invalid-token", | ||
| "--kong-admin-token", adminToken) | ||
| require.NoError(t, err, | ||
| "online validate should succeed: --kong-admin-token must override the "+ | ||
| "colliding Kong-Admin-Token supplied via --headers") | ||
| }) | ||
| } | ||
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.
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.