-
Notifications
You must be signed in to change notification settings - Fork 82
feat: Add Lambda-Runtime-Invocation-Id support for cross-wiring protection #214
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
Open
vip-amzn
wants to merge
1
commit into
main
Choose a base branch
from
feat/invocation-id-cross-wiring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
Binary file not shown.
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,137 @@ | ||
| diff --git a/include/aws/lambda-runtime/runtime.h b/include/aws/lambda-runtime/runtime.h | ||
| --- a/include/aws/lambda-runtime/runtime.h | ||
| +++ b/include/aws/lambda-runtime/runtime.h | ||
| @@ -67,6 +67,11 @@ | ||
| std::string tenant_id; | ||
|
|
||
| /** | ||
| + * The unique invocation ID for cross-wiring protection. | ||
| + */ | ||
| + std::string invocation_id; | ||
| + | ||
| + /** | ||
| * Function execution deadline counted in milliseconds since the Unix epoch. | ||
| */ | ||
| std::chrono::time_point<std::chrono::system_clock> deadline; | ||
| @@ -184,11 +189,15 @@ | ||
| /** | ||
| * Tells lambda that the function has succeeded. | ||
| */ | ||
| + post_outcome post_success(std::string const& request_id, invocation_response const& handler_response, std::string const& invocation_id); | ||
| + | ||
| post_outcome post_success(std::string const& request_id, invocation_response const& handler_response); | ||
|
|
||
| /** | ||
| * Tells lambda that the function has failed. | ||
| */ | ||
| + post_outcome post_failure(std::string const& request_id, invocation_response const& handler_response, std::string const& invocation_id); | ||
| + | ||
| post_outcome post_failure(std::string const& request_id, invocation_response const& handler_response); | ||
|
|
||
| /** | ||
| @@ -203,7 +212,8 @@ | ||
| std::string const& url, | ||
| std::string const& content_type, | ||
| std::string const& payload, | ||
| - std::string const& xray_response); | ||
| + std::string const& xray_response, | ||
| + std::string const& invocation_id); | ||
|
|
||
| private: | ||
| std::string const m_user_agent_header; | ||
| diff --git a/src/runtime.cpp b/src/runtime.cpp | ||
| --- a/src/runtime.cpp | ||
| +++ b/src/runtime.cpp | ||
| @@ -41,6 +41,7 @@ | ||
| static constexpr auto DEADLINE_MS_HEADER = "lambda-runtime-deadline-ms"; | ||
| static constexpr auto FUNCTION_ARN_HEADER = "lambda-runtime-invoked-function-arn"; | ||
| static constexpr auto TENANT_ID_HEADER = "lambda-runtime-aws-tenant-id"; | ||
| +static constexpr auto INVOCATION_ID_HEADER = "lambda-runtime-invocation-id"; | ||
|
|
||
| enum Endpoints { | ||
| INIT, | ||
| @@ -294,6 +295,10 @@ | ||
| req.tenant_id = resp.get_header(TENANT_ID_HEADER); | ||
| } | ||
|
|
||
| + if (resp.has_header(INVOCATION_ID_HEADER)) { | ||
| + req.invocation_id = resp.get_header(INVOCATION_ID_HEADER); | ||
| + } | ||
| + | ||
| if (resp.has_header(DEADLINE_MS_HEADER)) { | ||
| auto const& deadline_string = resp.get_header(DEADLINE_MS_HEADER); | ||
| constexpr int base = 10; | ||
| @@ -310,29 +315,40 @@ | ||
| return next_outcome(req); | ||
| } | ||
|
|
||
| -runtime::post_outcome runtime::post_success(std::string const& request_id, invocation_response const& handler_response) | ||
| +runtime::post_outcome runtime::post_success(std::string const& request_id, invocation_response const& handler_response, std::string const& invocation_id) | ||
| { | ||
| std::string const url = m_endpoints[Endpoints::RESULT] + request_id + "/response"; | ||
| - return do_post(url, handler_response.get_content_type(), handler_response.get_payload(), handler_response.get_xray_response()); | ||
| + return do_post(url, handler_response.get_content_type(), handler_response.get_payload(), handler_response.get_xray_response(), invocation_id); | ||
| } | ||
|
|
||
| -runtime::post_outcome runtime::post_failure(std::string const& request_id, invocation_response const& handler_response) | ||
| +runtime::post_outcome runtime::post_success(std::string const& request_id, invocation_response const& handler_response) | ||
| +{ | ||
| + return post_success(request_id, handler_response, ""); | ||
| +} | ||
| + | ||
| +runtime::post_outcome runtime::post_failure(std::string const& request_id, invocation_response const& handler_response, std::string const& invocation_id) | ||
| { | ||
| std::string const url = m_endpoints[Endpoints::RESULT] + request_id + "/error"; | ||
| - return do_post(url, handler_response.get_content_type(), handler_response.get_payload(), handler_response.get_xray_response()); | ||
| + return do_post(url, handler_response.get_content_type(), handler_response.get_payload(), handler_response.get_xray_response(), invocation_id); | ||
| +} | ||
| + | ||
| +runtime::post_outcome runtime::post_failure(std::string const& request_id, invocation_response const& handler_response) | ||
| +{ | ||
| + return post_failure(request_id, handler_response, ""); | ||
| } | ||
|
|
||
| runtime::post_outcome runtime::post_init_error(runtime_response const& init_error_response) | ||
| { | ||
| std::string const url = m_endpoints[Endpoints::INIT]; | ||
| - return do_post(url, init_error_response.get_content_type(), init_error_response.get_payload(), init_error_response.get_xray_response()); | ||
| + return do_post(url, init_error_response.get_content_type(), init_error_response.get_payload(), init_error_response.get_xray_response(), ""); | ||
| } | ||
|
|
||
| runtime::post_outcome runtime::do_post( | ||
| std::string const& url, | ||
| std::string const& content_type, | ||
| std::string const& payload, | ||
| - std::string const& xray_response) | ||
| + std::string const& xray_response, | ||
| + std::string const& invocation_id) | ||
| { | ||
| set_curl_post_result_options(); | ||
| curl_easy_setopt(m_curl_handle, CURLOPT_URL, url.c_str()); | ||
| @@ -351,6 +366,10 @@ | ||
| headers = curl_slist_append(headers, "transfer-encoding:"); | ||
| headers = curl_slist_append(headers, m_user_agent_header.c_str()); | ||
|
|
||
| + if (!invocation_id.empty()) { | ||
| + headers = curl_slist_append(headers, (std::string(INVOCATION_ID_HEADER) + ": " + invocation_id).c_str()); | ||
| + } | ||
| + | ||
| logging::log_debug( | ||
| LOG_TAG, "calculating content length... %s", ("content-length: " + std::to_string(payload.length())).c_str()); | ||
| headers = curl_slist_append(headers, ("content-length: " + std::to_string(payload.length())).c_str()); | ||
| @@ -448,13 +467,13 @@ | ||
| logging::log_info(LOG_TAG, "Invoking user handler completed."); | ||
|
|
||
| if (res.is_success()) { | ||
| - const auto post_outcome = rt.post_success(req.request_id, res); | ||
| + const auto post_outcome = rt.post_success(req.request_id, res, req.invocation_id); | ||
| if (!handle_post_outcome(post_outcome, req.request_id)) { | ||
| return; // TODO: implement a better retry strategy | ||
| } | ||
| } | ||
| else { | ||
| - const auto post_outcome = rt.post_failure(req.request_id, res); | ||
| + const auto post_outcome = rt.post_failure(req.request_id, res, req.invocation_id); | ||
| if (!handle_post_outcome(post_outcome, req.request_id)) { | ||
| return; // TODO: implement a better retry strategy | ||
| } |
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,21 @@ | ||
| --- a/CMakeLists.txt | ||
| +++ b/CMakeLists.txt | ||
| @@ -11,10 +11,17 @@ | ||
| add_library(${PROJECT_NAME} | ||
| "src/logging.cpp" | ||
| "src/runtime.cpp" | ||
| - "src/backward.cpp" | ||
| "${CMAKE_CURRENT_BINARY_DIR}/version.cpp" | ||
| ) | ||
|
|
||
| +include(CheckIncludeFileCXX) | ||
| +check_include_file_cxx("execinfo.h" HAVE_EXECINFO_H) | ||
| +if (HAVE_EXECINFO_H) | ||
| + target_sources(${PROJECT_NAME} PRIVATE "src/backward.cpp") | ||
| +else() | ||
| + message("-- execinfo.h not found, stack traces disabled (musl/Alpine Linux)") | ||
| +endif() | ||
| + | ||
| set_target_properties(${PROJECT_NAME} PROPERTIES | ||
| SOVERSION 0 | ||
| VERSION ${PROJECT_VERSION}) |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably working by chance because nothing is passed at line 909 and we are defaulting to none.
In general I think we should have a stack of test checking that the value is wired from the bootstrap to the client.