Skip to content
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

Refactor. Replace S3Cp with S3Client (S3Cp is too limiting of a name) #11813

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
98 changes: 98 additions & 0 deletions fdbclient/BlobTLSConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* BlobTLSConfig.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* 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 <algorithm>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <fcntl.h>
#include <filesystem>

#ifdef _WIN32
#include <io.h>
#endif

#include "fdbclient/BlobTLSConfig.h"
#include "fdbclient/BackupContainerFileSystem.h"
#include "flow/TLSConfig.actor.h"

void BlobTLSConfig::setupBlobCredentials() {
// Add blob credentials files from the environment to the list collected from the command line.
const char* blobCredsFromENV = getenv("FDB_BLOB_CREDENTIALS");
if (blobCredsFromENV != nullptr) {
StringRef t((uint8_t*)blobCredsFromENV, strlen(blobCredsFromENV));
do {
StringRef file = t.eat(":");
if (file.size() != 0)
blobCredentials.push_back(file.toString());
} while (t.size() != 0);
}

// Update the global blob credential files list
std::vector<std::string>* pFiles = (std::vector<std::string>*)g_network->global(INetwork::enBlobCredentialFiles);
if (pFiles != nullptr) {
for (auto& f : blobCredentials) {
pFiles->push_back(f);
}
}
}

