1 - API审计日志


Rancher附带API审计功能,用以记录每个用户发起的系统事件信息。您可以知道发生了什么、何时发生、谁发起的、以及事件对集群的影响。API审计也记录Rancher API的所有请求和响应,包括使用Rancher UI和通过编程使用Rancher API的所有其他用途。

您可以在Rancher安装或升级期间启用API审计。

一、API审计日志设置选项

以下定义了有关审计日志记录的内容以及包含哪些数据的规则:

参数描述
AUDIT_LEVEL0 - 禁用审计日志(默认设置)1 - 记录事件元数据2 - 记录事件元数据和请求内容. 3 - 记录事件元数据、请求内容和响应内容。请求/响应对的每个日志事务使用相同的auditID值。有关显示每个等级设置记录的具体内容,请参阅审计级别日志记录
AUDIT_LOG_PATHRancher Server API的日志路径,默认路径是:/var/log/auditlog/rancher-api-audit.log,您可以将日志目录挂载到主机。用法示例: AUDIT_LOG_PATH=/my/custom/path/
AUDIT_LOG_MAXAGE定义了保留旧审计日志文件的最大天数,默认为10天。
AUDIT_LOG_MAXBACKUP定义要保留的最大审计日志文件个数,默认值为10。
AUDIT_LOG_MAXSIZE定义单个审计日志文件的最大值(以兆字节为单位),默认大小为100M。

二、审计日志级别

下表显示了每个AUDIT_LEVEL设置,记录的API事务具体内容。

AUDIT_LEVEL设置Request HeaderRequest BodyResponse HeaderResponse Body
0
1
2
3

三、启用API审计日志

要启用API审计日志,请停止运行的Rancher容器,然后使用以下命令重新启动它。此命令包含打开API审计的参数,有关与API审计相关的每个AUDIT_LEVEL使用的详细信息,请参阅API审计日志设置选项

单节点安装启用

  1. docker run -d --restart=unless-stopped \
  2. -p 80:80 -p 443:443 \
  3. -v <主机路径>:/var/lib/rancher/ \
  4. -v /root/var/log/auditlog:/var/log/auditlog \
  5. -e AUDIT_LEVEL=3 \
  6. -e AUDIT_LOG_PATH=/var/log/auditlog/rancher-api-audit.log \
  7. -e AUDIT_LOG_MAXAGE=20 \
  8. -e AUDIT_LOG_MAXBACKUP=20 \
  9. -e AUDIT_LOG_MAXSIZE=100 \
  10. rancher/rancher:stable (或者rancher/rancher:latest)

HA安装启用

  • RKE HA 安装 (仅支持Rancher v2.0.8之前的版本)

在RKE 配置文件中,给Rancher容器添加以下参数:

  • 添加API审计功能参数到Rancher容器的args中;
  • 在容器的volumemount参数中声明一个mountPath
  • volumes配置中声明一个path;

示例配置:

  1. ...
  2. containers:
  3. - image: rancher/rancher:stable (或者rancher/rancher:latest)
  4. imagePullPolicy: Always
  5. name: cattle-server
  6. args: ["--audit-log-path", "/var/log/auditlog/rancher-api-audit.log", "--audit-log-maxbackup", "5", "--audit-log-maxsize", "50", "--audit-level", "2"]
  7. ports:
  8. - containerPort: 80
  9. protocol: TCP
  10. - containerPort: 443
  11. protocol: TCP
  12. volumeMounts:
  13. - mountPath: /etc/rancher/ssl
  14. name: cattle-keys-volume
  15. readOnly: true
  16. - mountPath: /var/log/auditlog
  17. name: audit-log-dir
  18. volumes:
  19. - name: cattle-keys-volume
  20. secret:
  21. defaultMode: 420
  22. secretName: cattle-keys-server
  23. - name: audit-log-dir
  24. hostPath:
  25. path: /var/log/rancher/auditlog
  26. type: Directory
  • Chart HA安装(适用于Rancherv2.1.0及以后版本)

