1 - API审计日志


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

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

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

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

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

    • 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 命名空间中。

    启用日志审计:

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

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

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

    通过CLI查看

    通过Rancher Web GUI查看

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

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

    Rancher Workload

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

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

    Select Audit Log

    收集API审计日志

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

    如果设置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. "stageTimestamp": "2018-07-20 10:22:43 +0800"
    15. }

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

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