Skip to content

Conversation

@Mielek
Copy link
Collaborator

@Mielek Mielek commented Nov 28, 2025

Closes #56, #184

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>

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 configuration file for compile-time value management Const strings are not handled

2 participants