在使用Helm chart安装Rancher时启用API审计功能,会在Rancher pod中创建一个rancher-audit-log sidecar容器。 此容器将API审计日志发送到标准输出,可以通过查看容器日志的方式查看API审计日志rancher-audit-log容器位于rancher pod所在的cattle-system 命名空间中。

启用日志审计:

在通过Chart安装Rancher时,添加参数—set auditLog.level=1。参数使用可参考Chart设置参数

四、查看API审计日志

Rancher单节点安装

单节点安装Rancher server时,与主机系统共享AUDIT_LOG_PATH目录(默认:/var/log/auditlog)。日志可以通过标准CLI工具进行查看,也可以转发到日志收集工具,如Fluentd,Filebeat,Logstash等。

Rancher HA安装

在使用Helm chart安装Rancher时启用API审计功能,会在Rancher pod中创建一个rancher-audit-log sidecar容器。 此容器将API审计日志发送到标准输出,可以通过查看容器日志的方式查看API审计日志。

rancher-audit-log 容器位于rancher pod所在的cattle-system 命名空间中。

通过CLI查看

  1. kubectl --kubeconfig=kube_configxxx.yml -n cattle-system logs -f rancher-84d886bdbb-s4s69 rancher-audit-log

通过Rancher Web GUI查看

  • 从下拉菜单中, 切换到 Cluster: local > System项目下

Local Cluster: System Project

  • 工作负载菜单中,找到cattle-system命名空间,并找到rancher工作负载。

Rancher Workload

  • 通过 Ellipsis (…) > View Logs查看rancher pods日志

View Logs

  • Logs下拉菜单中,选择rancher-audit-log

Select Audit Log

收集API审计日志

可以为集群启用Rancher的内置日志收集功能,将审计和其他服务日志发送到受支持的收集服务。

审计日志样本

启用审核后,Rancher以JSON的形式记录每个API请求或响应。以下每个代码示例都提供了如何标识每个API事务的示例。

如果设置AUDIT_LEVEL为1,Rancher会记录每个API请求的元数据标头,但不会记录正文。标题提供有关API事务的基本信息,例如事务的ID,发起事务的人员,事件发生的时间等。

  1. {
  2. "auditID": "30022177-9e2e-43d1-b0d0-06ef9d3db183",
  3. "requestURI": "/v3/schemas",
  4. "sourceIPs": [
  5. "::1"
  6. ],
  7. "user": {
  8. "name": "user-f4tt2",
  9. "group": [
  10. "system:authenticated"
  11. ]
  12. },
  13. "verb": "GET",
  14. "stage": "RequestReceived",
  15. "stageTimestamp": "2018-07-20 10:22:43 +0800"
  16. }

