-
Notifications
You must be signed in to change notification settings - Fork 7k
[Core] Add authentication token logic and related tests #58046
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce73705
Add authentication token logic and related tests
c821c21
revert unneeded changs from src/ray/rpc/tests/BUILD.bazel
a14dc69
readd dependencies
7834733
Merge branch 'master' into token_auth_1
sampan-s-nayak 4801ed7
address comments + fix build
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| load("//bazel:ray.bzl", "ray_cc_library") | ||
|
|
||
| ray_cc_library( | ||
| name = "authentication_mode", | ||
| srcs = ["authentication_mode.cc"], | ||
| hdrs = ["authentication_mode.h"], | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//src/ray/common:ray_config", | ||
| "@com_google_absl//absl/strings", | ||
| ], | ||
| ) | ||
|
|
||
| ray_cc_library( | ||
| name = "authentication_token", | ||
| hdrs = ["authentication_token.h"], | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//src/ray/common:constants", | ||
| "@com_github_grpc_grpc//:grpc++", | ||
| ], | ||
| ) | ||
|
|
||
| ray_cc_library( | ||
| name = "authentication_token_loader", | ||
| srcs = ["authentication_token_loader.cc"], | ||
| hdrs = ["authentication_token_loader.h"], | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| ":authentication_mode", | ||
| ":authentication_token", | ||
| "//src/ray/util:logging", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright 2025 The Ray 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 "ray/rpc/authentication/authentication_mode.h" | ||
|
|
||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| #include "absl/strings/ascii.h" | ||
| #include "ray/common/ray_config.h" | ||
|
|
||
| namespace ray { | ||
| namespace rpc { | ||
|
|
||
| AuthenticationMode GetAuthenticationMode() { | ||
| std::string auth_mode_lower = absl::AsciiStrToLower(RayConfig::instance().auth_mode()); | ||
|
|
||
| if (auth_mode_lower == "token") { | ||
| return AuthenticationMode::TOKEN; | ||
| } else { | ||
| return AuthenticationMode::DISABLED; | ||
| } | ||
| } | ||
|
|
||
| } // namespace rpc | ||
| } // namespace ray |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright 2025 The Ray 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace ray { | ||
| namespace rpc { | ||
|
|
||
| enum class AuthenticationMode { | ||
| DISABLED, | ||
| TOKEN, | ||
| }; | ||
|
|
||
| /// Get the authentication mode from the RayConfig. | ||
| /// \return The authentication mode enum value. returns AuthenticationMode::DISABLED if | ||
| /// the authentication mode is not set or is invalid. | ||
| AuthenticationMode GetAuthenticationMode(); | ||
|
|
||
| } // namespace rpc | ||
| } // namespace ray |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // Copyright 2025 The Ray 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <grpcpp/grpcpp.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "ray/common/constants.h" | ||
|
|
||
| namespace ray { | ||
| namespace rpc { | ||
|
|
||
| /// Secure wrapper for authentication tokens. | ||
| /// - Wipes memory on destruction | ||
| /// - Constant-time comparison | ||
| /// - Redacted output when logged or printed | ||
| class AuthenticationToken { | ||
| public: | ||
| AuthenticationToken() = default; | ||
| explicit AuthenticationToken(std::string value) : secret_(value.begin(), value.end()) {} | ||
|
|
||
| AuthenticationToken(const AuthenticationToken &other) : secret_(other.secret_) {} | ||
| AuthenticationToken &operator=(const AuthenticationToken &other) { | ||
| if (this != &other) { | ||
| SecureClear(); | ||
| secret_ = other.secret_; | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| // Move operations | ||
| AuthenticationToken(AuthenticationToken &&other) noexcept { | ||
| MoveFrom(std::move(other)); | ||
| } | ||
| AuthenticationToken &operator=(AuthenticationToken &&other) noexcept { | ||
| if (this != &other) { | ||
| SecureClear(); | ||
| MoveFrom(std::move(other)); | ||
| } | ||
| return *this; | ||
| } | ||
| ~AuthenticationToken() { SecureClear(); } | ||
|
|
||
| bool empty() const noexcept { return secret_.empty(); } | ||
|
|
||
| /// Constant-time equality comparison | ||
| bool Equals(const AuthenticationToken &other) const noexcept { | ||
| return ConstTimeEqual(secret_, other.secret_); | ||
| } | ||
|
|
||
| /// Equality operator (constant-time) | ||
| bool operator==(const AuthenticationToken &other) const noexcept { | ||
| return Equals(other); | ||
| } | ||
|
|
||
| /// Inequality operator | ||
| bool operator!=(const AuthenticationToken &other) const noexcept { | ||
| return !(*this == other); | ||
| } | ||
|
|
||
| /// Set authentication metadata on a gRPC client context | ||
| /// Only call this from client-side code | ||
| void SetMetadata(grpc::ClientContext &context) const { | ||
| if (!secret_.empty()) { | ||
| context.AddMetadata(kAuthTokenKey, | ||
| kBearerPrefix + std::string(secret_.begin(), secret_.end())); | ||
| } | ||
| } | ||
|
|
||
| /// Create AuthenticationToken from gRPC metadata value | ||
| /// Strips "Bearer " prefix and creates token object | ||
| /// @param metadata_value The raw value from server metadata (should include "Bearer " | ||
| /// prefix) | ||
| /// @return AuthenticationToken object (empty if format invalid) | ||
| static AuthenticationToken FromMetadata(std::string_view metadata_value) { | ||
| const std::string_view prefix(kBearerPrefix); | ||
| if (metadata_value.size() < prefix.size() || | ||
| metadata_value.substr(0, prefix.size()) != prefix) { | ||
| return AuthenticationToken(); // Invalid format, return empty | ||
| } | ||
| std::string_view token_part = metadata_value.substr(prefix.size()); | ||
| return AuthenticationToken(std::string(token_part)); | ||
| } | ||
|
|
||
| friend std::ostream &operator<<(std::ostream &os, const AuthenticationToken &t) { | ||
| return os << "<Redacted Authentication Token>"; | ||
| } | ||
|
|
||
| private: | ||
| std::vector<uint8_t> secret_; | ||
|
|
||
| // Constant-time string comparison to avoid timing attacks. | ||
| // https://en.wikipedia.org/wiki/Timing_attack | ||
| static bool ConstTimeEqual(const std::vector<uint8_t> &a, | ||
| const std::vector<uint8_t> &b) noexcept { | ||
| if (a.size() != b.size()) { | ||
| return false; | ||
| } | ||
| unsigned char diff = 0; | ||
| for (size_t i = 0; i < a.size(); ++i) { | ||
| diff |= a[i] ^ b[i]; | ||
| } | ||
| return diff == 0; | ||
| } | ||
|
|
||
| // replace the characters in the memory with 0 | ||
| static void ExplicitBurn(void *p, size_t n) noexcept { | ||
| #if defined(_MSC_VER) | ||
| SecureZeroMemory(p, n); | ||
| #elif defined(__STDC_LIB_EXT1__) | ||
| memset_s(p, n, 0, n); | ||
| #else | ||
| // Using array indexing instead of pointer arithmetic | ||
| volatile auto *vp = static_cast<volatile uint8_t *>(p); | ||
| for (size_t i = 0; i < n; ++i) { | ||
| vp[i] = 0; | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| void SecureClear() noexcept { | ||
| if (!secret_.empty()) { | ||
| ExplicitBurn(secret_.data(), secret_.size()); | ||
| secret_.clear(); | ||
| } | ||
| } | ||
|
|
||
| void MoveFrom(AuthenticationToken &&other) noexcept { | ||
| secret_ = std::move(other.secret_); | ||
| // Clear the moved-from object explicitly for security | ||
| // Note: 'other' is already an rvalue reference, no need to move again | ||
| other.SecureClear(); | ||
| } | ||
|
Comment on lines
+149
to
+152
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my only concern here is that this is a bit of an expensive move operation... we should keep it in mind once we run release/performance tests if there is any significant regression. |
||
| }; | ||
|
|
||
| } // namespace rpc | ||
| } // namespace ray | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.