Skip to content

Initial commit of s3 modular file system plugin (credit @vnvo2409) #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2020
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
1 change: 1 addition & 0 deletions tensorflow_io/core/plugins/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cc_library(
deps = [
"//tensorflow_io/core/plugins/az",
"//tensorflow_io/core/plugins/http",
"//tensorflow_io/core/plugins/s3",
],
alwayslink = 1,
)
3 changes: 2 additions & 1 deletion tensorflow_io/core/plugins/file_system_plugins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ limitations under the License.
void TF_InitPlugin(TF_FilesystemPluginInfo* info) {
info->plugin_memory_allocate = tensorflow::io::plugin_memory_allocate;
info->plugin_memory_free = tensorflow::io::plugin_memory_free;
info->num_schemes = 2;
info->num_schemes = 3;
info->ops = static_cast<TF_FilesystemPluginOps*>(
tensorflow::io::plugin_memory_allocate(info->num_schemes *
sizeof(info->ops[0])));
tensorflow::io::az::ProvideFilesystemSupportFor(&info->ops[0], "az");
tensorflow::io::http::ProvideFilesystemSupportFor(&info->ops[1], "http");
tensorflow::io::s3::ProvideFilesystemSupportFor(&info->ops[2], "s3e");
}
6 changes: 6 additions & 0 deletions tensorflow_io/core/plugins/file_system_plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ void ProvideFilesystemSupportFor(TF_FilesystemPluginOps* ops, const char* uri);

} // namespace http

namespace s3 {

void ProvideFilesystemSupportFor(TF_FilesystemPluginOps* ops, const char* uri);

} // namespace s3

} // namespace io
} // namespace tensorflow

Expand Down
29 changes: 29 additions & 0 deletions tensorflow_io/core/plugins/s3/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
licenses(["notice"]) # Apache 2.0

package(default_visibility = ["//visibility:public"])

load(
"//:tools/build/tensorflow_io.bzl",
"tf_io_copts",
)

cc_library(
name = "s3",
srcs = [
"aws_crypto.cc",
"aws_crypto.h",
"aws_logging.cc",
"aws_logging.h",
"s3_filesystem.cc",
"s3_filesystem.h",
],
copts = tf_io_copts(),
linkstatic = True,
deps = [
"//tensorflow_io/core/plugins:plugins_header",
"@aws-sdk-cpp",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
],
alwayslink = 1,
)
142 changes: 142 additions & 0 deletions tensorflow_io/core/plugins/s3/aws_crypto.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow_io/core/plugins/s3/aws_crypto.h"

#include <aws/core/utils/crypto/HashResult.h>
#include <aws/s3/S3Client.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include <openssl/sha.h>

