Skip to content

docs: Add examples for crypto.sha256 and base64.encode built-in functions#7762

Merged
charlieegan3 merged 29 commits intoopen-policy-agent:mainfrom
ToluGIT:main
Jul 23, 2025
Merged

docs: Add examples for crypto.sha256 and base64.encode built-in functions#7762
charlieegan3 merged 29 commits intoopen-policy-agent:mainfrom
ToluGIT:main

Conversation

@ToluGIT
Copy link
Copy Markdown
Contributor

@ToluGIT ToluGIT commented Jul 11, 2025

Summary

This PR adds examples for two commonly used built-in functions:

  • crypto.md5: Demonstrates payload digest verification with interactive guidance
  • base64.encode: Shows HTTP Basic Authentication header creation in Envoy external authorization

Why these changes are needed

Issue #3786 requests examples for built-in functions that currently lack documentation. The crypto.md5 and base64.encode functions are commonly used in authentication and security workflows but have no
practical examples showing integration patterns.

What's included

crypto.md5 example (/docs/docs/policy-reference/_examples/crypto/digest_verification/):

  • Verifies payload integrity using MD5 digest verification (commonly used for content verification)
  • Includes interactive guidance encouraging users to modify payload values to see verification fail
  • Explains real-world motivation for API/webhook integrity checking

base64.encode example (/docs/docs/policy-reference/_examples/encoding/base64_encode_http_auth/):

  • Creates HTTP Basic Auth headers in Envoy external authorization responses
  • Uses proper envoy.authz package convention and response_headers_to_add object format
  • Demonstrates client-server communication bridging when they "don't speak the same language"
  • Extracts credentials from request headers following OPA-Envoy integration patterns

Both examples follow documentation patterns with policy.rego, config.json, intro.md, and title.txt files, are integrated into their respective builtin documentation pages with proper headings, and include educational context explaining why these patterns are useful.

Notes to assist PR review:

  • Examples tested and validated with opa eval and opa fmt
  • Follows the exact same structure as existing examples in the _examples/ directory
  • Focused on DevOps/Security use cases as these are the target audience for these functions
  • No tests needed as these are documentation examples that demonstrate function usage

Further comments:

This addresses the long-standing issue #3786 incrementally - additional built-in functions can be documented in future PRs using the same approach. The examples chosen are foundational functions that many other security and
DevOps tools depend on.

@netlify
Copy link
Copy Markdown

netlify Bot commented Jul 11, 2025

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 885e3ae
🔍 Latest deploy log https://app.netlify.com/projects/openpolicyagent/deploys/6880f0ca75eeb90008491ac8
😎 Deploy Preview https://deploy-preview-7762--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there, thanks for working on some more examples here. You'll need to add them to the /docs/policy-reference/builtins/crypto page for them to be visible.

I have left a few other comments. The most important thing with these examples is to show a real or approximately real example, as simply as possible rather than cover all use cases for a function. If we want to show many examples, we can created independent examples with more detail in their explanations. Hope that makes sense! lmk if you have any questions.

Also, can you unmark the PR desc as fixing #3786 since that issue needs more work to close (examples for other functions that we can add in alter PRs).

Comment thread docs/docs/policy-reference/_examples/crypto/sha256/config.json Outdated
Comment thread docs/docs/policy-reference/_examples/crypto/sha256/intro.md Outdated
Comment thread docs/docs/policy-reference/_examples/encoding/base64_encode/policy.rego Outdated
Comment thread docs/docs/policy-reference/_examples/crypto/sha256/policy.rego Outdated
ToluGIT added 2 commits July 15, 2025 11:28
…ions

Add practical examples for two commonly used built-in functions in DevOps and security workflows:

- crypto.sha256: Demonstrates file integrity verification, container image validation, and configuration change detection
- base64.encode: Shows HTTP Basic Auth encoding, Kubernetes secret preparation, and general data encoding patterns

These examples address issue open-policy-agent#3786 by providing concrete use cases that help developers understand how to apply these functions in real-world scenarios.

Fixes open-policy-agent#3786

Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
- Simplify examples to focus on single use cases instead of multiple scenarios
- Add examples to builtin documentation pages for visibility using PlaygroundExample
- Fix formatting issues (missing newlines in config.json files)
- Refine intro descriptions to be more precise

Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
Comment thread docs/docs/policy-reference/_examples/crypto/sha256/policy.rego Outdated
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
Comment thread docs/docs/policy-reference/_examples/encoding/base64_encode/policy.rego Outdated
Comment thread docs/docs/policy-reference/builtins/encoding.mdx Outdated
Comment thread docs/docs/policy-reference/builtins/crypto.mdx Outdated
Comment thread docs/docs/policy-reference/_examples/encoding/base64_encode/policy.rego Outdated

# Encode credentials for HTTP Basic Authentication
username := "admin"
password := "secret123"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see an example here that shows a more realistic example of encode. We have the option to use data.json and input.json in these examples to better simulate real policies.

One example might be adding a new header to a downstream request in opa-envoy. e.g.

encoded_username := base64.encode(input.username)
encoded_password := base64.encode(input.password)

basic_auth_header := sprintf("Basic %s", [base64.encode(sprintf("%s:%s", [input.username, input.password]))])

If you have a look here:

https://www.envoyproxy.io/docs/envoy/latest/api-v3/service/auth/v3/external_auth.proto#service-auth-v3-checkresponse

and here:

https://github.com/open-policy-agent/opa-envoy-plugin/blob/main/examples/envoy-uds/quick_start.yaml#L204

you can see how the policy is used to add extra headers as part of the ext auth response. Perhaps something like that would be a good idea to show a more real-world use case here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, true, okay.
Instead of hardcoded fake credentials, now the modified example now takes real input data (username/password) and shows how OPA would process it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I don't see the update yet. Have you pushed your changes?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I've reviewed the changes to this example, but they're incomplete. response_headers_to_add is expected in such an integration. You'll need to base the example around that expected format in order for this to make sense.

Docs are here: https://www.envoyproxy.io/docs/envoy/latest/api-v3/service/auth/v3/external_auth.proto#service-auth-v3-checkresponse

You can also see some examples from this test: https://github.com/open-policy-agent/opa-envoy-plugin/blob/7ebf657a09b931ef2417d0502bad6bdf600267d5/envoyauth/response_test.go#L461

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I have added the complete response_headers_to_add structure as requested.

Thanks for flagging it.

Comment thread docs/docs/policy-reference/_examples/crypto/sha256/policy.rego Outdated
@@ -0,0 +1 @@
The `crypto.sha256` function computes SHA-256 hash values, commonly used for file integrity verification in DevOps workflows. No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really the case in OPA since generally OPA is not running against file system content. It'd be nice to update this description to be more about the real world example you choose to implement here instead of a general description of the function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of generic descriptions about what the functions do, refined the descriptions now to explain exactly what THIS specific example demonstrates.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? 😕 not sure what you mean here sorry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I was referring to the description

The current version now says, "This example shows how to use crypto.sha256 to verify data integrity by comparing a provided hash with the computed hash of the actual content."

Instead of the generic description about "file integrity verification in DevOps workflows," it now describes exactly what THIS specific example demonstrates. Comparing a provided hash with the computed hash of input data.

Comment thread docs/docs/policy-reference/_examples/crypto/sha256/config.json Outdated
ToluGIT added 2 commits July 15, 2025 15:45
- Rename base64_encode → base64_encode_http_auth
- Add realistic input.json files
- Add proper headings and positioning
- Enable input display in playground
- Update descriptions and fix formatting

Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
Comment thread docs/docs/policy-reference/builtins/encoding.mdx Outdated
Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
Comment thread docs/docs/policy-reference/_examples/encoding/base64_encode_http_auth/title.txt Outdated
srenatus and others added 5 commits July 15, 2025 17:34
Signed-off-by: Stephan Renatus <stephan@styra.com>
Change from generic 'Base64 Encoding' to specific 'HTTP Basic Auth Header Creation'
to clearly indicate the example's purpose

Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
- crypto.sha256: Change to signed payload verification with JSON marshaling
- base64.encode: Use proper Envoy response_headers_to_add format
- Fix missing newlines in title.txt files

Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
@@ -0,0 +1,13 @@
package base64_encode_http_auth
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically, envoy policies are invoked from envoy/authz/allow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I changed the package to "envoy.authz" following the standard OPA-Envoy convention for external authorisation policies.

package base64_encode_http_auth

# Envoy external auth response with Basic Auth header
allow := true
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually a default rule would be used there instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use proper default deny pattern with default allow := false and conditional allow rule based on credential presence

# Envoy external auth response with Basic Auth header
allow := true

response_headers_to_add := [
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response_headers_to_add is not a list, but an object of str:str

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected response_headers_to_add format from array to object (str:str) as per Envoy CheckResponse specification

{
"header": {
"key": "Authorization",
"value": sprintf("Basic %s", [base64.encode(sprintf("%s:%s", [input.username, input.password]))])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input.username is not set by OPA envoy, you'd need to have extracted this from either the request body or another header in the envoy case. There are in the input, but in different locations depending on body/header. Header is likely the easiest to use for this example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed input structure to use proper Envoy CheckRequest format. Now extracts credentials from request headers (x-username/x-password) as suggested, following actual OPA-Envoy integration patterns.

@@ -0,0 +1,7 @@
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this example. Can we rename the folder to something more descriptive than sha256?

Copy link
Copy Markdown
Contributor Author

@ToluGIT ToluGIT Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm
verify_signed_payload or signature_verification which would be preferable?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps something like digest verification? crypto.md5sum might be better as it's often used for such things. https://www.openpolicyagent.org/docs/policy-reference/builtins/crypto#builtin-crypto-cryptomd5

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to crypto.md5 as you suggested

it's indeed more commonly used for digest verification. Renamed folder to "digest_verification" for clarity.

ToluGIT added 2 commits July 16, 2025 19:17
- Use envoy.authz package convention
- Add default deny with conditional allow
- Fix response_headers_to_add format (object not array)
- Extract credentials from proper Envoy input structure

Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
@@ -0,0 +1 @@
This example shows how to use `crypto.sha256` to verify a signed payload by checking that the JSON representation matches a supplied signature hash.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something nice we can do in these intros is explain to users how they can play with the example. In this example, we should prompt users to change the contents of the payload to show that the signature verification would fail. Or something like that.

It'd also be nice to motivate why something like this might be getting done in a policy. Answering the question: why is this useful would be helpful here too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! modified the intro with both interactive guidance and motivation:

Added "Try it yourself" section that allows users to change any value in the payload object (like user name or resource path) and re-run the example to see digest_valid become false.

Added a "Why this is useful" section explaining that content verification is essential in APIs and webhooks where you need to ensure data hasn't been tampered with during transmission. The digest acts as a fingerprint for the payload.

Comment thread docs/docs/policy-reference/_examples/encoding/base64_encode_http_auth/title.txt Outdated
@@ -0,0 +1 @@
This example shows how to use `base64.encode` in Envoy external authorization to create HTTP Basic Authentication headers from request credentials and add them to downstream requests.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I think we need to explain that the context is using a base64 encoded example to set a value expected by some downstream service. It is a nice usecase that shows how OPA and envoy can glue things together when the client and server don't speak the same language. Does that make sense? we're using the base64 function here as a utility function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does make sense.
Now it clearly demonstrates how OPA and Envoy work together when client and server "don't speak the same language."

ToluGIT and others added 2 commits July 17, 2025 20:05
…tp_auth/title.txt

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
- Switch to crypto.md5 for digest verification (more common use case)
- Rename folder to digest_verification for clarity
- Add interactive guidance and real-world context to intros
- Explain base64 as utility bridging client-server communication

Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
Comment thread docs/docs/policy-reference/_examples/encoding/base64_encode_http_auth/title.txt Outdated
@@ -0,0 +1,6 @@
This example shows how `base64.encode` acts as a utility function to bridge communication between client and server when they don't speak the same language.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This example shows how `base64.encode` acts as a utility function to bridge communication between client and server when they don't speak the same language.
This example shows how `base64.encode` acts as a utility function to bridge communication between client and server when they expect differing request formats.

Comment thread docs/docs/policy-reference/_examples/encoding/base64_encode_http_auth/intro.md Outdated
Comment thread docs/docs/policy-reference/_examples/encoding/base64_encode_http_auth/intro.md Outdated
Comment thread docs/docs/policy-reference/_examples/crypto/digest_verification/intro.md Outdated
Comment thread docs/docs/policy-reference/_examples/crypto/digest_verification/intro.md Outdated
ToluGIT and others added 3 commits July 21, 2025 14:42
…n/intro.md

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
…tp_auth/intro.md

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
…n/intro.md

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
ToluGIT and others added 2 commits July 21, 2025 14:43
…tp_auth/intro.md

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
…tp_auth/title.txt

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

@ToluGIT ToluGIT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @charlieegan3

@@ -0,0 +1 @@
OPA Envoy Header Manipulation
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rename

docs/docs/policy-reference/_examples/encoding/base64_encode_http_auth/*

to match the title here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, true.

Done! Renamed the folder from "base64_encode_http_auth" to "envoy_header_manipulation" to match the "OPA Envoy Header Manipulation" title. Also updated the built-in page reference and verified everything still works correctly.

ToluGIT added 2 commits July 23, 2025 11:55
Rename base64_encode_http_auth → envoy_header_manipulation to match
'OPA Envoy Header Manipulation' title as requested

Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
Comment thread docs/docs/policy-reference/_examples/encoding/envoy_header_manipulation/intro.md Outdated
Comment thread docs/docs/policy-reference/_examples/crypto/digest_verification/policy.rego Outdated
@charlieegan3
Copy link
Copy Markdown
Contributor

Thanks for the back and forth here @ToluGIT, just a few minor edits needed now to get it looking nice in the website editors and we can get this merged.

ToluGIT and others added 6 commits July 23, 2025 14:50
…ipulation/policy.rego

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
…ipulation/policy.rego

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
…ipulation/policy.rego

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
…ipulation/policy.rego

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
…n/policy.rego

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
…ipulation/intro.md

Co-authored-by: Charlie Egan <git@charlieegan3.com>
Signed-off-by: ToluGIT <93225033+ToluGIT@users.noreply.github.com>
@ToluGIT
Copy link
Copy Markdown
Contributor Author

ToluGIT commented Jul 23, 2025

Thanks so much for all the detailed feedback and guidance throughout this process, @charlieegan3. Your insights have really helped shape these examples into something much more practical and educational. I appreciate you taking the time to walk through the proper OPA-Envoy integration patterns.

Thanks again for the thorough review; it's been a great learning experience working with you on this! 🙏

@ToluGIT ToluGIT closed this Jul 23, 2025
@ToluGIT ToluGIT reopened this Jul 23, 2025
@ToluGIT
Copy link
Copy Markdown
Contributor Author

ToluGIT commented Jul 23, 2025

Sorry about that! I accidentally closed the PR instead of waiting for you to merge it. It's reopened now and ready for merge. Thanks again for all the feedback and approval!

Comment thread docs/docs/policy-reference/_examples/crypto/digest_verification/policy.rego Outdated
Signed-off-by: Charlie Egan <git@charlieegan3.com>
@charlieegan3 charlieegan3 enabled auto-merge (squash) July 23, 2025 14:26
@charlieegan3 charlieegan3 merged commit e48e7f5 into open-policy-agent:main Jul 23, 2025
31 checks passed
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.

3 participants