Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "google/cloud/testing_util/status_matchers.h"
#include <gmock/gmock.h>
#include <iterator>
#include <string_view>

namespace google {
namespace cloud {
Expand All @@ -29,6 +30,9 @@ namespace {
using ::google::cloud::storage::testing::MockGenericStub;
using ::google::cloud::storage::testing::TempFile;
using ::google::cloud::testing_util::StatusIs;
using ::testing::AllOf;
using ::testing::HasSubstr;
using ::testing::Not;
using ::testing::Return;

class ConnectionImplFileUploadTest : public ::testing::Test {
Expand Down Expand Up @@ -144,6 +148,71 @@ TEST_F(ConnectionImplFileUploadTest, UploadFileResumableWithLimit) {
EXPECT_EQ("01234", actual);
}

/// @test Verify `EncryptionKey` option formatting in simple and resumable
/// file-upload error statuses.
TEST_F(ConnectionImplFileUploadTest, UploadFileErrorsFormatEncryptionKey) {
// /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64
constexpr std::string_view kKeyBinary = "0123456789-ABCDEFGHIJ-0123456789";
constexpr std::string_view kKeyBase64 =
"MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk=";
EncryptionKey const key =
EncryptionKey::FromBinaryKey(std::string{kKeyBinary});

std::shared_ptr<StorageConnectionImpl> connection = MakeConnection();
std::string const contents = "0123456789";
TempFile temp(contents);

InsertObjectMediaRequest simple_missing("test-bucket", "test-object", "");
simple_missing.set_option(key);
StatusOr<std::unique_ptr<std::string>> const missing_result =
connection->UploadFileSimple("/no/such/file/exists.txt", 123,
simple_missing);
EXPECT_THAT(missing_result,
StatusIs(StatusCode::kNotFound,
AllOf(HasSubstr("x-goog-encryption-key: [censored]"),
Not(HasSubstr(kKeyBase64)))));

InsertObjectMediaRequest simple_offset("test-bucket", "test-object", "");
simple_offset.set_multiple_options(key,
UploadFromOffset(contents.size() + 1));
StatusOr<std::unique_ptr<std::string>> const offset_result =
connection->UploadFileSimple(temp.name(), contents.size(), simple_offset);
EXPECT_THAT(offset_result,
StatusIs(StatusCode::kInvalidArgument,
AllOf(HasSubstr("x-goog-encryption-key: [censored]"),
Not(HasSubstr(kKeyBase64)))));

InsertObjectMediaRequest simple_short_read("test-bucket", "test-object", "");
simple_short_read.set_option(key);
StatusOr<std::unique_ptr<std::string>> const short_read_result =
connection->UploadFileSimple(temp.name(), contents.size() + 1,
simple_short_read);
EXPECT_THAT(short_read_result,
StatusIs(StatusCode::kInternal,
AllOf(HasSubstr("x-goog-encryption-key: [censored]"),
Not(HasSubstr(kKeyBase64)))));

ResumableUploadRequest resumable_missing("test-bucket", "test-object");
resumable_missing.set_option(key);
StatusOr<std::unique_ptr<std::istream>> const resumable_missing_result =
connection->UploadFileResumable("/no/such/file/exists.txt",
resumable_missing);
EXPECT_THAT(resumable_missing_result,
StatusIs(StatusCode::kNotFound,
AllOf(HasSubstr("x-goog-encryption-key: [censored]"),
Not(HasSubstr(kKeyBase64)))));

ResumableUploadRequest resumable_offset("test-bucket", "test-object");
resumable_offset.set_multiple_options(key,
UploadFromOffset(contents.size() + 1));
StatusOr<std::unique_ptr<std::istream>> const resumable_offset_result =
connection->UploadFileResumable(temp.name(), resumable_offset);
EXPECT_THAT(resumable_offset_result,
StatusIs(StatusCode::kInvalidArgument,
AllOf(HasSubstr("x-goog-encryption-key: [censored]"),
Not(HasSubstr(kKeyBase64)))));
}

} // namespace
} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
29 changes: 29 additions & 0 deletions google/cloud/storage/internal/connection_impl_object_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "google/cloud/storage/internal/connection_impl.h"
#include "google/cloud/storage/internal/object_requests.h"
#include "google/cloud/storage/object_read_stream.h"
#include "google/cloud/storage/options.h"
#include "google/cloud/storage/testing/canonical_errors.h"
#include "google/cloud/storage/testing/mock_generic_stub.h"
Expand All @@ -24,6 +25,7 @@
#include <gmock/gmock.h>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Expand All @@ -45,12 +47,14 @@ using ::google::cloud::storage::testing::canonical_errors::PermanentError;
using ::google::cloud::storage::testing::canonical_errors::TransientError;
using ::google::cloud::testing_util::IsOk;
using ::google::cloud::testing_util::ScopedLog;
using ::google::cloud::testing_util::StatusIs;
using ::testing::AllOf;
using ::testing::Contains;
using ::testing::Each;
using ::testing::HasSubstr;
using ::testing::IsEmpty;
using ::testing::Not;
using ::testing::Return;
using ::testing::SizeIs;

// Shaped like a real REST resumable session URL: the `upload_id` is a bearer
Expand Down Expand Up @@ -560,6 +564,31 @@ TEST(StorageConnectionImpl, RestoreObjectPermanentFailure) {
EXPECT_THAT(permanent.captured_authority_options(), RetryLoopUsesOptions());
}

/// @test Verify `EncryptionKey` option formatting in `DownloadStreamToFile`
/// error statuses.
TEST(StorageConnectionImpl, DownloadStreamToFileFormatsEncryptionKey) {
// /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64
constexpr std::string_view kKeyBinary = "0123456789-ABCDEFGHIJ-0123456789";
constexpr std::string_view kKeyBase64 =
"MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk=";

auto mock = std::make_unique<MockGenericStub>();
EXPECT_CALL(*mock, options).WillRepeatedly(Return(Options{}));
std::shared_ptr<StorageConnectionImpl> client =
StorageConnectionImpl::Create(std::move(mock), RetryTestOptions());
google::cloud::internal::OptionsSpan const span(client->options());

ReadObjectRangeRequest request("test-bucket", "test-object");
request.set_option(EncryptionKey::FromBinaryKey(std::string{kKeyBinary}));
ObjectReadStream stream;
Status const status = client->DownloadStreamToFile(
std::move(stream), "/no/such/directory/exists.bin", request);
EXPECT_THAT(status,
StatusIs(StatusCode::kInvalidArgument,
AllOf(HasSubstr("x-goog-encryption-key: [censored]"),
Not(HasSubstr(kKeyBase64)))));
}

} // namespace
} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
97 changes: 97 additions & 0 deletions google/cloud/storage/internal/logging_stub_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
// limitations under the License.

