Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / security / credentials / alts / alts_credentials.cc
1 /*
2  *
3  * Copyright 2018 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/alts/alts_credentials.h"
22
23 #include <cstring>
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29
30 #include "src/core/lib/security/credentials/alts/check_gcp_environment.h"
31 #include "src/core/lib/security/security_connector/alts/alts_security_connector.h"
32
33 #define GRPC_CREDENTIALS_TYPE_ALTS "Alts"
34 #define GRPC_ALTS_HANDSHAKER_SERVICE_URL "metadata.google.internal.:8080"
35
36 grpc_alts_credentials::grpc_alts_credentials(
37     const grpc_alts_credentials_options* options,
38     const char* handshaker_service_url)
39     : grpc_channel_credentials(GRPC_CREDENTIALS_TYPE_ALTS),
40       options_(grpc_alts_credentials_options_copy(options)),
41       handshaker_service_url_(handshaker_service_url == nullptr
42                                   ? gpr_strdup(GRPC_ALTS_HANDSHAKER_SERVICE_URL)
43                                   : gpr_strdup(handshaker_service_url)) {}
44
45 grpc_alts_credentials::~grpc_alts_credentials() {
46   grpc_alts_credentials_options_destroy(options_);
47   gpr_free(handshaker_service_url_);
48 }
49
50 grpc_core::RefCountedPtr<grpc_channel_security_connector>
51 grpc_alts_credentials::create_security_connector(
52     grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
53     const char* target_name, const grpc_channel_args* args,
54     grpc_channel_args** new_args) {
55   return grpc_alts_channel_security_connector_create(
56       this->Ref(), std::move(call_creds), target_name);
57 }
58
59 grpc_alts_server_credentials::grpc_alts_server_credentials(
60     const grpc_alts_credentials_options* options,
61     const char* handshaker_service_url)
62     : grpc_server_credentials(GRPC_CREDENTIALS_TYPE_ALTS),
63       options_(grpc_alts_credentials_options_copy(options)),
64       handshaker_service_url_(handshaker_service_url == nullptr
65                                   ? gpr_strdup(GRPC_ALTS_HANDSHAKER_SERVICE_URL)
66                                   : gpr_strdup(handshaker_service_url)) {}
67
68 grpc_core::RefCountedPtr<grpc_server_security_connector>
69 grpc_alts_server_credentials::create_security_connector() {
70   return grpc_alts_server_security_connector_create(this->Ref());
71 }
72
73 grpc_alts_server_credentials::~grpc_alts_server_credentials() {
74   grpc_alts_credentials_options_destroy(options_);
75   gpr_free(handshaker_service_url_);
76 }
77
78 grpc_channel_credentials* grpc_alts_credentials_create_customized(
79     const grpc_alts_credentials_options* options,
80     const char* handshaker_service_url, bool enable_untrusted_alts) {
81   if (!enable_untrusted_alts && !grpc_alts_is_running_on_gcp()) {
82     return nullptr;
83   }
84   return grpc_core::New<grpc_alts_credentials>(options, handshaker_service_url);
85 }
86
87 grpc_server_credentials* grpc_alts_server_credentials_create_customized(
88     const grpc_alts_credentials_options* options,
89     const char* handshaker_service_url, bool enable_untrusted_alts) {
90   if (!enable_untrusted_alts && !grpc_alts_is_running_on_gcp()) {
91     return nullptr;
92   }
93   return grpc_core::New<grpc_alts_server_credentials>(options,
94                                                       handshaker_service_url);
95 }
96
97 grpc_channel_credentials* grpc_alts_credentials_create(
98     const grpc_alts_credentials_options* options) {
99   return grpc_alts_credentials_create_customized(
100       options, GRPC_ALTS_HANDSHAKER_SERVICE_URL, false);
101 }
102
103 grpc_server_credentials* grpc_alts_server_credentials_create(
104     const grpc_alts_credentials_options* options) {
105   return grpc_alts_server_credentials_create_customized(
106       options, GRPC_ALTS_HANDSHAKER_SERVICE_URL, false);
107 }