bool BlobTLSConfig::setupTLS() {
if (tlsCertPath.size()) {
try {
setNetworkOption(FDBNetworkOptions::TLS_CERT_PATH, tlsCertPath);
} catch (Error& e) {
std::cerr << "ERROR: cannot set TLS certificate path to " << tlsCertPath << " (" << e.what() << ")\n";
return false;
}
}

if (tlsCAPath.size()) {
try {
setNetworkOption(FDBNetworkOptions::TLS_CA_PATH, tlsCAPath);
} catch (Error& e) {
std::cerr << "ERROR: cannot set TLS CA path to " << tlsCAPath << " (" << e.what() << ")\n";
return false;
}
}
if (tlsKeyPath.size()) {
try {
if (tlsPassword.size())
setNetworkOption(FDBNetworkOptions::TLS_PASSWORD, tlsPassword);

setNetworkOption(FDBNetworkOptions::TLS_KEY_PATH, tlsKeyPath);
} catch (Error& e) {
std::cerr << "ERROR: cannot set TLS key path to " << tlsKeyPath << " (" << e.what() << ")\n";
return false;
}
}
if (tlsVerifyPeers.size()) {
try {
setNetworkOption(FDBNetworkOptions::TLS_VERIFY_PEERS, tlsVerifyPeers);
} catch (Error& e) {
std::cerr << "ERROR: cannot set TLS peer verification to " << tlsVerifyPeers << " (" << e.what() << ")\n";
return false;
}
}
return true;
}
8 changes: 4 additions & 4 deletions fdbclient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/versions.h.cmake ${CMAKE_CURRENT_BINA
add_dependencies(fdbclient fdboptions)
target_link_libraries(fdbclient PUBLIC fdbrpc msgpack PRIVATE rapidxml)

add_flow_target(EXECUTABLE NAME s3cp SRCS S3Cp.actor.cpp)
target_include_directories(s3cp PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_BINARY_DIR}/include")
target_link_libraries(s3cp PRIVATE fdbclient)
add_flow_target(EXECUTABLE NAME s3client SRCS S3Client_cli.actor.cpp)
target_include_directories(s3client PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_BINARY_DIR}/include")
target_link_libraries(s3client PRIVATE fdbclient)

# Setup the Swift sources in FDBClient.
if(WITH_SWIFT)
Expand Down Expand Up @@ -144,5 +144,5 @@ endif()

if (NOT WIN32 AND NOT OPEN_FOR_IDE)
enable_testing()
add_test(NAME s3cp_test COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/s3cp_test.sh ${CMAKE_BINARY_DIR})
add_test(NAME s3client_test COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/s3client_test.sh ${CMAKE_BINARY_DIR})
endif()
166 changes: 166 additions & 0 deletions fdbclient/S3Client.actor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* S3Client.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* 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 <algorithm>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <fcntl.h>
#include <filesystem>

#ifdef _WIN32
#include <io.h>
#endif

#include "fdbclient/BackupContainerFileSystem.h"
#include "fdbclient/BlobTLSConfig.h"
#include "fdbclient/FDBTypes.h"
#include "fdbclient/IKnobCollection.h"
#include "fdbclient/Knobs.h"
#include "fdbclient/versions.h"
#include "fdbclient/S3Client.actor.h"
#include "flow/Platform.h"
#include "flow/FastRef.h"
#include "flow/Trace.h"
#include "flow/flow.h"
#include "flow/TLSConfig.actor.h"

#include "flow/actorcompiler.h" // has to be last include

// Get the endpoint for the given s3url.
// Populates parameters and resource with parse of s3url.
static Reference<S3BlobStoreEndpoint> getEndpoint(std::string s3url,
std::string& resource,
S3BlobStoreEndpoint::ParametersT& parameters) {
std::string error;
Reference<S3BlobStoreEndpoint> endpoint =
S3BlobStoreEndpoint::fromString(s3url, {}, &resource, &error, &parameters);
if (resource.empty()) {
TraceEvent(SevError, "S3ClientEmptyResource").detail("s3url", s3url);
throw backup_invalid_url();
}
for (auto c : resource) {
if (!isalnum(c) && c != '_' && c != '-' && c != '.' && c != '/') {
TraceEvent(SevError, "S3ClientIllegalCharacter").detail("s3url", s3url);
throw backup_invalid_url();
}
}
if (error.size()) {
TraceEvent(SevError, "S3ClientGetEndpointError").detail("s3url", s3url).detail("error", error);
throw backup_invalid_url();
}
return endpoint;
}

// Copy filepath to bucket at resource in s3.
ACTOR static Future<Void> copyUpFile(Reference<S3BlobStoreEndpoint> endpoint,
std::string bucket,
std::string resource,
std::string filepath) {
// Reading an SST file fully into memory is pretty obnoxious. They are about 16MB on
// average. Streaming would require changing this s3blobstore interface.
// Make 32MB the max size for now even though its arbitrary and way to big.
state std::string content = readFileBytes(filepath, 1024 * 1024 * 32);
wait(endpoint->writeEntireFile(bucket, resource, content));
TraceEvent("S3ClientUpload")
.detail("filepath", filepath)
.detail("bucket", bucket)
.detail("resource", resource)
.detail("size", content.size());
return Void();
}

ACTOR Future<Void> copyUpFile(std::string filepath, std::string s3url) {
std::string resource;
S3BlobStoreEndpoint::ParametersT parameters;
Reference<S3BlobStoreEndpoint> endpoint = getEndpoint(s3url, resource, parameters);
wait(copyUpFile(endpoint, parameters["bucket"], resource, filepath));
return Void();
}

ACTOR Future<Void> copyUpDirectory(std::string dirpath, std::string s3url) {
state std::string resource;
S3BlobStoreEndpoint::ParametersT parameters;
state Reference<S3BlobStoreEndpoint> endpoint = getEndpoint(s3url, resource, parameters);
state std::string bucket = parameters["bucket"];
state std::vector<std::string> files;
platform::findFilesRecursively(dirpath, files);
TraceEvent("S3ClientUploadDirStart")
.detail("filecount", files.size())
.detail("bucket", bucket)
.detail("resource", resource);
for (const auto& file : files) {
std::string filepath = file;
std::string s3path = resource + "/" + file.substr(dirpath.size() + 1);
wait(copyUpFile(endpoint, bucket, s3path, filepath));
}
TraceEvent("S3ClientUploadDirEnd").detail("bucket", bucket).detail("resource", resource);
return Void();
}

// Copy down file from s3 to filepath.
ACTOR static Future<Void> copyDownFile(Reference<S3BlobStoreEndpoint> endpoint,
std::string bucket,
std::string resource,
std::string filepath) {
std::string content = wait(endpoint->readEntireFile(bucket, resource));
auto parent = std::filesystem::path(filepath).parent_path();
if (parent != "" && !std::filesystem::exists(parent)) {
std::filesystem::create_directories(parent);
}
writeFile(filepath, content);
TraceEvent("S3ClientDownload")
.detail("filepath", filepath)
.detail("bucket", bucket)
.detail("resource", resource)
.detail("size", content.size());
return Void();
}

ACTOR Future<Void> copyDownFile(std::string s3url, std::string filepath) {
std::string resource;
S3BlobStoreEndpoint::ParametersT parameters;
Reference<S3BlobStoreEndpoint> endpoint = getEndpoint(s3url, resource, parameters);
wait(copyDownFile(endpoint, parameters["bucket"], resource, filepath));
return Void();
}

ACTOR Future<Void> copyDownDirectory(std::string s3url, std::string dirpath) {
state std::string resource;
S3BlobStoreEndpoint::ParametersT parameters;
state Reference<S3BlobStoreEndpoint> endpoint = getEndpoint(s3url, resource, parameters);
state std::string bucket = parameters["bucket"];
S3BlobStoreEndpoint::ListResult items = wait(endpoint->listObjects(parameters["bucket"], resource));
state std::vector<S3BlobStoreEndpoint::ObjectInfo> objects = items.objects;
TraceEvent("S3ClientDownloadDirStart")
.detail("filecount", objects.size())
.detail("bucket", bucket)
.detail("resource", resource);
for (const auto& object : objects) {
std::string filepath = dirpath + "/" + object.name.substr(resource.size());
std::string s3path = object.name;
wait(copyDownFile(endpoint, bucket, s3path, filepath));
}
TraceEvent("S3ClientDownloadDirEnd").detail("bucket", bucket).detail("resource", resource);
return Void();
}
Loading