注:
- 此教程需要访问 Oracle Cloud。要注册免费账户,请参阅开始使用 Oracle Cloud Infrastructure Free Tier 。
- 它使用 Oracle Cloud Infrastructure 身份证明、租户和区间示例值。完成实验室时,请将这些值替换为特定于云环境的那些值。
使用 OCI 审计实施多云安全性,以从 OCI 身份和访问管理中捕获事件
简介
Oracle Cloud Infrastructure Identity and Access Management (OCI IAM) 身份域根据管理员和最终用户执行的操作生成审计数据。针对创建或更新用户帐户或组成员资格以及成功或失败的登录尝试等操作生成审计事件。
要访问此审计数据,您可以直接在 Oracle Cloud Infrastructure (OCI) 控制台中生成报告,也可以将这些事件发送到外部安全监视解决方案,该解决方案可能在外部云平台中运行。组织通常会利用第三方安全事件和事件管理 (SIEM) 解决方案,一个典型的要求是将 OCI IAM 审计数据摄取到 SIEM 以进行威胁分析和合规性。满足该需求的一种方法是利用 OCI IAM 身份域 AuditEvents API,但身份域提供了另外一个选项,这些选项对于某些组织更易于实施,并且支持在多云环境中共享审计事件。
OCI 审计是一种 OCI 服务,它自动将所有受支持的 OCI API 的调用记录为事件。您可能选择从 OCI 审计服务提取 OCI IAM 审计事件而不使用 OCI IAM 身份域 API 的几个常见原因。OCI 审计具有以下优势:
- 对于具有多个身份域的客户,所有审计数据都在一个中心位置提供。使用 OCI IAM API,您需要单独查询每个身份域,这会产生额外的开销。
- 数据可以推送到 SIEM 等外部系统,而 OCI IAM AuditEvents API 需要轮询。
- OCI 审计会将事件数据存储一年;OCI IAM 将审计事件存储 90 天。
目标
将在 OCI IAM 身份域中生成的审计事件同步到外部存储。
先决条件
- 具有身份域的 OCI 租户
- 具有 Azure Log Analytics 工作区的 Azure 云账户
任务 1:了解如何利用 OCI 审计从 OCI IAM 捕获审计事件
为了演示使用 OCI 审计从 OCI IAM 捕获审计事件的方法,我们将使用以下方案:
您的租户具有两个 OCI IAM 身份域。租户默认域(用于租户管理)的域类型为“免费”。我们将调用管理员域。还有一个域类型为 Oracle Apps Premium 的附加身份域,所有员工都预配有权访问 Oracle SaaS 和内部部署应用。我们将其称为雇员域。从 Microsoft Azure AD 将账户预配到员工域中。这可能通过以下几种方式之一(跨域身份管理系统 [SCIM],即时预配或批量用户导入)。您还有一个 SIEM 解决方案(例如 Microsoft Sentinel),可从环境中的多个系统摄取审计数据,并且要求近乎实时地将两个身份域中的所有审计事件发布到 SIEM。
我们要解决这些需求的整体方法包括使用 OCI 审计服务、 OCI 事件服务、 OCI 函数服务和 Azure Log Analytics Workspace 。基本流如下:
- OCI IAM 身份域将审计事件写入 OCI 审计。
- OCI 事件服务具有一个规则,用于在 OCI IAM 中监视特定审计事件类型的 OCI 审计。触发规则时,它会调用 OCI 函数中的函数。
- 该函数将在其有效负载中接收原始审计日志,并调用 Azure Log Analytics 数据收集器 API 将数据发送到 Azure Log Analytics Workspace 。
- Azure Log Analytics Workspace 充当 Microsoft Sentinel 的数据存储。

任务 2:使用 OCI 审计从 OCI IAM 获取审计事件
-
此示例假定您在 Azure 门户中设置了 Azure Log Analytics 工作区。设置后,复制工作区 ID 以及您的工作区(名称、设置、代理、Log Analytics 代理说明)下的主(或辅助)键。在使用 OCI 函数编写自定义函数时,您需要这些值。

-
以租户管理员身份登录到 OCI 控制台。
-
编写将随 OCI 功能部署的定制功能。以下代码示例使用 Python,但是您可以轻松地以您选择的语言实现函数。我们按三个简单步骤添加函数代码。
-
创建一个名为
func.py的 Python 文件并添加以下必需的 import 语句和参数声明。参数值必须与 Log Analytics 工作区环境匹配。您可以在 Azure Portal 中的 Log Analytics 工作区、您的工作区名称和代理管理页下找到客户 ID(门户中的工作区 ID)和共享密钥值。日志类型参数是一个友好名称,用于在 Azure Log Analytics 工作区中定义新的(或选择现有的)定制日志,以及用于数据上载的目标位置。您可以使用 OCI 密钥服务安全地访问 OCI 函数中的值,而不是对 Log Analytics 工作区客户 ID 和共享密钥值进行硬编码。有关在代码中使用 OCI 密钥的步骤的详细说明,请参阅以下文章:
为了本教程的目的,我们假设您具有存储在 Vault 中的密钥,并设置 IAM 策略来授予对 OCI 函数实例主用户的访问权限。
{.python .numberLines .lineAnchors} import oci import io import json import requests import datetime import hashlib import hmac import base64 import logging from fdk import response # Get instance principal context, and initialize secrets client signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner() secret_client = oci.secrets.SecretsClient(config={}, signer=signer) # Replace values below with the ocids of your secrets customer_id_ocid = "ocid1.vaultsecret.oc1.<customer_id_ocid>" shared_key_ocid = "ocid1.vaultsecret.oc1.<shared_key_ocid>" # Retrieve secret def read_secret_value(secret_client, secret_id): response = secret_client.get_secret_bundle(secret_id) base64_Secret_content = response.data.secret_bundle_content.content base64_secret_bytes = base64_Secret_content.encode('ascii') base64_message_bytes = base64.b64decode(base64_secret_bytes) secret_content = base64_message_bytes.decode('ascii') return secret_content # Retrieve the customer ID using the secret client. customer_id = read_secret_value(secret_client, customer_id_ocid) # For the shared key, use either the primary or the secondary Connected Sources client authentication key _shared_key = read_secret_value(secret_client, shared_key_ocid) # The log type is the name of the event that is being submitted log_type = 'OCILogging' -
添加以下代码行以定义对 Azure 数据上载端点进行安全 REST 调用的任务。您可能需要验证此代码以根据 Microsoft 最新端点或版本语法验证语法。
注:此代码的部分内容是从以下 Microsoft 文档复制的。
{.python .numberLines .lineAnchors} # Build the API signature def build_signature(customer_id, shared_key, date, content_length, method, content_type, resource): x_headers = 'x-ms-date:' + date string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource bytes_to_hash = bytes(string_to_hash, encoding= "utf-8" ) decoded_key = base64.b64decode(shared_key) encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest()).decode() authorization = "SharedKey {}:{}" .format(customer_id,encoded_hash) return authorization # Build and send a request to the POST API def post_data(customer_id, shared_key, body, log_type, logger): method = 'POST' content_type = 'application/json' resource = '/api/logs' rfc1123date = datetime.datetime.utcnow().strftime( '%a, %d %b %Y %H:%M:%S GMT' ) content_length = len(body) signature = build_signature(customer_id, shared_key, rfc1123date, content_length, method, content_type, resource) uri = 'https://' + customer_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01' headers = { 'content-type' : content_type, 'Authorization' : signature, 'Log-Type' : log_type, 'x-ms-date' : rfc1123date } response = requests.post(uri,data=body, headers=headers) if (response.status_code >= 200 and response.status_code <= 299 ): logger.info( 'Upload accepted' ) else : logger.info( "Error during upload. Response code: {}" .format(response.status_code)) print(response.text) -
将以下代码块添加到
func.py代码文件以定义运行入口点和框架详细信息。除非要添加可选功能(例如高级数据解析、格式设置或定制异常处理),否则不需要进行任何更改。{.python .numberLines .lineAnchors} """ Entrypoint and initialization """ def handler(ctx, data: io.BytesIO= None ): logger = logging.getLogger() try : _log_body = data.getvalue() post_data(_customer_id, _shared_key, _log_body, _log_type, logger) except Exception as err: logger.error( "Error in main process: {}" .format(str(err))) return response.Response( ctx, response_data=json.dumps({ "status" : "Success" }), headers={ "Content-Type" : "application/json" } )
-
-
按照本指南使用 CloudShell 部署函数:CloudShell 上的函数快速入门。
-
在 OCI 事件服务中创建规则,该规则查找符合条件的审计日志,并调用在步骤 3 和步骤 4 中创建和部署的功能。
-
通过在控制台搜索栏或在左侧服务菜单内的搜索栏中搜索“事件”,转到事件服务。在搜索结果中单击规则。


-
使用条件创建规则,以查找管理员和员工域中的用户创建、更新或删除事件。
-
用户创建、更新和删除事件类型将在身份服务下可用。
-
使用两个身份域的 GUID 添加其他“属性”条件。
注:此选项是可选的。默认情况下,云账户中所有身份域的审计事件都匹配。但是,如果您只对一部分身份域感兴趣,可以显式指定范围内的域。)您可以在身份域概览页上找到身份域 GUID。
-
在操作面板中,选择函数操作类型,然后选择在上面的步骤 3 和步骤 4 中创建和部署的功能。

-
-
-
现在,当您在任一身份域中创建、更新或删除用户时,将使用原始审计日志作为有效负载调用 OCI 函数。然后,OCI 函数将数据发布到 Azure Log Analytics 工作区。您将看到名为
OCILogging_CL的定制表中的数据。
相关链接
确认
- Authors :Ari Kermaier(技术人员咨询成员、OCI 身份)和 Manoj Gaddam(OCI 身份首席产品经理)
更多学习资源
探索 docs.oracle.com/learn 上的其他实验室,或者访问 Oracle Learning YouTube 频道上的更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 成为 Oracle Learning Explorer。
有关产品文档,请访问 Oracle 帮助中心。
Implement multicloud security using OCI Audit to capture events from OCI Identity and Access Management
F83130-01
June 2023
Copyright © 2023, Oracle and/or its affiliates.