Skip to content

feat: Autogenerate admin api key if not passed#11080

Merged
moonming merged 87 commits into
apache:masterfrom
Revolyssup:revolyssup/autogeneratekey
Apr 10, 2024
Merged

feat: Autogenerate admin api key if not passed#11080
moonming merged 87 commits into
apache:masterfrom
Revolyssup:revolyssup/autogeneratekey

Conversation

@Revolyssup

@Revolyssup Revolyssup commented Mar 22, 2024

Copy link
Copy Markdown
Contributor

Description

Fixes # (issue)
This PR is part of the proposal which removes hardcoding of sensitive API keys by autogenerating them at either application level(APISIX) or deployment level.

Notes

  1. The admin api key is autogenerated if it is passed empty.
  2. All the CLI tests now retrieve the key from config.yaml first before sending request to the API server, which is why the change in tests related to CLI.
  3. If key is null or a non empty string, then it will not be changed.
  4. This is not recommended at application level and recommended at infra level. For instance, the helm chart can autogenerate key. Therefore users are advised to pass their own unique key. For simpler local, single host deployments, APISIX can autogenerate the key which the users can read from config.yaml.

Related PRs:
apache/apisix-helm-chart#740
apache/apisix-docker#548

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)

@Revolyssup
Revolyssup marked this pull request as draft March 22, 2024 08:24
@Revolyssup
Revolyssup marked this pull request as ready for review March 27, 2024 08:39

@kayx23 kayx23 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.

a few suggestions for the docs

Comment thread docs/en/latest/admin-api.md
Comment thread docs/en/latest/admin-api.md Outdated
Comment thread docs/zh/latest/plugins/proxy-mirror.md Outdated
Comment thread apisix/core/id.lua
Comment thread apisix/core/id.lua Outdated
-- Check if deployment.admin.admin_key is not nil and it's an empty string
local admin_keys = default_conf.deployment
and default_conf.deployment.admin
and default_conf.deployment.admin.admin_key

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.

I think you can try core.table.try_read_attr to improve your code.

Comment thread apisix/core/id.lua Outdated
local local_conf_path = profile:yaml_path("config")
local ok, err = write_file(local_conf_path, yaml_conf)
if not ok then
log.error(err)

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.

I think you should panic if we can't write file.

Comment thread apisix/core/id.lua
Comment thread conf/config-default.yaml
access_log: logs/access_stream.log # Location of the stream access log.
access_log_format: "$remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time" # Customize log format: http://nginx.org/en/docs/varindex.html
access_log_format: |
"$remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time" # Customize log format: http://nginx.org/en/docs/varindex.html

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.

This change has nothing to do with this modification. If you think it is necessary, it is recommended to open a new PR to deal with it.

Comment thread conf/config-default.yaml
Comment on lines +212 to +213
access_log_format: |
"$remote_addr - $remote_user [$time_local] $http_host \"$request\" $status $body_bytes_sent $request_time \"$http_referer\" \"$http_user_agent\" $upstream_addr $upstream_status $upstream_response_time \"$upstream_scheme://$upstream_host$upstream_uri\""

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.

ditto

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.

The lyaml.dump function which is being used here to convert lua table into yaml, messes up the formatting and adds a new line automatically. Using | operator avoids this issue by allowing for multiline string

moonming
moonming previously approved these changes Apr 4, 2024
Comment thread apisix/core/id.lua
Comment thread apisix/core/id.lua Outdated
local open = io.open


local try_read_attr = require("apisix.core.table").try_read_attr

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.

please maintain code beautification (equals on top of each other)

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.

done

Comment thread apisix/core/id.lua Outdated
local ngx_exit = ngx.exit

local _M = {version = 0.1}
local _M = { version = 0.1 }

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.

please revert this change.

Comment thread t/fuzzing/simple_http.py Outdated
print("Key not found in the YAML file.")
return
key = key.replace('"', '')
print("the key is",key)

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.

Suggested change
print("the key is",key)
print("the key is", key)

Comment thread .github/workflows/source-install.yml Outdated
run: |
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
wget https://github.com/mikefarah/yq/releases/download/3.4.1/yq_linux_amd64 -O /usr/bin/yq && sudo chmod +x /usr/bin/yq

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 block has 4 indents, where as everywhere in this file there are 2 indents.

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.

fixed

Comment thread .github/workflows/source-install.yml Outdated
cat logs/error.log
exit 125
fi
result_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://127.0.0.1:9080/get`

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 change is not needed.

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.

fixed

Comment thread conf/config-default.yaml
access_log: logs/access_stream.log # Location of the stream access log.
access_log_format: "$remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time" # Customize log format: http://nginx.org/en/docs/varindex.html
access_log_format: |
"$remote_addr [$time_local] $protocol $status $bytes_sent $bytes_received $session_time" # Customize log format: http://nginx.org/en/docs/varindex.html

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.

@Revolyssup please address this comment.

@Revolyssup

Copy link
Copy Markdown
Contributor Author

@shreemaan-abhishek Comment addressed here #11080 (comment)

Comment thread docs/en/latest/plugins/api-breaker.md Outdated
You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command:

```bash
admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')

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.

Suggested change
admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')

Remove extra space. Please replace all.

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.

done

@kayx23 kayx23 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.

Other doc changes LGTM.

Comment thread apisix/core/id.lua Outdated
Comment thread apisix/core/id.lua Outdated
Comment thread apisix/core/id.lua
Signed-off-by: Ashish Tiwari <[email protected]>

@kayx23 kayx23 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.

doc changes LGTM

@Revolyssup
Revolyssup requested review from moonming and starsz April 9, 2024 13:15
@moonming
moonming merged commit c6b9f99 into apache:master Apr 10, 2024
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.

5 participants