Skip to content

Commit 921759b

Browse files
authored
ci: add IdToken integration tests for universe domain (#14867)
* ci: add IdToken integration tests for universe domain * unify test class
1 parent 7411652 commit 921759b

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

ci/cloudbuild/builds/lib/universe_domain.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ if [[ -n "${UD_SERVICE_ACCOUNT}" ]]; then
3232
io::log "Created SA key file ${UD_SA_KEY_FILE}"
3333
fi
3434

35+
# Only create the IdToken SA key file if the secret is available.
36+
if [[ -n "${UD_IDTOKEN_SA_IMPERSONATION_CRED}" ]]; then
37+
ORIG_UMASK=$(umask)
38+
umask 077
39+
UD_IDTOKEN_SA_KEY_FILE=$(mktemp)
40+
echo "${UD_IDTOKEN_SA_IMPERSONATION_CRED}" >"${UD_IDTOKEN_SA_KEY_FILE}"
41+
umask "${ORIG_UMASK}"
42+
io::log "Created IdToken SA key file ${UD_IDTOKEN_SA_KEY_FILE}"
43+
fi
44+
3545
function ud::bazel_run() {
3646
io::log "Executing bazel run $1 with obscured arguments:"
3747
bazel run --ui_event_filters=-info -- "$@"
@@ -45,5 +55,6 @@ function ud::bazel_test() {
4555
--test_env=UD_REGION="${UD_REGION}" \
4656
--test_env=UD_ZONE="${UD_ZONE}" \
4757
--test_env=UD_IMPERSONATED_SERVICE_ACCOUNT_NAME="${UD_IMPERSONATED_SERVICE_ACCOUNT_NAME}" \
58+
--test_env=UD_IDTOKEN_SA_KEY_FILE="${UD_IDTOKEN_SA_KEY_FILE}" \
4859
--test_env=UD_PROJECT="${UD_PROJECT}" -- "$@"
4960
}

ci/cloudbuild/cloudbuild.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ availableSecrets:
7878
env: 'UD_SERVICE_ACCOUNT_NAME'
7979
- versionName: 'projects/${PROJECT_ID}/secrets/UD_IMPERSONATED_SERVICE_ACCOUNT_NAME/versions/latest'
8080
env: 'UD_IMPERSONATED_SERVICE_ACCOUNT_NAME'
81+
- versionName: 'projects/${PROJECT_ID}/secrets/UD_IDTOKEN_SA_IMPERSONATION_CRED/versions/latest'
82+
env: 'UD_IDTOKEN_SA_IMPERSONATION_CRED'
8183

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

@@ -115,7 +117,7 @@ steps:
115117
- name: '${_POOL_REGION}-docker.pkg.dev/${PROJECT_ID}/gcb/${_IMAGE}:${BUILD_ID}'
116118
entrypoint: 'ci/cloudbuild/build.sh'
117119
args: [ '--local', '--build', '${_BUILD_NAME}' ]
118-
secretEnv: ['CODECOV_TOKEN', 'UD', 'UD_PROJECT', 'UD_REGION', 'UD_ZONE', 'UD_SERVICE_ACCOUNT', 'UD_SERVICE_ACCOUNT_NAME', 'UD_IMPERSONATED_SERVICE_ACCOUNT_NAME']
120+
secretEnv: ['CODECOV_TOKEN', 'UD', 'UD_PROJECT', 'UD_REGION', 'UD_ZONE', 'UD_SERVICE_ACCOUNT', 'UD_SERVICE_ACCOUNT_NAME', 'UD_IMPERSONATED_SERVICE_ACCOUNT_NAME', 'UD_IDTOKEN_SA_IMPERSONATION_CRED']
119121
env: [
120122
'BAZEL_REMOTE_CACHE=https://storage.googleapis.com/${_CACHE_BUCKET}/bazel-cache/${_DISTRO}-${_BUILD_NAME}',
121123
'LIBRARIES=${_LIBRARIES}',

google/cloud/universe_domain/integration_tests/impersonation_tests.cc

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "google/cloud/internal/rest_options.h"
2121
#include "google/cloud/location.h"
2222
#include "google/cloud/testing_util/integration_test.h"
23+
#include "google/cloud/testing_util/scoped_environment.h"
2324
#include "google/cloud/testing_util/status_matchers.h"
2425
#include "google/cloud/universe_domain.h"
2526
#include "google/cloud/universe_domain_options.h"
@@ -33,6 +34,7 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3334
namespace {
3435

3536
namespace gc = ::google::cloud;
37+
using ::google::cloud::testing_util::ScopedEnvironment;
3638
using ::google::cloud::testing_util::StatusIs;
3739

3840
class DomainUniverseImpersonationTest
@@ -45,27 +47,42 @@ class DomainUniverseImpersonationTest
4547
ASSERT_FALSE(zone_id_.empty());
4648
region_id_ = gc::internal::GetEnv("UD_REGION").value_or("");
4749
ASSERT_FALSE(region_id_.empty());
50+
}
51+
52+
std::string project_id_;
53+
std::string zone_id_;
54+
std::string region_id_;
55+
};
56+
57+
class ServiceAccountImpersonationTest : public DomainUniverseImpersonationTest {
58+
protected:
59+
void SetUp() override {
60+
DomainUniverseImpersonationTest::SetUp();
61+
4862
impersonated_sa_ =
4963
gc::internal::GetEnv("UD_IMPERSONATED_SERVICE_ACCOUNT_NAME")
5064
.value_or("");
5165
ASSERT_FALSE(impersonated_sa_.empty());
52-
std::string const sa_key_file =
66+
67+
std::string const key_file =
5368
gc::internal::GetEnv("UD_SA_KEY_FILE").value_or("");
54-
ASSERT_FALSE(sa_key_file.empty());
69+
ASSERT_FALSE(key_file.empty());
5570

56-
auto is = std::ifstream(sa_key_file);
71+
auto is = std::ifstream(key_file);
5772
is.exceptions(std::ios::badbit);
5873
credential_ = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
74+
75+
id_token_key_file_ =
76+
gc::internal::GetEnv("UD_IDTOKEN_SA_KEY_FILE").value_or("");
77+
ASSERT_FALSE(id_token_key_file_.empty());
5978
}
6079

61-
std::string project_id_;
62-
std::string zone_id_;
63-
std::string region_id_;
6480
std::string impersonated_sa_;
6581
std::string credential_;
82+
std::string id_token_key_file_;
6683
};
6784

68-
TEST_F(DomainUniverseImpersonationTest, SAToSAImpersonationRest) {
85+
TEST_F(ServiceAccountImpersonationTest, SAToSAImpersonationRest) {
6986
namespace disks = ::google::cloud::compute_disks_v1;
7087

7188
gc::Options options;
@@ -84,7 +101,7 @@ TEST_F(DomainUniverseImpersonationTest, SAToSAImpersonationRest) {
84101
}
85102
}
86103

87-
TEST_F(DomainUniverseImpersonationTest, SAToSAImpersonationGrpc) {
104+
TEST_F(ServiceAccountImpersonationTest, SAToSAImpersonationGrpc) {
88105
namespace kms = ::google::cloud::kms_v1;
89106

90107
auto const location = gc::Location(project_id_, region_id_);
@@ -105,6 +122,21 @@ TEST_F(DomainUniverseImpersonationTest, SAToSAImpersonationGrpc) {
105122
}
106123
}
107124

125+
TEST_F(ServiceAccountImpersonationTest, IdTokenSAToSAImpersonationRest) {
126+
namespace disks = ::google::cloud::compute_disks_v1;
127+
// Use ADC credential
128+
ScopedEnvironment env("GOOGLE_APPLICATION_CREDENTIALS", id_token_key_file_);
129+
130+
auto ud_options = gc::AddUniverseDomainOption(gc::ExperimentalTag{});
131+
ASSERT_STATUS_OK(ud_options);
132+
133+
auto client = disks::DisksClient(disks::MakeDisksConnectionRest(*ud_options));
134+
135+
for (auto disk : client.ListDisks(project_id_, zone_id_)) {
136+
EXPECT_STATUS_OK(disk);
137+
}
138+
}
139+
108140
} // namespace
109141
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
110142
} // namespace universe_domain

0 commit comments

Comments
 (0)