forked from dapr/components-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalr.go
309 lines (265 loc) · 8.89 KB
/
signalr.go
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package signalr
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/dapr/components-contrib/bindings"
azauth "github.com/dapr/components-contrib/internal/authentication/azure"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/kit/logger"
)
const (
connectionStringKey = "connectionString"
accessKeyKey = "accessKey"
endpointKey = "endpoint"
hubKey = "hub"
// Invoke metadata keys.
groupKey = "group"
userKey = "user"
)
// Metadata keys.
// Azure AD credentials are parsed separately and not listed here.
type SignalRMetadata struct {
Endpoint string `mapstructure:"endpoint"`
AccessKey string `mapstructure:"accessKey"`
Hub string `mapstructure:"hub"`
ConnectionString string `mapstructure:"connectionString"`
}
// Global HTTP client
var httpClient *http.Client
func init() {
httpClient = &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
},
}
}
// NewSignalR creates a new output binding for Azure SignalR.
func NewSignalR(logger logger.Logger) bindings.OutputBinding {
return &SignalR{
logger: logger,
httpClient: httpClient,
}
}
// SignalR is an output binding for Azure SignalR.
type SignalR struct {
endpoint string
accessKey string
hub string
userAgent string
aadToken azcore.TokenCredential
httpClient *http.Client
logger logger.Logger
}
// Init is responsible for initializing the SignalR output based on the metadata.
func (s *SignalR) Init(_ context.Context, metadata bindings.Metadata) (err error) {
s.userAgent = "dapr-" + logger.DaprVersion
err = s.parseMetadata(metadata.Properties)
if err != nil {
return err
}
// If using AAD for authentication, init the token provider
if s.accessKey == "" {
var settings azauth.EnvironmentSettings
settings, err = azauth.NewEnvironmentSettings(metadata.Properties)
if err != nil {
return err
}
s.aadToken, err = settings.GetTokenCredential()
if err != nil {
return err
}
}
return nil
}
func (s *SignalR) parseMetadata(md map[string]string) (err error) {
m := SignalRMetadata{}
err = metadata.DecodeMetadata(md, &m)
if err != nil {
return err
}
// Start by parsing the connection string if present
if m.ConnectionString != "" {
// Expected options:
// Access key: "Endpoint=https://<servicename>.service.signalr.net;AccessKey=<access key>;Version=1.0;"
// System-assigned managed identity: "Endpoint=https://<servicename>.service.signalr.net;AuthType=aad;Version=1.0;"
// User-assigned managed identity: "Endpoint=https://<servicename>.service.signalr.net;AuthType=aad;ClientId=<clientid>;Version=1.0;"
// Azure AD application: "Endpoint=https://<servicename>.service.signalr.net;AuthType=aad;ClientId=<clientid>;ClientSecret=<clientsecret>;TenantId=<tenantid>;Version=1.0;"
// Note: connection string can't be used if the client secret contains the ; key
connectionValues := strings.Split(strings.TrimSpace(m.ConnectionString), ";")
useAAD := false
for _, connectionValue := range connectionValues {
if i := strings.Index(connectionValue, "="); i != -1 && len(connectionValue) > (i+1) {
k := connectionValue[0:i]
switch k {
case "Endpoint":
s.endpoint = connectionValue[i+1:]
case "AccessKey":
s.accessKey = connectionValue[i+1:]
case "AuthType":
if connectionValue[i+1:] != "aad" {
return fmt.Errorf("invalid value for AuthType in the connection string; only 'aad' is supported")
}
useAAD = true
case "ClientId", "ClientSecret", "TenantId":
v := connectionValue[i+1:]
// Set the values in the metadata map so they can be picked up by the azauth module
md["azure"+k] = v
case "Version":
v := connectionValue[i+1:]
// We only support version "1.0"
if v != "1.0" {
return fmt.Errorf("invalid value for Version in the connection string: '%s'; only version '1.0' is supported", v)
}
}
} else if len(connectionValue) != 0 {
return fmt.Errorf("the connection string is invalid or malformed")
}
}
// Check here because if we use a connection string, we'd have an explicit "AuthType=aad" option
// We would otherwise catch this issue later, but here we can be more explicit with the error
if s.accessKey == "" && !useAAD {
return fmt.Errorf("missing AccessKey in the connection string")
}
}
// Parse the other metadata keys, which could also override the values from the connection string
if m.Hub != "" {
s.hub = m.Hub
}
if m.Endpoint != "" {
s.endpoint = m.Endpoint
}
if m.AccessKey != "" {
s.accessKey = m.AccessKey
}
// Trim ending "/" from endpoint
s.endpoint = strings.TrimSuffix(s.endpoint, "/")
// Check for required values
if s.endpoint == "" {
return fmt.Errorf("missing endpoint in the metadata or connection string")
}
return nil
}
func (s *SignalR) resolveAPIURL(req *bindings.InvokeRequest) (string, error) {
hub, ok := req.Metadata[hubKey]
if !ok || hub == "" {
hub = s.hub
}
if hub == "" {
return "", errors.New("missing hub")
}
// Hub name is lower-cased in the official SDKs (e.g. .NET)
hub = strings.ToLower(hub)
var url string
if group, ok := req.Metadata[groupKey]; ok && group != "" {
url = fmt.Sprintf("%s/api/v1/hubs/%s/groups/%s", s.endpoint, hub, group)
} else if user, ok := req.Metadata[userKey]; ok && user != "" {
url = fmt.Sprintf("%s/api/v1/hubs/%s/users/%s", s.endpoint, hub, user)
} else {
url = fmt.Sprintf("%s/api/v1/hubs/%s", s.endpoint, hub)
}
return url, nil
}
func (s *SignalR) sendMessageToSignalR(ctx context.Context, url string, token string, data []byte) error {
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
return err
}
httpReq.Header.Set("Authorization", "Bearer "+token)
httpReq.Header.Set("Content-Type", "application/json; charset=utf-8")
httpReq.Header.Set("User-Agent", s.userAgent)
resp, err := s.httpClient.Do(httpReq)
if err != nil {
return fmt.Errorf("request to azure signalr api failed: %w", err)
}
defer resp.Body.Close()
// Read the body regardless to drain it and ensure the connection can be reused
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("azure signalr failed with code %d, content is '%s'", resp.StatusCode, string(body))
}
s.logger.Debugf("azure signalr call to '%s' completed with code %d", url, resp.StatusCode)
return nil
}
func (s *SignalR) Operations() []bindings.OperationKind {
return []bindings.OperationKind{bindings.CreateOperation}
}
func (s *SignalR) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
url, err := s.resolveAPIURL(req)
if err != nil {
return nil, err
}
token, err := s.getToken(ctx, url)
if err != nil {
return nil, err
}
err = s.sendMessageToSignalR(ctx, url, token, req.Data)
if err != nil {
return nil, err
}
return nil, nil
}
// Returns an access token for a request to the given URL
func (s *SignalR) getToken(ctx context.Context, url string) (string, error) {
var err error
// If we have an Azure AD token provider, use that first
if s.aadToken != nil {
var at azcore.AccessToken
at, err = s.aadToken.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string{"https://signalr.azure.com/.default"},
})
if err != nil {
return "", err
}
return at.Token, nil
}
now := time.Now()
token, err := jwt.NewBuilder().
Audience([]string{url}).
Expiration(now.Add(15 * time.Minute)).
Build()
if err != nil {
return "", fmt.Errorf("failed to build token: %w", err)
}
signed, err := jwt.Sign(token, jwt.WithKey(jwa.HS256, []byte(s.accessKey)))
if err != nil {
return "", fmt.Errorf("failed to sign token: %w", err)
}
return string(signed), nil
}
// GetComponentMetadata returns the metadata of the component.
func (s *SignalR) GetComponentMetadata() map[string]string {
metadataStruct := SignalRMetadata{}
metadataInfo := map[string]string{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.BindingType)
return metadataInfo
}