-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat: use env var instead of plain text for vault token #8866
Merged
spacewander
merged 24 commits into
apache:master
from
shreemaan-abhishek:vault-token-env-var
Feb 24, 2023
Merged
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
aa8b1bd
feat: use env var instead of plain text
shreemaan-abhishek 7f938e2
modify tests to use env var
shreemaan-abhishek c13c6ec
export env var
shreemaan-abhishek 6bc6d61
export env var for centos
shreemaan-abhishek 111ac11
fix breaking change, allow plain text env vars
shreemaan-abhishek 2d988b7
Revert "modify tests to use env var"
shreemaan-abhishek 62eb6f9
add tests
shreemaan-abhishek 26627b7
place env var in correct place
shreemaan-abhishek 0868b43
Revert "add tests"
shreemaan-abhishek c562216
add tests
shreemaan-abhishek 206dd90
fix tests
shreemaan-abhishek 3aeed0d
Merge branch 'master' into vault-token-env-var
shreemaan-abhishek 6c8cf91
add quotes
shreemaan-abhishek 756f3b4
use print instead of "say"
shreemaan-abhishek 37a3406
Revert "add quotes"
shreemaan-abhishek d4d5fb3
add line break
shreemaan-abhishek 30a296b
replace with \R
shreemaan-abhishek 1626dce
remove directory added by mistake
shreemaan-abhishek 46b992e
use regex
shreemaan-abhishek f2c1c27
trigger build
shreemaan-abhishek 9bfaede
remove unnecessary env var
shreemaan-abhishek 234346a
clean way to import
shreemaan-abhishek 8b72f12
remove newline
shreemaan-abhishek 74b3e66
trigger build
shreemaan-abhishek 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 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 |
---|---|---|
|
@@ -26,6 +26,7 @@ local norm_path = require("pl.path").normpath | |
local sub = core.string.sub | ||
local rfind_char = core.string.rfind_char | ||
|
||
local env = require("apisix.core.env") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we import it like |
||
|
||
local schema = { | ||
type = "object", | ||
|
@@ -53,10 +54,15 @@ local function make_request_to_vault(conf, method, key, data) | |
local req_addr = conf.uri .. norm_path("/v1/" | ||
.. conf.prefix .. "/" .. key) | ||
|
||
local token, _ = env.fetch_by_uri(conf.token) | ||
soulbird marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if not token then | ||
token = conf.token | ||
end | ||
|
||
local res, err = httpc:request_uri(req_addr, { | ||
method = method, | ||
headers = { | ||
["X-Vault-Token"] = conf.token | ||
["X-Vault-Token"] = token | ||
}, | ||
body = core.json.encode(data or {}, true) | ||
}) | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -14,6 +14,10 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
BEGIN { | ||
$ENV{VAULT_TOKEN} = "root"; | ||
} | ||
|
||
use t::APISIX 'no_plan'; | ||
|
||
repeat_each(2); | ||
|
@@ -540,3 +544,79 @@ GET /echo | |
Authorization: Basic Zm9vOmJhcg== | ||
--- response_headers | ||
Authorization: Basic Zm9vOmJhcg== | ||
|
||
|
||
|
||
=== TEST 25: set basic-auth conf with the token in an env var: password uses secret ref | ||
--- request | ||
GET /t | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
-- put secret vault config | ||
local code, body = t('/apisix/admin/secrets/vault/test1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"uri": "http://127.0.0.1:8200", | ||
"prefix" : "kv/apisix", | ||
"token" : "$ENV://VAULT_TOKEN" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
return ngx.say(body) | ||
end | ||
-- change consumer with secrets ref: vault | ||
code, body = t('/apisix/admin/consumers', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"username": "foo", | ||
"plugins": { | ||
"basic-auth": { | ||
"username": "foo", | ||
"password": "$secret://vault/test1/foo/passwd" | ||
} | ||
} | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
return ngx.say(body) | ||
end | ||
-- set route | ||
code, body = t('/apisix/admin/routes/1', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"plugins": { | ||
"basic-auth": { | ||
"hide_credentials": false | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/echo" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- response_body | ||
passed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only configured the route, you also need to verify whether the route is effective |
||
|
||
|
||
|
||
=== TEST 26: verify Authorization with foo/bar, request header should not hidden | ||
--- request | ||
GET /echo | ||
--- more_headers | ||
Authorization: Basic Zm9vOmJhcg== | ||
--- response_headers | ||
Authorization: Basic Zm9vOmJhcg== |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid the blank line here? Other files don't add a blank line among localized variables.