|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: Understanding policy conditions |
| 4 | + description: Detailed information on policy conditions within Scaleway IAM. |
| 5 | +content: |
| 6 | + h1: Understanding policy conditions |
| 7 | + paragraph: Learn how to use policy conditions to fine-tune access control within Scaleway IAM. |
| 8 | +tags: iam |
| 9 | +dates: |
| 10 | + validation: 2024-03-03 |
| 11 | +categories: |
| 12 | + - iam |
| 13 | + - console |
| 14 | +--- |
| 15 | + |
| 16 | +A condition is an additional layer of restrictions for your rule. You can configure conditions in IAM policies to allow access to specific user agents, IP addresses and on a given date or time. |
| 17 | + |
| 18 | +At Scaleway, IAM conditions are defined using Common Expression Language (CEL) expressions. |
| 19 | + |
| 20 | +<Message type="tip"> |
| 21 | + Refer to the [How to create a policy](/iam/how-to/create-policy/) and [How to manage policies](/iam/how-to/create-policy/) documentation pages to learn where and how to specify a condition. |
| 22 | +</Message> |
| 23 | + |
| 24 | +## Condition expressions |
| 25 | + |
| 26 | +An expression can be compared to a conditional statement in programming. It is a logical statement that evaluates to either true or false. The result determines whether the permission set defined in the rule is applied or not. |
| 27 | + |
| 28 | +Condition expressions are composed of one or several statements that declare a rule based on attributes. Attributes are like characteristics or properties of a resource or a user. For example, an attribute might be a given date or time, or an IP address. |
| 29 | + |
| 30 | +Expressions at Scaleway are defined in CEL, which provides a human-readable and flexible method of creating conditions. |
| 31 | + |
| 32 | +## Common Expression Language |
| 33 | + |
| 34 | +Common Expression Language is used to specify a IAM condition expression. |
| 35 | + |
| 36 | +Expressions consist of one or more statements that declare an attribute-based control rule, and determines whether a permission applies. |
| 37 | + |
| 38 | +IAM conditions use the following CEL features: |
| 39 | + - Variables |
| 40 | + - Operators and Logical Operators |
| 41 | + - Functions |
| 42 | + |
| 43 | +### Variables |
| 44 | + |
| 45 | +Conditions use variables to express attributes. Variables are populated with values based on the context at runtime. |
| 46 | + |
| 47 | +| Name | Type | Description | |
| 48 | +| ------------ | ------------------- | ------ | |
| 49 | +| `request.ip` | String | The IP address of the request. | |
| 50 | +| `request.time` | `google.protobuf.Timestamp` | The time of the request. Represented as a Protobuf object, allowing usage with [associated functions](https://github.com/google/cel-spec/blob/master/doc/langdef.md#datetime-functions).| |
| 51 | +| `request.user_agent` | String | The user-agent of the request. Truncated at 255 characters max.| |
| 52 | + |
| 53 | +### Operators |
| 54 | + |
| 55 | +Every data type, such as `timestamp` or `string`, supports a set of operators that can be used to create a logic expression. |
| 56 | + |
| 57 | +Most commonly, operators are used to compare the value contained in a variable with a literal value. |
| 58 | + |
| 59 | +For example, `==` is the operator in the following statement: |
| 60 | + |
| 61 | +``` |
| 62 | +request.time == "2024-03-03T14:30:00.000Z" |
| 63 | +``` |
| 64 | + |
| 65 | +Refer to the official [CEL syntax specification](https://github.com/google/cel-spec/blob/master/doc/langdef.md#syntax) for list of supported operators. |
| 66 | + |
| 67 | +#### Logical operators |
| 68 | + |
| 69 | +Conditions supports three logical operators that can be used to build complex logic expressions from basic expression statements: |
| 70 | + |
| 71 | +| Logical operator | Description | Example | |
| 72 | +| -- | ------------------- | ------ | |
| 73 | +| `&&` (AND) | Evaluates to true if both expressions are true. | `request.time.getFullYear() < 2020 && request.ip == '10.154.3.1'` | |
| 74 | +| `\|\|` (OR) | Evaluates to true if either expression is true. If the first expression is true, the second expression may not be evaluated. | `request.time.getFullYear() < 2020 \|\| request.ip == '10.154.3.1'` | |
| 75 | +| `!` (NOT) | Evaluates to true if the expression is false, and false if the expression is true. | `!(request.time.getFullYear() < 2020)` | |
| 76 | + |
| 77 | + |
| 78 | +### Functions |
| 79 | + |
| 80 | +A function is a compound operator for data types that supports more complex operations. In condition expressions, there are predefined functions that can be used with a given data type. |
| 81 | + |
| 82 | +| Function | Description | Parameters | |
| 83 | +| ------------ | ------------------- | ------ | |
| 84 | +| `inIpRange(IP: string, Subnet: string)` | Checks if the IP address is included in the IP subnet. | **IP**: (String) The IP address to check. | |
| 85 | +| | | **Subnet**: (String) The IP subnet to check against. | |
| 86 | + |
| 87 | + |
| 88 | +## Important considerations |
| 89 | + |
| 90 | +### Multiple policies |
| 91 | +If multiple policies with different conditions apply to the same principal, the presence of a single policy with met conditions (or no conditions) will override any denying rules from other policies, allowing the action to be taken. |
| 92 | + |
| 93 | +For example, if you set up a policy that grants access to a resource only on Monday while another policy grants access only on Tuesday, the action will still be permitted on Monday. |
| 94 | + |
| 95 | +### Timezones |
| 96 | + |
| 97 | +We recommend that you specify timezones when creating time-based conditions. |
| 98 | + |
| 99 | +Refer to the official [CEL specification](https://github.com/google/cel-spec/blob/master/doc/langdef.md#timezones) for the correct grammar to express timezones in conditions. |
| 100 | + |
| 101 | +### Timestamps |
| 102 | + |
| 103 | +Conditions based on timestamps might take up to a minute to be applied. |
| 104 | + |
| 105 | +For example, if a user has permission to perform an action until 11am, they may be able to perform it until 11:01am. |
| 106 | + |
| 107 | +### IAM condition limitations |
| 108 | + |
| 109 | +Currently it is only possible to edit conditions in the console using the **Advanced** expression editor. |
| 110 | + |
| 111 | +When creating a policy, you can define a simple condition expression with the help of the console form. When editing, you must define the changes by writing them in CEL in the Advanced editor. |
| 112 | + |
| 113 | +## Expression examples |
| 114 | + |
| 115 | +### User-agent conditions |
| 116 | + |
| 117 | +In the example below we check if the user-agent contains the term "Terraform": |
| 118 | +``` |
| 119 | +request.user_agent.contains("terraform/") |
| 120 | +``` |
| 121 | + |
| 122 | +### Time conditions |
| 123 | + |
| 124 | +To check if a request was performed at a specific timeslot you can use the following expression. In this example, use weekdays from 9am to 5pm as a timestamp. |
| 125 | +``` |
| 126 | +request.time.getDayOfWeek() != 0 && request.time.getDayOfWeek() != 6 |
| 127 | +&& request.time.getHours("Europe/Paris") < 17 |
| 128 | +&& request.time.getHours("Europe/Paris") > 8 |
| 129 | +``` |
| 130 | + |
| 131 | +To check if the request was performed over the weekend, you can use the expression below: |
| 132 | +``` |
| 133 | +request.time.getDayOfWeek() != 0 && request.time.getDayOfWeek() != 6 |
| 134 | +``` |
0 commit comments