| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <stdint.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include <functional> |
| 19 |
#include <string> |
| 20 |
#include <utility> |
| 21 |
|
| 22 |
#include "absl/status/status.h" |
| 23 |
#include "absl/strings/string_view.h" |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/grpc_security.h> |
| 27 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 28 |
#include <grpc/slice.h> |
| 29 |
#include <grpc/status.h> |
| 30 |
#include <grpc/support/alloc.h> |
| 31 |
#include <grpc/support/log.h> |
| 32 |
#include <grpc/support/sync.h> |
| 33 |
#include <grpc/support/time.h> |
| 34 |
|
| 35 |
#include "src/core/lib/channel/channel_args.h" |
| 36 |
#include "src/core/lib/gpr/useful.h" |
| 37 |
#include "src/core/lib/security/authorization/authorization_policy_provider.h" |
| 38 |
#include "src/core/lib/security/authorization/grpc_authorization_policy_provider.h" |
| 39 |
#include "test/core/end2end/cq_verifier.h" |
| 40 |
#include "test/core/end2end/end2end_tests.h" |
| 41 |
#include "test/core/util/test_config.h" |
| 42 |
#include "test/core/util/tls_utils.h" |
| 43 |
|
| 44 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 45 |
|
| 46 |
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 47 |
const char* test_name, |
| 48 |
grpc_channel_args* client_args, |
| 49 |
grpc_channel_args* server_args) { |
| 50 |
grpc_end2end_test_fixture f; |
| 51 |
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
| 52 |
f = config.create_fixture(client_args, server_args); |
| 53 |
config.init_server(&f, server_args); |
| 54 |
config.init_client(&f, client_args); |
| 55 |
return f; |
| 56 |
} |
| 57 |
|
| 58 |
static gpr_timespec n_seconds_from_now(int n) { |
| 59 |
return grpc_timeout_seconds_to_deadline(n); |
| 60 |
} |
| 61 |
|
| 62 |
static gpr_timespec five_seconds_from_now(void) { |
| 63 |
return n_seconds_from_now(5); |
| 64 |
} |
| 65 |
|
| 66 |
static void drain_cq(grpc_completion_queue* cq) { |
| 67 |
grpc_event ev; |
| 68 |
do { |
| 69 |
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
| 70 |
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 71 |
} |
| 72 |
|
| 73 |
static void shutdown_server(grpc_end2end_test_fixture* f) { |
| 74 |
if (!f->server) return; |
| 75 |
grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 76 |
grpc_event ev; |
| 77 |
do { |
| 78 |
ev = grpc_completion_queue_next(f->cq, five_seconds_from_now(), nullptr); |
| 79 |
} while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000)); |
| 80 |
grpc_server_destroy(f->server); |
| 81 |
f->server = nullptr; |
| 82 |
} |
| 83 |
|
| 84 |
static void shutdown_client(grpc_end2end_test_fixture* f) { |
| 85 |
if (!f->client) return; |
| 86 |
grpc_channel_destroy(f->client); |
| 87 |
f->client = nullptr; |
| 88 |
} |
| 89 |
|
| 90 |
static void end_test(grpc_end2end_test_fixture* f) { |
| 91 |
shutdown_server(f); |
| 92 |
shutdown_client(f); |
| 93 |
|
| 94 |
grpc_completion_queue_shutdown(f->cq); |
| 95 |
drain_cq(f->cq); |
| 96 |
grpc_completion_queue_destroy(f->cq); |
| 97 |
} |
| 98 |
|
| 99 |
static void test_allow_authorized_request(grpc_end2end_test_fixture f) { |
| 100 |
grpc_call* c; |
| 101 |
grpc_call* s; |
| 102 |
grpc_op ops[6]; |
| 103 |
grpc_op* op; |
| 104 |
grpc_metadata_array initial_metadata_recv; |
| 105 |
grpc_metadata_array trailing_metadata_recv; |
| 106 |
grpc_metadata_array request_metadata_recv; |
| 107 |
grpc_call_details call_details; |
| 108 |
grpc_status_code status; |
| 109 |
const char* error_string = nullptr; |
| 110 |
grpc_call_error error; |
| 111 |
grpc_slice details = grpc_empty_slice(); |
| 112 |
int was_cancelled = 2; |
| 113 |
|
| 114 |
grpc_core::CqVerifier cqv(f.cq); |
| 115 |
|
| 116 |
gpr_timespec deadline = five_seconds_from_now(); |
| 117 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 118 |
grpc_slice_from_static_string("/foo"), nullptr, |
| 119 |
deadline, nullptr); |
| 120 |
GPR_ASSERT(c); |
| 121 |
|
| 122 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 123 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 124 |
grpc_metadata_array_init(&request_metadata_recv); |
| 125 |
grpc_call_details_init(&call_details); |
| 126 |
|
| 127 |
memset(ops, 0, sizeof(ops)); |
| 128 |
op = ops; |
| 129 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 130 |
op->data.send_initial_metadata.count = 0; |
| 131 |
op->flags = 0; |
| 132 |
op->reserved = nullptr; |
| 133 |
op++; |
| 134 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 135 |
op->flags = 0; |
| 136 |
op->reserved = nullptr; |
| 137 |
op++; |
| 138 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 139 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 140 |
op->flags = 0; |
| 141 |
op->reserved = nullptr; |
| 142 |
op++; |
| 143 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 144 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 145 |
op->data.recv_status_on_client.status = &status; |
| 146 |
op->data.recv_status_on_client.status_details = &details; |
| 147 |
op->data.recv_status_on_client.error_string = &error_string; |
| 148 |
op->flags = 0; |
| 149 |
op->reserved = nullptr; |
| 150 |
op++; |
| 151 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 152 |
nullptr); |
| 153 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 154 |
|
| 155 |
error = |
| 156 |
grpc_server_request_call(f.server, &s, &call_details, |
| 157 |
&request_metadata_recv, f.cq, f.cq, tag(101)); |
| 158 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 159 |
cqv.Expect(tag(101), true); |
| 160 |
cqv.Verify(); |
| 161 |
|
| 162 |
memset(ops, 0, sizeof(ops)); |
| 163 |
op = ops; |
| 164 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 165 |
op->data.send_initial_metadata.count = 0; |
| 166 |
op->flags = 0; |
| 167 |
op->reserved = nullptr; |
| 168 |
op++; |
| 169 |
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 170 |
op->data.send_status_from_server.trailing_metadata_count = 0; |
| 171 |
op->data.send_status_from_server.status = GRPC_STATUS_OK; |
| 172 |
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 173 |
op->data.send_status_from_server.status_details = &status_details; |
| 174 |
op->flags = 0; |
| 175 |
op->reserved = nullptr; |
| 176 |
op++; |
| 177 |
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 178 |
op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 179 |
op->flags = 0; |
| 180 |
op->reserved = nullptr; |
| 181 |
op++; |
| 182 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102), |
| 183 |
nullptr); |
| 184 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 185 |
|
| 186 |
cqv.Expect(tag(102), true); |
| 187 |
cqv.Expect(tag(1), true); |
| 188 |
cqv.Verify(); |
| 189 |
GPR_ASSERT(GRPC_STATUS_OK == status); |
| 190 |
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
| 191 |
|
| 192 |
grpc_slice_unref(details); |
| 193 |
gpr_free(const_cast<char*>(error_string)); |
| 194 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 195 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 196 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 197 |
grpc_call_details_destroy(&call_details); |
| 198 |
|
| 199 |
grpc_call_unref(c); |
| 200 |
grpc_call_unref(s); |
| 201 |
} |
| 202 |
|
| 203 |
static void test_deny_unauthorized_request(grpc_end2end_test_fixture f) { |
| 204 |
grpc_call* c; |
| 205 |
grpc_op ops[6]; |
| 206 |
grpc_op* op; |
| 207 |
grpc_metadata_array initial_metadata_recv; |
| 208 |
grpc_metadata_array trailing_metadata_recv; |
| 209 |
grpc_status_code status; |
| 210 |
const char* error_string = nullptr; |
| 211 |
grpc_call_error error; |
| 212 |
grpc_slice details = grpc_empty_slice(); |
| 213 |
|
| 214 |
grpc_core::CqVerifier cqv(f.cq); |
| 215 |
|
| 216 |
gpr_timespec deadline = five_seconds_from_now(); |
| 217 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 218 |
grpc_slice_from_static_string("/foo"), nullptr, |
| 219 |
deadline, nullptr); |
| 220 |
GPR_ASSERT(c); |
| 221 |
|
| 222 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 223 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 224 |
|
| 225 |
memset(ops, 0, sizeof(ops)); |
| 226 |
op = ops; |
| 227 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 228 |
op->data.send_initial_metadata.count = 0; |
| 229 |
op->flags = 0; |
| 230 |
op->reserved = nullptr; |
| 231 |
op++; |
| 232 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 233 |
op->flags = 0; |
| 234 |
op->reserved = nullptr; |
| 235 |
op++; |
| 236 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 237 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 238 |
op->flags = 0; |
| 239 |
op->reserved = nullptr; |
| 240 |
op++; |
| 241 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 242 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 243 |
op->data.recv_status_on_client.status = &status; |
| 244 |
op->data.recv_status_on_client.status_details = &details; |
| 245 |
op->data.recv_status_on_client.error_string = &error_string; |
| 246 |
op->flags = 0; |
| 247 |
op->reserved = nullptr; |
| 248 |
op++; |
| 249 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 250 |
nullptr); |
| 251 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 252 |
cqv.Expect(tag(1), true); |
| 253 |
cqv.Verify(); |
| 254 |
|
| 255 |
GPR_ASSERT(GRPC_STATUS_PERMISSION_DENIED == status); |
| 256 |
GPR_ASSERT(0 == |
| 257 |
grpc_slice_str_cmp(details, "Unauthorized RPC request rejected.")); |
| 258 |
|
| 259 |
grpc_slice_unref(details); |
| 260 |
gpr_free(const_cast<char*>(error_string)); |
| 261 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 262 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 263 |
|
| 264 |
grpc_call_unref(c); |
| 265 |
} |
| 266 |
|
| 267 |
static void test_static_init_allow_authorized_request( |
| 268 |
grpc_end2end_test_config config) { |
| 269 |
const char* authz_policy = |
| 270 |
"{" |
| 271 |
" \"name\": \"authz\"," |
| 272 |
" \"allow_rules\": [" |
| 273 |
" {" |
| 274 |
" \"name\": \"allow_foo\"," |
| 275 |
" \"request\": {" |
| 276 |
" \"paths\": [" |
| 277 |
" \"*/foo\"" |
| 278 |
" ]" |
| 279 |
" }" |
| 280 |
" }" |
| 281 |
" ]" |
| 282 |
"}"; |
| 283 |
grpc_status_code code = GRPC_STATUS_OK; |
| 284 |
const char* error_details; |
| 285 |
grpc_authorization_policy_provider* provider = |
| 286 |
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
| 287 |
&error_details); |
| 288 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 289 |
grpc_arg args[] = { |
| 290 |
grpc_channel_arg_pointer_create( |
| 291 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 292 |
grpc_authorization_policy_provider_arg_vtable()), |
| 293 |
}; |
| 294 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 295 |
|
| 296 |
grpc_end2end_test_fixture f = |
| 297 |
begin_test(config, "test_static_init_allow_authorized_request", nullptr, |
| 298 |
&server_args); |
| 299 |
grpc_authorization_policy_provider_release(provider); |
| 300 |
test_allow_authorized_request(f); |
| 301 |
|
| 302 |
end_test(&f); |
| 303 |
config.tear_down_data(&f); |
| 304 |
} |
| 305 |
|
| 306 |
static void test_static_init_deny_unauthorized_request( |
| 307 |
grpc_end2end_test_config config) { |
| 308 |
const char* authz_policy = |
| 309 |
"{" |
| 310 |
" \"name\": \"authz\"," |
| 311 |
" \"allow_rules\": [" |
| 312 |
" {" |
| 313 |
" \"name\": \"allow_bar\"," |
| 314 |
" \"request\": {" |
| 315 |
" \"paths\": [" |
| 316 |
" \"*/bar\"" |
| 317 |
" ]" |
| 318 |
" }" |
| 319 |
" }" |
| 320 |
" ]," |
| 321 |
" \"deny_rules\": [" |
| 322 |
" {" |
| 323 |
" \"name\": \"deny_foo\"," |
| 324 |
" \"request\": {" |
| 325 |
" \"paths\": [" |
| 326 |
" \"*/foo\"" |
| 327 |
" ]" |
| 328 |
" }" |
| 329 |
" }" |
| 330 |
" ]" |
| 331 |
"}"; |
| 332 |
grpc_status_code code = GRPC_STATUS_OK; |
| 333 |
const char* error_details; |
| 334 |
grpc_authorization_policy_provider* provider = |
| 335 |
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
| 336 |
&error_details); |
| 337 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 338 |
grpc_arg args[] = { |
| 339 |
grpc_channel_arg_pointer_create( |
| 340 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 341 |
grpc_authorization_policy_provider_arg_vtable()), |
| 342 |
}; |
| 343 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 344 |
|
| 345 |
grpc_end2end_test_fixture f = |
| 346 |
begin_test(config, "test_static_init_deny_unauthorized_request", nullptr, |
| 347 |
&server_args); |
| 348 |
grpc_authorization_policy_provider_release(provider); |
| 349 |
test_deny_unauthorized_request(f); |
| 350 |
|
| 351 |
end_test(&f); |
| 352 |
config.tear_down_data(&f); |
| 353 |
} |
| 354 |
|
| 355 |
static void test_static_init_deny_request_no_match_in_policy( |
| 356 |
grpc_end2end_test_config config) { |
| 357 |
const char* authz_policy = |
| 358 |
"{" |
| 359 |
" \"name\": \"authz\"," |
| 360 |
" \"allow_rules\": [" |
| 361 |
" {" |
| 362 |
" \"name\": \"allow_bar\"," |
| 363 |
" \"request\": {" |
| 364 |
" \"paths\": [" |
| 365 |
" \"*/bar\"" |
| 366 |
" ]" |
| 367 |
" }" |
| 368 |
" }" |
| 369 |
" ]" |
| 370 |
"}"; |
| 371 |
grpc_status_code code = GRPC_STATUS_OK; |
| 372 |
const char* error_details; |
| 373 |
grpc_authorization_policy_provider* provider = |
| 374 |
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
| 375 |
&error_details); |
| 376 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 377 |
grpc_arg args[] = { |
| 378 |
grpc_channel_arg_pointer_create( |
| 379 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 380 |
grpc_authorization_policy_provider_arg_vtable()), |
| 381 |
}; |
| 382 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 383 |
|
| 384 |
grpc_end2end_test_fixture f = |
| 385 |
begin_test(config, "test_static_init_deny_request_no_match_in_policy", |
| 386 |
nullptr, &server_args); |
| 387 |
grpc_authorization_policy_provider_release(provider); |
| 388 |
test_deny_unauthorized_request(f); |
| 389 |
|
| 390 |
end_test(&f); |
| 391 |
config.tear_down_data(&f); |
| 392 |
} |
| 393 |
|
| 394 |
static void test_file_watcher_init_allow_authorized_request( |
| 395 |
grpc_end2end_test_config config) { |
| 396 |
const char* authz_policy = |
| 397 |
"{" |
| 398 |
" \"name\": \"authz\"," |
| 399 |
" \"allow_rules\": [" |
| 400 |
" {" |
| 401 |
" \"name\": \"allow_foo\"," |
| 402 |
" \"request\": {" |
| 403 |
" \"paths\": [" |
| 404 |
" \"*/foo\"" |
| 405 |
" ]" |
| 406 |
" }" |
| 407 |
" }" |
| 408 |
" ]" |
| 409 |
"}"; |
| 410 |
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
| 411 |
grpc_status_code code = GRPC_STATUS_OK; |
| 412 |
const char* error_details; |
| 413 |
grpc_authorization_policy_provider* provider = |
| 414 |
grpc_authorization_policy_provider_file_watcher_create( |
| 415 |
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
| 416 |
&error_details); |
| 417 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 418 |
grpc_arg args[] = { |
| 419 |
grpc_channel_arg_pointer_create( |
| 420 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 421 |
grpc_authorization_policy_provider_arg_vtable()), |
| 422 |
}; |
| 423 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 424 |
|
| 425 |
grpc_end2end_test_fixture f = |
| 426 |
begin_test(config, "test_file_watcher_init_allow_authorized_request", |
| 427 |
nullptr, &server_args); |
| 428 |
grpc_authorization_policy_provider_release(provider); |
| 429 |
test_allow_authorized_request(f); |
| 430 |
|
| 431 |
end_test(&f); |
| 432 |
config.tear_down_data(&f); |
| 433 |
} |
| 434 |
|
| 435 |
static void test_file_watcher_init_deny_unauthorized_request( |
| 436 |
grpc_end2end_test_config config) { |
| 437 |
const char* authz_policy = |
| 438 |
"{" |
| 439 |
" \"name\": \"authz\"," |
| 440 |
" \"allow_rules\": [" |
| 441 |
" {" |
| 442 |
" \"name\": \"allow_bar\"," |
| 443 |
" \"request\": {" |
| 444 |
" \"paths\": [" |
| 445 |
" \"*/bar\"" |
| 446 |
" ]" |
| 447 |
" }" |
| 448 |
" }" |
| 449 |
" ]," |
| 450 |
" \"deny_rules\": [" |
| 451 |
" {" |
| 452 |
" \"name\": \"deny_foo\"," |
| 453 |
" \"request\": {" |
| 454 |
" \"paths\": [" |
| 455 |
" \"*/foo\"" |
| 456 |
" ]" |
| 457 |
" }" |
| 458 |
" }" |
| 459 |
" ]" |
| 460 |
"}"; |
| 461 |
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
| 462 |
grpc_status_code code = GRPC_STATUS_OK; |
| 463 |
const char* error_details; |
| 464 |
grpc_authorization_policy_provider* provider = |
| 465 |
grpc_authorization_policy_provider_file_watcher_create( |
| 466 |
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
| 467 |
&error_details); |
| 468 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 469 |
grpc_arg args[] = { |
| 470 |
grpc_channel_arg_pointer_create( |
| 471 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 472 |
grpc_authorization_policy_provider_arg_vtable()), |
| 473 |
}; |
| 474 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 475 |
|
| 476 |
grpc_end2end_test_fixture f = |
| 477 |
begin_test(config, "test_file_watcher_init_deny_unauthorized_request", |
| 478 |
nullptr, &server_args); |
| 479 |
grpc_authorization_policy_provider_release(provider); |
| 480 |
test_deny_unauthorized_request(f); |
| 481 |
|
| 482 |
end_test(&f); |
| 483 |
config.tear_down_data(&f); |
| 484 |
} |
| 485 |
|
| 486 |
static void test_file_watcher_init_deny_request_no_match_in_policy( |
| 487 |
grpc_end2end_test_config config) { |
| 488 |
const char* authz_policy = |
| 489 |
"{" |
| 490 |
" \"name\": \"authz\"," |
| 491 |
" \"allow_rules\": [" |
| 492 |
" {" |
| 493 |
" \"name\": \"allow_bar\"," |
| 494 |
" \"request\": {" |
| 495 |
" \"paths\": [" |
| 496 |
" \"*/bar\"" |
| 497 |
" ]" |
| 498 |
" }" |
| 499 |
" }" |
| 500 |
" ]" |
| 501 |
"}"; |
| 502 |
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
| 503 |
grpc_status_code code = GRPC_STATUS_OK; |
| 504 |
const char* error_details; |
| 505 |
grpc_authorization_policy_provider* provider = |
| 506 |
grpc_authorization_policy_provider_file_watcher_create( |
| 507 |
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
| 508 |
&error_details); |
| 509 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 510 |
grpc_arg args[] = { |
| 511 |
grpc_channel_arg_pointer_create( |
| 512 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 513 |
grpc_authorization_policy_provider_arg_vtable()), |
| 514 |
}; |
| 515 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 516 |
|
| 517 |
grpc_end2end_test_fixture f = begin_test( |
| 518 |
config, "test_file_watcher_init_deny_request_no_match_in_policy", nullptr, |
| 519 |
&server_args); |
| 520 |
grpc_authorization_policy_provider_release(provider); |
| 521 |
test_deny_unauthorized_request(f); |
| 522 |
|
| 523 |
end_test(&f); |
| 524 |
config.tear_down_data(&f); |
| 525 |
} |
| 526 |
|
| 527 |
static void test_file_watcher_valid_policy_reload( |
| 528 |
grpc_end2end_test_config config) { |
| 529 |
const char* authz_policy = |
| 530 |
"{" |
| 531 |
" \"name\": \"authz\"," |
| 532 |
" \"allow_rules\": [" |
| 533 |
" {" |
| 534 |
" \"name\": \"allow_foo\"," |
| 535 |
" \"request\": {" |
| 536 |
" \"paths\": [" |
| 537 |
" \"*/foo\"" |
| 538 |
" ]" |
| 539 |
" }" |
| 540 |
" }" |
| 541 |
" ]" |
| 542 |
"}"; |
| 543 |
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
| 544 |
grpc_status_code code = GRPC_STATUS_OK; |
| 545 |
const char* error_details; |
| 546 |
grpc_authorization_policy_provider* provider = |
| 547 |
grpc_authorization_policy_provider_file_watcher_create( |
| 548 |
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
| 549 |
&error_details); |
| 550 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 551 |
grpc_arg args[] = { |
| 552 |
grpc_channel_arg_pointer_create( |
| 553 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 554 |
grpc_authorization_policy_provider_arg_vtable()), |
| 555 |
}; |
| 556 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 557 |
|
| 558 |
grpc_end2end_test_fixture f = begin_test( |
| 559 |
config, "test_file_watcher_valid_policy_reload", nullptr, &server_args); |
| 560 |
grpc_authorization_policy_provider_release(provider); |
| 561 |
test_allow_authorized_request(f); |
| 562 |
gpr_event on_reload_done; |
| 563 |
gpr_event_init(&on_reload_done); |
| 564 |
std::function<void(bool contents_changed, absl::Status status)> callback = |
| 565 |
[&on_reload_done](bool contents_changed, absl::Status status) { |
| 566 |
if (contents_changed) { |
| 567 |
GPR_ASSERT(status.ok()); |
| 568 |
gpr_event_set(&on_reload_done, reinterpret_cast<void*>(1)); |
| 569 |
} |
| 570 |
}; |
| 571 |
dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(provider) |
| 572 |
->SetCallbackForTesting(std::move(callback)); |
| 573 |
// Replace existing policy in file with a different authorization policy. |
| 574 |
authz_policy = |
| 575 |
"{" |
| 576 |
" \"name\": \"authz\"," |
| 577 |
" \"allow_rules\": [" |
| 578 |
" {" |
| 579 |
" \"name\": \"allow_bar\"," |
| 580 |
" \"request\": {" |
| 581 |
" \"paths\": [" |
| 582 |
" \"*/bar\"" |
| 583 |
" ]" |
| 584 |
" }" |
| 585 |
" }" |
| 586 |
" ]," |
| 587 |
" \"deny_rules\": [" |
| 588 |
" {" |
| 589 |
" \"name\": \"deny_foo\"," |
| 590 |
" \"request\": {" |
| 591 |
" \"paths\": [" |
| 592 |
" \"*/foo\"" |
| 593 |
" ]" |
| 594 |
" }" |
| 595 |
" }" |
| 596 |
" ]" |
| 597 |
"}"; |
| 598 |
tmp_policy.RewriteFile(authz_policy); |
| 599 |
GPR_ASSERT( |
| 600 |
reinterpret_cast<void*>(1) == |
| 601 |
gpr_event_wait(&on_reload_done, gpr_inf_future(GPR_CLOCK_MONOTONIC))); |
| 602 |
test_deny_unauthorized_request(f); |
| 603 |
dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(provider) |
| 604 |
->SetCallbackForTesting(nullptr); |
| 605 |
|
| 606 |
end_test(&f); |
| 607 |
config.tear_down_data(&f); |
| 608 |
} |
| 609 |
|
| 610 |
static void test_file_watcher_invalid_policy_skip_reload( |
| 611 |
grpc_end2end_test_config config) { |
| 612 |
const char* authz_policy = |
| 613 |
"{" |
| 614 |
" \"name\": \"authz\"," |
| 615 |
" \"allow_rules\": [" |
| 616 |
" {" |
| 617 |
" \"name\": \"allow_foo\"," |
| 618 |
" \"request\": {" |
| 619 |
" \"paths\": [" |
| 620 |
" \"*/foo\"" |
| 621 |
" ]" |
| 622 |
" }" |
| 623 |
" }" |
| 624 |
" ]" |
| 625 |
"}"; |
| 626 |
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
| 627 |
grpc_status_code code = GRPC_STATUS_OK; |
| 628 |
const char* error_details; |
| 629 |
grpc_authorization_policy_provider* provider = |
| 630 |
grpc_authorization_policy_provider_file_watcher_create( |
| 631 |
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
| 632 |
&error_details); |
| 633 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 634 |
grpc_arg args[] = { |
| 635 |
grpc_channel_arg_pointer_create( |
| 636 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 637 |
grpc_authorization_policy_provider_arg_vtable()), |
| 638 |
}; |
| 639 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 640 |
|
| 641 |
grpc_end2end_test_fixture f = |
| 642 |
begin_test(config, "test_file_watcher_invalid_policy_skip_reload", |
| 643 |
nullptr, &server_args); |
| 644 |
grpc_authorization_policy_provider_release(provider); |
| 645 |
test_allow_authorized_request(f); |
| 646 |
gpr_event on_reload_done; |
| 647 |
gpr_event_init(&on_reload_done); |
| 648 |
std::function<void(bool contents_changed, absl::Status status)> callback = |
| 649 |
[&on_reload_done](bool contents_changed, absl::Status status) { |
| 650 |
if (contents_changed) { |
| 651 |
GPR_ASSERT(absl::StatusCode::kInvalidArgument == status.code()); |
| 652 |
GPR_ASSERT("\"name\" field is not present." == status.message()); |
| 653 |
gpr_event_set(&on_reload_done, reinterpret_cast<void*>(1)); |
| 654 |
} |
| 655 |
}; |
| 656 |
dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(provider) |
| 657 |
->SetCallbackForTesting(std::move(callback)); |
| 658 |
// Replace exisiting policy in file with an invalid policy. |
| 659 |
authz_policy = "{}"; |
| 660 |
tmp_policy.RewriteFile(authz_policy); |
| 661 |
GPR_ASSERT( |
| 662 |
reinterpret_cast<void*>(1) == |
| 663 |
gpr_event_wait(&on_reload_done, gpr_inf_future(GPR_CLOCK_MONOTONIC))); |
| 664 |
test_allow_authorized_request(f); |
| 665 |
dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(provider) |
| 666 |
->SetCallbackForTesting(nullptr); |
| 667 |
|
| 668 |
end_test(&f); |
| 669 |
config.tear_down_data(&f); |
| 670 |
} |
| 671 |
|
| 672 |
static void test_file_watcher_recovers_from_failure( |
| 673 |
grpc_end2end_test_config config) { |
| 674 |
const char* authz_policy = |
| 675 |
"{" |
| 676 |
" \"name\": \"authz\"," |
| 677 |
" \"allow_rules\": [" |
| 678 |
" {" |
| 679 |
" \"name\": \"allow_foo\"," |
| 680 |
" \"request\": {" |
| 681 |
" \"paths\": [" |
| 682 |
" \"*/foo\"" |
| 683 |
" ]" |
| 684 |
" }" |
| 685 |
" }" |
| 686 |
" ]" |
| 687 |
"}"; |
| 688 |
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
| 689 |
grpc_status_code code = GRPC_STATUS_OK; |
| 690 |
const char* error_details; |
| 691 |
grpc_authorization_policy_provider* provider = |
| 692 |
grpc_authorization_policy_provider_file_watcher_create( |
| 693 |
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
| 694 |
&error_details); |
| 695 |
GPR_ASSERT(GRPC_STATUS_OK == code); |
| 696 |
grpc_arg args[] = { |
| 697 |
grpc_channel_arg_pointer_create( |
| 698 |
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
| 699 |
grpc_authorization_policy_provider_arg_vtable()), |
| 700 |
}; |
| 701 |
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
| 702 |
|
| 703 |
grpc_end2end_test_fixture f = begin_test( |
| 704 |
config, "test_file_watcher_recovers_from_failure", nullptr, &server_args); |
| 705 |
grpc_authorization_policy_provider_release(provider); |
| 706 |
test_allow_authorized_request(f); |
| 707 |
gpr_event on_first_reload_done; |
| 708 |
gpr_event_init(&on_first_reload_done); |
| 709 |
std::function<void(bool contents_changed, absl::Status status)> callback1 = |
| 710 |
[&on_first_reload_done](bool contents_changed, absl::Status status) { |
| 711 |
if (contents_changed) { |
| 712 |
GPR_ASSERT(absl::StatusCode::kInvalidArgument == status.code()); |
| 713 |
GPR_ASSERT("\"name\" field is not present." == status.message()); |
| 714 |
gpr_event_set(&on_first_reload_done, reinterpret_cast<void*>(1)); |
| 715 |
} |
| 716 |
}; |
| 717 |
dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(provider) |
| 718 |
->SetCallbackForTesting(std::move(callback1)); |
| 719 |
// Replace exisiting policy in file with an invalid policy. |
| 720 |
authz_policy = "{}"; |
| 721 |
tmp_policy.RewriteFile(authz_policy); |
| 722 |
GPR_ASSERT(reinterpret_cast<void*>(1) == |
| 723 |
gpr_event_wait(&on_first_reload_done, |
| 724 |
gpr_inf_future(GPR_CLOCK_MONOTONIC))); |
| 725 |
test_allow_authorized_request(f); |
| 726 |
gpr_event on_second_reload_done; |
| 727 |
gpr_event_init(&on_second_reload_done); |
| 728 |
std::function<void(bool contents_changed, absl::Status status)> callback2 = |
| 729 |
[&on_second_reload_done](bool contents_changed, absl::Status status) { |
| 730 |
if (contents_changed) { |
| 731 |
GPR_ASSERT(status.ok()); |
| 732 |
gpr_event_set(&on_second_reload_done, reinterpret_cast<void*>(1)); |
| 733 |
} |
| 734 |
}; |
| 735 |
dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(provider) |
| 736 |
->SetCallbackForTesting(std::move(callback2)); |
| 737 |
// Recover from reload errors, by replacing invalid policy in file with a |
| 738 |
// valid policy. |
| 739 |
authz_policy = |
| 740 |
"{" |
| 741 |
" \"name\": \"authz\"," |
| 742 |
" \"allow_rules\": [" |
| 743 |
" {" |
| 744 |
" \"name\": \"allow_bar\"," |
| 745 |
" \"request\": {" |
| 746 |
" \"paths\": [" |
| 747 |
" \"*/bar\"" |
| 748 |
" ]" |
| 749 |
" }" |
| 750 |
" }" |
| 751 |
" ]," |
| 752 |
" \"deny_rules\": [" |
| 753 |
" {" |
| 754 |
" \"name\": \"deny_foo\"," |
| 755 |
" \"request\": {" |
| 756 |
" \"paths\": [" |
| 757 |
" \"*/foo\"" |
| 758 |
" ]" |
| 759 |
" }" |
| 760 |
" }" |
| 761 |
" ]" |
| 762 |
"}"; |
| 763 |
tmp_policy.RewriteFile(authz_policy); |
| 764 |
GPR_ASSERT(reinterpret_cast<void*>(1) == |
| 765 |
gpr_event_wait(&on_second_reload_done, |
| 766 |
gpr_inf_future(GPR_CLOCK_MONOTONIC))); |
| 767 |
test_deny_unauthorized_request(f); |
| 768 |
dynamic_cast<grpc_core::FileWatcherAuthorizationPolicyProvider*>(provider) |
| 769 |
->SetCallbackForTesting(nullptr); |
| 770 |
|
| 771 |
end_test(&f); |
| 772 |
config.tear_down_data(&f); |
| 773 |
} |
| 774 |
|
| 775 |
void grpc_authz(grpc_end2end_test_config config) { |
| 776 |
test_static_init_allow_authorized_request(config); |
| 777 |
test_static_init_deny_unauthorized_request(config); |
| 778 |
test_static_init_deny_request_no_match_in_policy(config); |
| 779 |
test_file_watcher_init_allow_authorized_request(config); |
| 780 |
test_file_watcher_init_deny_unauthorized_request(config); |
| 781 |
test_file_watcher_init_deny_request_no_match_in_policy(config); |
| 782 |
test_file_watcher_valid_policy_reload(config); |
| 783 |
test_file_watcher_invalid_policy_skip_reload(config); |
| 784 |
test_file_watcher_recovers_from_failure(config); |
| 785 |
} |
| 786 |
|
| 787 |
void grpc_authz_pre_init(void) {} |