namespace tensorflow {
namespace io {
namespace s3 {
namespace tf_s3_filesystem {

namespace {
class AWSSha256HMACOpenSSLImpl : public Aws::Utils::Crypto::HMAC {
public:
AWSSha256HMACOpenSSLImpl() {}

virtual ~AWSSha256HMACOpenSSLImpl() = default;

Aws::Utils::Crypto::HashResult Calculate(
const Aws::Utils::ByteBuffer& toSign,
const Aws::Utils::ByteBuffer& secret) override {
unsigned int length = SHA256_DIGEST_LENGTH;
Aws::Utils::ByteBuffer digest(length);
memset(digest.GetUnderlyingData(), 0, length);

HMAC_CTX ctx;
HMAC_CTX_init(&ctx);

HMAC_Init_ex(&ctx, secret.GetUnderlyingData(),
static_cast<int>(secret.GetLength()), EVP_sha256(), NULL);
HMAC_Update(&ctx, toSign.GetUnderlyingData(), toSign.GetLength());
HMAC_Final(&ctx, digest.GetUnderlyingData(), &length);
HMAC_CTX_cleanup(&ctx);

return Aws::Utils::Crypto::HashResult(std::move(digest));
}
};

class AWSSha256OpenSSLImpl : public Aws::Utils::Crypto::Hash {
public:
AWSSha256OpenSSLImpl() {}

virtual ~AWSSha256OpenSSLImpl() = default;

Aws::Utils::Crypto::HashResult Calculate(const Aws::String& str) override {
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str.data(), str.size());

Aws::Utils::ByteBuffer hash(SHA256_DIGEST_LENGTH);
SHA256_Final(hash.GetUnderlyingData(), &sha256);

return Aws::Utils::Crypto::HashResult(std::move(hash));
}

Aws::Utils::Crypto::HashResult Calculate(Aws::IStream& stream) override {
SHA256_CTX sha256;
SHA256_Init(&sha256);

auto currentPos = stream.tellg();
if (currentPos == std::streampos(std::streamoff(-1))) {
currentPos = 0;
stream.clear();
}

stream.seekg(0, stream.beg);

char streamBuffer
[Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE];
while (stream.good()) {
stream.read(streamBuffer,
Aws::Utils::Crypto::Hash::INTERNAL_HASH_STREAM_BUFFER_SIZE);
auto bytesRead = stream.gcount();

if (bytesRead > 0) {
SHA256_Update(&sha256, streamBuffer, static_cast<size_t>(bytesRead));
}
}

stream.clear();
stream.seekg(currentPos, stream.beg);

Aws::Utils::ByteBuffer hash(SHA256_DIGEST_LENGTH);
SHA256_Final(hash.GetUnderlyingData(), &sha256);

return Aws::Utils::Crypto::HashResult(std::move(hash));
}
};

class AWSSecureRandomBytesImpl : public Aws::Utils::Crypto::SecureRandomBytes {
public:
AWSSecureRandomBytesImpl() {}
virtual ~AWSSecureRandomBytesImpl() = default;
void GetBytes(unsigned char* buffer, size_t bufferSize) override {
assert(buffer);
int success = RAND_bytes(buffer, static_cast<int>(bufferSize));
if (success != 1) {
m_failure = true;
}
}

private:
bool m_failure;
};

} // namespace

std::shared_ptr<Aws::Utils::Crypto::Hash>
AWSSHA256Factory::CreateImplementation() const {
return Aws::MakeShared<AWSSha256OpenSSLImpl>(AWSCryptoAllocationTag);
}

std::shared_ptr<Aws::Utils::Crypto::HMAC>
AWSSHA256HmacFactory::CreateImplementation() const {
return Aws::MakeShared<AWSSha256HMACOpenSSLImpl>(AWSCryptoAllocationTag);
}

std::shared_ptr<Aws::Utils::Crypto::SecureRandomBytes>
AWSSecureRandomFactory::CreateImplementation() const {
return Aws::MakeShared<AWSSecureRandomBytesImpl>(AWSCryptoAllocationTag);
}

} // namespace tf_s3_filesystem
} // namespace s3
} // namespace io
} // namespace tensorflow
53 changes: 53 additions & 0 deletions tensorflow_io/core/plugins/s3/aws_crypto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_S3_AWS_CRYPTO_H_
#define TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_S3_AWS_CRYPTO_H_

#include <aws/core/Aws.h>
#include <aws/core/utils/crypto/Factories.h>
#include <aws/core/utils/crypto/HMAC.h>
#include <aws/core/utils/crypto/Hash.h>
#include <aws/core/utils/crypto/SecureRandom.h>

namespace tensorflow {
namespace io {
namespace s3 {
namespace tf_s3_filesystem {
constexpr char AWSCryptoAllocationTag[] = "AWSCryptoAllocation";

class AWSSHA256Factory : public Aws::Utils::Crypto::HashFactory {
public:
std::shared_ptr<Aws::Utils::Crypto::Hash> CreateImplementation()
const override;
};

class AWSSHA256HmacFactory : public Aws::Utils::Crypto::HMACFactory {
public:
std::shared_ptr<Aws::Utils::Crypto::HMAC> CreateImplementation()
const override;
};

class AWSSecureRandomFactory : public Aws::Utils::Crypto::SecureRandomFactory {
public:
std::shared_ptr<Aws::Utils::Crypto::SecureRandomBytes> CreateImplementation()
const override;
};

} // namespace tf_s3_filesystem
} // namespace s3
} // namespace io
} // namespace tensorflow

#endif // TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_S3_AWS_CRYPTO_H_
Loading