如果设置AUDIT_LEVEL为2,Rancher会记录每个API请求的元数据标题和正文。下面的代码示例描述了一个API请求,包含其元数据头和正文。

  1. {
  2. "auditID": "ef1d249e-bfac-4fd0-a61f-cbdcad53b9bb",
  3. "requestURI": "/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx",
  4. "sourceIPs": [
  5. "::1"
  6. ],
  7. "user": {
  8. "name": "user-f4tt2",
  9. "group": [
  10. "system:authenticated"
  11. ]
  12. },
  13. "verb": "PUT",
  14. "stage": "RequestReceived",
  15. "stageTimestamp": "2018-07-20 10:28:08 +0800",
  16. "requestBody": {
  17. "hostIPC": false,
  18. "hostNetwork": false,
  19. "hostPID": false,
  20. "paused": false,
  21. "annotations": {},
  22. "baseType": "workload",
  23. "containers": [
  24. {
  25. "allowPrivilegeEscalation": false,
  26. "image": "nginx",
  27. "imagePullPolicy": "Always",
  28. "initContainer": false,
  29. "name": "nginx",
  30. "ports": [
  31. {
  32. "containerPort": 80,
  33. "dnsName": "nginx-nodeport",
  34. "kind": "NodePort",
  35. "name": "80tcp01",
  36. "protocol": "TCP",
  37. "sourcePort": 0,
  38. "type": "/v3/project/schemas/containerPort"
  39. }
  40. ],
  41. "privileged": false,
  42. "readOnly": false,
  43. "resources": {
  44. "type": "/v3/project/schemas/resourceRequirements",
  45. "requests": {},
  46. "limits": {}
  47. },
  48. "restartCount": 0,
  49. "runAsNonRoot": false,
  50. "stdin": true,
  51. "stdinOnce": false,
  52. "terminationMessagePath": "/dev/termination-log",
  53. "terminationMessagePolicy": "File",
  54. "tty": true,
  55. "type": "/v3/project/schemas/container",
  56. "environmentFrom": [],
  57. "capAdd": [],
  58. "capDrop": [],
  59. "livenessProbe": null,
  60. "volumeMounts": []
  61. }
  62. ],
  63. "created": "2018-07-18T07:34:16Z",
  64. "createdTS": 1531899256000,
  65. "creatorId": null,
  66. "deploymentConfig": {
  67. "maxSurge": 1,
  68. "maxUnavailable": 0,
  69. "minReadySeconds": 0,
  70. "progressDeadlineSeconds": 600,
  71. "revisionHistoryLimit": 10,
  72. "strategy": "RollingUpdate"
  73. },
  74. "deploymentStatus": {
  75. "availableReplicas": 1,
  76. "conditions": [
  77. {
  78. "lastTransitionTime": "2018-07-18T07:34:38Z",
  79. "lastTransitionTimeTS": 1531899278000,
  80. "lastUpdateTime": "2018-07-18T07:34:38Z",
  81. "lastUpdateTimeTS": 1531899278000,
  82. "message": "Deployment has minimum availability.",
  83. "reason": "MinimumReplicasAvailable",
  84. "status": "True",
  85. "type": "Available"
  86. },
  87. {
  88. "lastTransitionTime": "2018-07-18T07:34:16Z",
  89. "lastTransitionTimeTS": 1531899256000,
  90. "lastUpdateTime": "2018-07-18T07:34:38Z",
  91. "lastUpdateTimeTS": 1531899278000,
  92. "message": "ReplicaSet \"nginx-64d85666f9\" has successfully progressed.",
  93. "reason": "NewReplicaSetAvailable",
  94. "status": "True",
  95. "type": "Progressing"
  96. }
  97. ],
  98. "observedGeneration": 2,
  99. "readyReplicas": 1,
  100. "replicas": 1,
  101. "type": "/v3/project/schemas/deploymentStatus",
  102. "unavailableReplicas": 0,
  103. "updatedReplicas": 1
  104. },
  105. "dnsPolicy": "ClusterFirst",
  106. "id": "deployment:default:nginx",
  107. "labels": {
  108. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  109. },
  110. "name": "nginx",
  111. "namespaceId": "default",
  112. "projectId": "c-bcz5t:p-fdr4s",
  113. "publicEndpoints": [
  114. {
  115. "addresses": [
  116. "10.64.3.58"
  117. ],
  118. "allNodes": true,
  119. "ingressId": null,
  120. "nodeId": null,
  121. "podId": null,
  122. "port": 30917,
  123. "protocol": "TCP",
  124. "serviceId": "default:nginx-nodeport",
  125. "type": "publicEndpoint"
  126. }
  127. ],
  128. "restartPolicy": "Always",
  129. "scale": 1,
  130. "schedulerName": "default-scheduler",
  131. "selector": {
  132. "matchLabels": {
  133. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  134. },
  135. "type": "/v3/project/schemas/labelSelector"
  136. },
  137. "state": "active",
  138. "terminationGracePeriodSeconds": 30,
  139. "transitioning": "no",
  140. "transitioningMessage": "",
  141. "type": "deployment",
  142. "uuid": "f998037d-8a5c-11e8-a4cf-0245a7ebb0fd",
  143. "workloadAnnotations": {
  144. "deployment.kubernetes.io/revision": "1",
  145. "field.cattle.io/creatorId": "user-f4tt2"
  146. },
  147. "workloadLabels": {
  148. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  149. },
  150. "scheduling": {
  151. "node": {}
  152. },
  153. "description": "my description",
  154. "volumes": []
  155. }
  156. }

