Skip to content

Instantly share code, notes, and snippets.

@ralacher
Created September 9, 2022 17:04
Show Gist options
  • Save ralacher/8f583b3862965b8de15073a1ceb608ca to your computer and use it in GitHub Desktop.
Save ralacher/8f583b3862965b8de15073a1ceb608ca to your computer and use it in GitHub Desktop.
Azure API Management policy to selectively enforce mTLS
<policies>
<inbound>
<base />
<choose>
<!-- No certificate required -->
<when condition="@(context.Request.Headers.GetValueOrDefault("Host").Equals("no-mtls.example.org", StringComparison.OrdinalIgnoreCase))">
<return-response>
<set-status code="200" reason="OK" />
<set-body>No certificate required for no-mtls.example.org</set-body>
</return-response>
</when>
<!-- Deal with missing certificate -->
<when condition="@(context.Request.Headers.GetValueOrDefault("Host").Equals("yes-mtls.example.org", StringComparison.OrdinalIgnoreCase) && context.Request.Certificate == null)">
<return-response>
<set-status code="403" />
<set-body>No certificate provided</set-body>
</return-response>
</when>
<!-- Deal with invalid certificates -->
<when condition="@(context.Request.Headers.GetValueOrDefault("Host").Equals("yes-mtls.example.org", StringComparison.OrdinalIgnoreCase) && !context.Request.Certificate.Verify())">
<return-response>
<set-status code="403" />
<set-body>Invalid certificate</set-body>
</return-response>
</when>
<!-- Otherwise... -->
<otherwise></otherwise>
</choose>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment