| 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 <utility> |
| 24 |
|
| 25 |
#include "absl/strings/string_view.h" |
| 26 |
|
| 27 |
#include <grpc/grpc.h> |
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
#include <grpc/support/string_util.h> |
| 30 |
|
| 31 |
#include "src/core/lib/security/credentials/alts/check_gcp_environment.h" |
| 32 |
#include "src/core/lib/security/credentials/alts/grpc_alts_credentials_options.h" |
| 33 |
#include "src/core/lib/security/security_connector/alts/alts_security_connector.h" |
| 34 |
|
| 35 |
#define GRPC_ALTS_HANDSHAKER_SERVICE_URL "metadata.google.internal.:8080" |
| 36 |
|
| 37 |
grpc_alts_credentials::grpc_alts_credentials( |
| 38 |
const grpc_alts_credentials_options* options, |
| 39 |
const char* handshaker_service_url) |
| 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 |
|
| 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> |
| 53 |
grpc_alts_credentials::create_security_connector( |
| 54 |
grpc_core::RefCountedPtr<grpc_call_credentials> call_creds, |
| 55 |
const char* target_name, grpc_core::ChannelArgs* /*args*/) { |
| 56 |
return grpc_alts_channel_security_connector_create( |
| 57 |
this->Ref(), std::move(call_creds), target_name); |
| 58 |
} |
| 59 |
|
| 60 |
grpc_core::UniqueTypeName grpc_alts_credentials::type() const { |
| 61 |
static grpc_core::UniqueTypeName::Factory kFactory("Alts"); |
| 62 |
return kFactory.Create(); |
| 63 |
} |
| 64 |
|
| 65 |
grpc_alts_server_credentials::grpc_alts_server_credentials( |
| 66 |
const grpc_alts_credentials_options* options, |
| 67 |
const char* handshaker_service_url) |
| 68 |
: options_(grpc_alts_credentials_options_copy(options)), |
| 69 |
handshaker_service_url_(handshaker_service_url == nullptr |
| 70 |
? gpr_strdup(GRPC_ALTS_HANDSHAKER_SERVICE_URL) |
| 71 |
: gpr_strdup(handshaker_service_url)) { |
| 72 |
grpc_alts_set_rpc_protocol_versions(&options_->rpc_versions); |
| 73 |
} |
| 74 |
|
| 75 |
grpc_core::RefCountedPtr<grpc_server_security_connector> |
| 76 |
grpc_alts_server_credentials::create_security_connector( |
| 77 |
const grpc_core::ChannelArgs& /* args */) { |
| 78 |
return grpc_alts_server_security_connector_create(this->Ref()); |
| 79 |
} |
| 80 |
|
| 81 |
grpc_alts_server_credentials::~grpc_alts_server_credentials() { |
| 82 |
grpc_alts_credentials_options_destroy(options_); |
| 83 |
gpr_free(handshaker_service_url_); |
| 84 |
} |
| 85 |
|
| 86 |
grpc_core::UniqueTypeName grpc_alts_server_credentials::type() const { |
| 87 |
static grpc_core::UniqueTypeName::Factory kFactory("Alts"); |
| 88 |
return kFactory.Create(); |
| 89 |
} |
| 90 |
|
| 91 |
grpc_channel_credentials* grpc_alts_credentials_create_customized( |
| 92 |
const grpc_alts_credentials_options* options, |
| 93 |
const char* handshaker_service_url, bool enable_untrusted_alts) { |
| 94 |
if (!enable_untrusted_alts && !grpc_alts_is_running_on_gcp()) { |
| 95 |
return nullptr; |
| 96 |
} |
| 97 |
return new grpc_alts_credentials(options, handshaker_service_url); |
| 98 |
} |
| 99 |
|
| 100 |
grpc_server_credentials* grpc_alts_server_credentials_create_customized( |
| 101 |
const grpc_alts_credentials_options* options, |
| 102 |
const char* handshaker_service_url, bool enable_untrusted_alts) { |
| 103 |
if (!enable_untrusted_alts && !grpc_alts_is_running_on_gcp()) { |
| 104 |
return nullptr; |
| 105 |
} |
| 106 |
return new grpc_alts_server_credentials(options, handshaker_service_url); |
| 107 |
} |
| 108 |
|
| 109 |
grpc_channel_credentials* grpc_alts_credentials_create( |
| 110 |
const grpc_alts_credentials_options* options) { |
| 111 |
return grpc_alts_credentials_create_customized( |
| 112 |
options, GRPC_ALTS_HANDSHAKER_SERVICE_URL, false); |
| 113 |
} |
| 114 |
|
| 115 |
grpc_server_credentials* grpc_alts_server_credentials_create( |
| 116 |
const grpc_alts_credentials_options* options) { |
| 117 |
return grpc_alts_server_credentials_create_customized( |
| 118 |
options, GRPC_ALTS_HANDSHAKER_SERVICE_URL, false); |
| 119 |
} |