-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathAOAI_Policy-Token_Tracking_End_User_Context.xml
More file actions
170 lines (151 loc) · 7.91 KB
/
AOAI_Policy-Token_Tracking_End_User_Context.xml
File metadata and controls
170 lines (151 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<policies>
<inbound>
<set-backend-service id="apim-generated-policy" backend-id="azure-openai-openai-endpoint" />
<set-variable name="deploymentId" value="@(context.Request.Url.Path.Split('/').ElementAtOrDefault(3))" />
<set-variable name="forwardedIdentityToken" value="@{
var token = context.Request.Headers.GetValueOrDefault("x-apim-end-user-jwt", string.Empty);
if (String.IsNullOrEmpty(token))
{
token = context.Request.Headers.GetValueOrDefault("Authorization", string.Empty);
}
if (String.IsNullOrEmpty(token))
{
return String.Empty;
}
token = token.Trim();
if (token.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{
token = token.Substring(7).Trim();
}
return token;
}" />
<set-variable name="forwardedEndUserId" value="@{
var userId = context.Request.Headers.GetValueOrDefault("x-apim-end-user-id", string.Empty);
if (!String.IsNullOrEmpty(userId))
{
return userId;
}
var token = context.Variables.GetValueOrDefault<string>("forwardedIdentityToken", string.Empty);
if (String.IsNullOrEmpty(token))
{
return "unknown";
}
var jwt = token.AsJwt();
if (jwt == null)
{
return "unknown";
}
userId = jwt.Claims.GetValueOrDefault("oid", string.Empty);
if (!String.IsNullOrEmpty(userId))
{
return userId;
}
userId = jwt.Claims.GetValueOrDefault("sub", string.Empty);
return !String.IsNullOrEmpty(userId) ? userId : "unknown";
}" />
<set-variable name="forwardedEndUserTenantId" value="@{
var tenantId = context.Request.Headers.GetValueOrDefault("x-apim-end-user-tenant-id", string.Empty);
if (!String.IsNullOrEmpty(tenantId))
{
return tenantId;
}
var token = context.Variables.GetValueOrDefault<string>("forwardedIdentityToken", string.Empty);
if (String.IsNullOrEmpty(token))
{
return "unknown";
}
var jwt = token.AsJwt();
if (jwt == null)
{
return "unknown";
}
tenantId = jwt.Claims.GetValueOrDefault("tid", string.Empty);
return !String.IsNullOrEmpty(tenantId) ? tenantId : "unknown";
}" />
<set-variable name="clientApplicationId" value="@{
var clientApplicationId = context.Request.Headers.GetValueOrDefault("x-apim-client-application-id", string.Empty);
if (!String.IsNullOrEmpty(clientApplicationId))
{
return clientApplicationId;
}
var token = context.Variables.GetValueOrDefault<string>("forwardedIdentityToken", string.Empty);
if (String.IsNullOrEmpty(token))
{
return "unknown";
}
var jwt = token.AsJwt();
if (jwt == null)
{
return "unknown";
}
clientApplicationId = jwt.Claims.GetValueOrDefault("azp", string.Empty);
if (!String.IsNullOrEmpty(clientApplicationId))
{
return clientApplicationId;
}
clientApplicationId = jwt.Claims.GetValueOrDefault("appid", string.Empty);
if (!String.IsNullOrEmpty(clientApplicationId))
{
return clientApplicationId;
}
clientApplicationId = jwt.Claims.GetValueOrDefault("client_id", string.Empty);
return !String.IsNullOrEmpty(clientApplicationId) ? clientApplicationId : "unknown";
}" />
<set-variable name="identitySource" value="@{
if (!String.IsNullOrEmpty(context.Request.Headers.GetValueOrDefault("x-apim-end-user-id", string.Empty))
|| !String.IsNullOrEmpty(context.Request.Headers.GetValueOrDefault("x-apim-end-user-tenant-id", string.Empty))
|| !String.IsNullOrEmpty(context.Request.Headers.GetValueOrDefault("x-apim-client-application-id", string.Empty)))
{
return "header";
}
return !String.IsNullOrEmpty(context.Variables.GetValueOrDefault<string>("forwardedIdentityToken", string.Empty))
? "jwt"
: "unknown";
}" />
<set-variable name="tokenCounterKey" value="@{
if (context.Subscription != null && !String.IsNullOrEmpty(context.Subscription.Id))
{
return "subscription:" + context.Subscription.Id;
}
var clientApplicationId = context.Variables.GetValueOrDefault<string>("clientApplicationId", "unknown");
var endUserId = context.Variables.GetValueOrDefault<string>("forwardedEndUserId", "unknown");
if (!String.IsNullOrEmpty(clientApplicationId) && clientApplicationId != "unknown"
&& !String.IsNullOrEmpty(endUserId) && endUserId != "unknown")
{
return "app:" + clientApplicationId + "|user:" + endUserId;
}
if (!String.IsNullOrEmpty(endUserId) && endUserId != "unknown")
{
return "user:" + endUserId;
}
return "ip:" + context.Request.IpAddress;
}" />
<azure-openai-token-limit tokens-per-minute="10000000" counter-key="@(context.Variables.GetValueOrDefault<string>("tokenCounterKey", context.Request.IpAddress))" estimate-prompt-tokens="true" tokens-consumed-header-name="consumed-tokens" remaining-tokens-header-name="remaining-tokens" />
<azure-openai-emit-token-metric>
<dimension name="API ID" />
<dimension name="Subscription ID" />
<dimension name="User ID" />
<dimension name="Product ID" />
<dimension name="Deployment ID" value="@(context.Variables.GetValueOrDefault<string>("deploymentId", "unknown"))" />
<dimension name="End User ID" value="@(context.Variables.GetValueOrDefault<string>("forwardedEndUserId", "unknown"))" />
<dimension name="End User Tenant ID" value="@(context.Variables.GetValueOrDefault<string>("forwardedEndUserTenantId", "unknown"))" />
<dimension name="Client Application ID" value="@(context.Variables.GetValueOrDefault<string>("clientApplicationId", "unknown"))" />
<dimension name="Identity Source" value="@(context.Variables.GetValueOrDefault<string>("identitySource", "unknown"))" />
</azure-openai-emit-token-metric>
<set-header name="x-apim-end-user-id" exists-action="delete" />
<set-header name="x-apim-end-user-tenant-id" exists-action="delete" />
<set-header name="x-apim-client-application-id" exists-action="delete" />
<set-header name="x-apim-end-user-jwt" exists-action="delete" />
<authentication-managed-identity resource="https://cognitiveservices.azure.com/" />
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>