接口密钥与审计日志记录
使用带作用域的接口密钥而非密码保护程序化访问,并启用和读取 Elasticsearch 审计日志,以确认谁执行了什么操作。
接口密钥与审计日志记录 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond Passwords
Applications should not authenticate with a human's username and password. Elasticsearch provides API keys: scoped, revocable credentials ideal for services. Pair them with audit logging to track every security-relevant action.
What Is an API Key
An API key is a credential tied to a set of permissions, with an optional expiration. It can be limited to a subset of the creating user's privileges, following the principle of least privilege.
Creating a Key
Use the create API key endpoint. The response includes an id and api_key value shown only once, so store it securely.
POST /_security/api_key
{
"name": "logging-app",
"expiration": "30d"
}Restricting a Key
Attach role_descriptors to limit what the key can do, even if the creating user has more power. Here the key may only read one index.
POST /_security/api_key
{
"name": "reader",
"role_descriptors": {
"ro": {
"indices": [{ "names": ["logs-*"], "privileges": ["read"] }]
}
}
}Using a Key
Send the base64-encoded id:api_key pair in the Authorization header with the ApiKey scheme.
GET /logs-2024/_search
Authorization: ApiKey VnVhQ2ZHY0JDZGJrU...Revoking Keys
Compromised or retired keys are invalidated immediately, without changing any user's password. You can revoke by id, by name, or all keys owned by a user.
DELETE /_security/api_key
{
"name": "logging-app"
}Why Audit Logging
Audit logs answer the compliance question: who did what, when, and from where. They record authentication attempts, access grants and denials, and configuration changes.
Enabling the Audit Log
Audit logging is turned on in elasticsearch.yml. It is disabled by default because it generates significant volume.
xpack.security.audit.enabled: trueFiltering Events
Tune which events are captured with include/exclude lists to avoid drowning in noise. Common choices keep access_denied and authentication_failed while dropping routine reads.
xpack.security.audit.logfile.events.exclude: [ access_granted ]Reading Audit Output
Audit events are written as structured JSON to a dedicated log file. Each line includes the event type, user, client IP, request path, and outcome, making it easy to ship into Kibana for analysis.
Best Practices
Rotate API keys regularly, scope them tightly, store the audit log on durable storage separate from the cluster, and alert on repeated authentication_failed events that may signal an attack.
Quick Check
Test your understanding of API keys.
Recap
You learned to secure access and accountability:
- API keys are scoped, expiring, revocable credentials for applications.
- Use
role_descriptorsto enforce least privilege. - Audit logging records who did what, when, and from where.
- Filter audit events to manage volume and alert on failed authentications.
常见问题解答
「接口密钥与审计日志记录」课时是免费的吗?
是的 — 「接口密钥与审计日志记录」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「接口密钥与审计日志记录」这节课中我会学到什么?
使用带作用域的接口密钥而非密码保护程序化访问,并启用和读取 Elasticsearch 审计日志,以确认谁执行了什么操作。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「接口密钥与审计日志记录」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 用户身份验证与角色
- 字段级与文档级安全
- TLS/SSL 与网络安全
- 接口密钥与审计日志记录