Skip to content

Commit

Permalink
Support credentials.
Browse files Browse the repository at this point in the history
Fixes: milvus-io#183

Signed-off-by: Ji Bin <[email protected]>
  • Loading branch information
matrixji committed Sep 17, 2022
1 parent 0636640 commit d71c2d5
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 6 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cmake_minimum_required(VERSION 3.12)
project(milvus_sdk LANGUAGES CXX C)

set(CMAKE_VERBOSE_MAKEFILE OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

Expand Down
2 changes: 2 additions & 0 deletions cmake/FindClangTools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ endif ()

find_program(CLANG_TIDY_BIN
NAMES
clang-tidy-14
clang-tidy-13
clang-tidy-10
clang-tidy
Expand Down Expand Up @@ -94,6 +95,7 @@ if (CLANG_FORMAT_VERSION)
else()
find_program(CLANG_FORMAT_BIN
NAMES
clang-format-14
clang-format-13
clang-format-10
clang-format
Expand Down
17 changes: 14 additions & 3 deletions scripts/install_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ install_deps_for_ubuntu_common() {
${SUDO} apt-get -y install cmake
fi

# install new clang-tidy clang-format
llvm_version=14
if [ "${dist}" = "bionic" ] ; then
llvm_version=13
fi

# install stable clang-tidy clang-format
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | ${SUDO} apt-key add -
${SUDO} apt-get -y install software-properties-common
${SUDO} add-apt-repository "deb http://apt.llvm.org/${dist}/ llvm-toolchain-${dist}-13 main"
${SUDO} add-apt-repository -y "deb http://apt.llvm.org/${dist}/ llvm-toolchain-${dist}-${llvm_version} main"
${SUDO} apt-get update
${SUDO} apt-get install -y clang-format-13 clang-tidy-13
${SUDO} apt-get install -y clang-format-${llvm_version} clang-tidy-${llvm_version}
}

install_deps_for_ubuntu_1804() {
Expand All @@ -59,6 +64,10 @@ install_deps_for_ubuntu_2004() {
install_deps_for_ubuntu_common focal
}

install_deps_for_ubuntu_2204() {
install_deps_for_ubuntu_common jammy
}

install_deps_for_fedora_common() {
check_sudo
${SUDO} dnf -y install gcc gcc-c++ python2 gpg wget ccache make openssl-devel which lcov git rpm-build
Expand Down Expand Up @@ -102,6 +111,8 @@ if uname | grep -wq Linux ; then
install_deps_for_ubuntu_1804
elif grep -q 'Ubuntu 20.04' /etc/issue ; then
install_deps_for_ubuntu_2004
elif grep -q 'Ubuntu 22.04' /etc/issue ; then
install_deps_for_ubuntu_2204
fi
elif [ -x "$(command -v yum)" ] ; then
# for os support yum
Expand Down
68 changes: 68 additions & 0 deletions src/impl/MilvusClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,74 @@ MilvusClientImpl::GetCompactionPlans(int64_t compaction_id, CompactionPlans& pla
pre, &MilvusConnection::GetCompactionPlans, post);
}

Status
MilvusClientImpl::CreateCredential(const std::string& username, const std::string& password) {
auto pre = [&username, &password]() {
proto::milvus::CreateCredentialRequest rpc_request;
rpc_request.set_username(username);
rpc_request.set_password(milvus::Base64Encode(password));
// TODO: seconds or milliseconds?
auto timestamp =
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
.count();
rpc_request.set_created_utc_timestamps(timestamp);
rpc_request.set_modified_utc_timestamps(timestamp);
return rpc_request;
};

return apiHandler<proto::milvus::CreateCredentialRequest, proto::common::Status>(
pre, &MilvusConnection::CreateCredential, nullptr);
}

Status
MilvusClientImpl::UpdateCredential(const std::string& username, const std::string& old_password,
const std::string& new_password) {
auto pre = [&username, &old_password, &new_password]() {
proto::milvus::UpdateCredentialRequest rpc_request;
rpc_request.set_username(username);
rpc_request.set_oldpassword(milvus::Base64Encode(old_password));
rpc_request.set_newpassword(milvus::Base64Encode(new_password));
// TODO: seconds or milliseconds?
rpc_request.set_modified_utc_timestamps(
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
.count());
return rpc_request;
};

return apiHandler<proto::milvus::UpdateCredentialRequest, proto::common::Status>(
pre, &MilvusConnection::UpdateCredential, nullptr);
}

Status
MilvusClientImpl::DeleteCredential(const std::string& username) {
auto pre = [&username]() {
proto::milvus::DeleteCredentialRequest rpc_request;
rpc_request.set_username(username);
return rpc_request;
};

return apiHandler<proto::milvus::DeleteCredentialRequest, proto::common::Status>(
pre, &MilvusConnection::DeleteCredential, nullptr);
}

Status
MilvusClientImpl::ListCredUsers(std::vector<std::string>& users) {
auto pre = []() {
proto::milvus::ListCredUsersRequest rpc_request;
return rpc_request;
};

auto post = [&users](const proto::milvus::ListCredUsersResponse& response) {
users.clear();
for (const auto& user : response.usernames()) {
users.emplace_back(user);
}
};

return apiHandler<proto::milvus::ListCredUsersRequest, proto::milvus::ListCredUsersResponse>(
pre, &MilvusConnection::ListCredUsers, post);
}

Status
MilvusClientImpl::waitForStatus(std::function<Status(Progress&)> query_function,
const ProgressMonitor& progress_monitor) {
Expand Down
13 changes: 13 additions & 0 deletions src/impl/MilvusClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ class MilvusClientImpl : public MilvusClient {
Status
GetCompactionPlans(int64_t compaction_id, CompactionPlans& plans) final;

Status
CreateCredential(const std::string& username, const std::string& password) final;

Status
UpdateCredential(const std::string& username, const std::string& old_password,
const std::string& new_password) final;

Status
DeleteCredential(const std::string& username) final;

Status
ListCredUsers(std::vector<std::string>& users) final;

private:
/**
* Internal wait for status query done.
Expand Down
30 changes: 27 additions & 3 deletions src/impl/MilvusConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,17 @@ MilvusConnection::GetPartitionStatistics(const proto::milvus::GetPartitionStatis

Status
MilvusConnection::CreateAlias(const proto::milvus::CreateAliasRequest& request, proto::common::Status& response) {
return grpcCall("HasParition", &Stub::CreateAlias, request, response);
return grpcCall("CreateAlias", &Stub::CreateAlias, request, response);
}

Status
MilvusConnection::DropAlias(const proto::milvus::DropAliasRequest& request, proto::common::Status& response) {
return grpcCall("HasParition", &Stub::DropAlias, request, response);
return grpcCall("DropAlias", &Stub::DropAlias, request, response);
}

Status
MilvusConnection::AlterAlias(const proto::milvus::AlterAliasRequest& request, proto::common::Status& response) {
return grpcCall("HasParition", &Stub::AlterAlias, request, response);
return grpcCall("AlterAlias", &Stub::AlterAlias, request, response);
}

Status
Expand Down Expand Up @@ -259,4 +259,28 @@ MilvusConnection::GetCompactionPlans(const proto::milvus::GetCompactionPlansRequ
return grpcCall("GetCompactionPlans", &Stub::GetCompactionStateWithPlans, request, response);
}

Status
MilvusConnection::CreateCredential(const proto::milvus::CreateCredentialRequest& request,
proto::common::Status& response) {
return grpcCall("CreateCredential", &Stub::CreateCredential, request, response);
}

Status
MilvusConnection::UpdateCredential(const proto::milvus::UpdateCredentialRequest& request,
proto::common::Status& response) {
return grpcCall("UpdateCredential", &Stub::UpdateCredential, request, response);
}

Status
MilvusConnection::DeleteCredential(const proto::milvus::DeleteCredentialRequest& request,
proto::common::Status& response) {
return grpcCall("DeleteCredential", &Stub::DeleteCredential, request, response);
}

Status
MilvusConnection::ListCredUsers(const proto::milvus::ListCredUsersRequest& request,
proto::milvus::ListCredUsersResponse& response) {
return grpcCall("ListCredUsers", &Stub::ListCredUsers, request, response);
}

} // namespace milvus
12 changes: 12 additions & 0 deletions src/impl/MilvusConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ class MilvusConnection {
GetCompactionPlans(const proto::milvus::GetCompactionPlansRequest& request,
proto::milvus::GetCompactionPlansResponse& response);

Status
CreateCredential(const proto::milvus::CreateCredentialRequest& request, proto::common::Status& response);

Status
UpdateCredential(const proto::milvus::UpdateCredentialRequest& request, proto::common::Status& response);

Status
DeleteCredential(const proto::milvus::DeleteCredentialRequest& request, proto::common::Status& response);

Status
ListCredUsers(const proto::milvus::ListCredUsersRequest& request, proto::milvus::ListCredUsersResponse& response);

private:
std::unique_ptr<proto::milvus::MilvusService::Stub> stub_;
std::shared_ptr<grpc::Channel> channel_;
Expand Down
40 changes: 40 additions & 0 deletions src/impl/TypeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,46 @@ IsVectorType(DataType type) {
return (DataType::BINARY_VECTOR == type || DataType::FLOAT_VECTOR == type);
}

std::string
Base64Encode(const std::string& val) {
const char* base64_chars = {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/"};

auto len = val.size();
auto len_encoded = (len + 2) / 3 * 4;
std::string ret;
ret.reserve(len_encoded);

size_t pos = 0;

while (pos < len) {
ret.push_back(base64_chars[(val[pos + 0] & 0xfc) >> 2]);

if (pos + 1 < len) {
ret.push_back(base64_chars[((val[pos + 0] & 0x03) << 4) + ((val[pos + 1] & 0xf0) >> 4)]);

if (pos + 2 < len) {
ret.push_back(base64_chars[((val[pos + 1] & 0x0f) << 2) + ((val[pos + 2] & 0xc0) >> 6)]);
ret.push_back(base64_chars[val[pos + 2] & 0x3f]);
} else {
ret.push_back(base64_chars[(val[pos + 1] & 0x0f) << 2]);
ret.push_back('=');
}
} else {
ret.push_back(base64_chars[(val[pos + 0] & 0x03) << 4]);
ret.push_back('=');
ret.push_back('=');
}

pos += 3;
}

return ret;
}

} // namespace milvus

namespace std {
Expand Down
3 changes: 3 additions & 0 deletions src/impl/TypeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ IndexStateCast(proto::common::IndexState state);
bool
IsVectorType(DataType type);

std::string
Base64Encode(const std::string& val);

} // namespace milvus

namespace std {
Expand Down
39 changes: 39 additions & 0 deletions src/include/milvus/MilvusClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,45 @@ class MilvusClient {
*/
virtual Status
GetCompactionPlans(int64_t compaction_id, CompactionPlans& plans) = 0;

/**
* Create Credential
*
* @param [in] username the username for created
* @param [in] password the password for the user to be created
* @return Status operation successfully or not
*/
virtual Status
CreateCredential(const std::string& username, const std::string& password) = 0;

/**
* Update Credential
*
* @param [in] username the username for updated
* @param [in] old_password the old password for the user
* @param [in] new_password the updated password for the user
* @return Status operation successfully or not
*/
virtual Status
UpdateCredential(const std::string& username, const std::string& old_password, const std::string& new_password) = 0;

/**
* Delete Credential
*
* @param [in] username the username to be deleted
* @return Status operation successfully or not
*/
virtual Status
DeleteCredential(const std::string& username) = 0;

/**
* List Users
*
* @param [out] the usernames
* @return Status operation successfully or not
*/
virtual Status
ListCredUsers(std::vector<std::string>& names) = 0;
};

} // namespace milvus
Loading

0 comments on commit d71c2d5

Please sign in to comment.