Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: third_party/grpc/src/cpp/client/secure_credentials.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #include <grpc++/channel.h>
35 #include <grpc++/impl/grpc_library.h>
36 #include <grpc++/support/channel_arguments.h>
37 #include <grpc/support/log.h>
38 #include "src/cpp/client/create_channel_internal.h"
39 #include "src/cpp/client/secure_credentials.h"
40 #include "src/cpp/common/secure_auth_context.h"
41
42 namespace grpc {
43
44 static internal::GrpcLibraryInitializer g_gli_initializer;
45 SecureChannelCredentials::SecureChannelCredentials(
46 grpc_channel_credentials* c_creds)
47 : c_creds_(c_creds) {
48 g_gli_initializer.summon();
49 }
50
51 std::shared_ptr<grpc::Channel> SecureChannelCredentials::CreateChannel(
52 const string& target, const grpc::ChannelArguments& args) {
53 grpc_channel_args channel_args;
54 args.SetChannelArgs(&channel_args);
55 return CreateChannelInternal(
56 args.GetSslTargetNameOverride(),
57 grpc_secure_channel_create(c_creds_, target.c_str(), &channel_args,
58 nullptr));
59 }
60
61 SecureCallCredentials::SecureCallCredentials(grpc_call_credentials* c_creds)
62 : c_creds_(c_creds) {
63 internal::GrpcLibraryInitializer gli_initializer;
64 gli_initializer.summon();
65 }
66
67 bool SecureCallCredentials::ApplyToCall(grpc_call* call) {
68 return grpc_call_set_credentials(call, c_creds_) == GRPC_CALL_OK;
69 }
70
71 namespace {
72 std::shared_ptr<ChannelCredentials> WrapChannelCredentials(
73 grpc_channel_credentials* creds) {
74 return creds == nullptr ? nullptr : std::shared_ptr<ChannelCredentials>(
75 new SecureChannelCredentials(creds));
76 }
77
78 std::shared_ptr<CallCredentials> WrapCallCredentials(
79 grpc_call_credentials* creds) {
80 return creds == nullptr ? nullptr : std::shared_ptr<CallCredentials>(
81 new SecureCallCredentials(creds));
82 }
83 } // namespace
84
85 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials() {
86 GrpcLibrary init; // To call grpc_init().
87 return WrapChannelCredentials(grpc_google_default_credentials_create());
88 }
89
90 // Builds SSL Credentials given SSL specific options
91 std::shared_ptr<ChannelCredentials> SslCredentials(
92 const SslCredentialsOptions& options) {
93 GrpcLibrary init; // To call grpc_init().
94 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {
95 options.pem_private_key.c_str(), options.pem_cert_chain.c_str()};
96
97 grpc_channel_credentials* c_creds = grpc_ssl_credentials_create(
98 options.pem_root_certs.empty() ? nullptr : options.pem_root_certs.c_str(),
99 options.pem_private_key.empty() ? nullptr : &pem_key_cert_pair, nullptr);
100 return WrapChannelCredentials(c_creds);
101 }
102
103 // Builds credentials for use when running in GCE
104 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials() {
105 GrpcLibrary init; // To call grpc_init().
106 return WrapCallCredentials(
107 grpc_google_compute_engine_credentials_create(nullptr));
108 }
109
110 // Builds JWT credentials.
111 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
112 const grpc::string& json_key, long token_lifetime_seconds) {
113 GrpcLibrary init; // To call grpc_init().
114 if (token_lifetime_seconds <= 0) {
115 gpr_log(GPR_ERROR,
116 "Trying to create JWTCredentials with non-positive lifetime");
117 return WrapCallCredentials(nullptr);
118 }
119 gpr_timespec lifetime =
120 gpr_time_from_seconds(token_lifetime_seconds, GPR_TIMESPAN);
121 return WrapCallCredentials(grpc_service_account_jwt_access_credentials_create(
122 json_key.c_str(), lifetime, nullptr));
123 }
124
125 // Builds refresh token credentials.
126 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
127 const grpc::string& json_refresh_token) {
128 GrpcLibrary init; // To call grpc_init().
129 return WrapCallCredentials(grpc_google_refresh_token_credentials_create(
130 json_refresh_token.c_str(), nullptr));
131 }
132
133 // Builds access token credentials.
134 std::shared_ptr<CallCredentials> AccessTokenCredentials(
135 const grpc::string& access_token) {
136 GrpcLibrary init; // To call grpc_init().
137 return WrapCallCredentials(
138 grpc_access_token_credentials_create(access_token.c_str(), nullptr));
139 }
140
141 // Builds IAM credentials.
142 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
143 const grpc::string& authorization_token,
144 const grpc::string& authority_selector) {
145 GrpcLibrary init; // To call grpc_init().
146 return WrapCallCredentials(grpc_google_iam_credentials_create(
147 authorization_token.c_str(), authority_selector.c_str(), nullptr));
148 }
149
150 // Combines one channel credentials and one call credentials into a channel
151 // composite credentials.
152 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
153 const std::shared_ptr<ChannelCredentials>& channel_creds,
154 const std::shared_ptr<CallCredentials>& call_creds) {
155 // Note that we are not saving shared_ptrs to the two credentials passed in
156 // here. This is OK because the underlying C objects (i.e., channel_creds and
157 // call_creds) into grpc_composite_credentials_create will see their refcounts
158 // incremented.
159 SecureChannelCredentials* s_channel_creds =
160 channel_creds->AsSecureCredentials();
161 SecureCallCredentials* s_call_creds = call_creds->AsSecureCredentials();
162 if (s_channel_creds && s_call_creds) {
163 return WrapChannelCredentials(grpc_composite_channel_credentials_create(
164 s_channel_creds->GetRawCreds(), s_call_creds->GetRawCreds(), nullptr));
165 }
166 return nullptr;
167 }
168
169 void MetadataCredentialsPluginWrapper::Destroy(void* wrapper) {
170 if (wrapper == nullptr) return;
171 MetadataCredentialsPluginWrapper* w =
172 reinterpret_cast<MetadataCredentialsPluginWrapper*>(wrapper);
173 delete w;
174 }
175
176 void MetadataCredentialsPluginWrapper::GetMetadata(
177 void* wrapper, grpc_auth_metadata_context context,
178 grpc_credentials_plugin_metadata_cb cb, void* user_data) {
179 GPR_ASSERT(wrapper);
180 MetadataCredentialsPluginWrapper* w =
181 reinterpret_cast<MetadataCredentialsPluginWrapper*>(wrapper);
182 if (!w->plugin_) {
183 cb(user_data, NULL, 0, GRPC_STATUS_OK, NULL);
184 return;
185 }
186 if (w->plugin_->IsBlocking()) {
187 w->thread_pool_->Add(
188 std::bind(&MetadataCredentialsPluginWrapper::InvokePlugin, w, context,
189 cb, user_data));
190 } else {
191 w->InvokePlugin(context, cb, user_data);
192 }
193 }
194
195 void MetadataCredentialsPluginWrapper::InvokePlugin(
196 grpc_auth_metadata_context context, grpc_credentials_plugin_metadata_cb cb,
197 void* user_data) {
198 std::multimap<grpc::string, grpc::string> metadata;
199
200 // const_cast is safe since the SecureAuthContext does not take owndership and
201 // the object is passed as a const ref to plugin_->GetMetadata.
202 SecureAuthContext cpp_channel_auth_context(
203 const_cast<grpc_auth_context*>(context.channel_auth_context), false);
204
205 Status status = plugin_->GetMetadata(context.service_url, context.method_name,
206 cpp_channel_auth_context, &metadata);
207 std::vector<grpc_metadata> md;
208 for (auto it = metadata.begin(); it != metadata.end(); ++it) {
209 grpc_metadata md_entry;
210 md_entry.key = it->first.c_str();
211 md_entry.value = it->second.data();
212 md_entry.value_length = it->second.size();
213 md_entry.flags = 0;
214 md.push_back(md_entry);
215 }
216 cb(user_data, md.empty() ? nullptr : &md[0], md.size(),
217 static_cast<grpc_status_code>(status.error_code()),
218 status.error_message().c_str());
219 }
220
221 MetadataCredentialsPluginWrapper::MetadataCredentialsPluginWrapper(
222 std::unique_ptr<MetadataCredentialsPlugin> plugin)
223 : thread_pool_(CreateDefaultThreadPool()), plugin_(std::move(plugin)) {}
224
225 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
226 std::unique_ptr<MetadataCredentialsPlugin> plugin) {
227 GrpcLibrary init; // To call grpc_init().
228 const char* type = plugin->GetType();
229 MetadataCredentialsPluginWrapper* wrapper =
230 new MetadataCredentialsPluginWrapper(std::move(plugin));
231 grpc_metadata_credentials_plugin c_plugin = {
232 MetadataCredentialsPluginWrapper::GetMetadata,
233 MetadataCredentialsPluginWrapper::Destroy, wrapper, type};
234 return WrapCallCredentials(
235 grpc_metadata_credentials_create_from_plugin(c_plugin, nullptr));
236 }
237
238 } // namespace grpc
OLDNEW
« no previous file with comments | « third_party/grpc/src/cpp/client/secure_credentials.h ('k') | third_party/grpc/src/cpp/codegen/grpc_library.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698