• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
grpc_alts_credentials(const grpc_alts_credentials_options * options,const char * handshaker_service_url)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   grpc_alts_set_rpc_protocol_versions(&options_->rpc_versions);
45 }
46 
~grpc_alts_credentials()47 grpc_alts_credentials::~grpc_alts_credentials() {
48   grpc_alts_credentials_options_destroy(options_);
49   gpr_free(handshaker_service_url_);
50 }
51 
52 grpc_core::RefCountedPtr<grpc_channel_security_connector>
create_security_connector(grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,const char * target_name,const grpc_channel_args *,grpc_channel_args **)53 grpc_alts_credentials::create_security_connector(
54     grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
55     const char* target_name, const grpc_channel_args* /*args*/,
56     grpc_channel_args** /*new_args*/) {
57   return grpc_alts_channel_security_connector_create(
58       this->Ref(), std::move(call_creds), target_name);
59 }
60 
grpc_alts_server_credentials(const grpc_alts_credentials_options * options,const char * handshaker_service_url)61 grpc_alts_server_credentials::grpc_alts_server_credentials(
62     const grpc_alts_credentials_options* options,
63     const char* handshaker_service_url)
64     : grpc_server_credentials(GRPC_CREDENTIALS_TYPE_ALTS),
65       options_(grpc_alts_credentials_options_copy(options)),
66       handshaker_service_url_(handshaker_service_url == nullptr
67                                   ? gpr_strdup(GRPC_ALTS_HANDSHAKER_SERVICE_URL)
68                                   : gpr_strdup(handshaker_service_url)) {
69   grpc_alts_set_rpc_protocol_versions(&options_->rpc_versions);
70 }
71 
72 grpc_core::RefCountedPtr<grpc_server_security_connector>
create_security_connector(const grpc_channel_args *)73 grpc_alts_server_credentials::create_security_connector(
74     const grpc_channel_args* /* args */) {
75   return grpc_alts_server_security_connector_create(this->Ref());
76 }
77 
~grpc_alts_server_credentials()78 grpc_alts_server_credentials::~grpc_alts_server_credentials() {
79   grpc_alts_credentials_options_destroy(options_);
80   gpr_free(handshaker_service_url_);
81 }
82 
grpc_alts_credentials_create_customized(const grpc_alts_credentials_options * options,const char * handshaker_service_url,bool enable_untrusted_alts)83 grpc_channel_credentials* grpc_alts_credentials_create_customized(
84     const grpc_alts_credentials_options* options,
85     const char* handshaker_service_url, bool enable_untrusted_alts) {
86   if (!enable_untrusted_alts && !grpc_alts_is_running_on_gcp()) {
87     return nullptr;
88   }
89   return new grpc_alts_credentials(options, handshaker_service_url);
90 }
91 
grpc_alts_server_credentials_create_customized(const grpc_alts_credentials_options * options,const char * handshaker_service_url,bool enable_untrusted_alts)92 grpc_server_credentials* grpc_alts_server_credentials_create_customized(
93     const grpc_alts_credentials_options* options,
94     const char* handshaker_service_url, bool enable_untrusted_alts) {
95   if (!enable_untrusted_alts && !grpc_alts_is_running_on_gcp()) {
96     return nullptr;
97   }
98   return new grpc_alts_server_credentials(options, handshaker_service_url);
99 }
100 
grpc_alts_credentials_create(const grpc_alts_credentials_options * options)101 grpc_channel_credentials* grpc_alts_credentials_create(
102     const grpc_alts_credentials_options* options) {
103   return grpc_alts_credentials_create_customized(
104       options, GRPC_ALTS_HANDSHAKER_SERVICE_URL, false);
105 }
106 
grpc_alts_server_credentials_create(const grpc_alts_credentials_options * options)107 grpc_server_credentials* grpc_alts_server_credentials_create(
108     const grpc_alts_credentials_options* options) {
109   return grpc_alts_server_credentials_create_customized(
110       options, GRPC_ALTS_HANDSHAKER_SERVICE_URL, false);
111 }
112