#include "google/cloud/storage/internal/logging_stub.h"
#include "google/cloud/storage/client.h"
#include "google/cloud/storage/internal/bucket_metadata_parser.h"
#include "google/cloud/storage/internal/generic_stub_factory.h"
#include "google/cloud/storage/internal/object_metadata_parser.h"
#include "google/cloud/storage/testing/canonical_errors.h"
#include "google/cloud/storage/testing/mock_generic_stub.h"
#include "google/cloud/common_options.h"
#include "google/cloud/log.h"
#include "google/cloud/testing_util/scoped_environment.h"
#include <gmock/gmock.h>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Expand All @@ -33,7 +38,9 @@ namespace {

using ::google::cloud::storage::testing::MockGenericStub;
using ::google::cloud::storage::testing::canonical_errors::TransientError;
using ::google::cloud::testing_util::ScopedEnvironment;
using ::testing::HasSubstr;
using ::testing::Not;
using ::testing::Return;

class MockLogBackend : public google::cloud::LogBackend {
Expand Down Expand Up @@ -285,6 +292,96 @@ TEST_F(LoggingStubTest, ReadObjectSuccessLogsOnlyTheRequest) {
ReadObjectRangeRequest("my-bucket", "my-object"));
}

class LoggingStubComponentTest
: public LoggingStubTest,
public ::testing::WithParamInterface<std::string_view> {};

