Skip to content

Commit d20961d

Browse files
authored
impl: prevent logging options from being overwritten (#15090)
1 parent 7acace2 commit d20961d

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

google/cloud/internal/credentials_impl.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ Options PopulateAuthOptions(Options options) {
4242
.set<AccessTokenLifetimeOption>(kDefaultTokenLifetime));
4343
// Then apply any overrides.
4444
return MergeOptions(
45-
Options{}.set<LoggingComponentsOption>(DefaultTracingComponents()),
46-
std::move(options));
45+
std::move(options),
46+
Options{}.set<LoggingComponentsOption>(DefaultTracingComponents()));
4747
}
4848

4949
void CredentialsVisitor::dispatch(Credentials const& credentials,

google/cloud/internal/credentials_impl_test.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/internal/credentials_impl.h"
16+
#include "google/cloud/internal/populate_common_options.h"
1617
#include "google/cloud/testing_util/credentials.h"
1718
#include <gmock/gmock.h>
1819

@@ -24,6 +25,7 @@ namespace {
2425

2526
using ::google::cloud::testing_util::TestCredentialsVisitor;
2627
using ::testing::ElementsAre;
28+
using ::testing::Eq;
2729
using ::testing::IsEmpty;
2830
using ::testing::IsNull;
2931

@@ -122,6 +124,42 @@ TEST(Credentials, ApiKeyCredentials) {
122124
EXPECT_EQ("api-key", visitor.api_key);
123125
}
124126

127+
TEST(PopulateAuthOptions, EmptyOptions) {
128+
auto result_options = PopulateAuthOptions(Options{});
129+
130+
EXPECT_THAT(result_options.get<ScopesOption>(),
131+
ElementsAre("https://www.googleapis.com/auth/cloud-platform"));
132+
EXPECT_THAT(result_options.get<AccessTokenLifetimeOption>(),
133+
Eq(std::chrono::hours(1)));
134+
EXPECT_THAT(result_options.get<LoggingComponentsOption>(),
135+
Eq(DefaultTracingComponents()));
136+
}
137+
138+
TEST(PopulateAuthOptions, ExistingNonIntersectingOptions) {
139+
auto options = Options{}.set<EndpointOption>("my-endpoint");
140+
auto result_options = PopulateAuthOptions(options);
141+
142+
EXPECT_THAT(result_options.get<EndpointOption>(), Eq("my-endpoint"));
143+
EXPECT_THAT(result_options.get<ScopesOption>(),
144+
ElementsAre("https://www.googleapis.com/auth/cloud-platform"));
145+
EXPECT_THAT(result_options.get<AccessTokenLifetimeOption>(),
146+
Eq(std::chrono::hours(1)));
147+
EXPECT_THAT(result_options.get<LoggingComponentsOption>(),
148+
Eq(DefaultTracingComponents()));
149+
}
150+
151+
TEST(PopulateAuthOptions, ExistingIntersectingOptions) {
152+
auto options = Options{}
153+
.set<ScopesOption>({"my-scope"})
154+
.set<LoggingComponentsOption>({"my-logging-component"});
155+
auto result_options = PopulateAuthOptions(options);
156+
EXPECT_THAT(result_options.get<ScopesOption>(), ElementsAre("my-scope"));
157+
EXPECT_THAT(result_options.get<AccessTokenLifetimeOption>(),
158+
Eq(std::chrono::hours(1)));
159+
EXPECT_THAT(result_options.get<LoggingComponentsOption>(),
160+
ElementsAre("my-logging-component"));
161+
}
162+
125163
} // namespace
126164
} // namespace internal
127165
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

google/cloud/internal/populate_common_options.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,17 @@ TracingOptions DefaultTracingOptions() {
9494
return TracingOptions{}.SetOptions(*tracing_options);
9595
}
9696

97+
// TODO(#15089): Determine if this function needs to preserve more (or all) of
98+
// the options passed in.
9799
Options MakeAuthOptions(Options const& options) {
98100
Options opts;
99101
if (options.has<OpenTelemetryTracingOption>()) {
100102
opts.set<OpenTelemetryTracingOption>(
101103
options.get<OpenTelemetryTracingOption>());
102104
}
105+
if (options.has<LoggingComponentsOption>()) {
106+
opts.set<LoggingComponentsOption>(options.get<LoggingComponentsOption>());
107+
}
103108
return opts;
104109
}
105110

google/cloud/internal/populate_common_options_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,23 @@ TEST(MakeAuthOptions, WithTracing) {
299299
EXPECT_TRUE(auth_options.get<OpenTelemetryTracingOption>());
300300
}
301301

302+
TEST(MakeAuthOptions, WithoutLoggingComponents) {
303+
auto options = Options{}.set<EndpointOption>("endpoint_option");
304+
auto auth_options = MakeAuthOptions(options);
305+
EXPECT_FALSE(auth_options.has<EndpointOption>());
306+
EXPECT_FALSE(auth_options.has<LoggingComponentsOption>());
307+
}
308+
309+
TEST(MakeAuthOptions, WithLoggingComponents) {
310+
auto options = Options{}
311+
.set<EndpointOption>("endpoint_option")
312+
.set<LoggingComponentsOption>({"logging_component"});
313+
auto auth_options = MakeAuthOptions(options);
314+
EXPECT_FALSE(auth_options.has<EndpointOption>());
315+
EXPECT_THAT(auth_options.get<LoggingComponentsOption>(),
316+
ElementsAre("logging_component"));
317+
}
318+
302319
} // namespace
303320
} // namespace internal
304321
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

0 commit comments

Comments
 (0)