Skip to content

Commit 609bc24

Browse files
authored
init webpubsub
1 parent af1c101 commit 609bc24

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: SignalR Application Firewall (Preview)
3+
description: An introduction about why and how to setup Application Firewall for Azure SignalR service
4+
author: biqian
5+
ms.service: signalr
6+
ms.custom: devx-track-azurecli
7+
ms.topic: how-to
8+
ms.date: 07/10/2024
9+
ms.author: biqian
10+
---
11+
# Application Firewall for Azure SignalR Service
12+
13+
The Application Firewall provides sophisticated control over client connections in a distributed system. Before diving into its functionality and setup, let's clarify what the Application Firewall does not do:
14+
15+
1. It does not replace authentication. The firewall operates behind the client connection authentication layer.
16+
2. It is not related to network layer access control.
17+
18+
## What Does the Application Firewall Do?
19+
20+
The Application Firewall consists of various rule lists. Currently, there is a rule list called *Client Connection Count Rules*. Future updates will support more rule lists to control aspects like connection lifetime and message throughput.
21+
22+
This guideline is divided into three parts:
23+
1. Introduction to different application firewall rules.
24+
2. Instructions on configuring the rules using the Portal or Bicep on the SignalR service side.
25+
3. Steps to configure the token on the server side.
26+
27+
## Prerequisites
28+
29+
* An Azure SignalR Service in [Premium tier](https://azure.microsoft.com/pricing/details/signalr-service/).
30+
31+
## Client Connection Count Rules
32+
Client Connection Count Rules restrict concurrent client connections. When a client attempts to establish a new connection, the rules are checked **sequentially**. If any rule is violated, the connection is rejected with a status code 429.
33+
34+
### Rule introduction
35+
36+
#### ThrottleByUserIdRule
37+
This rule limits the concurrent connections of a user. For example, if a user opens multiple browser tabs or logs in using different devices, you can use this rule to restrict the number of concurrent connections for that user.
38+
39+
**Note:** The UserId must exist in the access token for this rule to work. Refer to [Configure access token](#configure-access-token).
40+
41+
42+
#### ThrottleByJwtSignatureRule
43+
This rule limits the concurrent connections of the same token to prevent malicious users from reusing tokens to establish infinite connections, which can exhaust connection quota.
44+
45+
**Note:** It's not guaranteed by default that tokens generated by the SDK are different each time. Though each token contaisn a timestamp, this timestamp might be the same if vast tokens are generated within seconds. To avoid identical tokens, insert a random claim into the token claims. Refer to [Configure access token](#configure-access-token).
46+
47+
48+
#### ThrottleByJwtCustomClaimRule
49+
50+
More advancedly, connections could be grouped into different groups according to custom claim. Connections with the same claim will be aggregated to do the check. For example, you could add a *ThrottleByJwtCustomClaimRule* to allow 5 concurrent connections for those with custom claim key "freeUser".
51+
52+
**Key point**: The rule applies to all claims with a certain claim name. The connection count aggregation is on the same claim (including claim name and claim value). The *ThrottleByUserIdRule* is a special case of this rule, applying to all connections with the userIdentity claim.
53+
54+
55+
56+
### Best Practice
57+
#### Avoid using too aggressive maxCount
58+
59+
Client connections may close without completing the tcp handshake. SignalR service can't detect those "half-closed" connections immediately. The connection is taken as active until the heartbeart failure. Therefore, aggressive throttling strategies might unexpectedly throttle clients. A smoother approach is to **leave some buffer** for the connection count, for example: double the *maxCount*.
60+
61+
62+
63+
## Setup Application Firewall
64+
65+
# [Portal](#tab/Portal)
66+
To use Application Firewall, navigate to the SignalR **Application Firewall** blade on the Azure portal and click **Add** to add a rule.
67+
68+
![Screenshot of creating replica for Azure SignalR on Portal.](./media/signalr-howto-config-application-firewall/add-application-firewall-rule.png "Add rule")
69+
70+
# [Bicep](#tab/Bicep)
71+
72+
Use Visual Studio Code or your favorite editor to create a file with the following content and name it main.bicep:
73+
74+
```bicep
75+
@description('The name for your SignalR service')
76+
param resourceName string = 'contoso'
77+
78+
resource signalr 'Microsoft.SignalRService/signalr@2024-04-01-preview' = {
79+
name: resourceName
80+
properties: {
81+
applicationFirewall:{
82+
clientConnectionCountRules:[
83+
// Add or remove rules as needed
84+
{
85+
// This rule will be skipped if no userId is set
86+
type: 'ThrottleByUserIdRule'
87+
maxCount: 5
88+
}
89+
{
90+
type: 'ThrottleByJwtSignatureRule'
91+
maxCount: 10
92+
}
93+
{
94+
// This rule will be skipped if no freeUser claim is set
95+
type: 'ThrottleByJwtCustomClaimRule'
96+
maxCount: 10
97+
claimName: 'freeUser'
98+
}
99+
{
100+
// This rule will be skipped if no paidUser claim is set
101+
type: 'ThrottleByJwtCustomClaimRule'
102+
maxCount: 100
103+
claimName: 'paidUser'
104+
}
105+
]
106+
}
107+
}
108+
}
109+
110+
```
111+
112+
Deploy the Bicep file using Azure CLI
113+
```azurecli
114+
az deployment group create --resource-group MyResourceGroup --template-file main.bicep
115+
```
116+
117+
----
118+
119+
120+
121+
## Configure access token
122+
The application firewall rules only take effect when the access token contains the corresponding claim. A rule will be **skipped** if the connection does not have the corresponding claim.
123+
124+
Below is an example to add userId or custom claim in the access token in **Default Mode**:
125+
126+
```cs
127+
services.AddSignalR().AddAzureSignalR(options =>
128+
{
129+
// Add necessary claims according to your rules.
130+
options.ClaimsProvider = context => new[]
131+
{
132+
// Add UserId: Used in ThrottleByUserIdRule
133+
new Claim(ClaimTypes.NameIdentifier, context.Request.Query["username"]),
134+
135+
// Add unique claim: Ensure uniqueness when using ThrottleByJwtSignatureRule.
136+
// The token name is not important. You could change it as you like.
137+
new Claim("uniqueToken", Guid.NewGuid().ToString()),
138+
139+
// Cutom claim: Used in ThrottleByJwtCustomClaimRule
140+
new Claim("<Custom Claim Name>", "<Custom Claim Value>"),
141+
// Custom claim example
142+
new Claim("freeUser", context.Request.Query["username"]),
143+
};
144+
});
145+
```
146+
The logic for **Serverless Mode** is similar.
147+
148+
For more details, refer to [Client negotiation](signalr-concept-client-negotiation.md#What-can-you-do-during-negotiation) .
149+
150+
151+
152+
153+

articles/azure-web-pubsub/media/howto-config-application-firewall/add-application-firewall-rule.png

Loading

0 commit comments

Comments
 (0)