/// @test Verify `EncryptionKey` and `SourceEncryptionKey` option formatting
/// across `LoggingStub` components.
TEST_P(LoggingStubComponentTest, FormatsEncryptionKeyOptions) {
// /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64
constexpr std::string_view kDestKeyBinary =
"0123456789-ABCDEFGHIJ-0123456789";
constexpr std::string_view kDestKeyBase64 =
"MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk=";
// /bin/echo -n "9876543210-JIHGFEDCBA-9876543210" | openssl base64
constexpr std::string_view kSourceKeyBinary =
"9876543210-JIHGFEDCBA-9876543210";
constexpr std::string_view kSourceKeyBase64 =
"OTg3NjU0MzIxMC1KSUhHRkVEQ0JBLTk4NzY1NDMyMTA=";

auto mock = std::make_unique<MockGenericStub>();
EXPECT_CALL(*mock, RewriteObject)
.WillOnce(Return(StatusOr<RewriteObjectResponse>(TransientError())));

EXPECT_CALL(*log_backend_, ProcessWithOwnership)
.WillOnce([kDestKeyBase64, kSourceKeyBase64](LogRecord const& lr) {
EXPECT_THAT(lr.message, HasSubstr("RewriteObjectRequest={"));
EXPECT_THAT(lr.message, HasSubstr("x-goog-encryption-key: [censored]"));
EXPECT_THAT(lr.message,
HasSubstr("x-goog-copy-source-encryption-key: [censored]"));
EXPECT_THAT(lr.message, Not(HasSubstr(kDestKeyBase64)));
EXPECT_THAT(lr.message, Not(HasSubstr(kSourceKeyBase64)));
})
.WillOnce([](LogRecord const& lr) {
EXPECT_THAT(lr.message, HasSubstr("status={"));
});

std::unique_ptr<storage_internal::GenericStub> client =
storage_internal::DecorateStub(
Options{}.set<LoggingComponentsOption>({std::string{GetParam()}}),
std::move(mock));
rest_internal::RestContext context;
RewriteObjectRequest request("src-bucket", "src-object", "dst-bucket",
"dst-object", "");
request.set_multiple_options(
EncryptionKey::FromBinaryKey(std::string{kDestKeyBinary}),
SourceEncryptionKey::FromBinaryKey(std::string{kSourceKeyBinary}));
(void)client->RewriteObject(context, Options{}, request);
}

INSTANTIATE_TEST_SUITE_P(LoggingComponents, LoggingStubComponentTest,
::testing::Values("raw-client", "rpc"));

