1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
22
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
28
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/gpr/string.h"
31 #include "src/core/lib/iomgr/executor.h"
32 #include "src/core/lib/security/security_connector/fake/fake_security_connector.h"
33
34 /* -- Fake transport security credentials. -- */
35
36 namespace {
37 class grpc_fake_channel_credentials final : public grpc_channel_credentials {
38 public:
grpc_fake_channel_credentials()39 grpc_fake_channel_credentials()
40 : grpc_channel_credentials(
41 GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY) {}
42 ~grpc_fake_channel_credentials() override = default;
43
44 grpc_core::RefCountedPtr<grpc_channel_security_connector>
create_security_connector(grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,const char * target,const grpc_channel_args * args,grpc_channel_args **)45 create_security_connector(
46 grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
47 const char* target, const grpc_channel_args* args,
48 grpc_channel_args** /*new_args*/) override {
49 return grpc_fake_channel_security_connector_create(
50 this->Ref(), std::move(call_creds), target, args);
51 }
52 };
53
54 class grpc_fake_server_credentials final : public grpc_server_credentials {
55 public:
grpc_fake_server_credentials()56 grpc_fake_server_credentials()
57 : grpc_server_credentials(
58 GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY) {}
59 ~grpc_fake_server_credentials() override = default;
60
61 grpc_core::RefCountedPtr<grpc_server_security_connector>
create_security_connector(const grpc_channel_args *)62 create_security_connector(const grpc_channel_args* /*args*/) override {
63 return grpc_fake_server_security_connector_create(this->Ref());
64 }
65 };
66 } // namespace
67
grpc_fake_transport_security_credentials_create()68 grpc_channel_credentials* grpc_fake_transport_security_credentials_create() {
69 return new grpc_fake_channel_credentials();
70 }
71
72 grpc_server_credentials*
grpc_fake_transport_security_server_credentials_create()73 grpc_fake_transport_security_server_credentials_create() {
74 return new grpc_fake_server_credentials();
75 }
76
grpc_fake_transport_expected_targets_arg(char * expected_targets)77 grpc_arg grpc_fake_transport_expected_targets_arg(char* expected_targets) {
78 return grpc_channel_arg_string_create(
79 const_cast<char*>(GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS),
80 expected_targets);
81 }
82
grpc_fake_transport_get_expected_targets(const grpc_channel_args * args)83 const char* grpc_fake_transport_get_expected_targets(
84 const grpc_channel_args* args) {
85 const grpc_arg* expected_target_arg =
86 grpc_channel_args_find(args, GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS);
87 return grpc_channel_arg_get_string(expected_target_arg);
88 }
89
90 /* -- Metadata-only test credentials. -- */
91
get_request_metadata(grpc_polling_entity *,grpc_auth_metadata_context,grpc_credentials_mdelem_array * md_array,grpc_closure * on_request_metadata,grpc_error **)92 bool grpc_md_only_test_credentials::get_request_metadata(
93 grpc_polling_entity* /*pollent*/, grpc_auth_metadata_context /*context*/,
94 grpc_credentials_mdelem_array* md_array, grpc_closure* on_request_metadata,
95 grpc_error** /*error*/) {
96 grpc_credentials_mdelem_array_add(md_array, md_);
97 if (is_async_) {
98 grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_request_metadata,
99 GRPC_ERROR_NONE);
100 return false;
101 }
102 return true;
103 }
104
cancel_get_request_metadata(grpc_credentials_mdelem_array *,grpc_error * error)105 void grpc_md_only_test_credentials::cancel_get_request_metadata(
106 grpc_credentials_mdelem_array* /*md_array*/, grpc_error* error) {
107 GRPC_ERROR_UNREF(error);
108 }
109
grpc_md_only_test_credentials_create(const char * md_key,const char * md_value,bool is_async)110 grpc_call_credentials* grpc_md_only_test_credentials_create(
111 const char* md_key, const char* md_value, bool is_async) {
112 return new grpc_md_only_test_credentials(md_key, md_value, is_async);
113 }
114