|
15 | 15 | templated_value_is_truthy, |
16 | 16 | ) |
17 | 17 |
|
| 18 | +EMAIL_SAMPLE_PAYLOAD = { |
| 19 | + "subject": "[Reminder] Review GKE getServerConfig API permission changes", |
| 20 | + "message": "Hello Google Kubernetes Customer,\r\n" |
| 21 | + "\r\n" |
| 22 | + "We’re writing to remind you that starting October 22, 2024, " |
| 23 | + "the \r\n" |
| 24 | + "getServerConfig API for Google Kubernetes Engine (GKE) will " |
| 25 | + "enforce \r\n" |
| 26 | + "Identity and Access Management (IAM) container.clusters.list " |
| 27 | + "checks. This \r\n" |
| 28 | + "change follows a series of security improvements as IAM \r\n" |
| 29 | + "container.clusters.list permissions are being enforced across " |
| 30 | + "the \r\n" |
| 31 | + "getServerConfig API.\r\n" |
| 32 | + "\r\n" |
| 33 | + "We’ve provided additional information below to guide you through " |
| 34 | + "this \r\n" |
| 35 | + "change.\r\n" |
| 36 | + "\r\n" |
| 37 | + "What you need to know\r\n" |
| 38 | + "\r\n" |
| 39 | + "The current implementation doesn’t apply a specific permissions " |
| 40 | + "check via \r\n" |
| 41 | + "getServerConfig API. After this change goes into effect for the " |
| 42 | + "Google \r\n" |
| 43 | + "Kubernetes Engine API getServerConfig, only authorized users with " |
| 44 | + "the \r\n" |
| 45 | + "container.clusters.list permissions will be able to call the \r\n" |
| 46 | + "GetServerConfig.\r\n", |
| 47 | + |
| 48 | +} |
| 49 | + |
18 | 50 |
|
19 | 51 | def test_apply_jinja_template(): |
20 | 52 | payload = {"name": "test"} |
@@ -127,25 +159,49 @@ def test_apply_jinja_template_json_dumps(): |
127 | 159 | assert result == expected |
128 | 160 |
|
129 | 161 |
|
| 162 | +@pytest.mark.filterwarnings("ignore:::jinja2.*") # ignore regex escape sequence warning |
130 | 163 | def test_apply_jinja_template_regex_match(): |
131 | | - payload = {"name": "test"} |
| 164 | + payload = { |
| 165 | + "name": "test", |
| 166 | + "message": json.dumps(EMAIL_SAMPLE_PAYLOAD), |
| 167 | + } |
132 | 168 |
|
133 | 169 | assert apply_jinja_template("{{ payload.name | regex_match('.*') }}", payload) == "True" |
134 | 170 | assert apply_jinja_template("{{ payload.name | regex_match('tes') }}", payload) == "True" |
135 | 171 | assert apply_jinja_template("{{ payload.name | regex_match('test1') }}", payload) == "False" |
| 172 | + # check for timeouts |
| 173 | + with patch("common.jinja_templater.filters.REGEX_TIMEOUT", 1): |
| 174 | + assert ( |
| 175 | + apply_jinja_template( |
| 176 | + "{{ payload.message | regex_match('(.|\\s)+Severity(.|\\s){2}High(.|\\s)+') }}", payload |
| 177 | + ) |
| 178 | + == "False" |
| 179 | + ) |
136 | 180 |
|
137 | 181 | # Check that exception is raised when regex is invalid |
138 | 182 | with pytest.raises(JinjaTemplateError): |
139 | 183 | apply_jinja_template("{{ payload.name | regex_match('*') }}", payload) |
140 | 184 |
|
141 | 185 |
|
| 186 | +@pytest.mark.filterwarnings("ignore:::jinja2.*") # ignore regex escape sequence warning |
142 | 187 | def test_apply_jinja_template_regex_search(): |
143 | | - payload = {"name": "test"} |
| 188 | + payload = { |
| 189 | + "name": "test", |
| 190 | + "message": json.dumps(EMAIL_SAMPLE_PAYLOAD), |
| 191 | + } |
144 | 192 |
|
145 | 193 | assert apply_jinja_template("{{ payload.name | regex_search('.*') }}", payload) == "True" |
146 | 194 | assert apply_jinja_template("{{ payload.name | regex_search('tes') }}", payload) == "True" |
147 | 195 | assert apply_jinja_template("{{ payload.name | regex_search('est') }}", payload) == "True" |
148 | 196 | assert apply_jinja_template("{{ payload.name | regex_search('test1') }}", payload) == "False" |
| 197 | + # check for timeouts |
| 198 | + with patch("common.jinja_templater.filters.REGEX_TIMEOUT", 1): |
| 199 | + assert ( |
| 200 | + apply_jinja_template( |
| 201 | + "{{ payload.message | regex_search('(.|\\s)+Severity(.|\\s){2}High(.|\\s)+') }}", payload |
| 202 | + ) |
| 203 | + == "False" |
| 204 | + ) |
149 | 205 |
|
150 | 206 | # Check that exception is raised when regex is invalid |
151 | 207 | with pytest.raises(JinjaTemplateError): |
|
0 commit comments