Skip to content

Conversation

@Mielek
Copy link
Contributor

@Mielek Mielek commented Nov 28, 2025

Closes #56, #184, #185

This allows having set of expressions and set of constants which can be reuse as policy parameters.

Limitations:

Reusable constants need to be defined as const.

Reusable expressions will not be inline if used inside other expressions.

This PR does not touch on reusable methods for policy fragments

Example:

Constants.cs

public static class Constants
{
    public const string Username = "{{username}}";
    public const string Password = "{{password}}";
    public const string AzureManagementUrl = "https://management.azure.com/";
}

Expressions.cs

public static class Expressions
{
     public bool IsFromCompanyIp(IExpressionContext context)
        => context.Request.IpAddress.StartsWith("10.0.0.");
}

PolicyDocumentA.cs

[Document]
public class PolicyDocumentA : IDocument
{
    public void Inbound(IInboundContext context) 
    { 
        if (Expressions.IsFromCompanyIp(context.ExpressionContext))
        {
            context.AuthenticationBasic(Constants.Username, Constants.Username);
        }
        else
        {
            context.AuthenticationManagedIdentity(new ManagedIdentityAuthenticationConfig()
            {
                Resource = Constants.AzureManagementUrl,
            });
        }
        // other policies
    }
}

PolicyDocumentB.cs

[Document]
public class PolicyDocumentB : IDocument
{
    public void Inbound(IInboundContext context) 
    { 
        if (Expressions.IsFromCompanyIp(context.ExpressionContext))
        {
            context.AuthenticationBasic(Constants.Username, Constants.Username);
        }
        else
        {
            context.AuthenticationManagedIdentity(new ManagedIdentityAuthenticationConfig()
            {
                Resource = Constants.AzureManagementUrl,
            });
        }
        // other policies
    }
}

After this PR compiling above should produce
PolicyDocumentA.xml

<policies>
  <inbound>
    <choose>
      <when condition="@(context.Request.IpAddress.StartsWith("10.0.0."))">
        <authentication-basic username="{{username}}" password="{{password}}" />
      </when>
      <otherwise>
        <authentication-managed-identity resource="https://management.azure.com/" />
      </otherwise>
    </choose>
    <!-- other policies -->
  <inbound>
<policies>

PolicyDocumentB.xml

<policies>
  <inbound>
    <choose>
      <when condition="@(context.Request.IpAddress.StartsWith("10.0.0."))">
        <authentication-basic username="{{username}}" password="{{password}}" />
      </when>
      <otherwise>
        <authentication-managed-identity resource="https://management.azure.com/" />
      </otherwise>
    </choose>
    <!-- other policies -->
  <inbound>
<policies>

@Mielek Mielek linked an issue Nov 28, 2025 that may be closed by this pull request
@Mielek Mielek linked an issue Dec 8, 2025 that may be closed by this pull request
@Mielek Mielek force-pushed the mielek/external-referancing branch from bb85e2d to 1708426 Compare December 18, 2025 16:38
Mielek and others added 2 commits December 19, 2025 11:34
Co-authored-by: Tom Kerkhove <kerkhove.tom@gmail.com>
Co-authored-by: Tom Kerkhove <kerkhove.tom@gmail.com>
@Mielek Mielek merged commit be47bdb into main Dec 19, 2025
11 checks passed
@tomkerkhove tomkerkhove deleted the mielek/external-referancing branch December 19, 2025 10:38
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.

Support for reusable expressions Support configuration file for compile-time value management Const strings are not handled

3 participants