Skip to content

Commit 48b493c

Browse files
authored
Merge pull request #56 from gfchaves/gfchaves/LoopbackRequestWithHostHeader
ADD: new policy example for loopback call, also with update on readme.md
2 parents dff4e42 + 57efc68 commit 48b493c

File tree

3 files changed

+152
-111
lines changed

3 files changed

+152
-111
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!--
2+
Send request for a service hosted at same API Management service that is deployed with a virtual network
3+
4+
This policy will ensure that your API Management service instance knows how to redirect the request into a service that is hosted on the same API Management service instance when is deployed with a virtual network.
5+
6+
Ensure that you add/set a HOST header with API Management service's domain for the request that could be redirected back into the same API Management service instance.
7+
8+
Subcription key is retrived from the original request to be forward into the new request. Save the new request result into a variable to return after request.
9+
10+
Maintainer: @gfchaves
11+
-->
12+
<policies>
13+
<inbound>
14+
<base />
15+
<set-variable name="subscriptionKey" value="@(context.Request.Headers.GetValueOrDefault("Ocp-Apim-Subscription-Key=""","scheme="" param="""))" />
16+
<send-request mode="new" response-variable-name="requestResponse" timeout="300" ignore-error="false">
17+
<set-url>https://localhost/{your API Management service endpoint url}</set-url>
18+
<set-method>GET</set-method>
19+
<set-header name="content-type" exists-action="override">
20+
<value>application/json</value>
21+
</set-header>
22+
<set-header name="HOST" exists-action="override">
23+
<value>{your API Management service domain}</value>
24+
</set-header>
25+
<set-header name="Ocp-Apim-Subscription-Key" exists-action="override">
26+
<value>@($"{(string)context.Variables["subscriptionKey"]}")</value>
27+
</set-header>
28+
</send-request>
29+
<return-response response-variable-name="requestResponse" />
30+
</inbound>
31+
<backend>
32+
<base />
33+
</backend>
34+
<outbound>
35+
<base />
36+
</outbound>
37+
<on-error>
38+
<base />
39+
</on-error>
40+
</policies>

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ Overview
3434
| <a href="Look up Key Vault secret using Managed Service Identity.policy.xml">Look up Key Vault secret using Managed Service Identity</a> | Look up and use a Key Vault secret using Managed Service Identity |
3535
| <a href="Look up Key Vault certificate using Managed Service Identity and call backend.policy.xml">Look up Key Vault certificate using Managed Service Identity</a> | Look up and use a Key Vault certificate using Managed Service Identity and call the backend using it as a client certificate |
3636
| <a href="Return HTTP 405 if the HTTP Method of the request is not defined.xml">Return HTTP 405 if the HTTP Method of the request is not defined</a> | Use this policy to translates the HTTP status code from 404 to 405 when the HTTP Method of the request doesn't match the one defined in the corresponding Operation. |
37+
| <a href="Loopback request for service at same API Management service.xml">Loopback request for service at same API Management service</a> | Use this policy to ensure that you can perform a request on a service that is hosted at the same API Management service instance deployed with a virtual network. |

policy-expressions/README.md

