|
|
@ -27,7 +27,6 @@ |
|
|
|
#include "src/core/lib/security/credentials/credentials.h" |
|
|
|
#include "src/core/lib/security/credentials/credentials.h" |
|
|
|
#include "test/core/end2end/cq_verifier.h" |
|
|
|
#include "test/core/end2end/cq_verifier.h" |
|
|
|
#include "test/core/end2end/end2end_tests.h" |
|
|
|
#include "test/core/end2end/end2end_tests.h" |
|
|
|
#include "test/core/util/tls_utils.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
|
|
|
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
|
|
|
|
|
|
|
|
|
|
@ -85,7 +84,7 @@ static void end_test(grpc_end2end_test_fixture* f) { |
|
|
|
grpc_completion_queue_destroy(f->shutdown_cq); |
|
|
|
grpc_completion_queue_destroy(f->shutdown_cq); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void test_allow_authorized_request(grpc_end2end_test_fixture f) { |
|
|
|
static void test_allow_authorized_request(grpc_end2end_test_config config) { |
|
|
|
grpc_call* c; |
|
|
|
grpc_call* c; |
|
|
|
grpc_call* s; |
|
|
|
grpc_call* s; |
|
|
|
grpc_op ops[6]; |
|
|
|
grpc_op ops[6]; |
|
|
@ -100,6 +99,36 @@ static void test_allow_authorized_request(grpc_end2end_test_fixture f) { |
|
|
|
grpc_slice details = grpc_empty_slice(); |
|
|
|
grpc_slice details = grpc_empty_slice(); |
|
|
|
int was_cancelled = 2; |
|
|
|
int was_cancelled = 2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char* authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_status_code code; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = begin_test( |
|
|
|
|
|
|
|
config, "test_allow_authorized_request", nullptr, &server_args); |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
cq_verifier* cqv = cq_verifier_create(f.cq); |
|
|
|
cq_verifier* cqv = cq_verifier_create(f.cq); |
|
|
|
|
|
|
|
|
|
|
|
gpr_timespec deadline = five_seconds_from_now(); |
|
|
|
gpr_timespec deadline = five_seconds_from_now(); |
|
|
@ -188,9 +217,12 @@ static void test_allow_authorized_request(grpc_end2end_test_fixture f) { |
|
|
|
grpc_call_unref(c); |
|
|
|
grpc_call_unref(c); |
|
|
|
grpc_call_unref(s); |
|
|
|
grpc_call_unref(s); |
|
|
|
cq_verifier_destroy(cqv); |
|
|
|
cq_verifier_destroy(cqv); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
|
|
|
|
config.tear_down_data(&f); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void test_deny_unauthorized_request(grpc_end2end_test_fixture f) { |
|
|
|
static void test_deny_unauthorized_request(grpc_end2end_test_config config) { |
|
|
|
grpc_call* c; |
|
|
|
grpc_call* c; |
|
|
|
grpc_op ops[6]; |
|
|
|
grpc_op ops[6]; |
|
|
|
grpc_op* op; |
|
|
|
grpc_op* op; |
|
|
@ -201,11 +233,51 @@ static void test_deny_unauthorized_request(grpc_end2end_test_fixture f) { |
|
|
|
grpc_call_error error; |
|
|
|
grpc_call_error error; |
|
|
|
grpc_slice details = grpc_empty_slice(); |
|
|
|
grpc_slice details = grpc_empty_slice(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char* authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]," |
|
|
|
|
|
|
|
" \"deny_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"deny_bar\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/bar\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_status_code code; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = begin_test( |
|
|
|
|
|
|
|
config, "test_deny_unauthorized_request", nullptr, &server_args); |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
cq_verifier* cqv = cq_verifier_create(f.cq); |
|
|
|
cq_verifier* cqv = cq_verifier_create(f.cq); |
|
|
|
|
|
|
|
|
|
|
|
gpr_timespec deadline = five_seconds_from_now(); |
|
|
|
gpr_timespec deadline = five_seconds_from_now(); |
|
|
|
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
|
|
|
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
|
|
|
grpc_slice_from_static_string("/foo"), nullptr, |
|
|
|
grpc_slice_from_static_string("/bar"), nullptr, |
|
|
|
deadline, nullptr); |
|
|
|
deadline, nullptr); |
|
|
|
GPR_ASSERT(c); |
|
|
|
GPR_ASSERT(c); |
|
|
|
|
|
|
|
|
|
|
@ -253,270 +325,23 @@ static void test_deny_unauthorized_request(grpc_end2end_test_fixture f) { |
|
|
|
|
|
|
|
|
|
|
|
grpc_call_unref(c); |
|
|
|
grpc_call_unref(c); |
|
|
|
cq_verifier_destroy(cqv); |
|
|
|
cq_verifier_destroy(cqv); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void test_static_init_allow_authorized_request( |
|
|
|
|
|
|
|
grpc_end2end_test_config config) { |
|
|
|
|
|
|
|
const char* authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = |
|
|
|
|
|
|
|
begin_test(config, "test_static_init_allow_authorized_request", nullptr, |
|
|
|
|
|
|
|
&server_args); |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
|
|
|
|
test_allow_authorized_request(f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
|
|
|
|
config.tear_down_data(&f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void test_static_init_deny_unauthorized_request( |
|
|
|
|
|
|
|
grpc_end2end_test_config config) { |
|
|
|
|
|
|
|
const char* authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_bar\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/bar\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]," |
|
|
|
|
|
|
|
" \"deny_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"deny_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = |
|
|
|
|
|
|
|
begin_test(config, "test_static_init_deny_unauthorized_request", nullptr, |
|
|
|
|
|
|
|
&server_args); |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
|
|
|
|
test_deny_unauthorized_request(f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
|
|
|
|
config.tear_down_data(&f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void test_static_init_deny_request_no_match_in_policy( |
|
|
|
|
|
|
|
grpc_end2end_test_config config) { |
|
|
|
|
|
|
|
const char* authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_bar\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/bar\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = |
|
|
|
|
|
|
|
begin_test(config, "test_static_init_deny_request_no_match_in_policy", |
|
|
|
|
|
|
|
nullptr, &server_args); |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
|
|
|
|
test_deny_unauthorized_request(f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
|
|
|
|
config.tear_down_data(&f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void test_file_watcher_init_allow_authorized_request( |
|
|
|
|
|
|
|
grpc_end2end_test_config config) { |
|
|
|
|
|
|
|
const char* authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
|
|
|
|
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_file_watcher_create( |
|
|
|
|
|
|
|
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = |
|
|
|
|
|
|
|
begin_test(config, "test_file_watcher_init_allow_authorized_request", |
|
|
|
|
|
|
|
nullptr, &server_args); |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
|
|
|
|
test_allow_authorized_request(f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
end_test(&f); |
|
|
|
config.tear_down_data(&f); |
|
|
|
config.tear_down_data(&f); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void test_file_watcher_init_deny_unauthorized_request( |
|
|
|
static void test_deny_request_no_match_in_policy( |
|
|
|
grpc_end2end_test_config config) { |
|
|
|
grpc_end2end_test_config config) { |
|
|
|
const char* authz_policy = |
|
|
|
grpc_call* c; |
|
|
|
"{" |
|
|
|
grpc_op ops[6]; |
|
|
|
" \"name\": \"authz\"," |
|
|
|
grpc_op* op; |
|
|
|
" \"allow_rules\": [" |
|
|
|
grpc_metadata_array initial_metadata_recv; |
|
|
|
" {" |
|
|
|
grpc_metadata_array trailing_metadata_recv; |
|
|
|
" \"name\": \"allow_bar\"," |
|
|
|
grpc_status_code status; |
|
|
|
" \"request\": {" |
|
|
|
const char* error_string = nullptr; |
|
|
|
" \"paths\": [" |
|
|
|
grpc_call_error error; |
|
|
|
" \"*/bar\"" |
|
|
|
grpc_slice details = grpc_empty_slice(); |
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]," |
|
|
|
|
|
|
|
" \"deny_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"deny_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
|
|
|
|
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_file_watcher_create( |
|
|
|
|
|
|
|
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = |
|
|
|
|
|
|
|
begin_test(config, "test_file_watcher_init_deny_unauthorized_request", |
|
|
|
|
|
|
|
nullptr, &server_args); |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
|
|
|
|
test_deny_unauthorized_request(f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
|
|
|
|
config.tear_down_data(&f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void test_file_watcher_init_deny_request_no_match_in_policy( |
|
|
|
|
|
|
|
grpc_end2end_test_config config) { |
|
|
|
|
|
|
|
const char* authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_bar\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/bar\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
|
|
|
|
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_file_watcher_create( |
|
|
|
|
|
|
|
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = begin_test( |
|
|
|
|
|
|
|
config, "test_file_watcher_init_deny_request_no_match_in_policy", nullptr, |
|
|
|
|
|
|
|
&server_args); |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
|
|
|
|
test_deny_unauthorized_request(f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
|
|
|
|
config.tear_down_data(&f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void test_file_watcher_valid_policy_reload( |
|
|
|
|
|
|
|
grpc_end2end_test_config config) { |
|
|
|
|
|
|
|
const char* authz_policy = |
|
|
|
const char* authz_policy = |
|
|
|
"{" |
|
|
|
"{" |
|
|
|
" \"name\": \"authz\"," |
|
|
|
" \"name\": \"authz\"," |
|
|
@ -531,12 +356,10 @@ static void test_file_watcher_valid_policy_reload( |
|
|
|
" }" |
|
|
|
" }" |
|
|
|
" ]" |
|
|
|
" ]" |
|
|
|
"}"; |
|
|
|
"}"; |
|
|
|
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
|
|
|
grpc_status_code code; |
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
const char* error_details; |
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
grpc_authorization_policy_provider_file_watcher_create( |
|
|
|
grpc_authorization_policy_provider_static_data_create(authz_policy, &code, |
|
|
|
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
&error_details); |
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
grpc_arg args[] = { |
|
|
|
grpc_arg args[] = { |
|
|
@ -547,176 +370,69 @@ static void test_file_watcher_valid_policy_reload( |
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = begin_test( |
|
|
|
grpc_end2end_test_fixture f = begin_test( |
|
|
|
config, "test_file_watcher_valid_policy_reload", nullptr, &server_args); |
|
|
|
config, "test_deny_request_no_match_in_policy", nullptr, &server_args); |
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
test_allow_authorized_request(f); |
|
|
|
cq_verifier* cqv = cq_verifier_create(f.cq); |
|
|
|
// Replace existing policy in file with a different authorization policy.
|
|
|
|
|
|
|
|
authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_bar\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/bar\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]," |
|
|
|
|
|
|
|
" \"deny_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"deny_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
tmp_policy.RewriteFile(authz_policy); |
|
|
|
|
|
|
|
// Wait 2 seconds for the provider's refresh thread to read the updated files.
|
|
|
|
|
|
|
|
gpr_sleep_until(grpc_timeout_seconds_to_deadline(2)); |
|
|
|
|
|
|
|
test_deny_unauthorized_request(f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
gpr_timespec deadline = five_seconds_from_now(); |
|
|
|
config.tear_down_data(&f); |
|
|
|
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
|
|
|
} |
|
|
|
grpc_slice_from_static_string("/bar"), nullptr, |
|
|
|
|
|
|
|
deadline, nullptr); |
|
|
|
|
|
|
|
GPR_ASSERT(c); |
|
|
|
|
|
|
|
|
|
|
|
static void test_file_watcher_invalid_policy_skip_reload( |
|
|
|
grpc_metadata_array_init(&initial_metadata_recv); |
|
|
|
grpc_end2end_test_config config) { |
|
|
|
grpc_metadata_array_init(&trailing_metadata_recv); |
|
|
|
const char* authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
|
|
|
|
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_file_watcher_create( |
|
|
|
|
|
|
|
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = |
|
|
|
memset(ops, 0, sizeof(ops)); |
|
|
|
begin_test(config, "test_file_watcher_invalid_policy_skip_reload", |
|
|
|
op = ops; |
|
|
|
nullptr, &server_args); |
|
|
|
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
op->data.send_initial_metadata.count = 0; |
|
|
|
test_allow_authorized_request(f); |
|
|
|
op->flags = 0; |
|
|
|
// Replace exisiting policy in file with an invalid policy.
|
|
|
|
op->reserved = nullptr; |
|
|
|
authz_policy = "{}"; |
|
|
|
op++; |
|
|
|
tmp_policy.RewriteFile(authz_policy); |
|
|
|
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
|
|
|
// Wait 2 seconds for the provider's refresh thread to read the updated files.
|
|
|
|
op->flags = 0; |
|
|
|
gpr_sleep_until(grpc_timeout_seconds_to_deadline(2)); |
|
|
|
op->reserved = nullptr; |
|
|
|
test_allow_authorized_request(f); |
|
|
|
op++; |
|
|
|
|
|
|
|
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
|
|
|
|
|
|
|
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
|
|
|
|
|
|
|
op->flags = 0; |
|
|
|
|
|
|
|
op->reserved = nullptr; |
|
|
|
|
|
|
|
op++; |
|
|
|
|
|
|
|
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
|
|
|
|
|
|
|
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
|
|
|
|
|
|
|
op->data.recv_status_on_client.status = &status; |
|
|
|
|
|
|
|
op->data.recv_status_on_client.status_details = &details; |
|
|
|
|
|
|
|
op->data.recv_status_on_client.error_string = &error_string; |
|
|
|
|
|
|
|
op->flags = 0; |
|
|
|
|
|
|
|
op->reserved = nullptr; |
|
|
|
|
|
|
|
op++; |
|
|
|
|
|
|
|
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
|
|
|
|
|
|
|
nullptr); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_CALL_OK == error); |
|
|
|
|
|
|
|
CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
|
|
|
|
|
|
|
cq_verify(cqv); |
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
GPR_ASSERT(GRPC_STATUS_PERMISSION_DENIED == status); |
|
|
|
config.tear_down_data(&f); |
|
|
|
GPR_ASSERT(0 == |
|
|
|
} |
|
|
|
grpc_slice_str_cmp(details, "Unauthorized RPC request rejected.")); |
|
|
|
|
|
|
|
|
|
|
|
static void test_file_watcher_recovers_from_failure( |
|
|
|
grpc_slice_unref(details); |
|
|
|
grpc_end2end_test_config config) { |
|
|
|
gpr_free(const_cast<char*>(error_string)); |
|
|
|
const char* authz_policy = |
|
|
|
grpc_metadata_array_destroy(&initial_metadata_recv); |
|
|
|
"{" |
|
|
|
grpc_metadata_array_destroy(&trailing_metadata_recv); |
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
grpc_core::testing::TmpFile tmp_policy(authz_policy); |
|
|
|
|
|
|
|
grpc_status_code code = GRPC_STATUS_OK; |
|
|
|
|
|
|
|
const char* error_details; |
|
|
|
|
|
|
|
grpc_authorization_policy_provider* provider = |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_file_watcher_create( |
|
|
|
|
|
|
|
tmp_policy.name().c_str(), /*refresh_interval_sec=*/1, &code, |
|
|
|
|
|
|
|
&error_details); |
|
|
|
|
|
|
|
GPR_ASSERT(GRPC_STATUS_OK == code); |
|
|
|
|
|
|
|
grpc_arg args[] = { |
|
|
|
|
|
|
|
grpc_channel_arg_pointer_create( |
|
|
|
|
|
|
|
const_cast<char*>(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER), provider, |
|
|
|
|
|
|
|
grpc_authorization_policy_provider_arg_vtable()), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
grpc_channel_args server_args = {GPR_ARRAY_SIZE(args), args}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grpc_end2end_test_fixture f = begin_test( |
|
|
|
grpc_call_unref(c); |
|
|
|
config, "test_file_watcher_valid_policy_reload", nullptr, &server_args); |
|
|
|
cq_verifier_destroy(cqv); |
|
|
|
grpc_authorization_policy_provider_release(provider); |
|
|
|
|
|
|
|
test_allow_authorized_request(f); |
|
|
|
|
|
|
|
// Replace exisiting policy in file with an invalid policy.
|
|
|
|
|
|
|
|
authz_policy = "{}"; |
|
|
|
|
|
|
|
tmp_policy.RewriteFile(authz_policy); |
|
|
|
|
|
|
|
// Wait 2 seconds for the provider's refresh thread to read the updated files.
|
|
|
|
|
|
|
|
gpr_sleep_until(grpc_timeout_seconds_to_deadline(2)); |
|
|
|
|
|
|
|
test_allow_authorized_request(f); |
|
|
|
|
|
|
|
// Recover from reload errors, by replacing invalid policy in file with a
|
|
|
|
|
|
|
|
// valid policy.
|
|
|
|
|
|
|
|
authz_policy = |
|
|
|
|
|
|
|
"{" |
|
|
|
|
|
|
|
" \"name\": \"authz\"," |
|
|
|
|
|
|
|
" \"allow_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"allow_bar\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/bar\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]," |
|
|
|
|
|
|
|
" \"deny_rules\": [" |
|
|
|
|
|
|
|
" {" |
|
|
|
|
|
|
|
" \"name\": \"deny_foo\"," |
|
|
|
|
|
|
|
" \"request\": {" |
|
|
|
|
|
|
|
" \"paths\": [" |
|
|
|
|
|
|
|
" \"*/foo\"" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" }" |
|
|
|
|
|
|
|
" ]" |
|
|
|
|
|
|
|
"}"; |
|
|
|
|
|
|
|
tmp_policy.RewriteFile(authz_policy); |
|
|
|
|
|
|
|
// Wait 2 seconds for the provider's refresh thread to read the updated files.
|
|
|
|
|
|
|
|
gpr_sleep_until(grpc_timeout_seconds_to_deadline(2)); |
|
|
|
|
|
|
|
test_deny_unauthorized_request(f); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end_test(&f); |
|
|
|
end_test(&f); |
|
|
|
config.tear_down_data(&f); |
|
|
|
config.tear_down_data(&f); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void sdk_authz(grpc_end2end_test_config config) { |
|
|
|
void sdk_authz(grpc_end2end_test_config config) { |
|
|
|
test_static_init_allow_authorized_request(config); |
|
|
|
test_allow_authorized_request(config); |
|
|
|
test_static_init_deny_unauthorized_request(config); |
|
|
|
test_deny_unauthorized_request(config); |
|
|
|
test_static_init_deny_request_no_match_in_policy(config); |
|
|
|
test_deny_request_no_match_in_policy(config); |
|
|
|
test_file_watcher_init_allow_authorized_request(config); |
|
|
|
|
|
|
|
test_file_watcher_init_deny_unauthorized_request(config); |
|
|
|
|
|
|
|
test_file_watcher_init_deny_request_no_match_in_policy(config); |
|
|
|
|
|
|
|
test_file_watcher_valid_policy_reload(config); |
|
|
|
|
|
|
|
test_file_watcher_invalid_policy_skip_reload(config); |
|
|
|
|
|
|
|
test_file_watcher_recovers_from_failure(config); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void sdk_authz_pre_init(void) {} |
|
|
|
void sdk_authz_pre_init(void) {} |
|
|
|