|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/completion_queue.h" |
| 16 | +#include "google/cloud/credentials.h" |
| 17 | +#include "google/cloud/internal/getenv.h" |
| 18 | +#include "google/cloud/internal/unified_grpc_credentials.h" |
| 19 | +#include "google/cloud/testing_util/scoped_environment.h" |
| 20 | +#include "google/cloud/testing_util/status_matchers.h" |
| 21 | +#include <gmock/gmock.h> |
| 22 | +#if __has_include(<grpcpp/version_info.h>) |
| 23 | +#include <grpcpp/version_info.h> |
| 24 | +#endif |
| 25 | +#include <fstream> |
| 26 | + |
| 27 | +namespace google { |
| 28 | +namespace cloud { |
| 29 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 30 | +namespace internal { |
| 31 | +namespace { |
| 32 | + |
| 33 | +using ::google::cloud::testing_util::IsOk; |
| 34 | +using ::google::cloud::testing_util::ScopedEnvironment; |
| 35 | +using ::testing::IsEmpty; |
| 36 | +using ::testing::Not; |
| 37 | +using ::testing::NotNull; |
| 38 | + |
| 39 | +TEST(GrpcGdchServiceAccountIntegrationTest, |
| 40 | + RetrievesBearerTokenFromMemoryInAdhocEnvironment) { |
| 41 | +#if !defined(GRPC_CPP_VERSION_MAJOR) || \ |
| 42 | + (GRPC_CPP_VERSION_MAJOR < 1 || \ |
| 43 | + (GRPC_CPP_VERSION_MAJOR == 1 && GRPC_CPP_VERSION_MINOR < 84)) |
| 44 | + GTEST_SKIP() << "GDCH credentials require gRPC >= 1.84.0"; |
| 45 | +#endif |
| 46 | + std::optional<std::string> key_file_env = GetEnv("GRPC_TEST_GDCH_KEY_FILE"); |
| 47 | + std::optional<std::string> audience_env = GetEnv("GRPC_TEST_GDCH_AUDIENCE"); |
| 48 | + if (!key_file_env.has_value() || !audience_env.has_value()) GTEST_SKIP(); |
| 49 | + |
| 50 | + std::ifstream is(*key_file_env); |
| 51 | + std::string contents = std::string{std::istreambuf_iterator<char>{is}, {}}; |
| 52 | + ASSERT_THAT(contents, Not(IsEmpty())); |
| 53 | + |
| 54 | + CompletionQueue cq; |
| 55 | + |
| 56 | + std::shared_ptr<Credentials> creds = |
| 57 | + MakeGDCHServiceAccountCredentials(contents, *audience_env); |
| 58 | + ASSERT_THAT(creds, NotNull()); |
| 59 | + |
| 60 | + std::shared_ptr<GrpcAuthenticationStrategy> auth = |
| 61 | + CreateAuthenticationStrategy(*creds, cq); |
| 62 | + ASSERT_THAT(auth, NotNull()); |
| 63 | + |
| 64 | + grpc::ClientContext context; |
| 65 | + Status status = auth->ConfigureContext(context); |
| 66 | + EXPECT_THAT(status, IsOk()); |
| 67 | + |
| 68 | + std::shared_ptr<grpc::Channel> channel = |
| 69 | + auth->CreateChannel("localhost:443", grpc::ChannelArguments{}); |
| 70 | + EXPECT_THAT(channel, NotNull()); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(GrpcGdchServiceAccountIntegrationTest, |
| 74 | + RetrievesBearerTokenFromFileInAdhocEnvironment) { |
| 75 | +#if !defined(GRPC_CPP_VERSION_MAJOR) || \ |
| 76 | + (GRPC_CPP_VERSION_MAJOR < 1 || \ |
| 77 | + (GRPC_CPP_VERSION_MAJOR == 1 && GRPC_CPP_VERSION_MINOR < 84)) |
| 78 | + GTEST_SKIP() << "GDCH credentials require gRPC >= 1.84.0"; |
| 79 | +#endif |
| 80 | + std::optional<std::string> key_file_env = GetEnv("GRPC_TEST_GDCH_KEY_FILE"); |
| 81 | + std::optional<std::string> audience_env = GetEnv("GRPC_TEST_GDCH_AUDIENCE"); |
| 82 | + if (!key_file_env.has_value() || !audience_env.has_value()) GTEST_SKIP(); |
| 83 | + |
| 84 | + std::ifstream is(*key_file_env); |
| 85 | + std::string contents = std::string{std::istreambuf_iterator<char>{is}, {}}; |
| 86 | + ASSERT_THAT(contents, Not(IsEmpty())); |
| 87 | + |
| 88 | + CompletionQueue cq; |
| 89 | + |
| 90 | + ScopedEnvironment env("GOOGLE_APPLICATION_CREDENTIALS", key_file_env); |
| 91 | + std::shared_ptr<Credentials> creds = |
| 92 | + MakeGDCHServiceAccountCredentials(*audience_env); |
| 93 | + ASSERT_THAT(creds, NotNull()); |
| 94 | + |
| 95 | + std::shared_ptr<GrpcAuthenticationStrategy> auth = |
| 96 | + CreateAuthenticationStrategy(*creds, cq); |
| 97 | + ASSERT_THAT(auth, NotNull()); |
| 98 | + |
| 99 | + grpc::ClientContext context; |
| 100 | + Status status = auth->ConfigureContext(context); |
| 101 | + EXPECT_THAT(status, IsOk()); |
| 102 | + |
| 103 | + std::shared_ptr<grpc::Channel> channel = |
| 104 | + auth->CreateChannel("localhost:443", grpc::ChannelArguments{}); |
| 105 | + EXPECT_THAT(channel, NotNull()); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace |
| 109 | +} // namespace internal |
| 110 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 111 | +} // namespace cloud |
| 112 | +} // namespace google |
0 commit comments