如果您设置AUDIT_LEVEL为3,Rancher将记录:

  • 每个API请求的元数据标头和正文。
  • 每个API响应的元数据标头和正文。

下面的代码示例描述了一个API请求,它有元数据头和正文。

  1. {
  2. "auditID": "a886fd9f-5d6b-4ae3-9a10-5bff8f3d68af",
  3. "requestURI": "/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx",
  4. "sourceIPs": [
  5. "::1"
  6. ],
  7. "user": {
  8. "name": "user-f4tt2",
  9. "group": [
  10. "system:authenticated"
  11. ]
  12. },
  13. "verb": "PUT",
  14. "stage": "RequestReceived",
  15. "stageTimestamp": "2018-07-20 10:33:06 +0800",
  16. "requestBody": {
  17. "hostIPC": false,
  18. "hostNetwork": false,
  19. "hostPID": false,
  20. "paused": false,
  21. "annotations": {},
  22. "baseType": "workload",
  23. "containers": [
  24. {
  25. "allowPrivilegeEscalation": false,
  26. "image": "nginx",
  27. "imagePullPolicy": "Always",
  28. "initContainer": false,
  29. "name": "nginx",
  30. "ports": [
  31. {
  32. "containerPort": 80,
  33. "dnsName": "nginx-nodeport",
  34. "kind": "NodePort",
  35. "name": "80tcp01",
  36. "protocol": "TCP",
  37. "sourcePort": 0,
  38. "type": "/v3/project/schemas/containerPort"
  39. }
  40. ],
  41. "privileged": false,
  42. "readOnly": false,
  43. "resources": {
  44. "type": "/v3/project/schemas/resourceRequirements",
  45. "requests": {},
  46. "limits": {}
  47. },
  48. "restartCount": 0,
  49. "runAsNonRoot": false,
  50. "stdin": true,
  51. "stdinOnce": false,
  52. "terminationMessagePath": "/dev/termination-log",
  53. "terminationMessagePolicy": "File",
  54. "tty": true,
  55. "type": "/v3/project/schemas/container",
  56. "environmentFrom": [],
  57. "capAdd": [],
  58. "capDrop": [],
  59. "livenessProbe": null,
  60. "volumeMounts": []
  61. }
  62. ],
  63. "created": "2018-07-18T07:34:16Z",
  64. "createdTS": 1531899256000,
  65. "creatorId": null,
  66. "deploymentConfig": {
  67. "maxSurge": 1,
  68. "maxUnavailable": 0,
  69. "minReadySeconds": 0,
  70. "progressDeadlineSeconds": 600,
  71. "revisionHistoryLimit": 10,
  72. "strategy": "RollingUpdate"
  73. },
  74. "deploymentStatus": {
  75. "availableReplicas": 1,
  76. "conditions": [
  77. {
  78. "lastTransitionTime": "2018-07-18T07:34:38Z",
  79. "lastTransitionTimeTS": 1531899278000,
  80. "lastUpdateTime": "2018-07-18T07:34:38Z",
  81. "lastUpdateTimeTS": 1531899278000,
  82. "message": "Deployment has minimum availability.",
  83. "reason": "MinimumReplicasAvailable",
  84. "status": "True",
  85. "type": "Available"
  86. },
  87. {
  88. "lastTransitionTime": "2018-07-18T07:34:16Z",
  89. "lastTransitionTimeTS": 1531899256000,
  90. "lastUpdateTime": "2018-07-18T07:34:38Z",
  91. "lastUpdateTimeTS": 1531899278000,
  92. "message": "ReplicaSet \"nginx-64d85666f9\" has successfully progressed.",
  93. "reason": "NewReplicaSetAvailable",
  94. "status": "True",
  95. "type": "Progressing"
  96. }
  97. ],
  98. "observedGeneration": 2,
  99. "readyReplicas": 1,
  100. "replicas": 1,
  101. "type": "/v3/project/schemas/deploymentStatus",
  102. "unavailableReplicas": 0,
  103. "updatedReplicas": 1
  104. },
  105. "dnsPolicy": "ClusterFirst",
  106. "id": "deployment:default:nginx",
  107. "labels": {
  108. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  109. },
  110. "name": "nginx",
  111. "namespaceId": "default",
  112. "projectId": "c-bcz5t:p-fdr4s",
  113. "publicEndpoints": [
  114. {
  115. "addresses": [
  116. "10.64.3.58"
  117. ],
  118. "allNodes": true,
  119. "ingressId": null,
  120. "nodeId": null,
  121. "podId": null,
  122. "port": 30917,
  123. "protocol": "TCP",
  124. "serviceId": "default:nginx-nodeport",
  125. "type": "publicEndpoint"
  126. }
  127. ],
  128. "restartPolicy": "Always",
  129. "scale": 1,
  130. "schedulerName": "default-scheduler",
  131. "selector": {
  132. "matchLabels": {
  133. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  134. },
  135. "type": "/v3/project/schemas/labelSelector"
  136. },
  137. "state": "active",
  138. "terminationGracePeriodSeconds": 30,
  139. "transitioning": "no",
  140. "transitioningMessage": "",
  141. "type": "deployment",
  142. "uuid": "f998037d-8a5c-11e8-a4cf-0245a7ebb0fd",
  143. "workloadAnnotations": {
  144. "deployment.kubernetes.io/revision": "1",
  145. "field.cattle.io/creatorId": "user-f4tt2"
  146. },
  147. "workloadLabels": {
  148. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  149. },
  150. "scheduling": {
  151. "node": {}
  152. },
  153. "description": "my decript",
  154. "volumes": []
  155. }
  156. }

