Skip to content

Commit

Permalink
Add the ability to specify KMS endpoint/credentials in the environment.
Browse files Browse the repository at this point in the history
Required to override them for signtool integration testing with FakeKMS.

Change-Id: I2c44e801d809996a85ca195ce3f1430a59ecad51
  • Loading branch information
bdhess committed May 12, 2023
1 parent 8b633b0 commit 850f451
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions kmscng/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ cc_test(
deps = [
":provider",
":version",
"//common/test:test_platform",
"//common/test:test_status_macros",
"//kmscng:cng_headers",
"//kmscng/test:matchers",
"@com_google_absl//absl/cleanup",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
3 changes: 3 additions & 0 deletions kmscng/cng_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ constexpr std::wstring_view kEndpointAddressProperty = L"KMSEndpointAddress";
constexpr std::wstring_view kChannelCredentialsProperty =
L"KMSChannelCredentials";

constexpr char kEndpointAddressEnvVariable[] = "KMS_ENDPOINT_ADDRESS";
constexpr char kChannelCredentialsEnvVariable[] = "KMS_CHANNEL_CREDENTIALS";

} // namespace cloud_kms::kmscng

#endif // KMSCNG_CNG_HEADERS_H_
13 changes: 11 additions & 2 deletions kmscng/provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ absl::flat_hash_set<std::wstring> mutable_properties = {
};

absl::flat_hash_map<std::wstring, std::string> BuildInfo() {
const char* env_endpoint_address = std::getenv(kEndpointAddressEnvVariable);
std::string endpoint_address = env_endpoint_address
? env_endpoint_address
: "cloudkms.googleapis.com:443";
const char* env_channel_credentials =
std::getenv(kChannelCredentialsEnvVariable);
std::string channel_credentials =
env_channel_credentials ? env_channel_credentials : "default";

return {
{NCRYPT_IMPL_TYPE_PROPERTY, Uint32ToBytes(NCRYPT_IMPL_HARDWARE_FLAG)},
{NCRYPT_VERSION_PROPERTY, Uint32ToBytes(kLibraryVersionHex)},
{std::wstring(kEndpointAddressProperty), "cloudkms.googleapis.com:443"},
{std::wstring(kChannelCredentialsProperty), "default"},
{std::wstring(kEndpointAddressProperty), endpoint_address},
{std::wstring(kChannelCredentialsProperty), channel_credentials},
};
}

Expand Down
22 changes: 22 additions & 0 deletions kmscng/provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "kmscng/provider.h"

#include "absl/cleanup/cleanup.h"
#include "common/test/test_platform.h"
#include "common/test/test_status_macros.h"
#include "gmock/gmock.h"
#include "kmscng/cng_headers.h"
Expand Down Expand Up @@ -59,6 +61,26 @@ TEST(ProviderTest, GetProviderPropertyChannelCredentialsSuccess) {
IsOkAndHolds("default"));
}

TEST(ProviderTest, SetEndpointAddressInEnvVariable) {
std::string address = "invalid.address";
SetEnvVariable(kEndpointAddressEnvVariable, address);
absl::Cleanup c = [] { ClearEnvVariable(kEndpointAddressEnvVariable); };

Provider provider;
EXPECT_THAT(provider.GetProperty(kEndpointAddressProperty),
IsOkAndHolds(address));
}

TEST(ProviderTest, SetChannelCredentialsInEnvVariable) {
std::string credentials = "unknown";
SetEnvVariable(kChannelCredentialsEnvVariable, credentials);
absl::Cleanup c = [] { ClearEnvVariable(kChannelCredentialsEnvVariable); };

Provider provider;
EXPECT_THAT(provider.GetProperty(kChannelCredentialsProperty),
IsOkAndHolds(credentials));
}

TEST(ProviderTest, SetProviderPropertyUnsupportedProperty) {
Provider provider;

Expand Down

0 comments on commit 850f451

Please sign in to comment.