Skip to content

Conversation

@PaulMec
Copy link

@PaulMec PaulMec commented Sep 4, 2025

C++ Plugins - generating CDN tokens on the fly

It's a good idea to open an issue first for discussion.

  • Tests pass
image
  • Appropriate changes to documentation are included in the PR
    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...

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
Update readme plugins add information of CDN Token Generator.
@PaulMec PaulMec requested a review from a team as a code owner September 4, 2025 00:20
@snippet-bot
Copy link

snippet-bot bot commented Sep 4, 2025

Here is the summary of changes.

You are about to add 2 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment


bool onConfigure(size_t /* config_len */) override {
// SOLUCIÓN TEMPORAL: Usar configuración hardcodeada para bypasear cualquier error
config_.privateKeyHex = "d8ef411f9f735c3d2b263606678ba5b7b1abc1973f1285f856935cc163e9d094";
Copy link
Collaborator

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) {
Copy link
Collaborator

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) {
Copy link
Collaborator

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);
Copy link
Collaborator

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]*)?$)");
Copy link
Collaborator

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) {
Copy link
Collaborator

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) {
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants