Skip to content
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/1-feature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ body:
- Authoring library
- Compiler
- Emulator library
- Documentation
validations:
required: true
- type: input
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/2-bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ body:
- Authoring library
- Compiler
- Emulator library
- Documentation
validations:
required: true
- type: input
Expand Down
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ This will greatly increase alignment of your PR with the project goals and reduc
For more information on how to set up the development environment,
see [Development environment setup](docs/DevEnvironmentSetup.md).

### Implementation guides

- [Add new policy guide](docs/AddPolicyGuide.md)

## License

This project welcomes contributions and suggestions. Most contributions require you to
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The policy toolkit changes that. It allows you to write policy documents in C# l

## Documentation

:exclamation: Packages are only avaliable for download from github release. We are working to bring them to public nuget.

#### Azure API Management policy toolkit documentation for users.
* [Quick start](docs/QuickStart.md)
* [Available policies](docs/AvailablePolicies.md)
Expand Down
150 changes: 150 additions & 0 deletions docs/AddPolicyGuide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Contributing a new policy (feature)

This guide outlines the steps to support a new policy, or new capabilities for an existing policy.

All contributions should follow the steps provided in this guide to support a policy (feature) and its configuration.

If you cannot follow the defined process, please open an issue to discuss before implementing it.

## TL;DR

When adding a new policy, you will typically need to create or modify the following files:

- **Introduce the configuration**: `src/Authoring/Configs/YourPolicyConfig.cs` (Exampe: `RateLimitConfig.cs`)
- **Enable using in respective section or fragment**: `src/Authoring/IInboundContext.cs` (or other context)
- **Support compilation**: `src/Core/Compiling/Policy/YourPolicyCompiler.cs` (Exampe: `RateLimitCompiler.cs`)
- **Provide automated tests**: `test/Test.Core/Compiling/YourPolicyTests.cs` (Exampe: `RateLimitTests.cs`)
- **Document your policy**: `docs/AvailablePolicies.md`

We recommend using existing policies as detailed examples, such as `RateLimit` or `Quota` policies. These contain all possible aspects of a policy compilation implementation.

## Steps to add a new policy
Copy link
Member

Choose a reason for hiding this comment

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

I'd break this apart in to the same steps defined in section above; this also allows you to provide deep links to people when needed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I do not understand your point. but let's for now keep it that way. We iterate on that guide after it will be tried by contributor.

Copy link
Member

@tomkerkhove tomkerkhove Nov 28, 2025

Choose a reason for hiding this comment

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

I would add sub headers similar to what you have in the list above

### Compiler

guidance

### Tests

guidance

But sounds good =)


- Create `src/Authoring/Configs/YourPolicyConfig.cs` as a public record.
- Required parameters as `required` `init` properties
- Optional properties should be `nullable`
- Properties allowing policy expressions should have `[ExpressionAllowed]` attribute assigned.
- Add XML documentation for the record and its properties.

```csharp
/// <summary>
/// Description of config.
/// </summary>
public record YourPolicyConfig
{
/// <summary>
/// Description of your property.
/// </summary>
[ExpressionAllowed]
public required string Property { get; init; }

/// <summary>
/// Optional property description.
/// </summary>
[ExpressionAllowed]
public int? OptionalProperty { get; init; }

// Add more properties as needed.
}
```

- Add a method signature to section context interfaces in which policy is avaliable (e.g. `src/Authoring/IInboundContext.cs`).
Add method to policy fragment context interface to make policy avaliable in policy fragment.
Make sure to add XML documentation.

```csharp
/// <summary>
/// Description of your policy.
/// <param name="config">
/// Configuration for the YourPolicy policy.
/// </param>
/// </summary>
void YourPolicy(YourPolicyConfig config);
```

- Create compiler class `src/Core/Compiling/Policy/YourPolicyCompiler.cs` implementing `IMethodPolicyHandler`.
This class will be responsible for translating the C# method call into the corresponding XML policy element.
- Make sure that the class is `public` and in `Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy` namespace.
This is required for the automatic adding your policy compiler to the compilation.
- Implement the `Handle` method to extract parameters from the method invocation and construct the XML element.
- Use `TryExtractingConfigParameter<T>` to extract the config object into initialization object.
- Use `AddAttribute` extension method to add attributes to the XML element.
- Report diagnostics for missing required parameters using `context.ReportDiagnostic`.
For avaliable errors see `CompilationErrors.cs` file.
- For complex policies with sub elements, refer to existing compilers for guidance (eg. RateLimitCompiler).

```csharp
namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling.Policy;

public class YourPolicyCompiler : IMethodPolicyHandler
{
public string MethodName => nameof(IInboundContext.YourPolicy);

public void Handle(IDocumentCompilationContext context, InvocationExpressionSyntax node)
{
if (!node.TryExtractingConfigParameter<YourPolicyConfig>(context, "your-policy", out var values))
{
return;
}

var element = new XElement("your-policy");

if (!element.AddAttribute(values, nameof(YourPolicyConfig.Property), "propery"))
{
context.Report(Diagnostic.Create(
CompilationErrors.RequiredParameterNotDefined,
node.GetLocation(),
"your-policy",
nameof(YourPolicyConfig.Property)
));
return;
}

element.AddAttribute(values, nameof(YourPolicyConfig.OptionalProperty), "optional-property");

context.AddPolicy(element);
}
}
```

- Add tests to `test/Test.Core/Compiling/YourPolicyTests.cs`.
- Use a `[DataRow]` for each test case, following the pattern of existing tests.
- Check that compiler
- handles compiling policy in all of sections which you added the method to
- required aparameters with constant values
- optional parameters with constant values
- expressions for properties which define [ExpressionAllowed] attribute

```csharp
[TestClass]
public class YourPolicyTests : PolicyCompilerTestBase
{
[TestMethod]
[DataRow(
"""
[Document]
public class PolicyDocument : IDocument
{
public void Inbound(IInboundContext context) {
context.YourPolicy(new YourPolicyConfig()
{
Property = "Value"
});
}
}
""",
"""
<policies>
<inbound>
<your-policy property="Value" />
</inbound>
</policies>
""")]
public void ShouldCompileYourPolicy(string code, string expectedXml)
{
code.CompileDocument().Should().BeSuccessful().And.DocumentEquivalentTo(expectedXml);
}
}
```

- Update `docs/AvailablePolicies.md` to include your new policy in the list of implemented policies.
146 changes: 96 additions & 50 deletions docs/AvailablePolicies.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,103 @@
# Available Policies

The Project is in the development stage.
That means that not all policies are implemented yet.
In this document, you can find a list of implemented policies. For policy details, see the Azure API Management [policy reference](https://learn.microsoft.com/azure/api-management/api-management-policies).

#### :white_check_mark: Implemented policies

* authentication-basic
* authentication-certificate
* authentication-managed-identity
* azure-openai-emit-token-metric
* azure-openai-semantic-cache-lookup
* azure-openai-semantic-cache-store
* base
* cache-lookup
* cache-lookup-value
* cache-remove-value
* cache-store
* cache-store-value
* check-header
* choose
* cors
* emit-metric
* find-and-replace
* forward-request
* ip-filter
* json-to-xml
* jsonp
* llm-emit-token-metric
* llm-semantic-cache-lookup
* llm-semantic-cache-store
* mock-response
* quota
* rate-limit
* rate-limit-by-key
* return-response
* rewrite-uri
* send-request
* set-backend-service
* set-body
* set-header
* set-method
* set-query-parameter
* set-variable
* validate-jwt

Policies not listed here are not implemented yet, we are curious to know which [ones you'd like to use and are happy to review contributions](./../CONTRIBUTING.md).

## InlinePolicy

InlinePolicy is a workaround until all the policies are implemented.
This document lists policy elements that the toolkit's authoring and compiler support by mapping C# APIs to Azure API
Management policy elements. The list below was generated from the public authoring interfaces (
inbound/outbound/backend/on-error/fragment contexts) and reflects which policies the toolkit can compile into XML.

If a policy you need is missing you can either:

- Use `InlinePolicy(...)` to insert raw XML into the compiled document, or
- Open an issue / contribute the feature (learn more in [our contribution guide](../CONTRIBUTING.md)).

Notes:

- The C# compiler maps C# constructs to policy constructs. For example, if/else in C# is compiled to the `choose`
policy (with `when` / `otherwise`).
- `InlinePolicy(string)` allows inserting arbitrary XML when a policy isn't implemented as a first-class API.

## Implemented policies

- authentication-basic
- authentication-certificate
- authentication-managed-identity
- azure-openai-emit-token-metric
- azure-openai-semantic-cache-lookup
- azure-openai-semantic-cache-store
- azure-openai-token-limit
- base
- cache-lookup
- cache-lookup-value
- cache-remove-value
- cache-store
- cache-store-value
- check-header
- choose (implemented via C# if/else -> choose/when/otherwise)
- cors
- cross-domain
- emit-metric
- find-and-replace
- forward-request
- get-authorization-context
- include-fragment
- inline-policy (method to insert raw XML)
- invoke-dapr-binding (publish/send to Dapr bindings)
- ip-filter
- json-to-xml
- jsonp
- limit-concurrency
- llm-content-safety
- llm-emit-token-metric
- llm-semantic-cache-lookup
- llm-semantic-cache-store
- llm-token-limit
- log-to-eventhub
- mock-response
- proxy
- publish-to-dapr
- quota
- quota-by-key
- rate-limit
- rate-limit-by-key
- redirect-content-urls
- remove-header (via SetHeader/RemoveHeader APIs)
- remove-query-parameter (via SetQueryParameter/Remove APIs)
- return-response
- retry
- rewrite-uri
- send-one-way-request
- send-request
- set-backend-service
- set-body
- set-header
- set-header-if-not-exist
- set-method
- set-query-parameter
- set-query-parameter-if-not-exist
- set-status
- set-variable
- trace
- validate-azure-ad-token
- validate-client-certificate
- validate-content
- validate-headers
- validate-jwt
- validate-odata-request
- validate-parameters
- validate-status-code
- wait
- xml-to-json
- xsl-transform

## How to work around missing policies

InlinePolicy is a workaround until all the policies are implemented or new policies are not added yet to toolkit.
It allows you to include policy not implemented yet to the document.

```csharp
c.InlinePolicy("<set-backend-service base-url=\"https://internal.contoso.example\" />");
```

## Contributing

If you'd like a specific policy implemented natively in the toolkit, please open an issue or a pull request in this
repository. See [CONTRIBUTING.md](../CONTRIBUTING.md) and [Add new policy guide](AddPolicyGuide.md) for guidance.
Loading
Loading