Skip to content

Commit 0692913

Browse files
authored
ci: add SA to SA impersonation integration tests for universe domain (#14862)
* ci: add impersonation integration tests for universe domain * remove integration tests tag * add integration tests tag * revert renaming * exclude tag ud-only * format * fix * address comments
1 parent 95126be commit 0692913

File tree

8 files changed

+165
-12
lines changed

8 files changed

+165
-12
lines changed

ci/cloudbuild/builds/integration-daily.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ integration::bazel_with_emulators test "${args[@]}" "${integration_args[@]}"
4848

4949
io::log_h2 "Running Bigtable integration tests (against prod)"
5050
bazel test "${args[@]}" "${integration_args[@]}" \
51-
--test_tag_filters="integration-test" -- \
51+
--test_tag_filters="integration-test,-ud-only" -- \
5252
"//google/cloud/bigtable/..." \
5353
"-//google/cloud/bigtable/examples:bigtable_grpc_credentials"
5454

5555
io::log_h2 "Running Spanner integration tests (against prod)"
5656
bazel test "${args[@]}" "${integration_args[@]}" \
57-
--test_tag_filters="integration-test" --test_timeout=-1,-1,-1,10800 \
57+
--test_tag_filters="integration-test,-ud-only" --test_timeout=-1,-1,-1,10800 \
5858
"//google/cloud/spanner/..."

ci/cloudbuild/builds/integration-production.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ excluded_rules=(
4040
io::log_h2 "Running the integration tests against prod"
4141
mapfile -t integration_args < <(integration::bazel_args)
4242
io::run bazel test "${args[@]}" "${integration_args[@]}" \
43-
--cache_test_results="auto" --test_tag_filters="integration-test" \
43+
--cache_test_results="auto" --test_tag_filters="integration-test,-ud-only" \
4444
-- "${BAZEL_TARGETS[@]}" "${excluded_rules[@]}"

ci/cloudbuild/builds/lib/integration.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,30 +178,30 @@ function integration::bazel_with_emulators() {
178178
"google/cloud:internal_unified_rest_credentials_integration_test"
179179
)
180180

181-
production_tests_tag_filters="integration-test"
181+
production_tests_tag_filters="integration-test,-ud-only"
182182
if echo "${args[@]}" | grep -w -q -- "--config=msan"; then
183-
production_tests_tag_filters="integration-test,-no-msan"
183+
production_tests_tag_filters="integration-test,-no-msan,-ud-only"
184184
fi
185185

186186
io::log_h2 "Running Pub/Sub integration tests (with emulator)"
187187
"google/cloud/pubsub/ci/${EMULATOR_SCRIPT}" \
188-
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test"
188+
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test,-ud-only"
189189

190190
io::log_h2 "Running Storage integration tests (with emulator)"
191191
"google/cloud/storage/ci/${EMULATOR_SCRIPT}" \
192-
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test"
192+
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test,-ud-only"
193193

194194
io::log_h2 "Running Spanner integration tests (with emulator)"
195195
"google/cloud/spanner/ci/${EMULATOR_SCRIPT}" \
196-
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test"
196+
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test,-ud-only"
197197

198198
io::log_h2 "Running Bigtable integration tests (with emulator)"
199199
"google/cloud/bigtable/ci/${EMULATOR_SCRIPT}" \
200-
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test"
200+
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test,-ud-only"
201201

202202
io::log_h2 "Running REST integration tests (with emulator)"
203203
"google/cloud/internal/ci/${EMULATOR_SCRIPT}" \
204-
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test"
204+
bazel "${verb}" "${args[@]}" --test_tag_filters="integration-test,-ud-only"
205205

206206
if [[ "${BAZEL_TARGETS[*]}" != "..." ]]; then
207207
io::log_h2 "Skipping some integration tests because BAZEL_TARGETS is not the default"

ci/cloudbuild/builds/lib/universe_domain.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ function ud::bazel_test() {
4343
bazel test "${args[@]}" --sandbox_add_mount_pair=/tmp \
4444
--test_env=UD_SA_KEY_FILE="${UD_SA_KEY_FILE}" \
4545
--test_env=UD_REGION="${UD_REGION}" \
46+
--test_env=UD_ZONE="${UD_ZONE}" \
47+
--test_env=UD_IMPERSONATED_SERVICE_ACCOUNT_NAME="${UD_IMPERSONATED_SERVICE_ACCOUNT_NAME}" \
4648
--test_env=UD_PROJECT="${UD_PROJECT}" -- "$@"
4749
}

ci/cloudbuild/builds/universe-domain-demo.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ if [[ -n "${UD_SA_KEY_FILE}" ]]; then
2929
ud::bazel_run //google/cloud/universe_domain/demo:kms_demo \
3030
"${UD_PROJECT}" "${UD_REGION}" "${UD_SA_KEY_FILE}"
3131
ud::bazel_test //google/cloud/storage/tests:universe_domain_integration_test
32+
ud::bazel_test //google/cloud/universe_domain/integration_tests:impersonation_tests
3233
else
3334
source module ci/etc/integration-tests-config.sh
3435
bazel run -- //google/cloud/universe_domain/demo:kms_demo \

ci/cloudbuild/cloudbuild.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,14 @@ availableSecrets:
7070
env: 'UD_PROJECT'
7171
- versionName: 'projects/${PROJECT_ID}/secrets/UD_REGION/versions/latest'
7272
env: 'UD_REGION'
73+
- versionName: 'projects/${PROJECT_ID}/secrets/UD_ZONE/versions/latest'
74+
env: 'UD_ZONE'
7375
- versionName: 'projects/${PROJECT_ID}/secrets/UD_SERVICE_ACCOUNT/versions/latest'
7476
env: 'UD_SERVICE_ACCOUNT'
7577
- versionName: 'projects/${PROJECT_ID}/secrets/UD_SERVICE_ACCOUNT_NAME/versions/latest'
7678
env: 'UD_SERVICE_ACCOUNT_NAME'
77-
79+
- versionName: 'projects/${PROJECT_ID}/secrets/UD_IMPERSONATED_SERVICE_ACCOUNT_NAME/versions/latest'
80+
env: 'UD_IMPERSONATED_SERVICE_ACCOUNT_NAME'
7881

7982
logsBucket: 'gs://${_LOGS_BUCKET}/logs/google-cloud-cpp/${_TRIGGER_SOURCE}/${COMMIT_SHA}/${_DISTRO}-${_BUILD_NAME}-${_SHARD}'
8083

@@ -112,7 +115,7 @@ steps:
112115
- name: '${_POOL_REGION}-docker.pkg.dev/${PROJECT_ID}/gcb/${_IMAGE}:${BUILD_ID}'
113116
entrypoint: 'ci/cloudbuild/build.sh'
114117
args: [ '--local', '--build', '${_BUILD_NAME}' ]
115-
secretEnv: ['CODECOV_TOKEN', 'UD', 'UD_PROJECT', 'UD_REGION', 'UD_SERVICE_ACCOUNT', 'UD_SERVICE_ACCOUNT_NAME']
118+
secretEnv: ['CODECOV_TOKEN', 'UD', 'UD_PROJECT', 'UD_REGION', 'UD_ZONE', 'UD_SERVICE_ACCOUNT', 'UD_SERVICE_ACCOUNT_NAME', 'UD_IMPERSONATED_SERVICE_ACCOUNT_NAME']
116119
env: [
117120
'BAZEL_REMOTE_CACHE=https://storage.googleapis.com/${_CACHE_BUCKET}/bazel-cache/${_DISTRO}-${_BUILD_NAME}',
118121
'LIBRARIES=${_LIBRARIES}',
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2024 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+
package(default_visibility = ["//visibility:private"])
16+
17+
licenses(["notice"]) # Apache 2.0
18+
19+
cc_test(
20+
name = "impersonation_tests",
21+
timeout = "long",
22+
srcs = ["impersonation_tests.cc"],
23+
tags = [
24+
"integration-test",
25+
"ud-only",
26+
],
27+
deps = [
28+
"//:common",
29+
"//:experimental-universe_domain",
30+
"//google/cloud/testing_util:google_cloud_cpp_testing_private",
31+
"@com_google_googletest//:gtest_main",
32+
"@google_cloud_cpp//:compute",
33+
"@google_cloud_cpp//:kms",
34+
],
35+
)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2024 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/compute/disks/v1/disks_client.h"
16+
#include "google/cloud/compute/disks/v1/disks_options.h"
17+
#include "google/cloud/kms/v1/key_management_client.h"
18+
#include "google/cloud/kms/v1/key_management_options.h"
19+
#include "google/cloud/internal/getenv.h"
20+
#include "google/cloud/internal/rest_options.h"
21+
#include "google/cloud/location.h"
22+
#include "google/cloud/testing_util/integration_test.h"
23+
#include "google/cloud/testing_util/status_matchers.h"
24+
#include "google/cloud/universe_domain.h"
25+
#include "google/cloud/universe_domain_options.h"
26+
#include <gmock/gmock.h>
27+
#include <fstream>
28+
29+
namespace google {
30+
namespace cloud {
31+
namespace universe_domain {
32+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
33+
namespace {
34+
35+
namespace gc = ::google::cloud;
36+
using ::google::cloud::testing_util::StatusIs;
37+
38+
class DomainUniverseImpersonationTest
39+
: public ::google::cloud::testing_util::IntegrationTest {
40+
protected:
41+
void SetUp() override {
42+
project_id_ = gc::internal::GetEnv("UD_PROJECT").value_or("");
43+
ASSERT_FALSE(project_id_.empty());
44+
zone_id_ = gc::internal::GetEnv("UD_ZONE").value_or("");
45+
ASSERT_FALSE(zone_id_.empty());
46+
region_id_ = gc::internal::GetEnv("UD_REGION").value_or("");
47+
ASSERT_FALSE(region_id_.empty());
48+
impersonated_sa_ =
49+
gc::internal::GetEnv("UD_IMPERSONATED_SERVICE_ACCOUNT_NAME")
50+
.value_or("");
51+
ASSERT_FALSE(impersonated_sa_.empty());
52+
std::string const sa_key_file =
53+
gc::internal::GetEnv("UD_SA_KEY_FILE").value_or("");
54+
ASSERT_FALSE(sa_key_file.empty());
55+
56+
auto is = std::ifstream(sa_key_file);
57+
is.exceptions(std::ios::badbit);
58+
credential_ = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
59+
}
60+
61+
std::string project_id_;
62+
std::string zone_id_;
63+
std::string region_id_;
64+
std::string impersonated_sa_;
65+
std::string credential_;
66+
};
67+
68+
TEST_F(DomainUniverseImpersonationTest, SAToSAImpersonationRest) {
69+
namespace disks = ::google::cloud::compute_disks_v1;
70+
71+
gc::Options options;
72+
options.set<google::cloud::UnifiedCredentialsOption>(
73+
google::cloud::MakeImpersonateServiceAccountCredentials(
74+
google::cloud::MakeServiceAccountCredentials(credential_),
75+
impersonated_sa_));
76+
77+
auto ud_options = gc::AddUniverseDomainOption(gc::ExperimentalTag{}, options);
78+
ASSERT_STATUS_OK(ud_options);
79+
80+
auto client = disks::DisksClient(disks::MakeDisksConnectionRest(*ud_options));
81+
82+
for (auto disk : client.ListDisks(project_id_, zone_id_)) {
83+
EXPECT_STATUS_OK(disk);
84+
}
85+
}
86+
87+
TEST_F(DomainUniverseImpersonationTest, SAToSAImpersonationGrpc) {
88+
namespace kms = ::google::cloud::kms_v1;
89+
90+
auto const location = gc::Location(project_id_, region_id_);
91+
gc::Options options;
92+
options.set<google::cloud::UnifiedCredentialsOption>(
93+
google::cloud::MakeImpersonateServiceAccountCredentials(
94+
google::cloud::MakeServiceAccountCredentials(credential_),
95+
impersonated_sa_));
96+
97+
auto ud_options = gc::AddUniverseDomainOption(gc::ExperimentalTag{}, options);
98+
ASSERT_STATUS_OK(ud_options);
99+
100+
auto client = kms::KeyManagementServiceClient(
101+
kms::MakeKeyManagementServiceConnection(*ud_options));
102+
103+
for (auto kr : client.ListKeyRings(location.FullName())) {
104+
EXPECT_STATUS_OK(kr);
105+
}
106+
}
107+
108+
} // namespace
109+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
110+
} // namespace universe_domain
111+
} // namespace cloud
112+
} // namespace google

0 commit comments

Comments
 (0)