-
Notifications
You must be signed in to change notification settings - Fork 2
feat(inspect): add support for NGWAF inspect api #75
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
6 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,26 @@ | ||
| //! @example ngwaf_inspect.cpp | ||
| #include "fastly/sdk.h" | ||
|
|
||
| int main() { | ||
| fastly::log::init_simple("logs"); | ||
| auto req{fastly::Request::from_client()}; | ||
|
|
||
| auto ires{fastly::security::inspect( | ||
| req, | ||
| fastly::security::InspectConfig().with_corp("my_corp").with_workspace( | ||
| "my_workspace"))}; | ||
| auto verdict{ires->verdict()}; | ||
|
|
||
| fastly::Body body{"NGWAF Verdict: "}; | ||
| if (verdict == fastly::security::InspectVerdict::Allow) { | ||
| body << "Allow"; | ||
| } else if (verdict == fastly::security::InspectVerdict::Block) { | ||
| body << "Block"; | ||
| } else if (verdict == fastly::security::InspectVerdict::Unauthorized) { | ||
| body << "Unauthorized"; | ||
| } else if (verdict == fastly::security::InspectVerdict::Other) { | ||
| body << *ires->unrecognized_verdict_info() << " (Other)"; | ||
| } | ||
|
|
||
| fastly::Response::from_body(std::move(body)).send_to_client(); | ||
| } |
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,110 @@ | ||
| #ifndef FASTLY_SECURITY_H | ||
| #define FASTLY_SECURITY_H | ||
|
|
||
| #include <fastly/detail/access_bridge_internals.h> | ||
| #include <fastly/expected.h> | ||
| #include <fastly/http/body.h> | ||
| #include <fastly/http/request.h> | ||
| #include <optional> | ||
|
|
||
| namespace fastly::security { | ||
| /// Configuration for inspecting a `Request` using Security. | ||
| class InspectConfig { | ||
| public: | ||
| /// Create a new default `InspectConfig` | ||
| InspectConfig() = default; | ||
|
|
||
| /// Specify an explicity client IP address to inspect. | ||
| /// By default, inspect will use the IP address that made the request to the | ||
| /// running Compute service, but you may want to use a different IP when | ||
| /// service chaining or if requests are proxied from outside of Fastly’s | ||
| /// network. | ||
| InspectConfig with_client_ip(std::string ip) && { | ||
| this->client_ip_ = std::move(ip); | ||
| return std::move(*this); | ||
| } | ||
|
|
||
| /// Set a corp name for the configuration. | ||
| InspectConfig with_corp(std::string name) && { | ||
| this->corp_ = std::move(name); | ||
| return std::move(*this); | ||
| } | ||
|
|
||
| /// Set a workspace name for the configuration. | ||
| InspectConfig with_workspace(std::string name) && { | ||
| this->workspace_ = std::move(name); | ||
| return std::move(*this); | ||
| } | ||
|
|
||
| /// Set a buffer size for the response. | ||
| InspectConfig with_buffer_size(std::size_t size) && { | ||
| this->buffer_size_ = size; | ||
| return std::move(*this); | ||
| } | ||
|
|
||
| const std::optional<std::string> &client_ip() const { return client_ip_; } | ||
| const std::optional<std::string> &corp() const { return corp_; } | ||
| const std::optional<std::string> &workspace() const { return workspace_; } | ||
| const std::optional<std::size_t> &buffer_size() const { return buffer_size_; } | ||
|
|
||
| private: | ||
| std::optional<std::string> client_ip_; | ||
| std::optional<std::string> corp_; | ||
| std::optional<std::string> workspace_; | ||
| std::optional<std::size_t> buffer_size_; | ||
| }; | ||
| using fastly::sys::security::InspectErrorCode; | ||
| using fastly::sys::security::InspectVerdict; | ||
| class InspectError { | ||
| public: | ||
| InspectError(fastly::sys::security::InspectError *e) | ||
| : err_(rust::Box<fastly::sys::security::InspectError>::from_raw(e)) {}; | ||
| InspectError(rust::Box<fastly::sys::security::InspectError> e) | ||
| : err_(std::move(e)) {}; | ||
| InspectErrorCode error_code(); | ||
| std::string error_msg(); | ||
| /// When getting `InspectErrorCode::BufferSizeError`, this can be used to get | ||
| /// the required size of the buffer before re-attempting the call. | ||
| std::optional<std::size_t> required_buffer_size(); | ||
|
|
||
| private: | ||
| rust::Box<fastly::sys::security::InspectError> err_; | ||
| }; | ||
|
|
||
| /// Results of asking Security to inspect a `Request` | ||
| class InspectResponse { | ||
| friend detail::AccessBridgeInternals; | ||
|
|
||
| public: | ||
| /// Security status code. | ||
| std::int16_t status() const; | ||
| /// A redirect URL returned from Security | ||
| std::optional<std::string> redirect_url() const; | ||
| /// Tags returned by Security | ||
| std::vector<std::string> tags() const; | ||
| /// Get Security's verdict on how to handle this request. | ||
| InspectVerdict verdict() const; | ||
| /// Get additional information for verdicts where `this->verdict()` is | ||
| /// `Other`. | ||
| std::optional<std::string> unrecognized_verdict_info() const; | ||
| /// How long Security spent determining its verdict. | ||
| std::chrono::milliseconds decision_ms() const; | ||
| /// A redirect URI returned by Security. | ||
| bool is_redirect() const; | ||
| /// Convert a redirect URI returned by Security into a `Response`. | ||
| std::optional<Response> into_redirect(); | ||
|
|
||
| private: | ||
| rust::Box<fastly::sys::security::InspectResponse> ir_; | ||
| InspectResponse(rust::Box<fastly::sys::security::InspectResponse> ir) | ||
| : ir_(std::move(ir)) {}; | ||
| }; | ||
|
|
||
| /// Inspect a `Request` using the [Fastly Next-Gen | ||
| /// WAF](https://docs.fastly.com/en/ngwaf/). | ||
| tl::expected<InspectResponse, InspectError> | ||
| inspect(fastly::http::Request &request, InspectConfig config); | ||
|
|
||
| } // namespace fastly::security | ||
|
|
||
| #endif | ||
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.
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.