下面的代码示例描述了一个API响应,其中包含它的元数据头和正文。

  1. {
  2. "auditID": "a886fd9f-5d6b-4ae3-9a10-5bff8f3d68af",
  3. "responseStatus": "200",
  4. "stage": "ResponseComplete",
  5. "stageTimestamp": "2018-07-20 10:33:06 +0800",
  6. "responseBody": {
  7. "actionLinks": {
  8. "pause": "https://localhost:8443/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx?action=pause",
  9. "resume": "https://localhost:8443/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx?action=resume",
  10. "rollback": "https://localhost:8443/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx?action=rollback"
  11. },
  12. "annotations": {},
  13. "baseType": "workload",
  14. "containers": [
  15. {
  16. "allowPrivilegeEscalation": false,
  17. "image": "nginx",
  18. "imagePullPolicy": "Always",
  19. "initContainer": false,
  20. "name": "nginx",
  21. "ports": [
  22. {
  23. "containerPort": 80,
  24. "dnsName": "nginx-nodeport",
  25. "kind": "NodePort",
  26. "name": "80tcp01",
  27. "protocol": "TCP",
  28. "sourcePort": 0,
  29. "type": "/v3/project/schemas/containerPort"
  30. }
  31. ],
  32. "privileged": false,
  33. "readOnly": false,
  34. "resources": {
  35. "type": "/v3/project/schemas/resourceRequirements"
  36. },
  37. "restartCount": 0,
  38. "runAsNonRoot": false,
  39. "stdin": true,
  40. "stdinOnce": false,
  41. "terminationMessagePath": "/dev/termination-log",
  42. "terminationMessagePolicy": "File",
  43. "tty": true,
  44. "type": "/v3/project/schemas/container"
  45. }
  46. ],
  47. "created": "2018-07-18T07:34:16Z",
  48. "createdTS": 1531899256000,
  49. "creatorId": null,
  50. "deploymentConfig": {
  51. "maxSurge": 1,
  52. "maxUnavailable": 0,
  53. "minReadySeconds": 0,
  54. "progressDeadlineSeconds": 600,
  55. "revisionHistoryLimit": 10,
  56. "strategy": "RollingUpdate"
  57. },
  58. "deploymentStatus": {
  59. "availableReplicas": 1,
  60. "conditions": [
  61. {
  62. "lastTransitionTime": "2018-07-18T07:34:38Z",
  63. "lastTransitionTimeTS": 1531899278000,
  64. "lastUpdateTime": "2018-07-18T07:34:38Z",
  65. "lastUpdateTimeTS": 1531899278000,
  66. "message": "Deployment has minimum availability.",
  67. "reason": "MinimumReplicasAvailable",
  68. "status": "True",
  69. "type": "Available"
  70. },
  71. {
  72. "lastTransitionTime": "2018-07-18T07:34:16Z",
  73. "lastTransitionTimeTS": 1531899256000,
  74. "lastUpdateTime": "2018-07-18T07:34:38Z",
  75. "lastUpdateTimeTS": 1531899278000,
  76. "message": "ReplicaSet \"nginx-64d85666f9\" has successfully progressed.",
  77. "reason": "NewReplicaSetAvailable",
  78. "status": "True",
  79. "type": "Progressing"
  80. }
  81. ],
  82. "observedGeneration": 2,
  83. "readyReplicas": 1,
  84. "replicas": 1,
  85. "type": "/v3/project/schemas/deploymentStatus",
  86. "unavailableReplicas": 0,
  87. "updatedReplicas": 1
  88. },
  89. "dnsPolicy": "ClusterFirst",
  90. "hostIPC": false,
  91. "hostNetwork": false,
  92. "hostPID": false,
  93. "id": "deployment:default:nginx",
  94. "labels": {
  95. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  96. },
  97. "links": {
  98. "remove": "https://localhost:8443/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx",
  99. "revisions": "https://localhost:8443/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx/revisions",
  100. "self": "https://localhost:8443/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx",
  101. "update": "https://localhost:8443/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx",
  102. "yaml": "https://localhost:8443/v3/project/c-bcz5t:p-fdr4s/workloads/deployment:default:nginx/yaml"
  103. },
  104. "name": "nginx",
  105. "namespaceId": "default",
  106. "paused": false,
  107. "projectId": "c-bcz5t:p-fdr4s",
  108. "publicEndpoints": [
  109. {
  110. "addresses": [
  111. "10.64.3.58"
  112. ],
  113. "allNodes": true,
  114. "ingressId": null,
  115. "nodeId": null,
  116. "podId": null,
  117. "port": 30917,
  118. "protocol": "TCP",
  119. "serviceId": "default:nginx-nodeport"
  120. }
  121. ],
  122. "restartPolicy": "Always",
  123. "scale": 1,
  124. "schedulerName": "default-scheduler",
  125. "selector": {
  126. "matchLabels": {
  127. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  128. },
  129. "type": "/v3/project/schemas/labelSelector"
  130. },
  131. "state": "active",
  132. "terminationGracePeriodSeconds": 30,
  133. "transitioning": "no",
  134. "transitioningMessage": "",
  135. "type": "deployment",
  136. "uuid": "f998037d-8a5c-11e8-a4cf-0245a7ebb0fd",
  137. "workloadAnnotations": {
  138. "deployment.kubernetes.io/revision": "1",
  139. "field.cattle.io/creatorId": "user-f4tt2"
  140. },
  141. "workloadLabels": {
  142. "workload.user.cattle.io/workloadselector": "deployment-default-nginx"
  143. }
  144. }
  145. }