-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Add a C++ plugin generate an signed URLs for Media CDN #270
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
base: main
Are you sure you want to change the base?
feat: Add a C++ plugin generate an signed URLs for Media CDN #270
Conversation
generate an signed URLs, e.g Ed25519 for Media CDN, without having to deploy code on my origin. Plugin BUILD Config Tests
Generate an signed URLs, e.g Ed25519 for Media CDN, without having to deploy code on my origin. Plugin.cc BUILD
|
Here is the summary of changes. You are about to add 2 region tags.
This comment is generated by snippet-bot.
|
|
|
||
| bool onConfigure(size_t /* config_len */) override { | ||
| // SOLUCIÓN TEMPORAL: Usar configuración hardcodeada para bypasear cualquier error | ||
| config_.privateKeyHex = "d8ef411f9f735c3d2b263606678ba5b7b1abc1973f1285f856935cc163e9d094"; |
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.
These should be parsed from config
| private: | ||
| Config config_; | ||
|
|
||
| bool parseConfig(const std::string& json) { |
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.
Rather than hand-implementing a JSON parser, how about defining a simple protobuf message with fields keyName, expirySeconds, urlHeaderName, and outputHeaderName, and then using proto2::TextFormat::ParseFromString to parse the protobuf from the config string?
| config_(static_cast<CDNTokenRootContext*>(root)->config()) {} | ||
|
|
||
| // Función para loguear sin prefijos de archivo/línea | ||
| void logExact(const std::string& message) { |
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.
remove this method and use logInfo directly
| } | ||
| logExact("Generating signed URL for: " + originalURL); | ||
|
|
||
| std::string signedURL = generateSignedURL(originalURL); |
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.
I think I'm unclear on the larger use case for this plugin. Here it is generating a signed URL and putting it in a request header, to be sent to a backend? I think I need to talk to Arman to understand the context better.
| bool validateURL(const std::string& url) { | ||
| if (url.size() > MAX_URL_LENGTH) return false; | ||
| // Regex similar to Go | ||
| std::regex url_pattern(R"(^https?://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(/[^\s]*)?$)"); |
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.
C++ examples should be using the RE2 library for regex operations.
Also, it looks like this is creating a regex (which is an expensive operation) for each request. Recommended practice is to instantiate regexp objects a single time and store in the root context, so they can be reused--see plugins/samples/regex_rewrite/plugin.cc for an illustration of this.
| return bytes; | ||
| } | ||
|
|
||
| std::string base64UrlEncode(const std::string& in) { |
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.
Can absl::WebSafeBase64Escape() be used?
| return true; | ||
| } | ||
|
|
||
| std::vector<uint8_t> hexToBytes(const std::string& hex) { |
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.
Use an Abseil function, like absl::HexStringToBytes(), or absl::Base64Unescape (if you decide to store the private key as base64)
C++ Plugins - generating CDN tokens on the fly
Generate an signed URLs, e.g Ed25519 for Media CDN, without having to deploy code on my origin.
Before and After Example
BEFORE (The simple URL you start with):
https://media.example.com/secret-video.mp4
AFTER (The signed URL you send back):
https://media.example.com/secret-video.mp4?URLPrefix=aHR...&Expires=1668816000&KeyName=my-key-name&Signature=bgp...