+111-111
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,112 @@
1-
# Common policy expressions
2-
This cheat-sheet contains common policy expressions that are often used when authoring Azure API Management policies.
3-
4-
## Interact with HTTP headers
5-
6-
**Get HTTP header**
7-
```c#
8-
context.Request.Headers.GetValueOrDefault("header-name","optional-default-value")
9-
```
10-
**Check HTTP header existence**
11-
```c#
12-
context.Request.Headers.ContainsKey("header-name") == true
13-
```
14-
**Check if HTTP header has expected value**
15-
```c#
16-
context.Request.Headers.GetValueOrDefault("header-name", "").Equals("expected-header-value", StringComparison.OrdinalIgnoreCase)
17-
```
18-
## Interact with URI parameters
19-
20-
**Get URI parameter**
21-
```c#
22-
context.Request.MatchedParameters.GetValueOrDefault("parameter-name","optional-default-value")
23-
```
24-
**Check URI parameter existence**
25-
```c#
26-
context.Request.MatchedParameters.ContainsKey("parameter-name") == true
27-
```
28-
**Check if URI parameter has expected value**
29-
```c#
30-
context.Request.MatchedParameters.GetValueOrDefault("parameter-name", "").Equals("expected-value", StringComparison.OrdinalIgnoreCase) == true
31-
```
32-
## Interact with query string parameters
33-
34-
**Get query string parameter**
35-
```c#
36-
context.Request.Url.Query.GetValueOrDefault("parameter-name", "optional-default-value")
37-
```
38-
**Check query string parameter existence**
39-
```c#
40-
context.Request.Url.Query.ContainsKey("parameter-name") == true
41-
```
42-
**Check if query string parameter has expected value**
43-
```c#
44-
context.Request.Url.Query.GetValueOrDefault("parameter-name", "").Equals("expected-value", StringComparison.OrdinalIgnoreCase) == true
45-
```
46-
## Interact with policy variables
47-
48-
**Get policy variable** *(assuming type string)*
49-
```c#
50-
context.Variables.GetValueOrDefault<string>("variable-name","optional-default-value")
51-
```
52-
**Check policy variable existence**
53-
```c#
54-
context.Variables.ContainsKey("variable-name") == true
55-
```
56-
**Check if policy variable has expected value** *(assuming type string)*
57-
```c#
58-
context.Variables.GetValueOrDefault<string>("variable-name","").Equals("expected-value", StringComparison.OrdinalIgnoreCase)
59-
```
60-
## Interact with JSON bodies
61-
62-
**Get value from JSON body**
63-
```c#
64-
(string)context.Request.Body.As<JObject>(preserveContent: true).SelectToken("root.child jsonpath")
65-
```
66-
**Get value from JSON response variable**
67-
```c#
68-
(string)((IResponse)context.Variables["response-variable-name"]).Body.As<JObject>().SelectToken("root.child jsonpath")
69-
```
70-
**Add property to JSON body**
71-
```c#
72-
JObject body = context.Request.Body.As<JObject>();
73-
body.Add(new JProperty("property-name", "property-value"));
74-
return body.ToString();
75-
```
76-
## Interact with JSON Web Tokens
77-
78-
**Read claim from bearer token**
79-
```c#
80-
context.Request.Headers.GetValueOrDefault("Authorization")?.Split(' ')?[1].AsJwt()?.Claims["claim-name"].FirstOrDefault()
81-
```
82-
83-
## Interact with client certificates
84-
85-
**Check client certificate existence**
86-
```c#
87-
context.Request.Certificate != null
88-
```
89-
**Check if client certificate is valid, including a certificate revocation check**
90-
```c#
91-
context.Request.Certificate.Verify() == true
92-
```
93-
**Check if client certificate is valid, excluding a certificate revocation check**
94-
```c#
95-
context.Request.Certificate.VerifyNoRevocation() == true
96-
```
97-
**Check if client certificate issuer has expected value**
98-
```c#
99-
context.Request.Certificate.Issuer == "trusted-issuer"
100-
```
101-
**Check if client certificate subject has expected value**
102-
```c#
103-
context.Request.Certificate.SubjectName.Name == "expected-subject-name"
104-
```
105-
**Check if client certificate thumbprint has expected value**
106-
```c#
107-
context.Request.Certificate.Thumbprint == "EXPECTED-THUMBPRINT-IN-UPPER-CASE"
108-
```
109-
**Check if client certificate is uploaded in API Management, based on thumbprint**
110-
```c#
111-
context.Deployment.Certificates.Any(c => c.Value.Thumbprint == context.Request.Certificate.Thumbprint) == true
1+
# Common policy expressions
2+
This cheat-sheet contains common policy expressions that are often used when authoring Azure API Management policies.
3+
4+
## Interact with HTTP headers
5+
6+
**Get HTTP header**
7+
```c#
8+
context.Request.Headers.GetValueOrDefault("header-name","optional-default-value")
9+
```
10+
**Check HTTP header existence**
11+
```c#
12+
context.Request.Headers.ContainsKey("header-name") == true
13+
```
14+
**Check if HTTP header has expected value**
15+
```c#
16+
context.Request.Headers.GetValueOrDefault("header-name", "").Equals("expected-header-value", StringComparison.OrdinalIgnoreCase)
17+
```
18+
## Interact with URI parameters
19+
20+
**Get URI parameter**
21+
```c#
22+
context.Request.MatchedParameters.GetValueOrDefault("parameter-name","optional-default-value")
23+
```
24+
**Check URI parameter existence**
25+
```c#
26+
context.Request.MatchedParameters.ContainsKey("parameter-name") == true
27+
```
28+
**Check if URI parameter has expected value**
29+
```c#
30+
context.Request.MatchedParameters.GetValueOrDefault("parameter-name", "").Equals("expected-value", StringComparison.OrdinalIgnoreCase) == true
31+
```
32+
## Interact with query string parameters
33+
34+
**Get query string parameter**
35+
```c#
36+
context.Request.Url.Query.GetValueOrDefault("parameter-name", "optional-default-value")
37+
```
38+
**Check query string parameter existence**
39+
```c#
40+
context.Request.Url.Query.ContainsKey("parameter-name") == true
41+
```
42+
**Check if query string parameter has expected value**
43+
```c#
44+
context.Request.Url.Query.GetValueOrDefault("parameter-name", "").Equals("expected-value", StringComparison.OrdinalIgnoreCase) == true
45+
```
46+
## Interact with policy variables
47+
48+
**Get policy variable** *(assuming type string)*
49+
```c#
50+
context.Variables.GetValueOrDefault<string>("variable-name","optional-default-value")
51+
```
52+
**Check policy variable existence**
53+
```c#
54+
context.Variables.ContainsKey("variable-name") == true
55+
```
56+
**Check if policy variable has expected value** *(assuming type string)*
57+
```c#
58+
context.Variables.GetValueOrDefault<string>("variable-name","").Equals("expected-value", StringComparison.OrdinalIgnoreCase)
59+
```
60+
## Interact with JSON bodies
61+
62+
**Get value from JSON body**
63+
```c#
64+
(string)context.Request.Body.As<JObject>(preserveContent: true).SelectToken("root.child jsonpath")
65+
```
66+
**Get value from JSON response variable**
67+
```c#
68+
(string)((IResponse)context.Variables["response-variable-name"]).Body.As<JObject>().SelectToken("root.child jsonpath")
69+
```
70+
**Add property to JSON body**
71+
```c#
72+
JObject body = context.Request.Body.As<JObject>();
73+
body.Add(new JProperty("property-name", "property-value"));
74+
return body.ToString();
75+
```
76+
## Interact with JSON Web Tokens
77+
78+
**Read claim from bearer token**
79+
```c#
80+
context.Request.Headers.GetValueOrDefault("Authorization")?.Split(' ')?[1].AsJwt()?.Claims["claim-name"].FirstOrDefault()
81+
```
82+
83+
## Interact with client certificates
84+
85+
**Check client certificate existence**
86+
```c#
87+
context.Request.Certificate != null
88+
```
89+
**Check if client certificate is valid, including a certificate revocation check**
90+
```c#
91+
context.Request.Certificate.Verify() == true
92+
```
93+
**Check if client certificate is valid, excluding a certificate revocation check**
94+
```c#
95+
context.Request.Certificate.VerifyNoRevocation() == true
96+
```
97+
**Check if client certificate issuer has expected value**
98+
```c#
99+
context.Request.Certificate.Issuer == "trusted-issuer"
100+
```
101+
**Check if client certificate subject has expected value**
102+
```c#
103+
context.Request.Certificate.SubjectName.Name == "expected-subject-name"
104+
```
105+
**Check if client certificate thumbprint has expected value**
106+
```c#
107+
context.Request.Certificate.Thumbprint == "EXPECTED-THUMBPRINT-IN-UPPER-CASE"
108+
```
109+
**Check if client certificate is uploaded in API Management, based on thumbprint**
110+
```c#
111+
context.Deployment.Certificates.Any(c => c.Value.Thumbprint == context.Request.Certificate.Thumbprint) == true
112112
```

0 commit comments

Comments
 (0)