Skip to content

feat: use env var instead of plain text for vault token#8866

Merged
spacewander merged 24 commits into
apache:masterfrom
shreemaan-abhishek:vault-token-env-var
Feb 24, 2023
Merged

feat: use env var instead of plain text for vault token#8866
spacewander merged 24 commits into
apache:masterfrom
shreemaan-abhishek:vault-token-env-var

Conversation

@shreemaan-abhishek

@shreemaan-abhishek shreemaan-abhishek commented Feb 15, 2023

Copy link
Copy Markdown
Contributor

Description

Currently, we use plain text to add vault as a secret resource. Like so:

curl http://127.0.0.1:9180/apisix/admin/secret/vault/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "uri": "https://127.0.0.1:8200",
    "prefix": "apisix",
    "token": "root"
}'

This PR implements a feature that would allow the use of environment variables in the following format: $ENV://NAME_OF_ENV_VAR.

This is the final outcome:

curl http://127.0.0.1:9180/apisix/admin/secret/vault/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "uri": "https://127.0.0.1:8200",
    "prefix": "apisix",
    "token": "$ENV://VAULT_TOKEN"
}'

A part of #8319

cc: @soulbird

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@shreemaan-abhishek
shreemaan-abhishek marked this pull request as ready for review February 15, 2023 09:03
Comment thread t/config-center-yaml/secret.t Outdated
- id: vault/1
prefix: kv/apisix
token: root
token: "$ENV://VAULT_TOKEN"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can no longer use such a configuration?

token: root

@shreemaan-abhishek shreemaan-abhishek Feb 15, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we won't be able to use such a configuration. Let me fix that quickly.

Comment thread ci/centos7-ci.sh Outdated
export_or_prefix
make init
set_coredns
export VAULT_TOKEN="root"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread t/config-center-yaml/secret.t Outdated
--- response_body
0
--- error_log
property "uri" validation failed: failed to match pattern "^[^\\/]+:\\/\\/([\\da-zA-Z.-]+|\\[[\\da-fA-F:]+\\])(:\\d+)?"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this test case? It doesn't look like it has anything to do with your modification?

@shreemaan-abhishek shreemaan-abhishek Feb 16, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just duplicated all the test cases containing the token with just one change: replace the token with the environment variable. Let me do something better. 😂

Comment thread t/config-center-yaml/secret.t Outdated
--- response_body
0
--- error_log
secret manager not exits

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread t/config-center-yaml/secret.t Outdated
prefix: kv/apisix
token: $ENV://VAULT_TOKEN
uri: http://127.0.0.1:8200

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to add this test

Comment thread t/config-center-yaml/secret.t Outdated



=== TEST 21: secret.fetch_by_uri, no sub key value with the token in an env var

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case already exists

Comment thread t/plugin/basic-auth.t
}
}
--- response_body
passed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Comment thread t/config-center-yaml/secret.t Outdated
# limitations under the License.
#
BEGIN {
$ENV{VAULT_TOKEN} = "root";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this added?

@soulbird

Copy link
Copy Markdown
Contributor

IMO, just adding a test case for t/secret/vault.t is enough. The test cases under t/plugin seem redundant, don't you think?

@shreemaan-abhishek

Copy link
Copy Markdown
Contributor Author

The test cases under t/plugin seem redundant, don't you think?

I thought the same. But then I thought they would provide better safety from potential nasty bugs in the future.

@soulbird

Copy link
Copy Markdown
Contributor

The test cases under t/plugin seem redundant, don't you think?

I thought the same. But then I thought they would provide better safety from potential nasty bugs in the future.

ok

soulbird
soulbird previously approved these changes Feb 20, 2023

@spacewander spacewander left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread apisix/secret/vault.lua Outdated
local sub = core.string.sub
local rfind_char = core.string.rfind_char

local env = require("apisix.core.env")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we import it like core.string.sub?

Comment thread apisix/secret/vault.lua
soulbird
soulbird previously approved these changes Feb 22, 2023
Comment thread apisix/secret/vault.lua Outdated
@@ -26,6 +26,7 @@ local norm_path = require("pl.path").normpath
local sub = core.string.sub
local rfind_char = core.string.rfind_char

Copy link
Copy Markdown
Member

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.

@spacewander
spacewander merged commit 14727fc into apache:master Feb 24, 2023
@shreemaan-abhishek
shreemaan-abhishek deleted the vault-token-env-var branch April 27, 2023 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants