View a markdown version of this page

Time-based 政策 - Amazon Bedrock AgentCore

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Time-based 政策

本节中的政策由挂钟时间决定,即促销窗口、营业时间或两者的组合。他们阅读context.system.now,服务部门会根据每个请求提供这些内容。

Time-based 条件是普通的 Cedar when 子句,因此它们与这些页面上的其他所有内容一起构成。本金支票、输入条件、护栏或时间条件都可以放在同一个策略中。

与创作指南的其余部分一样,这些示例是针对示例资源配置中描述的参考网关编写的,因此您可以将它们与其他示例组合在一个策略引擎上。

注意

Time-based 与时间不同。基于时间的条件询问现在几点;时间条件询问此会话中已经发生了什么。有关更多信息,请参阅临时策略示例。

工作原理

在策略评估期间,当前 UTC 时间戳作为评估上下文的一部分提供:

// Current datetime in UTC context.system.now

你可以使用 Cedar 的日期时间函数来创建基于时间的条件:

  • datetime("YYYY-MM-DDTHH:MM:SSZ")— 创建日期时间值

  • duration("Xh")— 创建持续时间(小时、分钟、秒)

  • .toTime()— 从日期时间中提取一天中的时间

  • 比较运算符:<⇐、、>、>=、==

绝对日期和时间范围限制

在特定的日历期内执行政策。

示例: Open-enrollment 窗口

permit ( principal, action == AgentCore::Action::"InsuranceAPI___calculate_premium", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/insurance-gw-a1b2c3d4e5" ) when { context.system.now >= datetime("2025-01-01T00:00:00Z") && context.system.now < datetime("2025-02-01T00:00:00Z") };

用例:仅在 2025 年 1 月的开放注册窗口期间报价保费。

每日重复时间限制

根据每天重复的时间执行策略。

示例:营业时间政策

permit ( principal, action == AgentCore::Action::"InsuranceAPI___disburse_payment", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/insurance-gw-a1b2c3d4e5" ) when { duration("9h") <= context.system.now.toTime() && context.system.now.toTime() <= duration("17h") };

用例:仅在工作时间(世界标准时间每天上午 9 点至下午 5 点)支付款项。

合并的日期和时间限制

将绝对日期与每日时间限制相结合。

示例:包含每日工时的注册窗口

permit ( principal, action == AgentCore::Action::"InsuranceAPI___update_coverage", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/insurance-gw-a1b2c3d4e5" ) when { // Valid dates: February 2025 context.system.now >= datetime("2025-02-01T00:00:00Z") && context.system.now < datetime("2025-03-01T00:00:00Z") && // Valid hours: 9am-9pm UTC daily duration("9h") <= context.system.now.toTime() && context.system.now.toTime() <= duration("21h") };

用例:仅允许在 2025 年 2 月(世界标准时间)每天上午 9 点至晚上 9 点之间更改覆盖范围。

时区处理

所有日期时间值都必须采用 UTC。策略引擎不支持时区转换或时区感知策略。

在策略中指定时间时,请务必使用 UTC。如果您的企业在不同的时区运营,请在创建政策之前将您的当地时间转换为 UTC。