/// @test Verify `EncryptionKey` option formatting when logging is enabled via
/// `CLOUD_STORAGE_ENABLE_TRACING`.
TEST_F(LoggingStubTest, EnvTracingFormatsEncryptionKey) {
// /bin/echo -n "0123456789-ABCDEFGHIJ-0123456789" | openssl base64
constexpr std::string_view kKeyBinary = "0123456789-ABCDEFGHIJ-0123456789";
constexpr std::string_view kKeyBase64 =
"MDEyMzQ1Njc4OS1BQkNERUZHSElKLTAxMjM0NTY3ODk=";

ScopedEnvironment env("CLOUD_STORAGE_ENABLE_TRACING", "raw-client");
// `DefaultOptions` logs an INFO message when applying the env var.
EXPECT_CALL(*log_backend_, ProcessWithOwnership)
.WillOnce([](LogRecord const& lr) {
EXPECT_THAT(lr.message, HasSubstr("Enabling logging for raw-client"));
});
Options const opts = DefaultOptions(Options{});
::testing::Mock::VerifyAndClearExpectations(log_backend_.get());

auto mock = std::make_unique<MockGenericStub>();
EXPECT_CALL(*mock, ReadObject)
.WillOnce(Return(
StatusOr<std::unique_ptr<ObjectReadSource>>(TransientError())));
EXPECT_CALL(*log_backend_, ProcessWithOwnership)
.WillOnce([kKeyBase64](LogRecord const& lr) {
EXPECT_THAT(lr.message, HasSubstr("ReadObjectRangeRequest={"));
EXPECT_THAT(lr.message, HasSubstr("x-goog-encryption-key: [censored]"));
EXPECT_THAT(lr.message, Not(HasSubstr(kKeyBase64)));
})
.WillOnce([](LogRecord const& lr) {
EXPECT_THAT(lr.message, HasSubstr("status={"));
});

std::unique_ptr<storage_internal::GenericStub> client =
storage_internal::DecorateStub(opts, std::move(mock));
rest_internal::RestContext context;
ReadObjectRangeRequest request("my-bucket", "my-object");
request.set_option(EncryptionKey::FromBinaryKey(std::string{kKeyBinary}));
(void)client->ReadObject(context, opts, request);
}

} // namespace
} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Expand Down
20 changes: 12 additions & 8 deletions google/cloud/storage/internal/object_requests_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ TEST(ObjectRequestsTest, CopyAllOptions) {
EXPECT_THAT(actual, HasSubstr("destinationKmsKeyName=test-only-kms-key"));
EXPECT_THAT(actual, HasSubstr("destinationPredefinedAcl=private"));
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-algorithm: AES256"));
// /bin/echo -n ABCD1234 | openssl base64 -e
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: MTIzNEFCQ0Q="));
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: [censored]"));
// /bin/echo -n 1234ABCD | openssl base64
EXPECT_THAT(actual, Not(HasSubstr("MTIzNEFCQ0Q=")));
// /bin/echo -n 1234ABCD | sha256sum | awk '{printf("%s", $1);}' |
// xxd -r -p | openssl base64
EXPECT_THAT(actual,
Expand All @@ -276,9 +277,10 @@ TEST(ObjectRequestsTest, CopyAllOptions) {
EXPECT_THAT(actual, HasSubstr("sourceGeneration=7"));
EXPECT_THAT(actual,
HasSubstr("x-goog-copy-source-encryption-algorithm: AES256"));
// /bin/echo -n ABCD1234 | openssl base64 -e
EXPECT_THAT(actual,
HasSubstr("x-goog-copy-source-encryption-key: QUJDRDEyMzQ="));
HasSubstr("x-goog-copy-source-encryption-key: [censored]"));
// /bin/echo -n ABCD1234 | openssl base64
EXPECT_THAT(actual, Not(HasSubstr("QUJDRDEyMzQ=")));
// /bin/echo -n ABCD1234 | sha256sum | awk '{printf("%s", $1);}' |
// xxd -r -p | openssl base64
EXPECT_THAT(actual,
Expand Down Expand Up @@ -414,8 +416,9 @@ TEST(ObjectRequestsTest, Update) {
EXPECT_THAT(actual, HasSubstr("content_type=application/json"));
EXPECT_THAT(actual, HasSubstr("generation=7"));
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-algorithm: AES256"));
// /bin/echo -n 1234ABCD | openssl base64 -e
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: MTIzNEFCQ0Q="));
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: [censored]"));
// /bin/echo -n 1234ABCD | openssl base64
EXPECT_THAT(actual, Not(HasSubstr("MTIzNEFCQ0Q=")));
// /bin/echo -n 1234ABCD | sha256sum | awk '{printf("%s", $1);}' |
// xxd -r -p | openssl base64
EXPECT_THAT(actual,
Expand Down Expand Up @@ -1076,8 +1079,9 @@ TEST(PatchObjectRequestTest, Builder) {
EXPECT_THAT(actual, HasSubstr("contentType"));
EXPECT_THAT(actual, HasSubstr("application/json"));
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-algorithm: AES256"));
// /bin/echo -n ABCD1234 | sha256sum #openssl base64 -e
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: QUJDRDEyMzQ="));
EXPECT_THAT(actual, HasSubstr("x-goog-encryption-key: [censored]"));
// /bin/echo -n ABCD1234 | openssl base64
EXPECT_THAT(actual, Not(HasSubstr("QUJDRDEyMzQ=")));
// /bin/echo -n ABCD1234 | sha256sum | awk '{printf("%s", $1);}' |
// xxd -r -p | openssl base64
EXPECT_THAT(actual,
Expand Down
Loading
Loading