• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nats-io / nats-server / 26016392020

15 May 2026 03:37PM UTC coverage: 83.064% (+2.1%) from 80.996%
26016392020

push

github

web-flow
Don't negotiate leafnode compression over WebSockets (#7969)

WebSockets already have compression, so this would be wasteful and
likely counter-productive to do so twice.

Signed-off-by: Neil Twigg <neil@nats.io>

77524 of 93331 relevant lines covered (83.06%)

541881.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.02
/server/leafnode.go
1
// Copyright 2019-2026 The NATS Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package server
15

16
import (
17
        "bufio"
18
        "bytes"
19
        "crypto/tls"
20
        "encoding/base64"
21
        "encoding/json"
22
        "fmt"
23
        "io"
24
        "math/rand"
25
        "net"
26
        "net/http"
27
        "net/url"
28
        "os"
29
        "path"
30
        "regexp"
31
        "runtime"
32
        "strconv"
33
        "strings"
34
        "sync"
35
        "sync/atomic"
36
        "time"
37

38
        "github.com/klauspost/compress/s2"
39
        "github.com/nats-io/jwt/v2"
40
        "github.com/nats-io/nkeys"
41
        "github.com/nats-io/nuid"
42
)
43

44
const (
45
        // Warning when user configures leafnode TLS insecure
46
        leafnodeTLSInsecureWarning = "TLS certificate chain and hostname of solicited leafnodes will not be verified. DO NOT USE IN PRODUCTION!"
47

48
        // When a loop is detected, delay the reconnect of solicited connection.
49
        leafNodeReconnectDelayAfterLoopDetected = 30 * time.Second
50

51
        // When a server receives a message causing a permission violation, the
52
        // connection is closed and it won't attempt to reconnect for that long.
53
        leafNodeReconnectAfterPermViolation = 30 * time.Second
54

55
        // When we have the same cluster name as the hub.
56
        leafNodeReconnectDelayAfterClusterNameSame = 30 * time.Second
57

58
        // Prefix for loop detection subject
59
        leafNodeLoopDetectionSubjectPrefix = "$LDS."
60

61
        // Path added to URL to indicate to WS server that the connection is a
62
        // LEAF connection as opposed to a CLIENT.
63
        leafNodeWSPath = "/leafnode"
64

65
        // When a soliciting leafnode is rejected because it does not meet the
66
        // configured minimum version, delay the next reconnect attempt by this long.
67
        leafNodeMinVersionReconnectDelay = 5 * time.Second
68
)
69

70
type leaf struct {
71
        // We have any auth stuff here for solicited connections.
72
        remote *leafNodeCfg
73
        // isSpoke tells us what role we are playing.
74
        // Used when we receive a connection but otherside tells us they are a hub.
75
        isSpoke bool
76
        // remoteCluster is when we are a hub but the spoke leafnode is part of a cluster.
77
        remoteCluster string
78
        // remoteServer holds onto the remote server's name or ID.
79
        remoteServer string
80
        // domain name of remote server
81
        remoteDomain string
82
        // account name of remote server
83
        remoteAccName string
84
        // Whether or not we want to propagate east-west interest from other LNs.
85
        isolated bool
86
        // Used to suppress sub and unsub interest. Same as routes but our audience
87
        // here is tied to this leaf node. This will hold all subscriptions except this
88
        // leaf nodes. This represents all the interest we want to send to the other side.
89
        smap map[string]int32
90
        // This map will contain all the subscriptions that have been added to the smap
91
        // during initLeafNodeSmapAndSendSubs. It is short lived and is there to avoid
92
        // race between processing of a sub where sub is added to account sublist but
93
        // updateSmap has not be called on that "thread", while in the LN readloop,
94
        // when processing CONNECT, initLeafNodeSmapAndSendSubs is invoked and add
95
        // this subscription to smap. When processing of the sub then calls updateSmap,
96
        // we would add it a second time in the smap causing later unsub to suppress the LS-.
97
        tsub  map[*subscription]struct{}
98
        tsubt *time.Timer
99
        // Selected compression mode, which may be different from the server configured mode.
100
        compression string
101
        // This is for GW map replies.
102
        gwSub *subscription
103
}
104

105
// Used for remote (solicited) leafnodes.
106
type leafNodeCfg struct {
107
        sync.RWMutex
108
        *RemoteLeafOpts
109
        urls           []*url.URL
110
        curURL         *url.URL
111
        tlsName        string
112
        username       string
113
        password       string
114
        perms          *Permissions
115
        connDelay      time.Duration // Delay before a connect, could be used while detecting loop condition, etc..
116
        jsMigrateTimer *time.Timer
117
        quitCh         chan struct{}
118
        removed        bool
119
        connInProgress bool
120
}
121

122
// Check to see if this is a solicited leafnode. We do special processing for solicited.
123
func (c *client) isSolicitedLeafNode() bool {
2,181✔
124
        return c.kind == LEAF && c.leaf != nil && c.leaf.remote != nil
2,181✔
125
}
2,181✔
126

127
// Returns true if this is a solicited leafnode and is not configured to be treated as a hub or a receiving
128
// connection leafnode where the otherside has declared itself to be the hub.
129
func (c *client) isSpokeLeafNode() bool {
11,457,564✔
130
        return c.kind == LEAF && c.leaf != nil && c.leaf.isSpoke
11,457,564✔
131
}
11,457,564✔
132

133
func (c *client) isHubLeafNode() bool {
17,846✔
134
        return c.kind == LEAF && c.leaf != nil && !c.leaf.isSpoke
17,846✔
135
}
17,846✔
136

137
func (c *client) isIsolatedLeafNode() bool {
11,183✔
138
        // TODO(nat): In future we may want to pass in and consider an isolation
11,183✔
139
        // group name here, which the hub and/or leaf could provide, so that we
11,183✔
140
        // can isolate away certain LNs but not others on an opt-in basis. For
11,183✔
141
        // now we will just isolate all LN interest until then.
11,183✔
142
        return c.kind == LEAF && c.leaf != nil && c.leaf.isolated
11,183✔
143
}
11,183✔
144

145
// This will spin up go routines to solicit the remote leaf node connections.
146
func (s *Server) solicitLeafNodeRemotes(remotes []*RemoteLeafOpts) {
1,297✔
147
        sysAccName := _EMPTY_
1,297✔
148
        sAcc := s.SystemAccount()
1,297✔
149
        if sAcc != nil {
2,571✔
150
                sysAccName = sAcc.Name
1,274✔
151
        }
1,274✔
152
        addRemote := func(r *RemoteLeafOpts, isSysAccRemote bool) *leafNodeCfg {
2,743✔
153
                s.mu.Lock()
1,446✔
154
                remote := newLeafNodeCfg(r)
1,446✔
155
                creds := remote.Credentials
1,446✔
156
                accName := remote.LocalAccount
1,446✔
157
                if s.leafRemoteCfgs == nil {
2,742✔
158
                        s.leafRemoteCfgs = make(map[*leafNodeCfg]struct{})
1,296✔
159
                }
1,296✔
160
                s.leafRemoteCfgs[remote] = struct{}{}
1,446✔
161
                // Print notice if
1,446✔
162
                if isSysAccRemote {
1,543✔
163
                        if len(remote.DenyExports) > 0 {
98✔
164
                                s.Noticef("Remote for System Account uses restricted export permissions")
1✔
165
                        }
1✔
166
                        if len(remote.DenyImports) > 0 {
98✔
167
                                s.Noticef("Remote for System Account uses restricted import permissions")
1✔
168
                        }
1✔
169
                }
170
                s.mu.Unlock()
1,446✔
171
                if creds != _EMPTY_ {
1,498✔
172
                        contents, err := os.ReadFile(creds)
52✔
173
                        defer wipeSlice(contents)
52✔
174
                        if err != nil {
52✔
175
                                s.Errorf("Error reading LeafNode Remote Credentials file %q: %v", creds, err)
×
176
                        } else if items := credsRe.FindAllSubmatch(contents, -1); len(items) < 2 {
52✔
177
                                s.Errorf("LeafNode Remote Credentials file %q malformed", creds)
×
178
                        } else if _, err := nkeys.FromSeed(items[1][1]); err != nil {
52✔
179
                                s.Errorf("LeafNode Remote Credentials file %q has malformed seed", creds)
×
180
                        } else if uc, err := jwt.DecodeUserClaims(string(items[0][1])); err != nil {
52✔
181
                                s.Errorf("LeafNode Remote Credentials file %q has malformed user jwt", creds)
×
182
                        } else if isSysAccRemote {
56✔
183
                                if !uc.Permissions.Pub.Empty() || !uc.Permissions.Sub.Empty() || uc.Permissions.Resp != nil {
5✔
184
                                        s.Noticef("LeafNode Remote for System Account uses credentials file %q with restricted permissions", creds)
1✔
185
                                }
1✔
186
                        } else {
48✔
187
                                if !uc.Permissions.Pub.Empty() || !uc.Permissions.Sub.Empty() || uc.Permissions.Resp != nil {
54✔
188
                                        s.Noticef("LeafNode Remote for Account %s uses credentials file %q with restricted permissions", accName, creds)
6✔
189
                                }
6✔
190
                        }
191
                }
192
                return remote
1,446✔
193
        }
194
        for _, r := range remotes {
2,743✔
195
                // We need to call this, even if the leaf is disabled. This is so that
1,446✔
196
                // the number of internal configuration matches the options' remote leaf
1,446✔
197
                // configuration required for configuration reload.
1,446✔
198
                remote := addRemote(r, r.LocalAccount == sysAccName)
1,446✔
199
                if !r.Disabled {
2,891✔
200
                        s.connectToRemoteLeafNodeAsynchronously(remote, true)
1,445✔
201
                }
1,445✔
202
        }
203
}
204

205
// Ensure that leafnode is properly configured.
206
func validateLeafNode(o *Options) error {
8,472✔
207
        if err := validateLeafNodeAuthOptions(o); err != nil {
8,474✔
208
                return err
2✔
209
        }
2✔
210

211
        if len(o.LeafNode.Remotes) > 0 {
9,811✔
212
                names := make(map[string]struct{})
1,341✔
213
                // Check for duplicate remotes, also, users can bind to any local account,
1,341✔
214
                // if its empty we will assume the $G account.
1,341✔
215
                for _, r := range o.LeafNode.Remotes {
2,842✔
216
                        if r.LocalAccount == _EMPTY_ {
1,957✔
217
                                r.LocalAccount = globalAccountName
456✔
218
                        }
456✔
219
                        rn := r.name()
1,501✔
220
                        if _, dup := names[rn]; dup {
1,504✔
221
                                return fmt.Errorf("duplicate remote %s", r.safeName())
3✔
222
                        }
3✔
223
                        names[rn] = struct{}{}
1,498✔
224
                }
225
        }
226

227
        // In local config mode, check that leafnode configuration refers to accounts that exist.
228
        if len(o.TrustedOperators) == 0 {
16,613✔
229
                accNames := map[string]struct{}{}
8,146✔
230
                for _, a := range o.Accounts {
17,121✔
231
                        accNames[a.Name] = struct{}{}
8,975✔
232
                }
8,975✔
233
                // global account is always created
234
                accNames[DEFAULT_GLOBAL_ACCOUNT] = struct{}{}
8,146✔
235
                // in the context of leaf nodes, empty account means global account
8,146✔
236
                accNames[_EMPTY_] = struct{}{}
8,146✔
237
                // system account either exists or, if not disabled, will be created
8,146✔
238
                if o.SystemAccount == _EMPTY_ && !o.NoSystemAccount {
14,724✔
239
                        accNames[DEFAULT_SYSTEM_ACCOUNT] = struct{}{}
6,578✔
240
                }
6,578✔
241
                checkAccountExists := func(accName string, cfgType string) error {
17,790✔
242
                        if _, ok := accNames[accName]; !ok {
9,646✔
243
                                return fmt.Errorf("cannot find local account %q specified in leafnode %s", accName, cfgType)
2✔
244
                        }
2✔
245
                        return nil
9,642✔
246
                }
247
                if err := checkAccountExists(o.LeafNode.Account, "authorization"); err != nil {
8,147✔
248
                        return err
1✔
249
                }
1✔
250
                for _, lu := range o.LeafNode.Users {
8,162✔
251
                        if lu.Account == nil { // means global account
27✔
252
                                continue
10✔
253
                        }
254
                        if err := checkAccountExists(lu.Account.Name, "authorization"); err != nil {
7✔
255
                                return err
×
256
                        }
×
257
                }
258
                for _, r := range o.LeafNode.Remotes {
9,636✔
259
                        if err := checkAccountExists(r.LocalAccount, "remote"); err != nil {
1,492✔
260
                                return err
1✔
261
                        }
1✔
262
                }
263
        } else {
321✔
264
                if len(o.LeafNode.Users) != 0 {
322✔
265
                        return fmt.Errorf("operator mode does not allow specifying users in leafnode config")
1✔
266
                }
1✔
267
                for _, r := range o.LeafNode.Remotes {
321✔
268
                        if !nkeys.IsValidPublicAccountKey(r.LocalAccount) {
2✔
269
                                return fmt.Errorf(
1✔
270
                                        "operator mode requires account nkeys in remotes. " +
1✔
271
                                                "Please add an `account` key to each remote in your `leafnodes` section, to assign it to an account. " +
1✔
272
                                                "Each account value should be a 56 character public key, starting with the letter 'A'")
1✔
273
                        }
1✔
274
                }
275
                if o.LeafNode.Port != 0 && o.LeafNode.Account != "" && !nkeys.IsValidPublicAccountKey(o.LeafNode.Account) {
320✔
276
                        return fmt.Errorf("operator mode and non account nkeys are incompatible")
1✔
277
                }
1✔
278
        }
279

280
        // Validate compression settings
281
        if o.LeafNode.Compression.Mode != _EMPTY_ {
13,141✔
282
                if err := validateAndNormalizeCompressionOption(&o.LeafNode.Compression, CompressionS2Auto); err != nil {
4,684✔
283
                        return err
5✔
284
                }
5✔
285
        }
286

287
        // If a remote has a websocket scheme, all need to have it.
288
        for _, rcfg := range o.LeafNode.Remotes {
9,947✔
289
                // Validate proxy configuration
1,490✔
290
                if _, err := validateLeafNodeProxyOptions(rcfg); err != nil {
1,496✔
291
                        return err
6✔
292
                }
6✔
293

294
                if len(rcfg.URLs) >= 2 {
1,692✔
295
                        firstIsWS, ok := isWSURL(rcfg.URLs[0]), true
208✔
296
                        for i := 1; i < len(rcfg.URLs); i++ {
653✔
297
                                u := rcfg.URLs[i]
445✔
298
                                if isWS := isWSURL(u); isWS && !firstIsWS || !isWS && firstIsWS {
452✔
299
                                        ok = false
7✔
300
                                        break
7✔
301
                                }
302
                        }
303
                        if !ok {
215✔
304
                                return fmt.Errorf("remote leaf node configuration cannot have a mix of websocket and non-websocket urls: %q", redactURLList(rcfg.URLs))
7✔
305
                        }
7✔
306
                }
307
                if !wsAllowedFIPS() {
1,477✔
308
                        for _, u := range rcfg.URLs {
×
309
                                if isWSURL(u) {
×
310
                                        return fmt.Errorf("remote leaf node URL %q cannot be used in FIPS-140 mode when built with this Go version, use Go 1.26 or later", redactURLString(u.String()))
×
311
                                }
×
312
                        }
313
                }
314
                // Validate compression settings
315
                if rcfg.Compression.Mode != _EMPTY_ {
2,948✔
316
                        if err := validateAndNormalizeCompressionOption(&rcfg.Compression, CompressionS2Auto); err != nil {
1,476✔
317
                                return err
5✔
318
                        }
5✔
319
                }
320
        }
321

322
        if o.LeafNode.Port == 0 {
12,808✔
323
                return nil
4,369✔
324
        }
4,369✔
325

326
        // If MinVersion is defined, check that it is valid.
327
        if mv := o.LeafNode.MinVersion; mv != _EMPTY_ {
4,074✔
328
                if err := checkLeafMinVersionConfig(mv); err != nil {
6✔
329
                        return err
2✔
330
                }
2✔
331
        }
332

333
        // The checks below will be done only when detecting that we are configured
334
        // with gateways. So if an option validation needs to be done regardless,
335
        // it MUST be done before this point!
336

337
        if o.Gateway.Name == _EMPTY_ && o.Gateway.Port == 0 {
7,451✔
338
                return nil
3,383✔
339
        }
3,383✔
340
        // If we are here we have both leaf nodes and gateways defined, make sure there
341
        // is a system account defined.
342
        if o.SystemAccount == _EMPTY_ {
686✔
343
                return fmt.Errorf("leaf nodes and gateways (both being defined) require a system account to also be configured")
1✔
344
        }
1✔
345
        if err := validatePinnedCerts(o.LeafNode.TLSPinnedCerts); err != nil {
684✔
346
                return fmt.Errorf("leafnode: %v", err)
×
347
        }
×
348
        return nil
684✔
349
}
350

351
func checkLeafMinVersionConfig(mv string) error {
8✔
352
        if ok, err := versionAtLeastCheckError(mv, 2, 8, 0); !ok || err != nil {
12✔
353
                if err != nil {
6✔
354
                        return fmt.Errorf("invalid leafnode's minimum version: %v", err)
2✔
355
                } else {
4✔
356
                        return fmt.Errorf("the minimum version should be at least 2.8.0")
2✔
357
                }
2✔
358
        }
359
        return nil
4✔
360
}
361

362
// Used to validate user names in LeafNode configuration.
363
// - rejects mix of single and multiple users.
364
// - rejects duplicate user names.
365
func validateLeafNodeAuthOptions(o *Options) error {
8,534✔
366
        if len(o.LeafNode.Users) == 0 {
17,041✔
367
                return nil
8,507✔
368
        }
8,507✔
369
        if o.LeafNode.Username != _EMPTY_ {
29✔
370
                return fmt.Errorf("can not have a single user/pass and a users array")
2✔
371
        }
2✔
372
        if o.LeafNode.Nkey != _EMPTY_ {
25✔
373
                return fmt.Errorf("can not have a single nkey and a users array")
×
374
        }
×
375
        users := map[string]struct{}{}
25✔
376
        for _, u := range o.LeafNode.Users {
66✔
377
                if _, exists := users[u.Username]; exists {
43✔
378
                        return fmt.Errorf("duplicate user %q detected in leafnode authorization", u.Username)
2✔
379
                }
2✔
380
                users[u.Username] = struct{}{}
39✔
381
        }
382
        return nil
23✔
383
}
384

385
func validateLeafNodeProxyOptions(remote *RemoteLeafOpts) ([]string, error) {
2,107✔
386
        var warnings []string
2,107✔
387

2,107✔
388
        if remote.Proxy.URL == _EMPTY_ {
4,188✔
389
                return warnings, nil
2,081✔
390
        }
2,081✔
391

392
        proxyURL, err := url.Parse(remote.Proxy.URL)
26✔
393
        if err != nil {
27✔
394
                return warnings, fmt.Errorf("invalid proxy URL: %v", err)
1✔
395
        }
1✔
396

397
        if proxyURL.Scheme != "http" && proxyURL.Scheme != "https" {
27✔
398
                return warnings, fmt.Errorf("proxy URL scheme must be http or https, got: %s", proxyURL.Scheme)
2✔
399
        }
2✔
400

401
        if proxyURL.Host == _EMPTY_ {
25✔
402
                return warnings, fmt.Errorf("proxy URL must specify a host")
2✔
403
        }
2✔
404

405
        if remote.Proxy.Timeout < 0 {
22✔
406
                return warnings, fmt.Errorf("proxy timeout must be >= 0")
1✔
407
        }
1✔
408

409
        if (remote.Proxy.Username == _EMPTY_) != (remote.Proxy.Password == _EMPTY_) {
24✔
410
                return warnings, fmt.Errorf("proxy username and password must both be specified or both be empty")
4✔
411
        }
4✔
412

413
        if len(remote.URLs) > 0 {
32✔
414
                hasWebSocketURL := false
16✔
415
                hasNonWebSocketURL := false
16✔
416

16✔
417
                for _, remoteURL := range remote.URLs {
33✔
418
                        if remoteURL.Scheme == wsSchemePrefix || remoteURL.Scheme == wsSchemePrefixTLS {
30✔
419
                                hasWebSocketURL = true
13✔
420
                                if (remoteURL.Scheme == wsSchemePrefixTLS) &&
13✔
421
                                        remote.TLSConfig == nil && !remote.TLS {
14✔
422
                                        return warnings, fmt.Errorf("proxy is configured but remote URL %s requires TLS and no TLS configuration is provided. When using proxy with TLS endpoints, ensure TLS is properly configured for the leafnode remote", remoteURL.String())
1✔
423
                                }
1✔
424
                        } else {
4✔
425
                                hasNonWebSocketURL = true
4✔
426
                        }
4✔
427
                }
428

429
                if !hasWebSocketURL {
18✔
430
                        warnings = append(warnings, "proxy configuration will be ignored: proxy settings only apply to WebSocket connections (ws:// or wss://), but all configured URLs use TCP connections (nats://)")
3✔
431
                } else if hasNonWebSocketURL {
16✔
432
                        warnings = append(warnings, "proxy configuration will only be used for WebSocket URLs: proxy settings do not apply to TCP connections (nats://)")
1✔
433
                }
1✔
434
        }
435

436
        return warnings, nil
15✔
437
}
438

439
// Wait for the configured reconnect interval before attempting to connect
440
// again to the remote leafnode.
441
func (s *Server) reConnectToRemoteLeafNode(remote *leafNodeCfg) {
241✔
442
        clearInProgress := true
241✔
443
        defer func() {
481✔
444
                s.grWG.Done()
240✔
445
                if clearInProgress {
307✔
446
                        remote.setConnectInProgress(false)
67✔
447
                }
67✔
448
        }()
449
        delay := s.getOpts().LeafNode.ReconnectInterval
241✔
450
        select {
241✔
451
        case <-time.After(delay):
180✔
452
        case <-remote.quitCh:
×
453
                return
×
454
        case <-s.quitCh:
61✔
455
                return
61✔
456
        }
457
        clearInProgress = !connectToRemoteLeafNode(s, remote, false)
180✔
458
}
459

460
// Creates a leafNodeCfg object that wraps the RemoteLeafOpts.
461
func newLeafNodeCfg(remote *RemoteLeafOpts) *leafNodeCfg {
1,446✔
462
        cfg := &leafNodeCfg{
1,446✔
463
                RemoteLeafOpts: remote,
1,446✔
464
                urls:           make([]*url.URL, 0, len(remote.URLs)),
1,446✔
465
                quitCh:         make(chan struct{}, 1),
1,446✔
466
        }
1,446✔
467
        if len(remote.DenyExports) > 0 || len(remote.DenyImports) > 0 {
1,456✔
468
                perms := &Permissions{}
10✔
469
                if len(remote.DenyExports) > 0 {
19✔
470
                        perms.Publish = &SubjectPermission{Deny: remote.DenyExports}
9✔
471
                }
9✔
472
                if len(remote.DenyImports) > 0 {
18✔
473
                        perms.Subscribe = &SubjectPermission{Deny: remote.DenyImports}
8✔
474
                }
8✔
475
                cfg.perms = perms
10✔
476
        }
477
        // Start with the one that is configured. We will add to this
478
        // array when receiving async leafnode INFOs.
479
        cfg.urls = append(cfg.urls, cfg.URLs...)
1,446✔
480
        // If allowed to randomize, do it on our copy of URLs
1,446✔
481
        if !remote.NoRandomize {
2,890✔
482
                rand.Shuffle(len(cfg.urls), func(i, j int) {
1,851✔
483
                        cfg.urls[i], cfg.urls[j] = cfg.urls[j], cfg.urls[i]
407✔
484
                })
407✔
485
        }
486
        // If we are TLS make sure we save off a proper servername if possible.
487
        // Do same for user/password since we may need them to connect to
488
        // a bare URL that we get from INFO protocol.
489
        for _, u := range cfg.urls {
3,329✔
490
                cfg.saveTLSHostname(u)
1,883✔
491
                cfg.saveUserPassword(u)
1,883✔
492
                // If the url(s) have the "wss://" scheme, and we don't have a TLS
1,883✔
493
                // config, mark that we should be using TLS anyway.
1,883✔
494
                if !cfg.TLS && isWSSURL(u) {
1,884✔
495
                        cfg.TLS = true
1✔
496
                }
1✔
497
        }
498
        return cfg
1,446✔
499
}
500

501
// Notifies the quit channel without blocking.
502
// No lock is needed to invoke this function.
503
func (cfg *leafNodeCfg) notifyQuitChannel() {
2✔
504
        select {
2✔
505
        case cfg.quitCh <- struct{}{}:
2✔
506
        default:
×
507
        }
508
}
509

510
// Sets the connect-in-progress status for this remote leaf configuration.
511
func (cfg *leafNodeCfg) setConnectInProgress(inProgress bool) {
3,797✔
512
        cfg.Lock()
3,797✔
513
        defer cfg.Unlock()
3,797✔
514
        // In both cases we want to drain the "quit" channel.
3,797✔
515
        select {
3,797✔
516
        case <-cfg.quitCh:
1✔
517
        default:
3,796✔
518
        }
519
        cfg.connInProgress = inProgress
3,797✔
520
}
521

522
// Returns `true` if this remote is in the middle of a connect, `false` otherwise.
523
func (cfg *leafNodeCfg) isConnectInProgress() bool {
×
524
        cfg.RLock()
×
525
        defer cfg.RUnlock()
×
526
        return cfg.connInProgress
×
527
}
×
528

529
// Mark that this remote is being removed from the configuration.
530
func (cfg *leafNodeCfg) markAsRemoved() {
×
531
        cfg.Lock()
×
532
        defer cfg.Unlock()
×
533
        // This function should be invoked only once, but protect.
×
534
        if cfg.removed {
×
535
                return
×
536
        }
×
537
        cfg.removed = true
×
538
        cfg.notifyQuitChannel()
×
539
}
540

541
// Returns false if it has been disabled or removed.
542
func (cfg *leafNodeCfg) stillValid() bool {
7,882✔
543
        cfg.RLock()
7,882✔
544
        defer cfg.RUnlock()
7,882✔
545
        return !cfg.Disabled && !cfg.removed
7,882✔
546
}
7,882✔
547

548
// Will pick an URL from the list of available URLs.
549
func (cfg *leafNodeCfg) pickNextURL() *url.URL {
6,395✔
550
        cfg.Lock()
6,395✔
551
        defer cfg.Unlock()
6,395✔
552
        // If the current URL is the first in the list and we have more than
6,395✔
553
        // one URL, then move that one to end of the list.
6,395✔
554
        if cfg.curURL != nil && len(cfg.urls) > 1 && urlsAreEqual(cfg.curURL, cfg.urls[0]) {
9,116✔
555
                first := cfg.urls[0]
2,721✔
556
                copy(cfg.urls, cfg.urls[1:])
2,721✔
557
                cfg.urls[len(cfg.urls)-1] = first
2,721✔
558
        }
2,721✔
559
        cfg.curURL = cfg.urls[0]
6,395✔
560
        return cfg.curURL
6,395✔
561
}
562

563
// Returns the current URL
564
func (cfg *leafNodeCfg) getCurrentURL() *url.URL {
73✔
565
        cfg.RLock()
73✔
566
        defer cfg.RUnlock()
73✔
567
        return cfg.curURL
73✔
568
}
73✔
569

570
// Returns how long the server should wait before attempting
571
// to solicit a remote leafnode connection.
572
func (cfg *leafNodeCfg) getConnectDelay() time.Duration {
1,627✔
573
        cfg.RLock()
1,627✔
574
        delay := cfg.connDelay
1,627✔
575
        cfg.RUnlock()
1,627✔
576
        return delay
1,627✔
577
}
1,627✔
578

579
// Sets the connect delay.
580
func (cfg *leafNodeCfg) setConnectDelay(delay time.Duration) {
157✔
581
        cfg.Lock()
157✔
582
        cfg.connDelay = delay
157✔
583
        cfg.Unlock()
157✔
584
}
157✔
585

586
// Ensure that non-exported options (used in tests) have
587
// been properly set.
588
func (s *Server) setLeafNodeNonExportedOptions() {
7,256✔
589
        opts := s.getOpts()
7,256✔
590
        s.leafNodeOpts.dialTimeout = opts.LeafNode.dialTimeout
7,256✔
591
        if s.leafNodeOpts.dialTimeout == 0 {
14,511✔
592
                // Use same timeouts as routes for now.
7,255✔
593
                s.leafNodeOpts.dialTimeout = DEFAULT_ROUTE_DIAL
7,255✔
594
        }
7,255✔
595
        s.leafNodeOpts.resolver = opts.LeafNode.resolver
7,256✔
596
        if s.leafNodeOpts.resolver == nil {
14,509✔
597
                s.leafNodeOpts.resolver = net.DefaultResolver
7,253✔
598
        }
7,253✔
599
}
600

601
const sharedSysAccDelay = 250 * time.Millisecond
602

603
// establishHTTPProxyTunnel establishes an HTTP CONNECT tunnel through a proxy server
604
func establishHTTPProxyTunnel(proxyURL, targetHost string, timeout time.Duration, username, password string) (net.Conn, error) {
11✔
605
        proxyAddr, err := url.Parse(proxyURL)
11✔
606
        if err != nil {
11✔
607
                // This should not happen since proxy URL is validated during configuration parsing
×
608
                return nil, fmt.Errorf("unexpected proxy URL parse error (URL was pre-validated): %v", err)
×
609
        }
×
610

611
        // Connect to the proxy server
612
        conn, err := natsDialTimeout("tcp", proxyAddr.Host, timeout)
11✔
613
        if err != nil {
11✔
614
                return nil, fmt.Errorf("failed to connect to proxy: %v", err)
×
615
        }
×
616

617
        // Set deadline for the entire proxy handshake
618
        if err := conn.SetDeadline(time.Now().Add(timeout)); err != nil {
11✔
619
                conn.Close()
×
620
                return nil, fmt.Errorf("failed to set deadline: %v", err)
×
621
        }
×
622

623
        req := &http.Request{
11✔
624
                Method: http.MethodConnect,
11✔
625
                URL:    &url.URL{Opaque: targetHost}, // Opaque is required for CONNECT
11✔
626
                Host:   targetHost,
11✔
627
                Header: make(http.Header),
11✔
628
        }
11✔
629

11✔
630
        // Add proxy authentication if provided
11✔
631
        if username != "" && password != "" {
13✔
632
                req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
2✔
633
        }
2✔
634

635
        if err := req.Write(conn); err != nil {
11✔
636
                conn.Close()
×
637
                return nil, fmt.Errorf("failed to write CONNECT request: %v", err)
×
638
        }
×
639

640
        resp, err := http.ReadResponse(bufio.NewReader(conn), req)
11✔
641
        if err != nil {
11✔
642
                conn.Close()
×
643
                return nil, fmt.Errorf("failed to read proxy response: %v", err)
×
644
        }
×
645

646
        if resp.StatusCode != http.StatusOK {
12✔
647
                resp.Body.Close()
1✔
648
                conn.Close()
1✔
649
                return nil, fmt.Errorf("proxy CONNECT failed: %s", resp.Status)
1✔
650
        }
1✔
651

652
        // Close the response body
653
        resp.Body.Close()
10✔
654

10✔
655
        // Clear the deadline now that we've finished the proxy handshake
10✔
656
        if err := conn.SetDeadline(time.Time{}); err != nil {
10✔
657
                conn.Close()
×
658
                return nil, fmt.Errorf("failed to clear deadline: %v", err)
×
659
        }
×
660

661
        return conn, nil
10✔
662
}
663

664
// Connect to a remote leaf node asynchronously (that is, this function will do
665
// the connect in a go routine).
666
func (s *Server) connectToRemoteLeafNodeAsynchronously(remote *leafNodeCfg, firstConnect bool) {
1,447✔
667
        remote.setConnectInProgress(true)
1,447✔
668
        s.startGoRoutine(func() {
2,894✔
669
                defer s.grWG.Done()
1,447✔
670
                if !connectToRemoteLeafNode(s, remote, firstConnect) {
2,243✔
671
                        remote.setConnectInProgress(false)
796✔
672
                }
796✔
673
        })
674
}
675

676
// Connect to a remote leaf node. Should only be invoked from
677
// `s.connectToRemoteLeafNodeAsynchronously()` or `s.reConnectToRemoteLeafNode()`.
678
// Returns `true` if this function invoked `s.createLeafNode()`, false otherwise.
679
func connectToRemoteLeafNode(s *Server, remote *leafNodeCfg, firstConnect bool) bool {
1,627✔
680

1,627✔
681
        if remote == nil || len(remote.URLs) == 0 {
1,627✔
682
                s.Debugf("Empty remote leafnode definition, nothing to connect")
×
683
                return false
×
684
        }
×
685

686
        opts := s.getOpts()
1,627✔
687
        reconnectDelay := opts.LeafNode.ReconnectInterval
1,627✔
688
        s.mu.RLock()
1,627✔
689
        dialTimeout := s.leafNodeOpts.dialTimeout
1,627✔
690
        resolver := s.leafNodeOpts.resolver
1,627✔
691
        var isSysAcc bool
1,627✔
692
        if s.eventsEnabled() {
3,220✔
693
                isSysAcc = remote.LocalAccount == s.sys.account.Name
1,593✔
694
        }
1,593✔
695
        jetstreamMigrateDelay := remote.JetStreamClusterMigrateDelay
1,627✔
696
        s.mu.RUnlock()
1,627✔
697

1,627✔
698
        // If we are sharing a system account and we are not standalone delay to gather some info prior.
1,627✔
699
        if firstConnect && isSysAcc && !s.standAloneMode() {
1,699✔
700
                s.Debugf("Will delay first leafnode connect to shared system account due to clustering")
72✔
701
                remote.setConnectDelay(sharedSysAccDelay)
72✔
702
        }
72✔
703

704
        if connDelay := remote.getConnectDelay(); connDelay > 0 {
1,704✔
705
                select {
77✔
706
                case <-time.After(connDelay):
71✔
707
                case <-remote.quitCh:
×
708
                        return false
×
709
                case <-s.quitCh:
6✔
710
                        return false
6✔
711
                }
712
                remote.setConnectDelay(0)
71✔
713
        }
714

715
        var conn net.Conn
1,621✔
716

1,621✔
717
        const connErrFmt = "Error trying to connect as leafnode to remote server %q (attempt %v): %v"
1,621✔
718

1,621✔
719
        // Capture proxy configuration once before the loop with proper locking
1,621✔
720
        remote.RLock()
1,621✔
721
        proxyURL := remote.Proxy.URL
1,621✔
722
        proxyUsername := remote.Proxy.Username
1,621✔
723
        proxyPassword := remote.Proxy.Password
1,621✔
724
        proxyTimeout := remote.Proxy.Timeout
1,621✔
725
        remote.RUnlock()
1,621✔
726

1,621✔
727
        // Set default proxy timeout if not specified
1,621✔
728
        if proxyTimeout == 0 {
3,234✔
729
                proxyTimeout = dialTimeout
1,613✔
730
        }
1,613✔
731

732
        attempts := 0
1,621✔
733

1,621✔
734
        // In case the migrate timer was created but not canceled, do it when
1,621✔
735
        // this function exits. Note that the timer would not be created if
1,621✔
736
        // `jetstreamMigrateDelay == 0`.
1,621✔
737
        if jetstreamMigrateDelay > 0 {
1,629✔
738
                defer remote.cancelMigrateTimer()
8✔
739
        }
8✔
740

741
        for s.isRunning() && remote.stillValid() {
8,016✔
742
                rURL := remote.pickNextURL()
6,395✔
743
                url, err := s.getRandomIP(resolver, rURL.Host, nil)
6,395✔
744
                if err == nil {
12,783✔
745
                        var ipStr string
6,388✔
746
                        if url != rURL.Host {
6,452✔
747
                                ipStr = fmt.Sprintf(" (%s)", url)
64✔
748
                        }
64✔
749
                        // Some test may want to disable remotes from connecting
750
                        if s.isLeafConnectDisabled() {
6,519✔
751
                                s.Debugf("Will not attempt to connect to remote server on %q%s, leafnodes currently disabled", rURL.Host, ipStr)
131✔
752
                                err = ErrLeafNodeDisabled
131✔
753
                        } else {
6,388✔
754
                                s.Debugf("Trying to connect as leafnode to remote server on %q%s", rURL.Host, ipStr)
6,257✔
755

6,257✔
756
                                // Check if proxy is configured
6,257✔
757
                                if proxyURL != _EMPTY_ {
6,265✔
758
                                        targetHost := rURL.Host
8✔
759
                                        // If URL doesn't include port, add the default port for the scheme
8✔
760
                                        if rURL.Port() == _EMPTY_ {
8✔
761
                                                defaultPort := "80"
×
762
                                                if rURL.Scheme == wsSchemePrefixTLS {
×
763
                                                        defaultPort = "443"
×
764
                                                }
×
765
                                                targetHost = net.JoinHostPort(rURL.Hostname(), defaultPort)
×
766
                                        }
767

768
                                        conn, err = establishHTTPProxyTunnel(proxyURL, targetHost, proxyTimeout, proxyUsername, proxyPassword)
8✔
769
                                } else {
6,249✔
770
                                        // Direct connection
6,249✔
771
                                        conn, err = natsDialTimeout("tcp", url, dialTimeout)
6,249✔
772
                                }
6,249✔
773
                        }
774
                }
775
                if err != nil {
11,966✔
776
                        jitter := time.Duration(rand.Int63n(int64(reconnectDelay)))
5,571✔
777
                        delay := reconnectDelay + jitter
5,571✔
778
                        attempts++
5,571✔
779
                        if s.shouldReportConnectErr(firstConnect, attempts) {
9,105✔
780
                                s.Errorf(connErrFmt, rURL.Host, attempts, err)
3,534✔
781
                        } else {
5,571✔
782
                                s.Debugf(connErrFmt, rURL.Host, attempts, err)
2,037✔
783
                        }
2,037✔
784
                        remote.Lock()
5,571✔
785
                        // if we are using a delay to start migrating assets, kick off a migrate timer.
5,571✔
786
                        if remote.jsMigrateTimer == nil && jetstreamMigrateDelay > 0 {
5,579✔
787
                                remote.jsMigrateTimer = time.AfterFunc(jetstreamMigrateDelay, func() {
16✔
788
                                        s.checkJetStreamMigrate(remote)
8✔
789
                                })
8✔
790
                        }
791
                        remote.Unlock()
5,571✔
792
                        select {
5,571✔
793
                        case <-s.quitCh:
786✔
794
                                return false
786✔
795
                        case <-remote.quitCh:
1✔
796
                                return false
1✔
797
                        case <-time.After(delay):
4,783✔
798
                                // Check if we should migrate any JetStream assets immediately while this remote is down.
4,783✔
799
                                // This will be used if JetStreamClusterMigrateDelay was not set
4,783✔
800
                                if jetstreamMigrateDelay == 0 {
9,492✔
801
                                        s.checkJetStreamMigrate(remote)
4,709✔
802
                                }
4,709✔
803
                                continue
4,783✔
804
                        }
805
                }
806
                remote.cancelMigrateTimer()
824✔
807
                // We can check here, but really we will have to check again when the server
824✔
808
                // is about to add to the `s.leafs` map later in the process.
824✔
809
                if !remote.stillValid() {
824✔
810
                        conn.Close()
×
811
                        return false
×
812
                }
×
813

814
                // We have a connection here to a remote server.
815
                // Go ahead and create our leaf node and return.
816
                s.createLeafNode(conn, rURL, remote, nil)
824✔
817

824✔
818
                // Clear any observer states if we had them.
824✔
819
                s.clearObserverState(remote)
824✔
820

824✔
821
                return true
824✔
822
        }
823

824
        return false
9✔
825
}
826

827
func (cfg *leafNodeCfg) cancelMigrateTimer() {
832✔
828
        cfg.Lock()
832✔
829
        stopAndClearTimer(&cfg.jsMigrateTimer)
832✔
830
        cfg.Unlock()
832✔
831
}
832✔
832

833
// This will clear any observer state such that stream or consumer assets on this server can become leaders again.
834
func (s *Server) clearObserverState(remote *leafNodeCfg) {
824✔
835
        s.mu.RLock()
824✔
836
        accName := remote.LocalAccount
824✔
837
        s.mu.RUnlock()
824✔
838

824✔
839
        acc, err := s.LookupAccount(accName)
824✔
840
        if err != nil {
826✔
841
                s.Warnf("Error looking up account [%s] checking for JetStream clear observer state on a leafnode", accName)
2✔
842
                return
2✔
843
        }
2✔
844

845
        acc.jscmMu.Lock()
822✔
846
        defer acc.jscmMu.Unlock()
822✔
847

822✔
848
        // Walk all streams looking for any clustered stream, skip otherwise.
822✔
849
        for _, mset := range acc.streams() {
843✔
850
                node := mset.raftNode()
21✔
851
                if node == nil {
34✔
852
                        // Not R>1
13✔
853
                        continue
13✔
854
                }
855
                // Check consumers
856
                for _, o := range mset.getConsumers() {
10✔
857
                        if n := o.raftNode(); n != nil {
4✔
858
                                // Ensure we can become a leader again.
2✔
859
                                n.SetObserver(false)
2✔
860
                        }
2✔
861
                }
862
                // Ensure we can not become a leader again.
863
                node.SetObserver(false)
8✔
864
        }
865
}
866

867
// Check to see if we should migrate any assets from this account.
868
func (s *Server) checkJetStreamMigrate(remote *leafNodeCfg) {
4,717✔
869
        s.mu.RLock()
4,717✔
870
        accName, shouldMigrate := remote.LocalAccount, remote.JetStreamClusterMigrate
4,717✔
871
        s.mu.RUnlock()
4,717✔
872

4,717✔
873
        if !shouldMigrate {
9,369✔
874
                return
4,652✔
875
        }
4,652✔
876

877
        acc, err := s.LookupAccount(accName)
65✔
878
        if err != nil {
65✔
879
                s.Warnf("Error looking up account [%s] checking for JetStream migration on a leafnode", accName)
×
880
                return
×
881
        }
×
882

883
        acc.jscmMu.Lock()
65✔
884
        defer acc.jscmMu.Unlock()
65✔
885

65✔
886
        // Walk all streams looking for any clustered stream, skip otherwise.
65✔
887
        // If we are the leader force stepdown.
65✔
888
        for _, mset := range acc.streams() {
96✔
889
                node := mset.raftNode()
31✔
890
                if node == nil {
31✔
891
                        // Not R>1
×
892
                        continue
×
893
                }
894
                // Collect any consumers
895
                for _, o := range mset.getConsumers() {
50✔
896
                        if n := o.raftNode(); n != nil {
38✔
897
                                n.StepDown()
19✔
898
                                // Ensure we can not become a leader while in this state.
19✔
899
                                n.SetObserver(true)
19✔
900
                        }
19✔
901
                }
902
                // Stepdown if this stream was leader.
903
                node.StepDown()
31✔
904
                // Ensure we can not become a leader while in this state.
31✔
905
                node.SetObserver(true)
31✔
906
        }
907
}
908

909
// Helper for checking.
910
func (s *Server) isLeafConnectDisabled() bool {
6,388✔
911
        s.mu.RLock()
6,388✔
912
        defer s.mu.RUnlock()
6,388✔
913
        return s.leafDisableConnect
6,388✔
914
}
6,388✔
915

916
// Save off the tlsName for when we use TLS and mix hostnames and IPs. IPs usually
917
// come from the server we connect to.
918
//
919
// We used to save the name only if there was a TLSConfig or scheme equal to "tls".
920
// However, this was causing failures for users that did not set the scheme (and
921
// their remote connections did not have a tls{} block).
922
// We now save the host name regardless in case the remote returns an INFO indicating
923
// that TLS is required.
924
//
925
// Lock held on entry.
926
func (cfg *leafNodeCfg) saveTLSHostname(u *url.URL) {
2,547✔
927
        if cfg.tlsName == _EMPTY_ && net.ParseIP(u.Hostname()) == nil {
2,566✔
928
                cfg.tlsName = u.Hostname()
19✔
929
        }
19✔
930
}
931

932
// Save off the username/password for when we connect using a bare URL
933
// that we get from the INFO protocol.
934
//
935
// Lock held on entry.
936
func (cfg *leafNodeCfg) saveUserPassword(u *url.URL) {
1,883✔
937
        if cfg.username == _EMPTY_ && u.User != nil {
2,206✔
938
                cfg.username = u.User.Username()
323✔
939
                cfg.password, _ = u.User.Password()
323✔
940
        }
323✔
941
}
942

943
// This starts the leafnode accept loop in a go routine, unless it
944
// is detected that the server has already been shutdown.
945
func (s *Server) startLeafNodeAcceptLoop() {
4,048✔
946
        // Snapshot server options.
4,048✔
947
        opts := s.getOpts()
4,048✔
948

4,048✔
949
        port := opts.LeafNode.Port
4,048✔
950
        if port == -1 {
7,920✔
951
                port = 0
3,872✔
952
        }
3,872✔
953

954
        if s.isShuttingDown() {
4,048✔
955
                return
×
956
        }
×
957

958
        s.mu.Lock()
4,048✔
959
        hp := net.JoinHostPort(opts.LeafNode.Host, strconv.Itoa(port))
4,048✔
960
        l, e := natsListen("tcp", hp)
4,048✔
961
        s.leafNodeListenerErr = e
4,048✔
962
        if e != nil {
4,048✔
963
                s.mu.Unlock()
×
964
                s.Fatalf("Error listening on leafnode port: %d - %v", opts.LeafNode.Port, e)
×
965
                return
×
966
        }
×
967

968
        s.Noticef("Listening for leafnode connections on %s",
4,048✔
969
                net.JoinHostPort(opts.LeafNode.Host, strconv.Itoa(l.Addr().(*net.TCPAddr).Port)))
4,048✔
970

4,048✔
971
        tlsRequired := opts.LeafNode.TLSConfig != nil
4,048✔
972
        tlsVerify := tlsRequired && opts.LeafNode.TLSConfig.ClientAuth == tls.RequireAndVerifyClientCert
4,048✔
973
        // Do not set compression in this Info object, it would possibly cause
4,048✔
974
        // issues when sending asynchronous INFO to the remote.
4,048✔
975
        info := Info{
4,048✔
976
                ID:            s.info.ID,
4,048✔
977
                Name:          s.info.Name,
4,048✔
978
                Version:       s.info.Version,
4,048✔
979
                GitCommit:     gitCommit,
4,048✔
980
                GoVersion:     runtime.Version(),
4,048✔
981
                AuthRequired:  true,
4,048✔
982
                TLSRequired:   tlsRequired,
4,048✔
983
                TLSVerify:     tlsVerify,
4,048✔
984
                MaxPayload:    s.info.MaxPayload, // TODO(dlc) - Allow override?
4,048✔
985
                Headers:       s.supportsHeaders(),
4,048✔
986
                JetStream:     opts.JetStream,
4,048✔
987
                Domain:        opts.JetStreamDomain,
4,048✔
988
                Proto:         s.getServerProto(),
4,048✔
989
                InfoOnConnect: true,
4,048✔
990
                JSApiLevel:    JSApiLevel,
4,048✔
991
        }
4,048✔
992
        // If we have selected a random port...
4,048✔
993
        if port == 0 {
7,920✔
994
                // Write resolved port back to options.
3,872✔
995
                opts.LeafNode.Port = l.Addr().(*net.TCPAddr).Port
3,872✔
996
        }
3,872✔
997

998
        s.leafNodeInfo = info
4,048✔
999
        // Possibly override Host/Port and set IP based on Cluster.Advertise
4,048✔
1000
        if err := s.setLeafNodeInfoHostPortAndIP(); err != nil {
4,048✔
1001
                s.Fatalf("Error setting leafnode INFO with LeafNode.Advertise value of %s, err=%v", opts.LeafNode.Advertise, err)
×
1002
                l.Close()
×
1003
                s.mu.Unlock()
×
1004
                return
×
1005
        }
×
1006
        s.leafURLsMap[s.leafNodeInfo.IP]++
4,048✔
1007
        s.generateLeafNodeInfoJSON()
4,048✔
1008

4,048✔
1009
        // Setup state that can enable shutdown
4,048✔
1010
        s.leafNodeListener = l
4,048✔
1011

4,048✔
1012
        // As of now, a server that does not have remotes configured would
4,048✔
1013
        // never solicit a connection, so we should not have to warn if
4,048✔
1014
        // InsecureSkipVerify is set in main LeafNodes config (since
4,048✔
1015
        // this TLS setting matters only when soliciting a connection).
4,048✔
1016
        // Still, warn if insecure is set in any of LeafNode block.
4,048✔
1017
        // We need to check remotes, even if tls is not required on accept.
4,048✔
1018
        warn := tlsRequired && opts.LeafNode.TLSConfig.InsecureSkipVerify
4,048✔
1019
        if !warn {
8,094✔
1020
                for _, r := range opts.LeafNode.Remotes {
4,239✔
1021
                        if r.TLSConfig != nil && r.TLSConfig.InsecureSkipVerify {
193✔
1022
                                warn = true
×
1023
                                break
×
1024
                        }
1025
                }
1026
        }
1027
        if warn {
4,050✔
1028
                s.Warnf(leafnodeTLSInsecureWarning)
2✔
1029
        }
2✔
1030
        go s.acceptConnections(l, "Leafnode", func(conn net.Conn) { s.createLeafNode(conn, nil, nil, nil) }, nil)
4,921✔
1031
        s.mu.Unlock()
4,048✔
1032
}
1033

1034
// RegEx to match a creds file with user JWT and Seed.
1035
var credsRe = regexp.MustCompile(`\s*(?:(?:[-]{3,}.*[-]{3,}\r?\n)([\w\-.=]+)(?:\r?\n[-]{3,}.*[-]{3,}(\r?\n|\z)))`)
1036

1037
// clusterName is provided as argument to avoid lock ordering issues with the locked client c
1038
// Lock should be held entering here.
1039
func (c *client) sendLeafConnect(clusterName string, headers bool) error {
703✔
1040
        // We support basic user/pass and operator based user JWT with signatures.
703✔
1041
        cinfo := leafConnectInfo{
703✔
1042
                Version:       VERSION,
703✔
1043
                ID:            c.srv.info.ID,
703✔
1044
                Domain:        c.srv.info.Domain,
703✔
1045
                Name:          c.srv.info.Name,
703✔
1046
                Hub:           c.leaf.remote.Hub,
703✔
1047
                Cluster:       clusterName,
703✔
1048
                Headers:       headers,
703✔
1049
                JetStream:     c.acc.jetStreamConfigured(),
703✔
1050
                DenyPub:       c.leaf.remote.DenyImports,
703✔
1051
                Compression:   c.leaf.compression,
703✔
1052
                RemoteAccount: c.acc.GetName(),
703✔
1053
                Proto:         c.srv.getServerProto(),
703✔
1054
                Isolate:       c.leaf.remote.RequestIsolation,
703✔
1055
        }
703✔
1056

703✔
1057
        // If a signature callback is specified, this takes precedence over anything else.
703✔
1058
        if cb := c.leaf.remote.SignatureCB; cb != nil {
708✔
1059
                nonce := c.nonce
5✔
1060
                c.mu.Unlock()
5✔
1061
                jwt, sigraw, err := cb(nonce)
5✔
1062
                c.mu.Lock()
5✔
1063
                if err == nil && c.isClosed() {
6✔
1064
                        err = ErrConnectionClosed
1✔
1065
                }
1✔
1066
                if err != nil {
7✔
1067
                        c.Errorf("Error signing the nonce: %v", err)
2✔
1068
                        return err
2✔
1069
                }
2✔
1070
                sig := base64.RawURLEncoding.EncodeToString(sigraw)
3✔
1071
                cinfo.JWT, cinfo.Sig = jwt, sig
3✔
1072

1073
        } else if creds := c.leaf.remote.Credentials; creds != _EMPTY_ {
754✔
1074
                // Check for credentials first, that will take precedence..
56✔
1075
                c.Debugf("Authenticating with credentials file %q", c.leaf.remote.Credentials)
56✔
1076
                contents, err := os.ReadFile(creds)
56✔
1077
                if err != nil {
56✔
1078
                        c.Errorf("%v", err)
×
1079
                        return err
×
1080
                }
×
1081
                defer wipeSlice(contents)
56✔
1082
                items := credsRe.FindAllSubmatch(contents, -1)
56✔
1083
                if len(items) < 2 {
56✔
1084
                        c.Errorf("Credentials file malformed")
×
1085
                        return err
×
1086
                }
×
1087
                // First result should be the user JWT.
1088
                // We copy here so that the file containing the seed will be wiped appropriately.
1089
                raw := items[0][1]
56✔
1090
                tmp := make([]byte, len(raw))
56✔
1091
                copy(tmp, raw)
56✔
1092
                // Seed is second item.
56✔
1093
                kp, err := nkeys.FromSeed(items[1][1])
56✔
1094
                if err != nil {
56✔
1095
                        c.Errorf("Credentials file has malformed seed")
×
1096
                        return err
×
1097
                }
×
1098
                // Wipe our key on exit.
1099
                defer kp.Wipe()
56✔
1100

56✔
1101
                sigraw, _ := kp.Sign(c.nonce)
56✔
1102
                sig := base64.RawURLEncoding.EncodeToString(sigraw)
56✔
1103
                cinfo.JWT = bytesToString(tmp)
56✔
1104
                cinfo.Sig = sig
56✔
1105
        } else if nkey := c.leaf.remote.Nkey; nkey != _EMPTY_ {
647✔
1106
                kp, err := nkeys.FromSeed([]byte(nkey))
5✔
1107
                if err != nil {
5✔
1108
                        c.Errorf("Remote nkey has malformed seed")
×
1109
                        return err
×
1110
                }
×
1111
                // Wipe our key on exit.
1112
                defer kp.Wipe()
5✔
1113
                sigraw, _ := kp.Sign(c.nonce)
5✔
1114
                sig := base64.RawURLEncoding.EncodeToString(sigraw)
5✔
1115
                pkey, _ := kp.PublicKey()
5✔
1116
                cinfo.Nkey = pkey
5✔
1117
                cinfo.Sig = sig
5✔
1118
        }
1119
        // In addition, and this is to allow auth callout, set user/password or
1120
        // token if applicable.
1121
        if userInfo := c.leaf.remote.curURL.User; userInfo != nil {
1,047✔
1122
                cinfo.User = userInfo.Username()
346✔
1123
                var ok bool
346✔
1124
                cinfo.Pass, ok = userInfo.Password()
346✔
1125
                // For backward compatibility, if only username is provided, set both
346✔
1126
                // Token and User, not just Token.
346✔
1127
                if !ok {
355✔
1128
                        cinfo.Token = cinfo.User
9✔
1129
                }
9✔
1130
        } else if c.leaf.remote.username != _EMPTY_ {
363✔
1131
                cinfo.User = c.leaf.remote.username
8✔
1132
                cinfo.Pass = c.leaf.remote.password
8✔
1133
                // For backward compatibility, if only username is provided, set both
8✔
1134
                // Token and User, not just Token.
8✔
1135
                if cinfo.Pass == _EMPTY_ {
8✔
1136
                        cinfo.Token = cinfo.User
×
1137
                }
×
1138
        }
1139
        b, err := json.Marshal(cinfo)
701✔
1140
        if err != nil {
701✔
1141
                c.Errorf("Error marshaling CONNECT to remote leafnode: %v\n", err)
×
1142
                return err
×
1143
        }
×
1144
        // Although this call is made before the writeLoop is created,
1145
        // we don't really need to send in place. The protocol will be
1146
        // sent out by the writeLoop.
1147
        c.enqueueProto([]byte(fmt.Sprintf(ConProto, b)))
701✔
1148
        return nil
701✔
1149
}
1150

1151
// Makes a deep copy of the LeafNode Info structure.
1152
// The server lock is held on entry.
1153
func (s *Server) copyLeafNodeInfo() *Info {
2,807✔
1154
        clone := s.leafNodeInfo
2,807✔
1155
        // Copy the array of urls.
2,807✔
1156
        if len(s.leafNodeInfo.LeafNodeURLs) > 0 {
5,107✔
1157
                clone.LeafNodeURLs = append([]string(nil), s.leafNodeInfo.LeafNodeURLs...)
2,300✔
1158
        }
2,300✔
1159
        return &clone
2,807✔
1160
}
1161

1162
// Adds a LeafNode URL that we get when a route connects to the Info structure.
1163
// Regenerates the JSON byte array so that it can be sent to LeafNode connections.
1164
// Returns a boolean indicating if the URL was added or not.
1165
// Server lock is held on entry
1166
func (s *Server) addLeafNodeURL(urlStr string) bool {
8,070✔
1167
        if s.leafURLsMap.addUrl(urlStr) {
16,135✔
1168
                s.generateLeafNodeInfoJSON()
8,065✔
1169
                return true
8,065✔
1170
        }
8,065✔
1171
        return false
5✔
1172
}
1173

1174
// Removes a LeafNode URL of the route that is disconnecting from the Info structure.
1175
// Regenerates the JSON byte array so that it can be sent to LeafNode connections.
1176
// Returns a boolean indicating if the URL was removed or not.
1177
// Server lock is held on entry.
1178
func (s *Server) removeLeafNodeURL(urlStr string) bool {
8,064✔
1179
        // Don't need to do this if we are removing the route connection because
8,064✔
1180
        // we are shuting down...
8,064✔
1181
        if s.isShuttingDown() {
12,364✔
1182
                return false
4,300✔
1183
        }
4,300✔
1184
        if s.leafURLsMap.removeUrl(urlStr) {
7,524✔
1185
                s.generateLeafNodeInfoJSON()
3,760✔
1186
                return true
3,760✔
1187
        }
3,760✔
1188
        return false
4✔
1189
}
1190

1191
// Server lock is held on entry
1192
func (s *Server) generateLeafNodeInfoJSON() {
15,873✔
1193
        s.leafNodeInfo.Cluster = s.cachedClusterName()
15,873✔
1194
        s.leafNodeInfo.LeafNodeURLs = s.leafURLsMap.getAsStringSlice()
15,873✔
1195
        s.leafNodeInfo.WSConnectURLs = s.websocket.connectURLsMap.getAsStringSlice()
15,873✔
1196
        s.leafNodeInfoJSON = generateInfoJSON(&s.leafNodeInfo)
15,873✔
1197
}
15,873✔
1198

1199
// Sends an async INFO protocol so that the connected servers can update
1200
// their list of LeafNode urls.
1201
func (s *Server) sendAsyncLeafNodeInfo() {
11,825✔
1202
        for _, c := range s.leafs {
11,937✔
1203
                c.mu.Lock()
112✔
1204
                c.enqueueProto(s.leafNodeInfoJSON)
112✔
1205
                c.mu.Unlock()
112✔
1206
        }
112✔
1207
}
1208

1209
// Called when an inbound leafnode connection is accepted or we create one for a solicited leafnode.
1210
func (s *Server) createLeafNode(conn net.Conn, rURL *url.URL, remote *leafNodeCfg, ws *websocket) *client {
1,732✔
1211
        // Snapshot server options.
1,732✔
1212
        opts := s.getOpts()
1,732✔
1213

1,732✔
1214
        maxPay := int32(opts.MaxPayload)
1,732✔
1215
        maxSubs := int32(opts.MaxSubs)
1,732✔
1216
        // For system, maxSubs of 0 means unlimited, so re-adjust here.
1,732✔
1217
        if maxSubs == 0 {
3,463✔
1218
                maxSubs = -1
1,731✔
1219
        }
1,731✔
1220
        now := time.Now().UTC()
1,732✔
1221

1,732✔
1222
        c := &client{srv: s, nc: conn, kind: LEAF, opts: defaultOpts, mpay: maxPay, msubs: maxSubs, start: now, last: now}
1,732✔
1223
        // Do not update the smap here, we need to do it in initLeafNodeSmapAndSendSubs
1,732✔
1224
        c.leaf = &leaf{}
1,732✔
1225

1,732✔
1226
        // If the leafnode subject interest should be isolated, flag it here.
1,732✔
1227
        s.optsMu.RLock()
1,732✔
1228
        if c.leaf.isolated = s.opts.LeafNode.IsolateLeafnodeInterest; !c.leaf.isolated && remote != nil {
2,554✔
1229
                c.leaf.isolated = remote.LocalIsolation
822✔
1230
        }
822✔
1231
        s.optsMu.RUnlock()
1,732✔
1232

1,732✔
1233
        // For accepted LN connections, ws will be != nil if it was accepted
1,732✔
1234
        // through the Websocket port.
1,732✔
1235
        c.ws = ws
1,732✔
1236

1,732✔
1237
        // For remote, check if the scheme starts with "ws", if so, we will initiate
1,732✔
1238
        // a remote Leaf Node connection as a websocket connection.
1,732✔
1239
        if remote != nil && rURL != nil && isWSURL(rURL) {
1,786✔
1240
                remote.RLock()
54✔
1241
                c.ws = &websocket{compress: remote.Websocket.Compression, maskwrite: !remote.Websocket.NoMasking}
54✔
1242
                remote.RUnlock()
54✔
1243
        }
54✔
1244

1245
        // Determines if we are soliciting the connection or not.
1246
        var solicited bool
1,732✔
1247
        var acc *Account
1,732✔
1248
        var remoteSuffix string
1,732✔
1249
        if remote != nil {
2,556✔
1250
                // For now, if lookup fails, we will constantly try
824✔
1251
                // to recreate this LN connection.
824✔
1252
                lacc := remote.LocalAccount
824✔
1253
                var err error
824✔
1254
                acc, err = s.LookupAccount(lacc)
824✔
1255
                if err != nil {
826✔
1256
                        // An account not existing is something that can happen with nats/http account resolver and the account
2✔
1257
                        // has not yet been pushed, or the request failed for other reasons.
2✔
1258
                        // remote needs to be set or retry won't happen
2✔
1259
                        c.leaf.remote = remote
2✔
1260
                        c.closeConnection(MissingAccount)
2✔
1261
                        s.Errorf("Unable to lookup account %s for solicited leafnode connection: %v", lacc, err)
2✔
1262
                        return nil
2✔
1263
                }
2✔
1264
                remoteSuffix = fmt.Sprintf(" for account: %s", acc.traceLabel())
822✔
1265
        }
1266

1267
        c.mu.Lock()
1,730✔
1268
        c.initClient()
1,730✔
1269
        c.Noticef("Leafnode connection created%s %s", remoteSuffix, c.opts.Name)
1,730✔
1270

1,730✔
1271
        var (
1,730✔
1272
                tlsFirst         bool
1,730✔
1273
                tlsFirstFallback time.Duration
1,730✔
1274
                infoTimeout      time.Duration
1,730✔
1275
        )
1,730✔
1276
        if remote != nil {
2,552✔
1277
                solicited = true
822✔
1278
                remote.Lock()
822✔
1279
                c.leaf.remote = remote
822✔
1280
                c.setPermissions(remote.perms)
822✔
1281
                if !c.leaf.remote.Hub {
1,626✔
1282
                        c.leaf.isSpoke = true
804✔
1283
                }
804✔
1284
                tlsFirst = remote.TLSHandshakeFirst
822✔
1285
                infoTimeout = remote.FirstInfoTimeout
822✔
1286
                remote.Unlock()
822✔
1287
                c.acc = acc
822✔
1288
        } else {
908✔
1289
                c.flags.set(expectConnect)
908✔
1290
                if ws != nil {
943✔
1291
                        c.Debugf("Leafnode compression=%v", c.ws.compress)
35✔
1292
                }
35✔
1293
                tlsFirst = opts.LeafNode.TLSHandshakeFirst
908✔
1294
                if f := opts.LeafNode.TLSHandshakeFirstFallback; f > 0 {
909✔
1295
                        tlsFirstFallback = f
1✔
1296
                }
1✔
1297
        }
1298
        c.mu.Unlock()
1,730✔
1299

1,730✔
1300
        var nonce [nonceLen]byte
1,730✔
1301
        var info *Info
1,730✔
1302

1,730✔
1303
        // Grab this before the client lock below.
1,730✔
1304
        if !solicited {
2,638✔
1305
                // Grab server variables
908✔
1306
                s.mu.Lock()
908✔
1307
                info = s.copyLeafNodeInfo()
908✔
1308
                // For tests that want to simulate old servers, do not set the compression
908✔
1309
                // on the INFO protocol if configured with CompressionNotSupported.
908✔
1310
                // Also suppress it if WebSocket compression is already in use, otherwise
908✔
1311
                // an old soliciting peer would honor the advertised mode, switch to S2,
908✔
1312
                // and then wait forever for a compressed INFO response from us.
908✔
1313
                if cm := opts.LeafNode.Compression.Mode; cm != CompressionNotSupported && (ws == nil || !ws.compress) {
1,808✔
1314
                        info.Compression = cm
900✔
1315
                }
900✔
1316
                // We always send a nonce for LEAF connections. Do not change that without
1317
                // taking into account presence of proxy trusted keys.
1318
                s.generateNonce(nonce[:])
908✔
1319
                s.mu.Unlock()
908✔
1320
        }
1321

1322
        // Grab lock
1323
        c.mu.Lock()
1,730✔
1324

1,730✔
1325
        var preBuf []byte
1,730✔
1326
        if solicited {
2,552✔
1327
                // For websocket connection, we need to send an HTTP request,
822✔
1328
                // and get the response before starting the readLoop to get
822✔
1329
                // the INFO, etc..
822✔
1330
                if c.isWebsocket() {
876✔
1331
                        var err error
54✔
1332
                        var closeReason ClosedState
54✔
1333

54✔
1334
                        preBuf, closeReason, err = c.leafNodeSolicitWSConnection(opts, rURL, remote)
54✔
1335
                        if err != nil {
75✔
1336
                                c.Errorf("Error soliciting websocket connection: %v", err)
21✔
1337
                                c.mu.Unlock()
21✔
1338
                                if closeReason != 0 {
38✔
1339
                                        c.closeConnection(closeReason)
17✔
1340
                                }
17✔
1341
                                return nil
21✔
1342
                        }
1343
                } else {
768✔
1344
                        // If configured to do TLS handshake first
768✔
1345
                        if tlsFirst {
772✔
1346
                                if _, err := c.leafClientHandshakeIfNeeded(remote, opts); err != nil {
5✔
1347
                                        c.mu.Unlock()
1✔
1348
                                        return nil
1✔
1349
                                }
1✔
1350
                        }
1351
                        // We need to wait for the info, but not for too long.
1352
                        c.nc.SetReadDeadline(time.Now().Add(infoTimeout))
767✔
1353
                }
1354

1355
                // We will process the INFO from the readloop and finish by
1356
                // sending the CONNECT and finish registration later.
1357
        } else {
908✔
1358
                // Send our info to the other side.
908✔
1359
                // Remember the nonce we sent here for signatures, etc.
908✔
1360
                c.nonce = make([]byte, nonceLen)
908✔
1361
                copy(c.nonce, nonce[:])
908✔
1362
                info.Nonce = bytesToString(c.nonce)
908✔
1363
                info.CID = c.cid
908✔
1364
                proto := generateInfoJSON(info)
908✔
1365

908✔
1366
                var pre []byte
908✔
1367
                // We need first to check for "TLS First" fallback delay.
908✔
1368
                if tlsFirstFallback > 0 {
909✔
1369
                        // We wait and see if we are getting any data. Since we did not send
1✔
1370
                        // the INFO protocol yet, only clients that use TLS first should be
1✔
1371
                        // sending data (the TLS handshake). We don't really check the content:
1✔
1372
                        // if it is a rogue agent and not an actual client performing the
1✔
1373
                        // TLS handshake, the error will be detected when performing the
1✔
1374
                        // handshake on our side.
1✔
1375
                        pre = make([]byte, 4)
1✔
1376
                        c.nc.SetReadDeadline(time.Now().Add(tlsFirstFallback))
1✔
1377
                        n, _ := io.ReadFull(c.nc, pre[:])
1✔
1378
                        c.nc.SetReadDeadline(time.Time{})
1✔
1379
                        // If we get any data (regardless of possible timeout), we will proceed
1✔
1380
                        // with the TLS handshake.
1✔
1381
                        if n > 0 {
1✔
1382
                                pre = pre[:n]
×
1383
                        } else {
1✔
1384
                                // We did not get anything so we will send the INFO protocol.
1✔
1385
                                pre = nil
1✔
1386
                                // Set the boolean to false for the rest of the function.
1✔
1387
                                tlsFirst = false
1✔
1388
                        }
1✔
1389
                }
1390

1391
                if !tlsFirst {
1,811✔
1392
                        // We have to send from this go routine because we may
903✔
1393
                        // have to block for TLS handshake before we start our
903✔
1394
                        // writeLoop go routine. The other side needs to receive
903✔
1395
                        // this before it can initiate the TLS handshake..
903✔
1396
                        c.sendProtoNow(proto)
903✔
1397

903✔
1398
                        // The above call could have marked the connection as closed (due to TCP error).
903✔
1399
                        if c.isClosed() {
903✔
1400
                                c.mu.Unlock()
×
1401
                                c.closeConnection(WriteError)
×
1402
                                return nil
×
1403
                        }
×
1404
                }
1405

1406
                // Check to see if we need to spin up TLS.
1407
                if !c.isWebsocket() && info.TLSRequired {
979✔
1408
                        // If we have a prebuffer create a multi-reader.
71✔
1409
                        if len(pre) > 0 {
71✔
1410
                                c.nc = &tlsMixConn{c.nc, bytes.NewBuffer(pre)}
×
1411
                        }
×
1412
                        // Perform server-side TLS handshake.
1413
                        if err := c.doTLSServerHandshake(tlsHandshakeLeaf, opts.LeafNode.TLSConfig, opts.LeafNode.TLSTimeout, opts.LeafNode.TLSPinnedCerts); err != nil {
115✔
1414
                                c.mu.Unlock()
44✔
1415
                                return nil
44✔
1416
                        }
44✔
1417
                }
1418

1419
                // If the user wants the TLS handshake to occur first, now that it is
1420
                // done, send the INFO protocol.
1421
                if tlsFirst {
867✔
1422
                        c.flags.set(didTLSFirst)
3✔
1423
                        c.sendProtoNow(proto)
3✔
1424
                        if c.isClosed() {
3✔
1425
                                c.mu.Unlock()
×
1426
                                c.closeConnection(WriteError)
×
1427
                                return nil
×
1428
                        }
×
1429
                }
1430

1431
                // Leaf nodes will always require a CONNECT to let us know
1432
                // when we are properly bound to an account.
1433
                //
1434
                // If compression is configured, we can't set the authTimer here because
1435
                // it would cause the parser to fail any incoming protocol that is not a
1436
                // CONNECT (and we need to exchange INFO protocols for compression
1437
                // negotiation). So instead, use the ping timer until we are done with
1438
                // negotiation and can set the auth timer.
1439
                timeout := secondsToDuration(opts.LeafNode.AuthTimeout)
864✔
1440
                if needsCompression(opts.LeafNode.Compression.Mode) {
1,498✔
1441
                        c.ping.tmr = time.AfterFunc(timeout, func() {
643✔
1442
                                c.authTimeout()
9✔
1443
                        })
9✔
1444
                } else {
230✔
1445
                        c.setAuthTimer(timeout)
230✔
1446
                }
230✔
1447
        }
1448

1449
        // Keep track in case server is shutdown before we can successfully register.
1450
        if !s.addToTempClients(c.cid, c) {
1,665✔
1451
                c.mu.Unlock()
1✔
1452
                c.setNoReconnect()
1✔
1453
                c.closeConnection(ServerShutdown)
1✔
1454
                return nil
1✔
1455
        }
1✔
1456

1457
        // Spin up the read loop.
1458
        s.startGoRoutine(func() { c.readLoop(preBuf) })
3,326✔
1459

1460
        // We will spin the write loop for solicited connections only
1461
        // when processing the INFO and after switching to TLS if needed.
1462
        if !solicited {
2,527✔
1463
                s.startGoRoutine(func() { c.writeLoop() })
1,728✔
1464
        }
1465

1466
        c.mu.Unlock()
1,663✔
1467

1,663✔
1468
        return c
1,663✔
1469
}
1470

1471
// Will perform the client-side TLS handshake if needed. Assumes that this
1472
// is called by the solicit side (remote will be non nil). Returns `true`
1473
// if TLS is required, `false` otherwise.
1474
// Lock held on entry.
1475
func (c *client) leafClientHandshakeIfNeeded(remote *leafNodeCfg, opts *Options) (bool, error) {
1,984✔
1476
        // Check if TLS is required and gather TLS config variables.
1,984✔
1477
        tlsRequired, tlsConfig, tlsName, tlsTimeout := c.leafNodeGetTLSConfigForSolicit(remote)
1,984✔
1478
        if !tlsRequired {
3,895✔
1479
                return false, nil
1,911✔
1480
        }
1,911✔
1481

1482
        // If TLS required, peform handshake.
1483
        // Get the URL that was used to connect to the remote server.
1484
        rURL := remote.getCurrentURL()
73✔
1485

73✔
1486
        // Perform the client-side TLS handshake.
73✔
1487
        if resetTLSName, err := c.doTLSClientHandshake(tlsHandshakeLeaf, rURL, tlsConfig, tlsName, tlsTimeout, opts.LeafNode.TLSPinnedCerts); err != nil {
105✔
1488
                // Check if we need to reset the remote's TLS name.
32✔
1489
                if resetTLSName {
32✔
1490
                        remote.Lock()
×
1491
                        remote.tlsName = _EMPTY_
×
1492
                        remote.Unlock()
×
1493
                }
×
1494
                return false, err
32✔
1495
        }
1496
        return true, nil
41✔
1497
}
1498

1499
func (c *client) processLeafnodeInfo(info *Info) {
2,759✔
1500
        c.mu.Lock()
2,759✔
1501
        if c.leaf == nil || c.isClosed() {
2,761✔
1502
                c.mu.Unlock()
2✔
1503
                return
2✔
1504
        }
2✔
1505
        s := c.srv
2,757✔
1506
        opts := s.getOpts()
2,757✔
1507
        remote := c.leaf.remote
2,757✔
1508
        didSolicit := remote != nil
2,757✔
1509
        firstINFO := !c.flags.isSet(infoReceived)
2,757✔
1510

2,757✔
1511
        // In case of websocket, the TLS handshake has been already done.
2,757✔
1512
        // So check only for non websocket connections and for configurations
2,757✔
1513
        // where the TLS Handshake was not done first.
2,757✔
1514
        if didSolicit && !c.flags.isSet(handshakeComplete) && !c.isWebsocket() && !remote.TLSHandshakeFirst {
4,683✔
1515
                // If the server requires TLS, we need to set this in the remote
1,926✔
1516
                // otherwise if there is no TLS configuration block for the remote,
1,926✔
1517
                // the solicit side will not attempt to perform the TLS handshake.
1,926✔
1518
                if firstINFO && info.TLSRequired {
1,983✔
1519
                        // Check for TLS/proxy configuration mismatch
57✔
1520
                        if remote.Proxy.URL != _EMPTY_ && !remote.TLS && remote.TLSConfig == nil {
57✔
1521
                                c.mu.Unlock()
×
1522
                                c.Errorf("TLS configuration mismatch: Hub requires TLS but leafnode remote is not configured for TLS. When using a proxy, ensure TLS leafnode configuration matches the Hub requirements.")
×
1523
                                c.closeConnection(TLSHandshakeError)
×
1524
                                return
×
1525
                        }
×
1526
                        remote.TLS = true
57✔
1527
                }
1528
                if _, err := c.leafClientHandshakeIfNeeded(remote, opts); err != nil {
1,953✔
1529
                        c.mu.Unlock()
27✔
1530
                        return
27✔
1531
                }
27✔
1532
        }
1533

1534
        // Check for compression, unless already done.
1535
        if firstINFO && !c.flags.isSet(compressionNegotiated) {
4,088✔
1536
                // A solicited leafnode connection must first receive a leafnode INFO.
1,358✔
1537
                // Classify wrong-port connections before any leaf-specific negotiation.
1,358✔
1538
                if didSolicit && (info.CID == 0 || info.LeafNodeURLs == nil) {
1,412✔
1539
                        c.mu.Unlock()
54✔
1540
                        c.Errorf(ErrConnectedToWrongPort.Error())
54✔
1541
                        c.closeConnection(WrongPort)
54✔
1542
                        return
54✔
1543
                }
54✔
1544

1545
                // Prevent from getting back here.
1546
                c.flags.set(compressionNegotiated)
1,304✔
1547

1,304✔
1548
                var co *CompressionOpts
1,304✔
1549
                if !didSolicit {
1,901✔
1550
                        co = &opts.LeafNode.Compression
597✔
1551
                } else {
1,304✔
1552
                        co = &remote.Compression
707✔
1553
                }
707✔
1554
                if needsCompression(co.Mode) {
2,593✔
1555
                        // Release client lock since following function will need server lock.
1,289✔
1556
                        c.mu.Unlock()
1,289✔
1557
                        compress, err := s.negotiateLeafCompression(c, didSolicit, info.Compression, co)
1,289✔
1558
                        if err != nil {
1,289✔
1559
                                c.sendErrAndErr(err.Error())
×
1560
                                c.closeConnection(ProtocolViolation)
×
1561
                                return
×
1562
                        }
×
1563
                        if compress {
2,479✔
1564
                                // Done for now, will get back another INFO protocol...
1,190✔
1565
                                return
1,190✔
1566
                        }
1,190✔
1567
                        // No compression because one side does not want/can't, so proceed.
1568
                        c.mu.Lock()
99✔
1569
                        // Check that the connection did not close if the lock was released.
99✔
1570
                        if c.isClosed() {
99✔
1571
                                c.mu.Unlock()
×
1572
                                return
×
1573
                        }
×
1574
                } else {
15✔
1575
                        // Coming from an old server, the Compression field would be the empty
15✔
1576
                        // string. For servers that are configured with CompressionNotSupported,
15✔
1577
                        // this makes them behave as old servers.
15✔
1578
                        if info.Compression == _EMPTY_ || co.Mode == CompressionNotSupported {
17✔
1579
                                c.leaf.compression = CompressionNotSupported
2✔
1580
                        } else {
15✔
1581
                                c.leaf.compression = CompressionOff
13✔
1582
                        }
13✔
1583
                }
1584
                // Accepting side does not normally process an INFO protocol during
1585
                // initial connection handshake. So we keep it consistent by returning
1586
                // if we are not soliciting.
1587
                if !didSolicit {
118✔
1588
                        // If we had created the ping timer instead of the auth timer, we will
4✔
1589
                        // clear the ping timer and set the auth timer now that the compression
4✔
1590
                        // negotiation is done.
4✔
1591
                        if info.Compression != _EMPTY_ && c.ping.tmr != nil {
5✔
1592
                                clearTimer(&c.ping.tmr)
1✔
1593
                                c.setAuthTimer(secondsToDuration(opts.LeafNode.AuthTimeout))
1✔
1594
                        }
1✔
1595
                        c.mu.Unlock()
4✔
1596
                        return
4✔
1597
                }
1598
                // Fall through and process the INFO protocol as usual.
1599
        }
1600

1601
        // Note: For now, only the initial INFO has a nonce. We
1602
        // will probably do auto key rotation at some point.
1603
        if firstINFO {
2,231✔
1604
                // Mark that the INFO protocol has been received.
749✔
1605
                c.flags.set(infoReceived)
749✔
1606
                // Prevent connecting to non leafnode port. Need to do this only for
749✔
1607
                // the first INFO, not for async INFO updates...
749✔
1608
                //
749✔
1609
                // Content of INFO sent by the server when accepting a tcp connection.
749✔
1610
                // -------------------------------------------------------------------
749✔
1611
                // Listen Port Of | CID | ClientConnectURLs | LeafNodeURLs | Gateway |
749✔
1612
                // -------------------------------------------------------------------
749✔
1613
                //      CLIENT    |  X* |        X**        |              |         |
749✔
1614
                //      ROUTE     |     |        X**        |      X***    |         |
749✔
1615
                //     GATEWAY    |     |                   |              |    X    |
749✔
1616
                //     LEAFNODE   |  X  |                   |       X      |         |
749✔
1617
                // -------------------------------------------------------------------
749✔
1618
                // *   Not on older servers.
749✔
1619
                // **  Not if "no advertise" is enabled.
749✔
1620
                // *** Not if leafnode's "no advertise" is enabled.
749✔
1621
                //
749✔
1622
                // Reject a cluster that contains spaces.
749✔
1623
                if info.Cluster != _EMPTY_ && strings.Contains(info.Cluster, " ") {
750✔
1624
                        c.mu.Unlock()
1✔
1625
                        c.sendErrAndErr(ErrClusterNameHasSpaces.Error())
1✔
1626
                        c.closeConnection(ProtocolViolation)
1✔
1627
                        return
1✔
1628
                }
1✔
1629
                // For solicited outbound leaf connections, capture the remote's nonce.
1630
                // For inbound leaf connections, keep using the server-issued nonce that
1631
                // was sent in our initial INFO and must be signed in CONNECT.
1632
                if didSolicit {
1,451✔
1633
                        c.nonce = []byte(info.Nonce)
703✔
1634
                }
703✔
1635
                if info.TLSRequired && didSolicit {
778✔
1636
                        remote.TLS = true
30✔
1637
                }
30✔
1638
                supportsHeaders := c.srv.supportsHeaders()
748✔
1639
                c.headers = supportsHeaders && info.Headers
748✔
1640

748✔
1641
                // Remember the remote server.
748✔
1642
                // Pre 2.2.0 servers are not sending their server name.
748✔
1643
                // In that case, use info.ID, which, for those servers, matches
748✔
1644
                // the content of the field `Name` in the leafnode CONNECT protocol.
748✔
1645
                if info.Name == _EMPTY_ {
750✔
1646
                        c.leaf.remoteServer = info.ID
2✔
1647
                } else {
748✔
1648
                        c.leaf.remoteServer = info.Name
746✔
1649
                }
746✔
1650
                c.leaf.remoteDomain = info.Domain
748✔
1651
                c.leaf.remoteCluster = info.Cluster
748✔
1652
                // We send the protocol version in the INFO protocol.
748✔
1653
                // Keep track of it, so we know if this connection supports message
748✔
1654
                // tracing for instance.
748✔
1655
                c.opts.Protocol = info.Proto
748✔
1656
        }
1657

1658
        // For both initial INFO and async INFO protocols, Possibly
1659
        // update our list of remote leafnode URLs we can connect to,
1660
        // unless we are instructed not to.
1661
        if didSolicit && !remote.IgnoreDiscoveredServers &&
1,481✔
1662
                (len(info.LeafNodeURLs) > 0 || len(info.WSConnectURLs) > 0) {
2,861✔
1663
                // Consider the incoming array as the most up-to-date
1,380✔
1664
                // representation of the remote cluster's list of URLs.
1,380✔
1665
                c.updateLeafNodeURLs(info)
1,380✔
1666
        }
1,380✔
1667

1668
        // Only solicited leafnode connections trust permission updates from INFO.
1669
        if didSolicit && (info.Import != nil || info.Export != nil) {
1,501✔
1670
                perms := &Permissions{
20✔
1671
                        Publish:   info.Export,
20✔
1672
                        Subscribe: info.Import,
20✔
1673
                }
20✔
1674
                // Check if we have local deny clauses that we need to merge.
20✔
1675
                if remote := c.leaf.remote; remote != nil {
40✔
1676
                        if len(remote.DenyExports) > 0 {
21✔
1677
                                if perms.Publish == nil {
1✔
1678
                                        perms.Publish = &SubjectPermission{}
×
1679
                                }
×
1680
                                perms.Publish.Deny = append(perms.Publish.Deny, remote.DenyExports...)
1✔
1681
                        }
1682
                        if len(remote.DenyImports) > 0 {
21✔
1683
                                if perms.Subscribe == nil {
1✔
1684
                                        perms.Subscribe = &SubjectPermission{}
×
1685
                                }
×
1686
                                perms.Subscribe.Deny = append(perms.Subscribe.Deny, remote.DenyImports...)
1✔
1687
                        }
1688
                }
1689
                c.setPermissions(perms)
20✔
1690
        }
1691

1692
        var resumeConnect bool
1,481✔
1693

1,481✔
1694
        // If this is a remote connection and this is the first INFO protocol,
1,481✔
1695
        // then we need to finish the connect process by sending CONNECT, etc..
1,481✔
1696
        if firstINFO && didSolicit {
2,184✔
1697
                // Clear deadline that was set in createLeafNode while waiting for the INFO.
703✔
1698
                c.nc.SetDeadline(time.Time{})
703✔
1699
                resumeConnect = true
703✔
1700
        } else if !firstINFO && didSolicit {
2,161✔
1701
                c.leaf.remoteAccName = info.RemoteAccount
680✔
1702
        }
680✔
1703

1704
        // Check if we have the remote account information and if so make sure it's stored.
1705
        if info.RemoteAccount != _EMPTY_ {
2,147✔
1706
                if c.acc == nil {
667✔
1707
                        c.mu.Unlock()
1✔
1708
                        c.sendErr("Authorization Violation")
1✔
1709
                        c.closeConnection(ProtocolViolation)
1✔
1710
                        return
1✔
1711
                }
1✔
1712
                s.leafRemoteAccounts.Store(c.acc.Name, info.RemoteAccount)
665✔
1713
        }
1714
        c.mu.Unlock()
1,480✔
1715

1,480✔
1716
        finishConnect := info.ConnectInfo
1,480✔
1717
        if resumeConnect && s != nil {
2,183✔
1718
                s.leafNodeResumeConnectProcess(c)
703✔
1719
                if !info.InfoOnConnect {
703✔
1720
                        finishConnect = true
×
1721
                }
×
1722
        }
1723
        if finishConnect {
2,146✔
1724
                s.leafNodeFinishConnectProcess(c)
666✔
1725
        }
666✔
1726

1727
        // Check to see if we need to kick any internal source or mirror consumers.
1728
        // This will be a no-op if JetStream not enabled for this server or if the bound account
1729
        // does not have jetstream.
1730
        s.checkInternalSyncConsumers(c.acc)
1,480✔
1731
}
1732

1733
func (s *Server) negotiateLeafCompression(c *client, didSolicit bool, infoCompression string, co *CompressionOpts) (bool, error) {
1,289✔
1734
        // If WebSocket compression is already negotiated on this connection then
1,289✔
1735
        // we shouldn't layer S2 compression on top of it.
1,289✔
1736
        c.mu.Lock()
1,289✔
1737
        if c.ws != nil && c.ws.compress {
1,295✔
1738
                c.leaf.compression = CompressionOff
6✔
1739
                c.mu.Unlock()
6✔
1740
                return false, nil
6✔
1741
        }
6✔
1742
        c.mu.Unlock()
1,283✔
1743
        // Negotiate the appropriate compression mode (or no compression)
1,283✔
1744
        cm, err := selectCompressionMode(co.Mode, infoCompression)
1,283✔
1745
        if err != nil {
1,283✔
1746
                return false, err
×
1747
        }
×
1748
        c.mu.Lock()
1,283✔
1749
        // For "auto" mode, set the initial compression mode based on RTT
1,283✔
1750
        if cm == CompressionS2Auto {
2,437✔
1751
                if c.rttStart.IsZero() {
2,308✔
1752
                        c.rtt = computeRTT(c.start)
1,154✔
1753
                }
1,154✔
1754
                cm = selectS2AutoModeBasedOnRTT(c.rtt, co.RTTThresholds)
1,154✔
1755
        }
1756
        // Keep track of the negotiated compression mode.
1757
        c.leaf.compression = cm
1,283✔
1758
        cid := c.cid
1,283✔
1759
        var nonce string
1,283✔
1760
        if !didSolicit {
1,879✔
1761
                nonce = bytesToString(c.nonce)
596✔
1762
        }
596✔
1763
        c.mu.Unlock()
1,283✔
1764

1,283✔
1765
        if !needsCompression(cm) {
1,376✔
1766
                return false, nil
93✔
1767
        }
93✔
1768

1769
        // If we end-up doing compression...
1770

1771
        // Generate an INFO with the chosen compression mode.
1772
        s.mu.Lock()
1,190✔
1773
        info := s.copyLeafNodeInfo()
1,190✔
1774
        info.Compression, info.CID, info.Nonce = compressionModeForInfoProtocol(co, cm), cid, nonce
1,190✔
1775
        infoProto := generateInfoJSON(info)
1,190✔
1776
        s.mu.Unlock()
1,190✔
1777

1,190✔
1778
        // If we solicited, then send this INFO protocol BEFORE switching
1,190✔
1779
        // to compression writer. However, if we did not, we send it after.
1,190✔
1780
        c.mu.Lock()
1,190✔
1781
        if didSolicit {
1,787✔
1782
                c.enqueueProto(infoProto)
597✔
1783
                // Make sure it is completely flushed (the pending bytes goes to
597✔
1784
                // 0) before proceeding.
597✔
1785
                for c.out.pb > 0 && !c.isClosed() {
1,193✔
1786
                        c.flushOutbound()
596✔
1787
                }
596✔
1788
        }
1789
        // This is to notify the readLoop that it should switch to a
1790
        // (de)compression reader.
1791
        c.in.flags.set(switchToCompression)
1,190✔
1792
        // Create the compress writer before queueing the INFO protocol for
1,190✔
1793
        // a route that did not solicit. It will make sure that that proto
1,190✔
1794
        // is sent with compression on.
1,190✔
1795
        c.out.cw = s2.NewWriter(nil, s2WriterOptions(cm)...)
1,190✔
1796
        if !didSolicit {
1,783✔
1797
                c.enqueueProto(infoProto)
593✔
1798
        }
593✔
1799
        c.mu.Unlock()
1,190✔
1800
        return true, nil
1,190✔
1801
}
1802

1803
// When getting a leaf node INFO protocol, use the provided
1804
// array of urls to update the list of possible endpoints.
1805
func (c *client) updateLeafNodeURLs(info *Info) {
1,380✔
1806
        cfg := c.leaf.remote
1,380✔
1807
        cfg.Lock()
1,380✔
1808
        defer cfg.Unlock()
1,380✔
1809

1,380✔
1810
        // We have ensured that if a remote has a WS scheme, then all are.
1,380✔
1811
        // So check if first is WS, then add WS URLs, otherwise, add non WS ones.
1,380✔
1812
        if len(cfg.URLs) > 0 && isWSURL(cfg.URLs[0]) {
1,446✔
1813
                // It does not really matter if we use "ws://" or "wss://" here since
66✔
1814
                // we will have already marked that the remote should use TLS anyway.
66✔
1815
                // But use proper scheme for log statements, etc...
66✔
1816
                proto := wsSchemePrefix
66✔
1817
                if cfg.TLS {
66✔
1818
                        proto = wsSchemePrefixTLS
×
1819
                }
×
1820
                c.doUpdateLNURLs(cfg, proto, info.WSConnectURLs)
66✔
1821
                return
66✔
1822
        }
1823
        c.doUpdateLNURLs(cfg, "nats-leaf", info.LeafNodeURLs)
1,314✔
1824
}
1825

1826
func (c *client) doUpdateLNURLs(cfg *leafNodeCfg, scheme string, URLs []string) {
1,380✔
1827
        cfg.urls = make([]*url.URL, 0, 1+len(URLs))
1,380✔
1828
        // Add the ones we receive in the protocol
1,380✔
1829
        for _, surl := range URLs {
3,787✔
1830
                url, err := url.Parse(fmt.Sprintf("%s://%s", scheme, surl))
2,407✔
1831
                if err != nil {
2,407✔
1832
                        // As per below, the URLs we receive should not have contained URL info, so this should be safe to log.
×
1833
                        c.Errorf("Error parsing url %q: %v", surl, err)
×
1834
                        continue
×
1835
                }
1836
                // Do not add if it's the same as what we already have configured.
1837
                var dup bool
2,407✔
1838
                for _, u := range cfg.URLs {
6,063✔
1839
                        // URLs that we receive never have user info, but the
3,656✔
1840
                        // ones that were configured may have. Simply compare
3,656✔
1841
                        // host and port to decide if they are equal or not.
3,656✔
1842
                        if url.Host == u.Host && url.Port() == u.Port() {
5,399✔
1843
                                dup = true
1,743✔
1844
                                break
1,743✔
1845
                        }
1846
                }
1847
                if !dup {
3,071✔
1848
                        cfg.urls = append(cfg.urls, url)
664✔
1849
                        cfg.saveTLSHostname(url)
664✔
1850
                }
664✔
1851
        }
1852
        // Add the configured one
1853
        cfg.urls = append(cfg.urls, cfg.URLs...)
1,380✔
1854
}
1855

1856
// Similar to setInfoHostPortAndGenerateJSON, but for leafNodeInfo.
1857
func (s *Server) setLeafNodeInfoHostPortAndIP() error {
4,048✔
1858
        opts := s.getOpts()
4,048✔
1859
        if opts.LeafNode.Advertise != _EMPTY_ {
4,059✔
1860
                advHost, advPort, err := parseHostPort(opts.LeafNode.Advertise, opts.LeafNode.Port)
11✔
1861
                if err != nil {
11✔
1862
                        return err
×
1863
                }
×
1864
                s.leafNodeInfo.Host = advHost
11✔
1865
                s.leafNodeInfo.Port = advPort
11✔
1866
        } else {
4,037✔
1867
                s.leafNodeInfo.Host = opts.LeafNode.Host
4,037✔
1868
                s.leafNodeInfo.Port = opts.LeafNode.Port
4,037✔
1869
                // If the host is "0.0.0.0" or "::" we need to resolve to a public IP.
4,037✔
1870
                // This will return at most 1 IP.
4,037✔
1871
                hostIsIPAny, ips, err := s.getNonLocalIPsIfHostIsIPAny(s.leafNodeInfo.Host, false)
4,037✔
1872
                if err != nil {
4,037✔
1873
                        return err
×
1874
                }
×
1875
                if hostIsIPAny {
4,353✔
1876
                        if len(ips) == 0 {
316✔
1877
                                s.Errorf("Could not find any non-local IP for leafnode's listen specification %q",
×
1878
                                        s.leafNodeInfo.Host)
×
1879
                        } else {
316✔
1880
                                // Take the first from the list...
316✔
1881
                                s.leafNodeInfo.Host = ips[0]
316✔
1882
                        }
316✔
1883
                }
1884
        }
1885
        // Use just host:port for the IP
1886
        s.leafNodeInfo.IP = net.JoinHostPort(s.leafNodeInfo.Host, strconv.Itoa(s.leafNodeInfo.Port))
4,048✔
1887
        if opts.LeafNode.Advertise != _EMPTY_ {
4,059✔
1888
                s.Noticef("Advertise address for leafnode is set to %s", s.leafNodeInfo.IP)
11✔
1889
        }
11✔
1890
        return nil
4,048✔
1891
}
1892

1893
// Add the connection to the map of leaf nodes.
1894
// If `checkForDup` is true (invoked when a leafnode is accepted), then we check
1895
// if a connection already exists for the same server name and account.
1896
// That can happen when the remote is attempting to reconnect while the accepting
1897
// side did not detect the connection as broken yet.
1898
// But it can also happen when there is a misconfiguration and the remote is
1899
// creating two (or more) connections that bind to the same account on the accept
1900
// side.
1901
// When a duplicate is found, the new connection is accepted and the old is closed
1902
// (this solves the stale connection situation). An error is returned to help the
1903
// remote detect the misconfiguration when the duplicate is the result of that
1904
// misconfiguration.
1905
func (s *Server) addLeafNodeConnection(c *client, srvName, clusterName string, checkForDup bool) bool {
1,371✔
1906
        var accName string
1,371✔
1907
        c.mu.Lock()
1,371✔
1908
        cid := c.cid
1,371✔
1909
        acc := c.acc
1,371✔
1910
        if acc != nil {
2,742✔
1911
                accName = acc.Name
1,371✔
1912
        }
1,371✔
1913
        myRemoteDomain := c.leaf.remoteDomain
1,371✔
1914
        mySrvName := c.leaf.remoteServer
1,371✔
1915
        remoteAccName := c.leaf.remoteAccName
1,371✔
1916
        myClustName := c.leaf.remoteCluster
1,371✔
1917
        remote := c.leaf.remote
1,371✔
1918
        solicited := remote != nil
1,371✔
1919
        c.mu.Unlock()
1,371✔
1920

1,371✔
1921
        var old *client
1,371✔
1922
        s.mu.Lock()
1,371✔
1923
        // We check for empty because in some test we may send empty CONNECT{}
1,371✔
1924
        if checkForDup && srvName != _EMPTY_ {
2,040✔
1925
                for _, ol := range s.leafs {
1,050✔
1926
                        ol.mu.Lock()
381✔
1927
                        // We care here only about non solicited Leafnode. This function
381✔
1928
                        // is more about replacing stale connections than detecting loops.
381✔
1929
                        // We have code for the loop detection elsewhere, which also delays
381✔
1930
                        // attempt to reconnect.
381✔
1931
                        if !ol.isSolicitedLeafNode() && ol.leaf.remoteServer == srvName &&
381✔
1932
                                ol.leaf.remoteCluster == clusterName && ol.acc.Name == accName &&
381✔
1933
                                remoteAccName != _EMPTY_ && ol.leaf.remoteAccName == remoteAccName {
383✔
1934
                                old = ol
2✔
1935
                        }
2✔
1936
                        ol.mu.Unlock()
381✔
1937
                        if old != nil {
383✔
1938
                                break
2✔
1939
                        }
1940
                }
1941
        }
1942
        // Now that we are under the server lock and before adding it to the map,
1943
        // for a solicited leaf, we need to make sure that it has not been removed
1944
        // from the config or disabled.
1945
        if solicited {
2,034✔
1946
                // If no longer valid, do not add to the server map. The connection
663✔
1947
                // should have been marked so that it can't reconnect. When the caller
663✔
1948
                // calls closeConnection(), cleanup (including clearing the connect-
663✔
1949
                // in-progress flag) will occur at the appropriate time.
663✔
1950
                if !remote.stillValid() {
663✔
1951
                        // Prevent reconnect in case it was not yet done.
×
1952
                        c.setNoReconnect()
×
1953
                        s.mu.Unlock()
×
1954
                        s.removeFromTempClients(cid)
×
1955
                        return false
×
1956
                }
×
1957
                remote.setConnectInProgress(false)
663✔
1958
        }
1959
        // Store new connection in the map
1960
        s.leafs[cid] = c
1,371✔
1961
        s.mu.Unlock()
1,371✔
1962
        s.removeFromTempClients(cid)
1,371✔
1963

1,371✔
1964
        // If applicable, evict the old one.
1,371✔
1965
        if old != nil {
1,373✔
1966
                old.sendErrAndErr(DuplicateRemoteLeafnodeConnection.String())
2✔
1967
                old.closeConnection(DuplicateRemoteLeafnodeConnection)
2✔
1968
                c.Warnf("Replacing connection from same server")
2✔
1969
        }
2✔
1970

1971
        srvDecorated := func() string {
1,586✔
1972
                if myClustName == _EMPTY_ {
242✔
1973
                        return mySrvName
27✔
1974
                }
27✔
1975
                return fmt.Sprintf("%s/%s", mySrvName, myClustName)
188✔
1976
        }
1977

1978
        opts := s.getOpts()
1,371✔
1979
        sysAcc := s.SystemAccount()
1,371✔
1980
        js := s.getJetStream()
1,371✔
1981
        var meta *raft
1,371✔
1982
        if js != nil {
1,930✔
1983
                if mg := js.getMetaGroup(); mg != nil {
992✔
1984
                        meta = mg.(*raft)
433✔
1985
                }
433✔
1986
        }
1987
        blockMappingOutgoing := false
1,371✔
1988
        // Deny (non domain) JetStream API traffic unless system account is shared
1,371✔
1989
        // and domain names are identical and extending is not disabled
1,371✔
1990

1,371✔
1991
        // Check if backwards compatibility has been enabled and needs to be acted on
1,371✔
1992
        forceSysAccDeny := false
1,371✔
1993
        if len(opts.JsAccDefaultDomain) > 0 {
1,409✔
1994
                if acc == sysAcc {
49✔
1995
                        for _, d := range opts.JsAccDefaultDomain {
22✔
1996
                                if d == _EMPTY_ {
19✔
1997
                                        // Extending JetStream via leaf node is mutually exclusive with a domain mapping to the empty/default domain.
8✔
1998
                                        // As soon as one mapping to "" is found, disable the ability to extend JS via a leaf node.
8✔
1999
                                        c.Noticef("Not extending remote JetStream domain %q due to presence of empty default domain", myRemoteDomain)
8✔
2000
                                        forceSysAccDeny = true
8✔
2001
                                        break
8✔
2002
                                }
2003
                        }
2004
                } else if domain, ok := opts.JsAccDefaultDomain[accName]; ok && domain == _EMPTY_ {
43✔
2005
                        // for backwards compatibility with old setups that do not have a domain name set
16✔
2006
                        c.Debugf("Skipping deny %q for account %q due to default domain", jsAllAPI, accName)
16✔
2007
                        return true
16✔
2008
                }
16✔
2009
        }
2010

2011
        // If the server has JS disabled, it may still be part of a JetStream that could be extended.
2012
        // This is either signaled by js being disabled and a domain set,
2013
        // or in cases where no domain name exists, an extension hint is set.
2014
        // However, this is only relevant in mixed setups.
2015
        //
2016
        // If the system account connects but default domains are present, JetStream can't be extended.
2017
        if opts.JetStreamDomain != myRemoteDomain || (!opts.JetStream && (opts.JetStreamDomain == _EMPTY_ && opts.JetStreamExtHint != jsWillExtend)) ||
1,355✔
2018
                sysAcc == nil || acc == nil || forceSysAccDeny {
2,551✔
2019
                // If domain names mismatch always deny. This applies to system accounts as well as non system accounts.
1,196✔
2020
                // Not having a system account, account or JetStream disabled is considered a mismatch as well.
1,196✔
2021
                if acc != nil && acc == sysAcc {
1,339✔
2022
                        c.Noticef("System account connected from %s", srvDecorated())
143✔
2023
                        c.Noticef("JetStream not extended, domains differ")
143✔
2024
                        c.mergeDenyPermissionsLocked(both, denyAllJs)
143✔
2025
                        // When a remote with a system account is present in a server, unless otherwise disabled, the server will be
143✔
2026
                        // started in observer mode. Now that it is clear that this not used, turn the observer mode off.
143✔
2027
                        if solicited && meta != nil && meta.IsObserver() {
173✔
2028
                                meta.setObserver(false, extNotExtended)
30✔
2029
                                c.Debugf("Turning JetStream metadata controller Observer Mode off")
30✔
2030
                                // Take note that the domain was not extended to avoid this state from startup.
30✔
2031
                                writePeerState(js.config.StoreDir, meta.currentPeerState())
30✔
2032
                                // Meta controller can't be leader yet.
30✔
2033
                                // Yet it is possible that due to observer mode every server already stopped campaigning.
30✔
2034
                                // Therefore this server needs to be kicked into campaigning gear explicitly.
30✔
2035
                                meta.Campaign()
30✔
2036
                        }
30✔
2037
                } else {
1,053✔
2038
                        c.Noticef("JetStream using domains: local %q, remote %q", opts.JetStreamDomain, myRemoteDomain)
1,053✔
2039
                        c.mergeDenyPermissionsLocked(both, denyAllClientJs)
1,053✔
2040
                }
1,053✔
2041
                blockMappingOutgoing = true
1,196✔
2042
        } else if acc == sysAcc {
231✔
2043
                // system account and same domain
72✔
2044
                s.sys.client.Noticef("Extending JetStream domain %q as System Account connected from server %s",
72✔
2045
                        myRemoteDomain, srvDecorated())
72✔
2046
                // In an extension use case, pin leadership to server remotes connect to.
72✔
2047
                // Therefore, server with a remote that are not already in observer mode, need to be put into it.
72✔
2048
                if solicited && meta != nil && !meta.IsObserver() {
76✔
2049
                        c.Debugf("Turning JetStream metadata controller Observer Mode on - System Account Connected")
4✔
2050
                        // Discard any local metagroup state accumulated before the SYS-account
4✔
2051
                        // leaf came up (e.g. the wrong-hint case where this server bootstrapped
4✔
2052
                        // its own metagroup). The parent's view is now authoritative; without
4✔
2053
                        // this reset the two raft logs stay forked because the standalone log's
4✔
2054
                        // commit prefix short-circuits the follower's AE handling.
4✔
2055
                        meta.setObserver(true, extExtended)
4✔
2056
                        meta.Reset()
4✔
2057
                }
4✔
2058
        } else {
87✔
2059
                // This deny is needed in all cases (system account shared or not)
87✔
2060
                // If the system account is shared, jsAllAPI traffic will go through the system account.
87✔
2061
                // So in order to prevent duplicate delivery (from system and actual account) suppress it on the account.
87✔
2062
                // If the system account is NOT shared, jsAllAPI traffic has no business
87✔
2063
                c.Debugf("Adding deny %+v for account %q", denyAllClientJs, accName)
87✔
2064
                c.mergeDenyPermissionsLocked(both, denyAllClientJs)
87✔
2065
        }
87✔
2066
        // If we have a specified JetStream domain we will want to add a mapping to
2067
        // allow access cross domain for each non-system account.
2068
        if opts.JetStreamDomain != _EMPTY_ && opts.JetStream && acc != nil && acc != sysAcc {
1,618✔
2069
                for src, dest := range generateJSMappingTable(opts.JetStreamDomain) {
2,630✔
2070
                        if err := acc.AddMapping(src, dest); err != nil {
2,367✔
2071
                                c.Debugf("Error adding JetStream domain mapping: %s", err.Error())
×
2072
                        } else {
2,367✔
2073
                                c.Debugf("Adding JetStream Domain Mapping %q -> %s to account %q", src, dest, accName)
2,367✔
2074
                        }
2,367✔
2075
                }
2076
                if blockMappingOutgoing {
495✔
2077
                        src := fmt.Sprintf(jsDomainAPI, opts.JetStreamDomain)
232✔
2078
                        // make sure that messages intended for this domain, do not leave the cluster via this leaf node connection
232✔
2079
                        // This is a guard against a miss-config with two identical domain names and will only cover some forms
232✔
2080
                        // of this issue, not all of them.
232✔
2081
                        // This guards against a hub and a spoke having the same domain name.
232✔
2082
                        // But not two spokes having the same one and the request coming from the hub.
232✔
2083
                        c.mergeDenyPermissionsLocked(pub, []string{src})
232✔
2084
                        c.Debugf("Adding deny %q for outgoing messages to account %q", src, accName)
232✔
2085
                }
232✔
2086
        }
2087
        return true
1,355✔
2088
}
2089

2090
func (s *Server) removeLeafNodeConnection(c *client) {
1,740✔
2091
        s.mu.Lock()
1,740✔
2092
        c.mu.Lock()
1,740✔
2093
        cid := c.cid
1,740✔
2094
        if c.leaf != nil {
3,478✔
2095
                if c.leaf.tsubt != nil {
2,987✔
2096
                        c.leaf.tsubt.Stop()
1,249✔
2097
                        c.leaf.tsubt = nil
1,249✔
2098
                }
1,249✔
2099
                if c.leaf.gwSub != nil {
2,401✔
2100
                        s.gwLeafSubs.Remove(c.leaf.gwSub)
663✔
2101
                        // We need to set this to nil for GC to release the connection
663✔
2102
                        c.leaf.gwSub = nil
663✔
2103
                }
663✔
2104
                if remote := c.leaf.remote; remote != nil {
2,562✔
2105
                        // If "noReconnect" is true, then we won't attempt to reconnect, so
824✔
2106
                        // we will clear the "connect-in-progress" flag. However, if we can
824✔
2107
                        // reconnect, then we should set "connect-in-progress" to true while
824✔
2108
                        // we are under the server/client lock. The go routine that performs
824✔
2109
                        // the reconnect will be started later and there would be a gap with
824✔
2110
                        // the wrong flag value otherwise.
824✔
2111
                        remote.setConnectInProgress(!c.flags.isSet(noReconnect))
824✔
2112
                }
824✔
2113
        }
2114
        proxyKey := c.proxyKey
1,740✔
2115
        c.mu.Unlock()
1,740✔
2116
        delete(s.leafs, cid)
1,740✔
2117
        if proxyKey != _EMPTY_ {
1,744✔
2118
                s.removeProxiedConn(proxyKey, cid)
4✔
2119
        }
4✔
2120
        s.mu.Unlock()
1,740✔
2121
        s.removeFromTempClients(cid)
1,740✔
2122
}
2123

2124
// Connect information for solicited leafnodes.
2125
type leafConnectInfo struct {
2126
        Version   string   `json:"version,omitempty"`
2127
        Nkey      string   `json:"nkey,omitempty"`
2128
        JWT       string   `json:"jwt,omitempty"`
2129
        Sig       string   `json:"sig,omitempty"`
2130
        User      string   `json:"user,omitempty"`
2131
        Pass      string   `json:"pass,omitempty"`
2132
        Token     string   `json:"auth_token,omitempty"`
2133
        ID        string   `json:"server_id,omitempty"`
2134
        Domain    string   `json:"domain,omitempty"`
2135
        Name      string   `json:"name,omitempty"`
2136
        Hub       bool     `json:"is_hub,omitempty"`
2137
        Cluster   string   `json:"cluster,omitempty"`
2138
        Headers   bool     `json:"headers,omitempty"`
2139
        JetStream bool     `json:"jetstream,omitempty"`
2140
        DenyPub   []string `json:"deny_pub,omitempty"`
2141
        Isolate   bool     `json:"isolate,omitempty"`
2142

2143
        // There was an existing field called:
2144
        // >> Comp bool `json:"compression,omitempty"`
2145
        // that has never been used. With support for compression, we now need
2146
        // a field that is a string. So we use a different json tag:
2147
        Compression string `json:"compress_mode,omitempty"`
2148

2149
        // Just used to detect wrong connection attempts.
2150
        Gateway string `json:"gateway,omitempty"`
2151

2152
        // Tells the accept side which account the remote is binding to.
2153
        RemoteAccount string `json:"remote_account,omitempty"`
2154

2155
        // The accept side of a LEAF connection, unlike ROUTER and GATEWAY, receives
2156
        // only the CONNECT protocol, and no INFO. So we need to send the protocol
2157
        // version as part of the CONNECT. It will indicate if a connection supports
2158
        // some features, such as message tracing.
2159
        // We use `protocol` as the JSON tag, so this is automatically unmarshal'ed
2160
        // in the low level process CONNECT.
2161
        Proto int `json:"protocol,omitempty"`
2162
}
2163

2164
// processLeafNodeConnect will process the inbound connect args.
2165
// Once we are here we are bound to an account, so can send any interest that
2166
// we would have to the other side.
2167
func (c *client) processLeafNodeConnect(s *Server, arg []byte, lang string) error {
713✔
2168
        // Way to detect clients that incorrectly connect to the route listen
713✔
2169
        // port. Client provided "lang" in the CONNECT protocol while LEAFNODEs don't.
713✔
2170
        if lang != _EMPTY_ {
713✔
2171
                c.sendErrAndErr(ErrClientConnectedToLeafNodePort.Error())
×
2172
                c.closeConnection(WrongPort)
×
2173
                return ErrClientConnectedToLeafNodePort
×
2174
        }
×
2175

2176
        // Unmarshal as a leaf node connect protocol
2177
        proto := &leafConnectInfo{}
713✔
2178
        if err := json.Unmarshal(arg, proto); err != nil {
713✔
2179
                return err
×
2180
        }
×
2181

2182
        // Reject a cluster that contains spaces.
2183
        if proto.Cluster != _EMPTY_ && strings.Contains(proto.Cluster, " ") {
714✔
2184
                c.sendErrAndErr(ErrClusterNameHasSpaces.Error())
1✔
2185
                c.closeConnection(ProtocolViolation)
1✔
2186
                return ErrClusterNameHasSpaces
1✔
2187
        }
1✔
2188

2189
        // Check for cluster name collisions.
2190
        if cn := s.cachedClusterName(); cn != _EMPTY_ && proto.Cluster != _EMPTY_ && proto.Cluster == cn {
715✔
2191
                c.sendErrAndErr(ErrLeafNodeHasSameClusterName.Error())
3✔
2192
                c.closeConnection(ClusterNamesIdentical)
3✔
2193
                return ErrLeafNodeHasSameClusterName
3✔
2194
        }
3✔
2195

2196
        // Reject if this has Gateway which means that it would be from a gateway
2197
        // connection that incorrectly connects to the leafnode port.
2198
        if proto.Gateway != _EMPTY_ {
709✔
2199
                errTxt := fmt.Sprintf("Rejecting connection from gateway %q on the leafnode port", proto.Gateway)
×
2200
                c.Errorf(errTxt)
×
2201
                c.sendErr(errTxt)
×
2202
                c.closeConnection(WrongGateway)
×
2203
                return ErrWrongGateway
×
2204
        }
×
2205

2206
        if mv := s.getOpts().LeafNode.MinVersion; mv != _EMPTY_ {
711✔
2207
                major, minor, update, _ := versionComponents(mv)
2✔
2208
                if !versionAtLeast(proto.Version, major, minor, update) {
3✔
2209
                        // Send back an INFO so recent remote servers process the rejection
1✔
2210
                        // cleanly, then close immediately. The soliciting side applies the
1✔
2211
                        // reconnect delay when it processes the error.
1✔
2212
                        s.sendPermsAndAccountInfo(c)
1✔
2213
                        c.sendErrAndErr(fmt.Sprintf("%s %q", ErrLeafNodeMinVersionRejected, mv))
1✔
2214
                        c.closeConnection(MinimumVersionRequired)
1✔
2215
                        return ErrMinimumVersionRequired
1✔
2216
                }
1✔
2217
        }
2218

2219
        // Check if this server supports headers.
2220
        supportHeaders := c.srv.supportsHeaders()
708✔
2221

708✔
2222
        c.mu.Lock()
708✔
2223
        // Leaf Nodes do not do echo or verbose or pedantic.
708✔
2224
        c.opts.Verbose = false
708✔
2225
        c.opts.Echo = false
708✔
2226
        c.opts.Pedantic = false
708✔
2227
        // This inbound connection will be marked as supporting headers if this server
708✔
2228
        // support headers and the remote has sent in the CONNECT protocol that it does
708✔
2229
        // support headers too.
708✔
2230
        c.headers = supportHeaders && proto.Headers
708✔
2231
        // If the compression level is still not set, set it based on what has been
708✔
2232
        // given to us in the CONNECT protocol.
708✔
2233
        if c.leaf.compression == _EMPTY_ {
852✔
2234
                // But if proto.Compression is _EMPTY_, set it to CompressionNotSupported
144✔
2235
                if proto.Compression == _EMPTY_ {
186✔
2236
                        c.leaf.compression = CompressionNotSupported
42✔
2237
                } else {
144✔
2238
                        c.leaf.compression = proto.Compression
102✔
2239
                }
102✔
2240
        }
2241

2242
        // Remember the remote server.
2243
        c.leaf.remoteServer = proto.Name
708✔
2244
        // Remember the remote account name
708✔
2245
        c.leaf.remoteAccName = proto.RemoteAccount
708✔
2246
        // Remember if the leafnode requested isolation.
708✔
2247
        c.leaf.isolated = c.leaf.isolated || proto.Isolate
708✔
2248

708✔
2249
        // If the other side has declared itself a hub, so we will take on the spoke role.
708✔
2250
        if proto.Hub {
726✔
2251
                c.leaf.isSpoke = true
18✔
2252
        }
18✔
2253

2254
        // The soliciting side is part of a cluster.
2255
        if proto.Cluster != _EMPTY_ {
1,252✔
2256
                c.leaf.remoteCluster = proto.Cluster
544✔
2257
        }
544✔
2258

2259
        c.leaf.remoteDomain = proto.Domain
708✔
2260

708✔
2261
        // When a leaf solicits a connection to a hub, the perms that it will use on the soliciting leafnode's
708✔
2262
        // behalf are correct for them, but inside the hub need to be reversed since data is flowing in the opposite direction.
708✔
2263
        if !c.isSolicitedLeafNode() && c.perms != nil {
731✔
2264
                sp, pp := c.perms.sub, c.perms.pub
23✔
2265
                c.perms.sub, c.perms.pub = pp, sp
23✔
2266
                if c.opts.Import != nil {
45✔
2267
                        c.darray = c.opts.Import.Deny
22✔
2268
                } else {
23✔
2269
                        c.darray = nil
1✔
2270
                }
1✔
2271
        }
2272

2273
        // Set the Ping timer
2274
        c.setFirstPingTimer()
708✔
2275

708✔
2276
        // If we received pub deny permissions from the other end, merge with existing ones.
708✔
2277
        c.mergeDenyPermissions(pub, proto.DenyPub)
708✔
2278

708✔
2279
        acc := c.acc
708✔
2280
        c.mu.Unlock()
708✔
2281

708✔
2282
        // If the account is not set (e.g. connection was closed due to auth
708✔
2283
        // timeout while still being processed), bail out to avoid a panic.
708✔
2284
        if acc == nil {
708✔
2285
                c.closeConnection(MissingAccount)
×
2286
                return ErrMissingAccount
×
2287
        }
×
2288

2289
        // Register the cluster, even if empty, as long as we are acting as a hub.
2290
        if !proto.Hub {
1,398✔
2291
                acc.registerLeafNodeCluster(proto.Cluster)
690✔
2292
        }
690✔
2293

2294
        // Add in the leafnode here since we passed through auth at this point.
2295
        s.addLeafNodeConnection(c, proto.Name, proto.Cluster, true)
708✔
2296

708✔
2297
        // If we have permissions bound to this leafnode we need to send then back to the
708✔
2298
        // origin server for local enforcement.
708✔
2299
        s.sendPermsAndAccountInfo(c)
708✔
2300

708✔
2301
        // Create and initialize the smap since we know our bound account now.
708✔
2302
        // This will send all registered subs too.
708✔
2303
        s.initLeafNodeSmapAndSendSubs(c)
708✔
2304

708✔
2305
        // Announce the account connect event for a leaf node.
708✔
2306
        // This will be a no-op as needed.
708✔
2307
        s.sendLeafNodeConnect(c.acc)
708✔
2308

708✔
2309
        // Check to see if we need to kick any internal source or mirror consumers.
708✔
2310
        // This will be a no-op if JetStream not enabled for this server or if the bound account
708✔
2311
        // does not have jetstream.
708✔
2312
        s.checkInternalSyncConsumers(acc)
708✔
2313

708✔
2314
        return nil
708✔
2315
}
2316

2317
// checkInternalSyncConsumers
2318
func (s *Server) checkInternalSyncConsumers(acc *Account) {
2,188✔
2319
        // Grab our js
2,188✔
2320
        js := s.getJetStream()
2,188✔
2321

2,188✔
2322
        // Only applicable if we have JS and the leafnode has JS as well.
2,188✔
2323
        // We check for remote JS outside.
2,188✔
2324
        if !js.isEnabled() || acc == nil {
3,445✔
2325
                return
1,257✔
2326
        }
1,257✔
2327

2328
        // We will check all streams in our local account. They must be a leader and
2329
        // be sourcing or mirroring. We will check the external config on the stream itself
2330
        // if this is cross domain, or if the remote domain is empty, meaning we might be
2331
        // extending the system across this leafnode connection and hence we would be extending
2332
        // our own domain.
2333
        jsa := js.lookupAccount(acc)
931✔
2334
        if jsa == nil {
1,287✔
2335
                return
356✔
2336
        }
356✔
2337

2338
        var streams []*stream
575✔
2339
        jsa.mu.RLock()
575✔
2340
        for _, mset := range jsa.streams {
637✔
2341
                mset.cfgMu.RLock()
62✔
2342
                // We need to have a mirror or source defined.
62✔
2343
                // We do not want to force another lock here to look for leader status,
62✔
2344
                // so collect and after we release jsa will make sure.
62✔
2345
                if mset.cfg.Mirror != nil || len(mset.cfg.Sources) > 0 {
75✔
2346
                        streams = append(streams, mset)
13✔
2347
                }
13✔
2348
                mset.cfgMu.RUnlock()
62✔
2349
        }
2350
        jsa.mu.RUnlock()
575✔
2351

575✔
2352
        // Now loop through all candidates and check if we are the leader and have NOT
575✔
2353
        // created the sync up consumer.
575✔
2354
        for _, mset := range streams {
588✔
2355
                mset.retryDisconnectedSyncConsumers()
13✔
2356
        }
13✔
2357
}
2358

2359
// Returns the remote cluster name. This is set only once so does not require a lock.
2360
func (c *client) remoteCluster() string {
155,154✔
2361
        if c.leaf == nil {
155,154✔
2362
                return _EMPTY_
×
2363
        }
×
2364
        return c.leaf.remoteCluster
155,154✔
2365
}
2366

2367
// Sends back an info block to the soliciting leafnode to let it know about
2368
// its permission settings for local enforcement.
2369
func (s *Server) sendPermsAndAccountInfo(c *client) {
709✔
2370
        // Copy
709✔
2371
        s.mu.Lock()
709✔
2372
        info := s.copyLeafNodeInfo()
709✔
2373
        s.mu.Unlock()
709✔
2374
        c.mu.Lock()
709✔
2375
        info.CID = c.cid
709✔
2376
        info.Import = c.opts.Import
709✔
2377
        info.Export = c.opts.Export
709✔
2378
        info.RemoteAccount = c.acc.Name
709✔
2379
        // s.SystemAccount() uses an atomic operation and does not get the server lock, so this is safe.
709✔
2380
        info.IsSystemAccount = c.acc == s.SystemAccount()
709✔
2381
        info.ConnectInfo = true
709✔
2382
        c.enqueueProto(generateInfoJSON(info))
709✔
2383
        c.mu.Unlock()
709✔
2384
}
709✔
2385

2386
// Snapshot the current subscriptions from the sublist into our smap which
2387
// we will keep updated from now on.
2388
// Also send the registered subscriptions.
2389
func (s *Server) initLeafNodeSmapAndSendSubs(c *client) {
1,371✔
2390
        acc := c.acc
1,371✔
2391
        if acc == nil {
1,371✔
2392
                c.Debugf("Leafnode does not have an account bound")
×
2393
                return
×
2394
        }
×
2395
        // Collect all account subs here.
2396
        _subs := [1024]*subscription{}
1,371✔
2397
        subs := _subs[:0]
1,371✔
2398
        ims := []string{}
1,371✔
2399

1,371✔
2400
        // Hold the client lock otherwise there can be a race and miss some subs.
1,371✔
2401
        c.mu.Lock()
1,371✔
2402
        defer c.mu.Unlock()
1,371✔
2403

1,371✔
2404
        acc.mu.RLock()
1,371✔
2405
        accName := acc.Name
1,371✔
2406
        accNTag := acc.nameTag
1,371✔
2407

1,371✔
2408
        // To make printing look better when no friendly name present.
1,371✔
2409
        if accNTag != _EMPTY_ {
1,383✔
2410
                accNTag = "/" + accNTag
12✔
2411
        }
12✔
2412

2413
        // If we are solicited we only send interest for local clients.
2414
        if c.isSpokeLeafNode() {
2,034✔
2415
                acc.sl.localSubs(&subs, true)
663✔
2416
        } else {
1,371✔
2417
                acc.sl.All(&subs)
708✔
2418
        }
708✔
2419

2420
        // Check if we have an existing service import reply.
2421
        siReply := copyBytes(acc.siReply)
1,371✔
2422

1,371✔
2423
        // Since leaf nodes only send on interest, if the bound
1,371✔
2424
        // account has import services we need to send those over.
1,371✔
2425
        for isubj := range acc.imports.services {
6,501✔
2426
                if c.isSpokeLeafNode() && !c.canSubscribe(isubj) {
5,435✔
2427
                        c.Debugf("Not permitted to import service %q on behalf of %s%s", isubj, accName, accNTag)
305✔
2428
                        continue
305✔
2429
                }
2430
                ims = append(ims, isubj)
4,825✔
2431
        }
2432
        // Likewise for mappings.
2433
        for _, m := range acc.mappings {
3,860✔
2434
                if c.isSpokeLeafNode() && !c.canSubscribe(m.src) {
2,535✔
2435
                        c.Debugf("Not permitted to import mapping %q on behalf of %s%s", m.src, accName, accNTag)
46✔
2436
                        continue
46✔
2437
                }
2438
                ims = append(ims, m.src)
2,443✔
2439
        }
2440

2441
        // Create a unique subject that will be used for loop detection.
2442
        lds := acc.lds
1,371✔
2443
        acc.mu.RUnlock()
1,371✔
2444

1,371✔
2445
        // Check if we have to create the LDS.
1,371✔
2446
        if lds == _EMPTY_ {
2,454✔
2447
                lds = leafNodeLoopDetectionSubjectPrefix + nuid.Next()
1,083✔
2448
                acc.mu.Lock()
1,083✔
2449
                acc.lds = lds
1,083✔
2450
                acc.mu.Unlock()
1,083✔
2451
        }
1,083✔
2452

2453
        // Now check for gateway interest. Leafnodes will put this into
2454
        // the proper mode to propagate, but they are not held in the account.
2455
        gwsa := [16]*client{}
1,371✔
2456
        gws := gwsa[:0]
1,371✔
2457
        s.getOutboundGatewayConnections(&gws)
1,371✔
2458
        for _, cgw := range gws {
1,454✔
2459
                cgw.mu.Lock()
83✔
2460
                gw := cgw.gw
83✔
2461
                cgw.mu.Unlock()
83✔
2462
                if gw != nil {
166✔
2463
                        if ei, _ := gw.outsim.Load(accName); ei != nil {
166✔
2464
                                if e := ei.(*outsie); e != nil && e.sl != nil {
166✔
2465
                                        e.sl.All(&subs)
83✔
2466
                                }
83✔
2467
                        }
2468
                }
2469
        }
2470

2471
        applyGlobalRouting := s.gateway.enabled
1,371✔
2472
        if c.isSpokeLeafNode() {
2,034✔
2473
                // Add a fake subscription for this solicited leafnode connection
663✔
2474
                // so that we can send back directly for mapped GW replies.
663✔
2475
                // We need to keep track of this subscription so it can be removed
663✔
2476
                // when the connection is closed so that the GC can release it.
663✔
2477
                c.leaf.gwSub = &subscription{client: c, subject: []byte(gwReplyPrefix + ">")}
663✔
2478
                c.srv.gwLeafSubs.Insert(c.leaf.gwSub)
663✔
2479
        }
663✔
2480

2481
        // Now walk the results and add them to our smap
2482
        rc := c.leaf.remoteCluster
1,371✔
2483
        c.leaf.smap = make(map[string]int32)
1,371✔
2484
        for _, sub := range subs {
40,473✔
2485
                // Check perms regardless of role.
39,102✔
2486
                if c.perms != nil && !c.canSubscribe(string(sub.subject)) {
41,543✔
2487
                        c.Debugf("Not permitted to subscribe to %q on behalf of %s%s", sub.subject, accName, accNTag)
2,441✔
2488
                        continue
2,441✔
2489
                }
2490
                // Don't advertise interest from leafnodes to other isolated leafnodes.
2491
                if sub.client.kind == LEAF && c.isIsolatedLeafNode() {
36,672✔
2492
                        continue
11✔
2493
                }
2494
                // We ignore ourselves here.
2495
                // Also don't add the subscription if it has a origin cluster and the
2496
                // cluster name matches the one of the client we are sending to.
2497
                if c != sub.client && (sub.origin == nil || (bytesToString(sub.origin) != rc)) {
67,853✔
2498
                        count := int32(1)
31,203✔
2499
                        if len(sub.queue) > 0 && sub.qw > 0 {
31,215✔
2500
                                count = sub.qw
12✔
2501
                        }
12✔
2502
                        c.leaf.smap[keyFromSub(sub)] += count
31,203✔
2503
                        if c.leaf.tsub == nil {
32,490✔
2504
                                c.leaf.tsub = make(map[*subscription]struct{})
1,287✔
2505
                        }
1,287✔
2506
                        c.leaf.tsub[sub] = struct{}{}
31,203✔
2507
                }
2508
        }
2509
        // FIXME(dlc) - We need to update appropriately on an account claims update.
2510
        for _, isubj := range ims {
8,639✔
2511
                c.leaf.smap[isubj]++
7,268✔
2512
        }
7,268✔
2513
        // If we have gateways enabled we need to make sure the other side sends us responses
2514
        // that have been augmented from the original subscription.
2515
        // TODO(dlc) - Should we lock this down more?
2516
        if applyGlobalRouting {
1,475✔
2517
                c.leaf.smap[oldGWReplyPrefix+"*.>"]++
104✔
2518
                c.leaf.smap[gwReplyPrefix+">"]++
104✔
2519
        }
104✔
2520
        // Detect loops by subscribing to a specific subject and checking
2521
        // if this sub is coming back to us.
2522
        c.leaf.smap[lds]++
1,371✔
2523

1,371✔
2524
        // Check if we need to add an existing siReply to our map.
1,371✔
2525
        // This will be a prefix so add on the wildcard.
1,371✔
2526
        if siReply != nil {
1,391✔
2527
                wcsub := append(siReply, '>')
20✔
2528
                c.leaf.smap[string(wcsub)]++
20✔
2529
        }
20✔
2530
        // Queue all protocols. There is no max pending limit for LN connection,
2531
        // so we don't need chunking. The writes will happen from the writeLoop.
2532
        var b bytes.Buffer
1,371✔
2533
        for key, n := range c.leaf.smap {
28,936✔
2534
                c.writeLeafSub(&b, key, n)
27,565✔
2535
        }
27,565✔
2536
        if b.Len() > 0 {
2,742✔
2537
                c.enqueueProto(b.Bytes())
1,371✔
2538
        }
1,371✔
2539
        if c.leaf.tsub != nil {
2,659✔
2540
                // Clear the tsub map after 5 seconds.
1,288✔
2541
                c.leaf.tsubt = time.AfterFunc(5*time.Second, func() {
1,327✔
2542
                        c.mu.Lock()
39✔
2543
                        if c.leaf != nil {
78✔
2544
                                c.leaf.tsub = nil
39✔
2545
                                c.leaf.tsubt = nil
39✔
2546
                        }
39✔
2547
                        c.mu.Unlock()
39✔
2548
                })
2549
        }
2550
}
2551

2552
// updateInterestForAccountOnGateway called from gateway code when processing RS+ and RS-.
2553
func (s *Server) updateInterestForAccountOnGateway(accName string, sub *subscription, delta int32) {
206,766✔
2554
        // Since we're in the gateway's readLoop, and we would otherwise block, don't allow fetching.
206,766✔
2555
        acc, err := s.lookupOrFetchAccount(accName, false)
206,766✔
2556
        if acc == nil || err != nil {
207,120✔
2557
                s.Debugf("No or bad account for %q, failed to update interest from gateway", accName)
354✔
2558
                return
354✔
2559
        }
354✔
2560
        acc.updateLeafNodes(sub, delta)
206,412✔
2561
}
2562

2563
// updateLeafNodesEx will make sure to update the account smap for the subscription.
2564
// Will also forward to all leaf nodes as needed.
2565
// If `hubOnly` is true, then will update only leaf nodes that connect to this server
2566
// (that is, for which this server acts as a hub to them).
2567
func (acc *Account) updateLeafNodesEx(sub *subscription, delta int32, hubOnly bool) {
2,518,280✔
2568
        if acc == nil || sub == nil {
2,518,280✔
2569
                return
×
2570
        }
×
2571

2572
        // We will do checks for no leafnodes and same cluster here inline and under the
2573
        // general account read lock.
2574
        // If we feel we need to update the leafnodes we will do that out of line to avoid
2575
        // blocking routes or GWs.
2576

2577
        acc.mu.RLock()
2,518,280✔
2578
        // First check if we even have leafnodes here.
2,518,280✔
2579
        if acc.nleafs == 0 {
4,964,389✔
2580
                acc.mu.RUnlock()
2,446,109✔
2581
                return
2,446,109✔
2582
        }
2,446,109✔
2583

2584
        // Is this a loop detection subject.
2585
        isLDS := bytes.HasPrefix(sub.subject, []byte(leafNodeLoopDetectionSubjectPrefix))
72,171✔
2586

72,171✔
2587
        // Capture the cluster even if its empty.
72,171✔
2588
        var cluster string
72,171✔
2589
        if sub.origin != nil {
123,320✔
2590
                cluster = bytesToString(sub.origin)
51,149✔
2591
        }
51,149✔
2592

2593
        // If we have an isolated cluster we can return early, as long as it is not a loop detection subject.
2594
        // Empty clusters will return false for the check.
2595
        if !isLDS && acc.isLeafNodeClusterIsolated(cluster) {
93,941✔
2596
                acc.mu.RUnlock()
21,770✔
2597
                return
21,770✔
2598
        }
21,770✔
2599

2600
        // We can release the general account lock.
2601
        acc.mu.RUnlock()
50,401✔
2602

50,401✔
2603
        // We can hold the list lock here to avoid having to copy a large slice.
50,401✔
2604
        acc.lmu.RLock()
50,401✔
2605
        defer acc.lmu.RUnlock()
50,401✔
2606

50,401✔
2607
        // Do this once.
50,401✔
2608
        subject := string(sub.subject)
50,401✔
2609

50,401✔
2610
        // Walk the connected leafnodes from a random starting point to avoid
50,401✔
2611
        // concurrent callers all contending over leafs in the same order.
50,401✔
2612
        nleafs := len(acc.lleafs)
50,401✔
2613
        start := 0
50,401✔
2614
        if nleafs > 1 {
58,083✔
2615
                start = rand.Intn(nleafs)
7,682✔
2616
        }
7,682✔
2617
        for i := 0; i < nleafs; i++ {
112,243✔
2618
                ln := acc.lleafs[(start+i)%nleafs]
61,842✔
2619
                if ln == sub.client {
93,669✔
2620
                        continue
31,827✔
2621
                }
2622
                ln.mu.RLock()
30,015✔
2623
                // Don't advertise interest from leafnodes to other isolated leafnodes.
30,015✔
2624
                if sub.client.kind == LEAF && ln.isIsolatedLeafNode() {
30,051✔
2625
                        ln.mu.RUnlock()
36✔
2626
                        continue
36✔
2627
                }
2628
                // If `hubOnly` is true, it means that we want to update only leafnodes
2629
                // that connect to this server (so isHubLeafNode() would return `true`).
2630
                if hubOnly && !ln.isHubLeafNode() {
29,985✔
2631
                        ln.mu.RUnlock()
6✔
2632
                        continue
6✔
2633
                }
2634
                // Check to make sure this sub does not have an origin cluster that matches the leafnode.
2635
                // If skipped, make sure that we still let go the "$LDS." subscription that allows
2636
                // the detection of loops as long as different cluster.
2637
                clusterDifferent := cluster != ln.remoteCluster()
29,973✔
2638
                update := (isLDS && clusterDifferent) ||
29,973✔
2639
                        ((cluster == _EMPTY_ || clusterDifferent) && (delta <= 0 || ln.canSubscribeInternal(subject)))
29,973✔
2640
                ln.mu.RUnlock()
29,973✔
2641
                if update {
55,115✔
2642
                        ln.mu.Lock()
25,142✔
2643
                        // The leaf role, isolation mode, and remote cluster are stable
25,142✔
2644
                        // for the connection. Recheck canSubscribe here since permissions
25,142✔
2645
                        // can change, and to initializes mperms for wildcard subscriptions
25,142✔
2646
                        // that collide with deny rules.
25,142✔
2647
                        if isLDS || delta <= 0 || ln.canSubscribe(subject) {
50,284✔
2648
                                ln.updateSmap(sub, delta, isLDS)
25,142✔
2649
                        }
25,142✔
2650
                        ln.mu.Unlock()
25,142✔
2651
                }
2652
        }
2653
}
2654

2655
// updateLeafNodes will make sure to update the account smap for the subscription.
2656
// Will also forward to all leaf nodes as needed.
2657
func (acc *Account) updateLeafNodes(sub *subscription, delta int32) {
2,518,257✔
2658
        acc.updateLeafNodesEx(sub, delta, false)
2,518,257✔
2659
}
2,518,257✔
2660

2661
// This will make an update to our internal smap and determine if we should send out
2662
// an interest update to the remote side.
2663
// Lock should be held.
2664
func (c *client) updateSmap(sub *subscription, delta int32, isLDS bool) {
25,142✔
2665
        if c.leaf.smap == nil {
25,161✔
2666
                return
19✔
2667
        }
19✔
2668

2669
        // If we are solicited make sure this is a local client or a non-solicited leaf node
2670
        skind := sub.client.kind
25,123✔
2671
        updateClient := skind == CLIENT || skind == SYSTEM || skind == JETSTREAM || skind == ACCOUNT
25,123✔
2672
        if !isLDS && c.isSpokeLeafNode() && !(updateClient || (skind == LEAF && !sub.client.isSpokeLeafNode())) {
33,591✔
2673
                return
8,468✔
2674
        }
8,468✔
2675

2676
        // For additions, check if that sub has just been processed during initLeafNodeSmapAndSendSubs
2677
        if delta > 0 && c.leaf.tsub != nil {
24,632✔
2678
                if _, present := c.leaf.tsub[sub]; present {
7,980✔
2679
                        delete(c.leaf.tsub, sub)
3✔
2680
                        if len(c.leaf.tsub) == 0 {
3✔
2681
                                c.leaf.tsub = nil
×
2682
                                c.leaf.tsubt.Stop()
×
2683
                                c.leaf.tsubt = nil
×
2684
                        }
×
2685
                        return
3✔
2686
                }
2687
        }
2688

2689
        key := keyFromSub(sub)
16,652✔
2690
        n, ok := c.leaf.smap[key]
16,652✔
2691
        if delta < 0 && !ok {
17,926✔
2692
                return
1,274✔
2693
        }
1,274✔
2694

2695
        // We will update if its a queue, if count is zero (or negative), or we were 0 and are N > 0.
2696
        update := sub.queue != nil || (n <= 0 && n+delta > 0) || (n > 0 && n+delta <= 0)
15,378✔
2697
        n += delta
15,378✔
2698
        if n > 0 {
26,864✔
2699
                c.leaf.smap[key] = n
11,486✔
2700
        } else {
15,378✔
2701
                delete(c.leaf.smap, key)
3,892✔
2702
        }
3,892✔
2703
        if update {
25,732✔
2704
                c.sendLeafNodeSubUpdate(key, n)
10,354✔
2705
        }
10,354✔
2706
}
2707

2708
// Used to force add subjects to the subject map.
2709
func (c *client) forceAddToSmap(subj string) {
13✔
2710
        c.mu.Lock()
13✔
2711
        defer c.mu.Unlock()
13✔
2712

13✔
2713
        if c.leaf.smap == nil {
13✔
2714
                return
×
2715
        }
×
2716
        n := c.leaf.smap[subj]
13✔
2717
        if n != 0 {
14✔
2718
                return
1✔
2719
        }
1✔
2720
        // Place into the map since it was not there.
2721
        c.leaf.smap[subj] = 1
12✔
2722
        c.sendLeafNodeSubUpdate(subj, 1)
12✔
2723
}
2724

2725
// Used to force remove a subject from the subject map.
2726
func (c *client) forceRemoveFromSmap(subj string) {
1✔
2727
        c.mu.Lock()
1✔
2728
        defer c.mu.Unlock()
1✔
2729

1✔
2730
        if c.leaf.smap == nil {
1✔
2731
                return
×
2732
        }
×
2733
        n := c.leaf.smap[subj]
1✔
2734
        if n == 0 {
1✔
2735
                return
×
2736
        }
×
2737
        n--
1✔
2738
        if n == 0 {
2✔
2739
                // Remove is now zero
1✔
2740
                delete(c.leaf.smap, subj)
1✔
2741
                c.sendLeafNodeSubUpdate(subj, 0)
1✔
2742
        } else {
1✔
2743
                c.leaf.smap[subj] = n
×
2744
        }
×
2745
}
2746

2747
// Send the subscription interest change to the other side.
2748
// Lock should be held.
2749
func (c *client) sendLeafNodeSubUpdate(key string, n int32) {
10,367✔
2750
        // If we are a spoke, we need to check if we are allowed to send this subscription over to the hub.
10,367✔
2751
        if c.isSpokeLeafNode() {
12,951✔
2752
                checkPerms := true
2,584✔
2753
                if len(key) > 0 && (key[0] == '$' || key[0] == '_') {
4,177✔
2754
                        if strings.HasPrefix(key, leafNodeLoopDetectionSubjectPrefix) ||
1,593✔
2755
                                strings.HasPrefix(key, oldGWReplyPrefix) ||
1,593✔
2756
                                strings.HasPrefix(key, gwReplyPrefix) {
1,690✔
2757
                                checkPerms = false
97✔
2758
                        }
97✔
2759
                }
2760
                if checkPerms {
5,071✔
2761
                        var subject string
2,487✔
2762
                        if sep := strings.IndexByte(key, ' '); sep != -1 {
2,977✔
2763
                                subject = key[:sep]
490✔
2764
                        } else {
2,487✔
2765
                                subject = key
1,997✔
2766
                        }
1,997✔
2767
                        if !c.canSubscribe(subject) {
2,496✔
2768
                                return
9✔
2769
                        }
9✔
2770
                }
2771
        }
2772
        // If we are here we can send over to the other side.
2773
        _b := [64]byte{}
10,358✔
2774
        b := bytes.NewBuffer(_b[:0])
10,358✔
2775
        c.writeLeafSub(b, key, n)
10,358✔
2776
        c.enqueueProto(b.Bytes())
10,358✔
2777
}
2778

2779
// Helper function to build the key.
2780
func keyFromSub(sub *subscription) string {
48,893✔
2781
        var sb strings.Builder
48,893✔
2782
        sb.Grow(len(sub.subject) + len(sub.queue) + 1)
48,893✔
2783
        sb.Write(sub.subject)
48,893✔
2784
        if sub.queue != nil {
52,709✔
2785
                // Just make the key subject spc group, e.g. 'foo bar'
3,816✔
2786
                sb.WriteByte(' ')
3,816✔
2787
                sb.Write(sub.queue)
3,816✔
2788
        }
3,816✔
2789
        return sb.String()
48,893✔
2790
}
2791

2792
const (
2793
        keyRoutedSub         = "R"
2794
        keyRoutedSubByte     = 'R'
2795
        keyRoutedLeafSub     = "L"
2796
        keyRoutedLeafSubByte = 'L'
2797
)
2798

2799
// Helper function to build the key that prevents collisions between normal
2800
// routed subscriptions and routed subscriptions on behalf of a leafnode.
2801
// Keys will look like this:
2802
// "R foo"          -> plain routed sub on "foo"
2803
// "R foo bar"      -> queue routed sub on "foo", queue "bar"
2804
// "L foo bar"      -> plain routed leaf sub on "foo", leaf "bar"
2805
// "L foo bar baz"  -> queue routed sub on "foo", queue "bar", leaf "baz"
2806
func keyFromSubWithOrigin(sub *subscription) string {
636,395✔
2807
        var sb strings.Builder
636,395✔
2808
        sb.Grow(2 + len(sub.origin) + 1 + len(sub.subject) + 1 + len(sub.queue))
636,395✔
2809
        leaf := len(sub.origin) > 0
636,395✔
2810
        if leaf {
653,250✔
2811
                sb.WriteByte(keyRoutedLeafSubByte)
16,855✔
2812
        } else {
636,395✔
2813
                sb.WriteByte(keyRoutedSubByte)
619,540✔
2814
        }
619,540✔
2815
        sb.WriteByte(' ')
636,395✔
2816
        sb.Write(sub.subject)
636,395✔
2817
        if sub.queue != nil {
666,919✔
2818
                sb.WriteByte(' ')
30,524✔
2819
                sb.Write(sub.queue)
30,524✔
2820
        }
30,524✔
2821
        if leaf {
653,250✔
2822
                sb.WriteByte(' ')
16,855✔
2823
                sb.Write(sub.origin)
16,855✔
2824
        }
16,855✔
2825
        return sb.String()
636,395✔
2826
}
2827

2828
// Lock should be held.
2829
func (c *client) writeLeafSub(w *bytes.Buffer, key string, n int32) {
37,923✔
2830
        if key == _EMPTY_ {
37,923✔
2831
                return
×
2832
        }
×
2833
        if n > 0 {
71,953✔
2834
                w.WriteString("LS+ " + key)
34,030✔
2835
                // Check for queue semantics, if found write n.
34,030✔
2836
                if strings.Contains(key, " ") {
36,343✔
2837
                        w.WriteString(" ")
2,313✔
2838
                        var b [12]byte
2,313✔
2839
                        var i = len(b)
2,313✔
2840
                        for l := n; l > 0; l /= 10 {
5,543✔
2841
                                i--
3,230✔
2842
                                b[i] = digits[l%10]
3,230✔
2843
                        }
3,230✔
2844
                        w.Write(b[i:])
2,313✔
2845
                        if c.trace {
2,313✔
2846
                                arg := fmt.Sprintf("%s %d", key, n)
×
2847
                                c.traceOutOp("LS+", []byte(arg))
×
2848
                        }
×
2849
                } else if c.trace {
31,920✔
2850
                        c.traceOutOp("LS+", []byte(key))
203✔
2851
                }
203✔
2852
        } else {
3,893✔
2853
                w.WriteString("LS- " + key)
3,893✔
2854
                if c.trace {
3,909✔
2855
                        c.traceOutOp("LS-", []byte(key))
16✔
2856
                }
16✔
2857
        }
2858
        w.WriteString(CR_LF)
37,923✔
2859
}
2860

2861
// processLeafSub will process an inbound sub request for the remote leaf node.
2862
func (c *client) processLeafSub(argo []byte) (err error) {
33,658✔
2863
        // Indicate activity.
33,658✔
2864
        c.in.subs++
33,658✔
2865

33,658✔
2866
        srv := c.srv
33,658✔
2867
        if srv == nil {
33,658✔
2868
                return nil
×
2869
        }
×
2870

2871
        // Copy so we do not reference a potentially large buffer
2872
        arg := make([]byte, len(argo))
33,658✔
2873
        copy(arg, argo)
33,658✔
2874

33,658✔
2875
        args := splitArg(arg)
33,658✔
2876
        sub := &subscription{client: c}
33,658✔
2877

33,658✔
2878
        delta := int32(1)
33,658✔
2879
        switch len(args) {
33,658✔
2880
        case 1:
31,444✔
2881
                sub.queue = nil
31,444✔
2882
        case 3:
2,214✔
2883
                sub.queue = args[1]
2,214✔
2884
                sub.qw = int32(parseSize(args[2]))
2,214✔
2885
                // TODO: (ik) We should have a non empty queue name and a queue
2,214✔
2886
                // weight >= 1. For 2.11, we may want to return an error if that
2,214✔
2887
                // is not the case, but for now just overwrite `delta` if queue
2,214✔
2888
                // weight is greater than 1 (it is possible after a reconnect/
2,214✔
2889
                // server restart to receive a queue weight > 1 for a new sub).
2,214✔
2890
                if sub.qw > 1 {
3,834✔
2891
                        delta = sub.qw
1,620✔
2892
                }
1,620✔
2893
        default:
×
2894
                return fmt.Errorf("processLeafSub Parse Error: '%s'", arg)
×
2895
        }
2896
        sub.subject = args[0]
33,658✔
2897

33,658✔
2898
        c.mu.Lock()
33,658✔
2899
        if c.isClosed() {
33,681✔
2900
                c.mu.Unlock()
23✔
2901
                return nil
23✔
2902
        }
23✔
2903

2904
        acc := c.acc
33,635✔
2905
        // Guard against LS+ arriving before CONNECT has been processed, which
33,635✔
2906
        // can happen when compression is enabled.
33,635✔
2907
        if acc == nil {
33,635✔
2908
                c.mu.Unlock()
×
2909
                c.sendErr("Authorization Violation")
×
2910
                c.closeConnection(ProtocolViolation)
×
2911
                return nil
×
2912
        }
×
2913
        // Check if we have a loop.
2914
        ldsPrefix := bytes.HasPrefix(sub.subject, []byte(leafNodeLoopDetectionSubjectPrefix))
33,635✔
2915

33,635✔
2916
        if ldsPrefix && bytesToString(sub.subject) == acc.getLDSubject() {
33,642✔
2917
                c.mu.Unlock()
7✔
2918
                c.handleLeafNodeLoop(true)
7✔
2919
                return nil
7✔
2920
        }
7✔
2921

2922
        // Check permissions if applicable. (but exclude the $LDS, $GR and _GR_)
2923
        checkPerms := true
33,628✔
2924
        if sub.subject[0] == '$' || sub.subject[0] == '_' {
64,325✔
2925
                if ldsPrefix ||
30,697✔
2926
                        bytes.HasPrefix(sub.subject, []byte(oldGWReplyPrefix)) ||
30,697✔
2927
                        bytes.HasPrefix(sub.subject, []byte(gwReplyPrefix)) {
32,787✔
2928
                        checkPerms = false
2,090✔
2929
                }
2,090✔
2930
        }
2931

2932
        // If we are a hub check that we can publish to this subject.
2933
        if checkPerms {
65,166✔
2934
                subj := string(sub.subject)
31,538✔
2935
                if subjectIsLiteral(subj) && !c.pubAllowedFullCheck(subj, true, true) {
31,848✔
2936
                        c.mu.Unlock()
310✔
2937
                        c.leafSubPermViolation(sub.subject)
310✔
2938
                        c.Debugf(fmt.Sprintf("Permissions Violation for Subscription to %q", sub.subject))
310✔
2939
                        return nil
310✔
2940
                }
310✔
2941
        }
2942

2943
        // Check if we have a maximum on the number of subscriptions.
2944
        if c.subsAtLimit() {
33,326✔
2945
                c.mu.Unlock()
8✔
2946
                c.maxSubsExceeded()
8✔
2947
                return nil
8✔
2948
        }
8✔
2949

2950
        // If we have an origin cluster associated mark that in the sub.
2951
        if rc := c.remoteCluster(); rc != _EMPTY_ {
62,463✔
2952
                sub.origin = []byte(rc)
29,153✔
2953
        }
29,153✔
2954

2955
        // Like Routes, we store local subs by account and subject and optionally queue name.
2956
        // If we have a queue it will have a trailing weight which we do not want.
2957
        if sub.queue != nil {
35,251✔
2958
                sub.sid = arg[:len(arg)-len(args[2])-1]
1,941✔
2959
        } else {
33,310✔
2960
                sub.sid = arg
31,369✔
2961
        }
31,369✔
2962
        key := bytesToString(sub.sid)
33,310✔
2963
        osub := c.subs[key]
33,310✔
2964
        if osub == nil {
65,133✔
2965
                c.subs[key] = sub
31,823✔
2966
                // Now place into the account sl.
31,823✔
2967
                if err := acc.sl.Insert(sub); err != nil {
31,823✔
2968
                        delete(c.subs, key)
×
2969
                        c.mu.Unlock()
×
2970
                        c.Errorf("Could not insert subscription: %v", err)
×
2971
                        c.sendErr("Invalid Subscription")
×
2972
                        return nil
×
2973
                }
×
2974
        } else if sub.queue != nil {
2,973✔
2975
                // For a queue we need to update the weight.
1,486✔
2976
                delta = sub.qw - atomic.LoadInt32(&osub.qw)
1,486✔
2977
                atomic.StoreInt32(&osub.qw, sub.qw)
1,486✔
2978
                acc.sl.UpdateRemoteQSub(osub)
1,486✔
2979
        }
1,486✔
2980
        spoke := c.isSpokeLeafNode()
33,310✔
2981
        c.mu.Unlock()
33,310✔
2982

33,310✔
2983
        // Only add in shadow subs if a new sub or qsub.
33,310✔
2984
        if osub == nil {
65,133✔
2985
                if err := c.addShadowSubscriptions(acc, sub); err != nil {
31,823✔
2986
                        c.Errorf(err.Error())
×
2987
                }
×
2988
        }
2989

2990
        // If we are not solicited, treat leaf node subscriptions similar to a
2991
        // client subscription, meaning we forward them to routes, gateways and
2992
        // other leaf nodes as needed.
2993
        if !spoke {
45,058✔
2994
                // If we are routing add to the route map for the associated account.
11,748✔
2995
                srv.updateRouteSubscriptionMap(acc, sub, delta)
11,748✔
2996
                if srv.gateway.enabled {
13,275✔
2997
                        srv.gatewayUpdateSubInterest(acc.Name, sub, delta)
1,527✔
2998
                }
1,527✔
2999
        }
3000
        // Now check on leafnode updates for other leaf nodes. We understand solicited
3001
        // and non-solicited state in this call so we will do the right thing.
3002
        acc.updateLeafNodes(sub, delta)
33,310✔
3003

33,310✔
3004
        return nil
33,310✔
3005
}
3006

3007
// If the leafnode is a solicited, set the connect delay based on default
3008
// or private option (for tests). Sends the error to the other side, log and
3009
// close the connection.
3010
func (c *client) handleLeafNodeLoop(sendErr bool) {
17✔
3011
        accName, delay := c.setLeafConnectDelayIfSoliciting(leafNodeReconnectDelayAfterLoopDetected)
17✔
3012
        errTxt := fmt.Sprintf("Loop detected for leafnode account=%q. Delaying attempt to reconnect for %v", accName, delay)
17✔
3013
        if sendErr {
26✔
3014
                c.sendErr(errTxt)
9✔
3015
        }
9✔
3016

3017
        c.Errorf(errTxt)
17✔
3018
        // If we are here with "sendErr" false, it means that this is the server
17✔
3019
        // that received the error. The other side will have closed the connection,
17✔
3020
        // but does not hurt to close here too.
17✔
3021
        c.closeConnection(ProtocolViolation)
17✔
3022
}
3023

3024
// processLeafUnsub will process an inbound unsub request for the remote leaf node.
3025
func (c *client) processLeafUnsub(arg []byte) error {
3,385✔
3026
        // Indicate any activity, so pub and sub or unsubs.
3,385✔
3027
        c.in.subs++
3,385✔
3028

3,385✔
3029
        srv := c.srv
3,385✔
3030

3,385✔
3031
        c.mu.Lock()
3,385✔
3032
        if c.isClosed() {
3,404✔
3033
                c.mu.Unlock()
19✔
3034
                return nil
19✔
3035
        }
19✔
3036

3037
        acc := c.acc
3,366✔
3038
        // Guard against LS- arriving before CONNECT has been processed.
3,366✔
3039
        if acc == nil {
3,366✔
3040
                c.mu.Unlock()
×
3041
                c.sendErr("Authorization Violation")
×
3042
                c.closeConnection(ProtocolViolation)
×
3043
                return nil
×
3044
        }
×
3045

3046
        spoke := c.isSpokeLeafNode()
3,366✔
3047
        // We store local subs by account and subject and optionally queue name.
3,366✔
3048
        // LS- will have the arg exactly as the key.
3,366✔
3049
        sub, ok := c.subs[string(arg)]
3,366✔
3050
        if !ok {
3,366✔
3051
                // If not found, don't try to update routes/gws/leaf nodes.
×
3052
                c.mu.Unlock()
×
3053
                return nil
×
3054
        }
×
3055
        delta := int32(1)
3,366✔
3056
        if len(sub.queue) > 0 {
3,777✔
3057
                delta = sub.qw
411✔
3058
        }
411✔
3059
        c.mu.Unlock()
3,366✔
3060

3,366✔
3061
        c.unsubscribe(acc, sub, true, true)
3,366✔
3062
        if !spoke {
4,390✔
3063
                // If we are routing subtract from the route map for the associated account.
1,024✔
3064
                srv.updateRouteSubscriptionMap(acc, sub, -delta)
1,024✔
3065
                // Gateways
1,024✔
3066
                if srv.gateway.enabled {
1,261✔
3067
                        srv.gatewayUpdateSubInterest(acc.Name, sub, -delta)
237✔
3068
                }
237✔
3069
        }
3070
        // Now check on leafnode updates for other leaf nodes.
3071
        acc.updateLeafNodes(sub, -delta)
3,366✔
3072
        return nil
3,366✔
3073
}
3074

3075
func (c *client) processLeafHeaderMsgArgs(arg []byte) error {
575✔
3076
        // Unroll splitArgs to avoid runtime/heap issues
575✔
3077
        args := c.argsa[:0]
575✔
3078
        start := -1
575✔
3079
        for i, b := range arg {
103,554✔
3080
                switch b {
102,979✔
3081
                case ' ', '\t', '\r', '\n':
1,650✔
3082
                        if start >= 0 {
3,300✔
3083
                                args = append(args, arg[start:i])
1,650✔
3084
                                start = -1
1,650✔
3085
                        }
1,650✔
3086
                default:
101,329✔
3087
                        if start < 0 {
103,554✔
3088
                                start = i
2,225✔
3089
                        }
2,225✔
3090
                }
3091
        }
3092
        if start >= 0 {
1,150✔
3093
                args = append(args, arg[start:])
575✔
3094
        }
575✔
3095

3096
        c.pa.arg = arg
575✔
3097
        switch len(args) {
575✔
3098
        case 0, 1, 2:
×
3099
                return fmt.Errorf("processLeafHeaderMsgArgs Parse Error: '%s'", args)
×
3100
        case 3:
93✔
3101
                c.pa.reply = nil
93✔
3102
                c.pa.queues = nil
93✔
3103
                c.pa.hdb = args[1]
93✔
3104
                c.pa.hdr = parseSize(args[1])
93✔
3105
                c.pa.szb = args[2]
93✔
3106
                c.pa.size = parseSize(args[2])
93✔
3107
        case 4:
468✔
3108
                c.pa.reply = args[1]
468✔
3109
                c.pa.queues = nil
468✔
3110
                c.pa.hdb = args[2]
468✔
3111
                c.pa.hdr = parseSize(args[2])
468✔
3112
                c.pa.szb = args[3]
468✔
3113
                c.pa.size = parseSize(args[3])
468✔
3114
        default:
14✔
3115
                // args[1] is our reply indicator. Should be + or | normally.
14✔
3116
                if len(args[1]) != 1 {
14✔
3117
                        return fmt.Errorf("processLeafHeaderMsgArgs Bad or Missing Reply Indicator: '%s'", args[1])
×
3118
                }
×
3119
                switch args[1][0] {
14✔
3120
                case '+':
4✔
3121
                        c.pa.reply = args[2]
4✔
3122
                case '|':
10✔
3123
                        c.pa.reply = nil
10✔
3124
                default:
×
3125
                        return fmt.Errorf("processLeafHeaderMsgArgs Bad or Missing Reply Indicator: '%s'", args[1])
×
3126
                }
3127
                // Grab header size.
3128
                c.pa.hdb = args[len(args)-2]
14✔
3129
                c.pa.hdr = parseSize(c.pa.hdb)
14✔
3130

14✔
3131
                // Grab size.
14✔
3132
                c.pa.szb = args[len(args)-1]
14✔
3133
                c.pa.size = parseSize(c.pa.szb)
14✔
3134

14✔
3135
                // Grab queue names.
14✔
3136
                if c.pa.reply != nil {
18✔
3137
                        c.pa.queues = args[3 : len(args)-2]
4✔
3138
                } else {
14✔
3139
                        c.pa.queues = args[2 : len(args)-2]
10✔
3140
                }
10✔
3141
        }
3142
        if c.pa.hdr < 0 {
575✔
3143
                return fmt.Errorf("processLeafHeaderMsgArgs Bad or Missing Header Size: '%s'", arg)
×
3144
        }
×
3145
        if c.pa.size < 0 {
575✔
3146
                return fmt.Errorf("processLeafHeaderMsgArgs Bad or Missing Size: '%s'", args)
×
3147
        }
×
3148
        if c.pa.hdr > c.pa.size {
575✔
3149
                return fmt.Errorf("processLeafHeaderMsgArgs Header Size larger then TotalSize: '%s'", arg)
×
3150
        }
×
3151
        maxPayload := atomic.LoadInt32(&c.mpay)
575✔
3152
        if maxPayload != jwt.NoLimit && int64(c.pa.size) > int64(maxPayload) {
576✔
3153
                c.maxPayloadViolation(c.pa.size, maxPayload)
1✔
3154
                return ErrMaxPayload
1✔
3155
        }
1✔
3156

3157
        // Common ones processed after check for arg length
3158
        c.pa.subject = args[0]
574✔
3159

574✔
3160
        return nil
574✔
3161
}
3162

3163
func (c *client) processLeafMsgArgs(arg []byte) error {
67,522✔
3164
        // Unroll splitArgs to avoid runtime/heap issues
67,522✔
3165
        args := c.argsa[:0]
67,522✔
3166
        start := -1
67,522✔
3167
        for i, b := range arg {
2,247,582✔
3168
                switch b {
2,180,060✔
3169
                case ' ', '\t', '\r', '\n':
119,066✔
3170
                        if start >= 0 {
238,132✔
3171
                                args = append(args, arg[start:i])
119,066✔
3172
                                start = -1
119,066✔
3173
                        }
119,066✔
3174
                default:
2,060,994✔
3175
                        if start < 0 {
2,247,582✔
3176
                                start = i
186,588✔
3177
                        }
186,588✔
3178
                }
3179
        }
3180
        if start >= 0 {
135,044✔
3181
                args = append(args, arg[start:])
67,522✔
3182
        }
67,522✔
3183

3184
        c.pa.arg = arg
67,522✔
3185
        switch len(args) {
67,522✔
3186
        case 0, 1:
×
3187
                return fmt.Errorf("processLeafMsgArgs Parse Error: '%s'", args)
×
3188
        case 2:
38,687✔
3189
                c.pa.reply = nil
38,687✔
3190
                c.pa.queues = nil
38,687✔
3191
                c.pa.szb = args[1]
38,687✔
3192
                c.pa.size = parseSize(args[1])
38,687✔
3193
        case 3:
6,285✔
3194
                c.pa.reply = args[1]
6,285✔
3195
                c.pa.queues = nil
6,285✔
3196
                c.pa.szb = args[2]
6,285✔
3197
                c.pa.size = parseSize(args[2])
6,285✔
3198
        default:
22,550✔
3199
                // args[1] is our reply indicator. Should be + or | normally.
22,550✔
3200
                if len(args[1]) != 1 {
22,550✔
3201
                        return fmt.Errorf("processLeafMsgArgs Bad or Missing Reply Indicator: '%s'", args[1])
×
3202
                }
×
3203
                switch args[1][0] {
22,550✔
3204
                case '+':
159✔
3205
                        c.pa.reply = args[2]
159✔
3206
                case '|':
22,391✔
3207
                        c.pa.reply = nil
22,391✔
3208
                default:
×
3209
                        return fmt.Errorf("processLeafMsgArgs Bad or Missing Reply Indicator: '%s'", args[1])
×
3210
                }
3211
                // Grab size.
3212
                c.pa.szb = args[len(args)-1]
22,550✔
3213
                c.pa.size = parseSize(c.pa.szb)
22,550✔
3214

22,550✔
3215
                // Grab queue names.
22,550✔
3216
                if c.pa.reply != nil {
22,709✔
3217
                        c.pa.queues = args[3 : len(args)-1]
159✔
3218
                } else {
22,550✔
3219
                        c.pa.queues = args[2 : len(args)-1]
22,391✔
3220
                }
22,391✔
3221
        }
3222
        if c.pa.size < 0 {
67,522✔
3223
                return fmt.Errorf("processLeafMsgArgs Bad or Missing Size: '%s'", args)
×
3224
        }
×
3225
        maxPayload := atomic.LoadInt32(&c.mpay)
67,522✔
3226
        if maxPayload != jwt.NoLimit && int64(c.pa.size) > int64(maxPayload) {
67,523✔
3227
                c.maxPayloadViolation(c.pa.size, maxPayload)
1✔
3228
                return ErrMaxPayload
1✔
3229
        }
1✔
3230

3231
        // Common ones processed after check for arg length
3232
        c.pa.subject = args[0]
67,521✔
3233

67,521✔
3234
        return nil
67,521✔
3235
}
3236

3237
// processInboundLeafMsg is called to process an inbound msg from a leaf node.
3238
func (c *client) processInboundLeafMsg(msg []byte) {
66,489✔
3239
        // Update statistics
66,489✔
3240
        // The msg includes the CR_LF, so pull back out for accounting.
66,489✔
3241
        c.in.msgs++
66,489✔
3242
        c.in.bytes += int32(len(msg) - LEN_CR_LF)
66,489✔
3243

66,489✔
3244
        srv, acc, subject := c.srv, c.acc, string(c.pa.subject)
66,489✔
3245

66,489✔
3246
        // Mostly under testing scenarios.
66,489✔
3247
        if srv == nil || acc == nil {
66,491✔
3248
                return
2✔
3249
        }
2✔
3250

3251
        // Check that leaf messages respect the subject permissions.
3252
        if c.perms != nil && !c.leafMsgAllowed() {
66,492✔
3253
                c.leafPubPermViolation(c.pa.subject)
5✔
3254
                return
5✔
3255
        }
5✔
3256

3257
        // Match the subscriptions. We will use our own L1 map if
3258
        // it's still valid, avoiding contention on the shared sublist.
3259
        var r *SublistResult
66,482✔
3260
        var ok bool
66,482✔
3261

66,482✔
3262
        genid := atomic.LoadUint64(&c.acc.sl.genid)
66,482✔
3263
        if genid == c.in.genid && c.in.results != nil {
130,527✔
3264
                r, ok = c.in.results[subject]
64,045✔
3265
        } else {
66,482✔
3266
                // Reset our L1 completely.
2,437✔
3267
                c.in.results = make(map[string]*SublistResult)
2,437✔
3268
                c.in.genid = genid
2,437✔
3269
        }
2,437✔
3270

3271
        // Go back to the sublist data structure.
3272
        if !ok {
102,671✔
3273
                r = c.acc.sl.Match(subject)
36,189✔
3274
                // Prune the results cache. Keeps us from unbounded growth. Random delete.
36,189✔
3275
                if len(c.in.results) >= maxResultCacheSize {
37,109✔
3276
                        n := 0
920✔
3277
                        for subj := range c.in.results {
31,280✔
3278
                                delete(c.in.results, subj)
30,360✔
3279
                                if n++; n > pruneSize {
31,280✔
3280
                                        break
920✔
3281
                                }
3282
                        }
3283
                }
3284
                // Then add the new cache entry.
3285
                c.in.results[subject] = r
36,189✔
3286
        }
3287

3288
        // Collect queue names if needed.
3289
        var qnames [][]byte
66,482✔
3290

66,482✔
3291
        // Check for no interest, short circuit if so.
66,482✔
3292
        // This is the fanout scale.
66,482✔
3293
        if len(r.psubs)+len(r.qsubs) > 0 {
132,507✔
3294
                flag := pmrNoFlag
66,025✔
3295
                // If we have queue subs in this cluster, then if we run in gateway
66,025✔
3296
                // mode and the remote gateways have queue subs, then we need to
66,025✔
3297
                // collect the queue groups this message was sent to so that we
66,025✔
3298
                // exclude them when sending to gateways.
66,025✔
3299
                if len(r.qsubs) > 0 && c.srv.gateway.enabled &&
66,025✔
3300
                        atomic.LoadInt64(&c.srv.gateway.totalQSubs) > 0 {
78,351✔
3301
                        flag |= pmrCollectQueueNames
12,326✔
3302
                }
12,326✔
3303
                // If this is a mapped subject that means the mapped interest
3304
                // is what got us here, but this might not have a queue designation
3305
                // If that is the case, make sure we ignore to process local queue subscribers.
3306
                if len(c.pa.mapped) > 0 && len(c.pa.queues) == 0 {
66,368✔
3307
                        flag |= pmrIgnoreEmptyQueueFilter
343✔
3308
                }
343✔
3309
                _, qnames = c.processMsgResults(acc, r, msg, nil, c.pa.subject, c.pa.reply, flag)
66,025✔
3310
        }
3311

3312
        // Now deal with gateways
3313
        if c.srv.gateway.enabled {
79,896✔
3314
                c.sendMsgToGateways(acc, msg, c.pa.subject, c.pa.reply, qnames, true)
13,414✔
3315
        }
13,414✔
3316
}
3317

3318
// Checks whether the inbound leaf message is allowed by the
3319
// connection's permissions. On the hub side this enforces what
3320
// the remote leaf may publish. On the spoke side this enforces
3321
// import restrictions such as deny_imports.
3322
func (c *client) leafMsgAllowed() bool {
62,671✔
3323
        wireSubject := c.pa.subject
62,671✔
3324
        if len(c.pa.mapped) > 0 {
63,014✔
3325
                // Mappings rewrite c.pa.subject to the internal
343✔
3326
                // destination. For leaf ACLs, need to check
343✔
3327
                // the original wire subject from the remote side.
343✔
3328
                wireSubject = c.pa.mapped
343✔
3329
        }
343✔
3330
        // Strip any gateway routing prefix for the permission check.
3331
        subjectToCheck, isGW := getGWRoutedSubjectOrSelf(wireSubject)
62,671✔
3332

62,671✔
3333
        // Service-import replies (_R_), JS ack subjects ($JS.ACK.)
62,671✔
3334
        // are internal routing subjects forwarded via LS+ without
62,671✔
3335
        // permission checks.
62,671✔
3336
        if isServiceReply(subjectToCheck) || isJSAckSubject(subjectToCheck) {
62,714✔
3337
                return true
43✔
3338
        }
43✔
3339

3340
        c.mu.RLock()
62,628✔
3341
        if c.isSpokeLeafNode() {
91,506✔
3342
                // Gateway routed replies are forwarded without
28,878✔
3343
                // permission checks.
28,878✔
3344
                if isGW || c.leafReceiveAllowed(subjectToCheck) {
57,754✔
3345
                        c.mu.RUnlock()
28,876✔
3346
                        return true
28,876✔
3347
                }
28,876✔
3348
        } else if c.leafSendAllowed(subjectToCheck) {
67,494✔
3349
                c.mu.RUnlock()
33,744✔
3350
                return true
33,744✔
3351
        }
33,744✔
3352

3353
        // If allow_responses is not configured, or there is no tracked reply for
3354
        // this subject, the answer is "denied" and we can return it while still
3355
        // holding only the read lock.
3356
        replySubject := bytesToString(wireSubject)
8✔
3357
        if c.perms == nil || c.perms.resp == nil || c.replies[replySubject] == nil {
13✔
3358
                c.mu.RUnlock()
5✔
3359
                return false
5✔
3360
        }
5✔
3361
        c.mu.RUnlock()
3✔
3362

3✔
3363
        // Check tracked reply permissions (allow_responses).
3✔
3364
        // Use the pre-strip subject since deliverMsg tracks
3✔
3365
        // replies under the original form, which includes
3✔
3366
        // the GW routing prefix for routed requests.
3✔
3367
        c.mu.Lock()
3✔
3368
        defer c.mu.Unlock()
3✔
3369
        return c.responseAllowed(replySubject)
3✔
3370
}
3371

3372
// Returns true if the leaf side ACLs allow importing this subject,
3373
// based on the permissions received over INFO and any local deny_imports.
3374
// At least a read lock must be held.
3375
func (c *client) leafReceiveAllowed(subject []byte) bool {
28,878✔
3376
        return c.canSubscribeInternal(bytesToString(subject))
28,878✔
3377
}
28,878✔
3378

3379
// Returns true if the hub side ACLs allow the remote leaf to send
3380
// this subject.
3381
// At least a read lock must be held.
3382
func (c *client) leafSendAllowed(bsubject []byte) bool {
33,750✔
3383
        // Use the original export ACL captured for this accepted leaf.
33,750✔
3384
        // The live perms also contain additional JetStream denies used by
33,750✔
3385
        // the normal forwarding path, and applying them here would reject
33,750✔
3386
        // legitimate inbound JS API requests.
33,750✔
3387
        subject := bytesToString(bsubject)
33,750✔
3388
        perms := c.opts.Export
33,750✔
3389
        if perms == nil || (perms.Allow == nil && perms.Deny == nil) {
67,475✔
3390
                return true
33,725✔
3391
        }
33,725✔
3392

3393
        allowed := true
25✔
3394
        if perms.Allow != nil && !strings.HasPrefix(subject, mqttPrefix) {
36✔
3395
                allowed = false
11✔
3396
                for _, allowSubj := range perms.Allow {
21✔
3397
                        if matchLiteral(subject, allowSubj) {
16✔
3398
                                allowed = true
6✔
3399
                                break
6✔
3400
                        }
3401
                }
3402
        }
3403

3404
        if allowed && len(perms.Deny) > 0 {
39✔
3405
                for _, denySubj := range perms.Deny {
40✔
3406
                        if matchLiteral(subject, denySubj) {
27✔
3407
                                allowed = false
1✔
3408
                                break
1✔
3409
                        }
3410
                }
3411
        }
3412
        return allowed
25✔
3413
}
3414

3415
// Handles a subscription permission violation.
3416
// See leafPermViolation() for details.
3417
func (c *client) leafSubPermViolation(subj []byte) {
310✔
3418
        c.leafPermViolation(false, subj)
310✔
3419
}
310✔
3420

3421
// Handles a publish permission violation.
3422
// See leafPermViolation() for details.
3423
func (c *client) leafPubPermViolation(subj []byte) {
5✔
3424
        c.leafPermViolation(true, subj)
5✔
3425
}
5✔
3426

3427
// Common function to process publish or subscribe leafnode permission violation.
3428
// Sends the permission violation error to the remote, logs it and closes the connection.
3429
// If this is from a server soliciting, the reconnection will be delayed.
3430
func (c *client) leafPermViolation(pub bool, subj []byte) {
315✔
3431
        if c.isSpokeLeafNode() {
627✔
3432
                // For spokes these are no-ops since the hub server told us our permissions.
312✔
3433
                // We just need to not send these over to the other side since we will get cutoff.
312✔
3434
                return
312✔
3435
        }
312✔
3436
        // FIXME(dlc) ?
3437
        c.setLeafConnectDelayIfSoliciting(leafNodeReconnectAfterPermViolation)
3✔
3438
        var action string
3✔
3439
        if pub {
6✔
3440
                c.sendErr(fmt.Sprintf("Permissions Violation for Publish to %q", subj))
3✔
3441
                action = "Publish"
3✔
3442
        } else {
3✔
3443
                c.sendErr(fmt.Sprintf("Permissions Violation for Subscription to %q", subj))
×
3444
                action = "Subscription"
×
3445
        }
×
3446
        c.Errorf("%s Violation on %q - Check other side configuration", action, subj)
3✔
3447
        // TODO: add a new close reason that is more appropriate?
3✔
3448
        c.closeConnection(ProtocolViolation)
3✔
3449
}
3450

3451
// Invoked from generic processErr() for LEAF connections.
3452
func (c *client) leafProcessErr(errStr string) {
49✔
3453
        // Check if we got a cluster name collision.
49✔
3454
        if strings.Contains(errStr, ErrLeafNodeHasSameClusterName.Error()) {
52✔
3455
                _, delay := c.setLeafConnectDelayIfSoliciting(leafNodeReconnectDelayAfterClusterNameSame)
3✔
3456
                c.Errorf("Leafnode connection dropped with same cluster name error. Delaying attempt to reconnect for %v", delay)
3✔
3457
                return
3✔
3458
        }
3✔
3459
        if strings.Contains(errStr, ErrLeafNodeMinVersionRejected.Error()) {
47✔
3460
                _, delay := c.setLeafConnectDelayIfSoliciting(leafNodeMinVersionReconnectDelay)
1✔
3461
                c.Errorf("Leafnode connection dropped due to minimum version requirement. Delaying attempt to reconnect for %v", delay)
1✔
3462
                return
1✔
3463
        }
1✔
3464

3465
        // We will look for Loop detected error coming from the other side.
3466
        // If we solicit, set the connect delay.
3467
        if !strings.Contains(errStr, "Loop detected") {
82✔
3468
                return
37✔
3469
        }
37✔
3470
        c.handleLeafNodeLoop(false)
8✔
3471
}
3472

3473
// If this leaf connection solicits, sets the connect delay to the given value,
3474
// or the one from the server option's LeafNode.connDelay if one is set (for tests).
3475
// Returns the connection's account name and delay.
3476
func (c *client) setLeafConnectDelayIfSoliciting(delay time.Duration) (string, time.Duration) {
24✔
3477
        c.mu.Lock()
24✔
3478
        if c.isSolicitedLeafNode() {
38✔
3479
                if s := c.srv; s != nil {
28✔
3480
                        if srvdelay := s.getOpts().LeafNode.connDelay; srvdelay != 0 {
19✔
3481
                                delay = srvdelay
5✔
3482
                        }
5✔
3483
                }
3484
                c.leaf.remote.setConnectDelay(delay)
14✔
3485
        }
3486
        var accName string
24✔
3487
        if c.acc != nil {
48✔
3488
                accName = c.acc.Name
24✔
3489
        }
24✔
3490
        c.mu.Unlock()
24✔
3491
        return accName, delay
24✔
3492
}
3493

3494
// For the given remote Leafnode configuration, this function returns
3495
// if TLS is required, and if so, will return a clone of the TLS Config
3496
// (since some fields will be changed during handshake), the TLS server
3497
// name that is remembered, and the TLS timeout.
3498
func (c *client) leafNodeGetTLSConfigForSolicit(remote *leafNodeCfg) (bool, *tls.Config, string, float64) {
1,984✔
3499
        var (
1,984✔
3500
                tlsConfig  *tls.Config
1,984✔
3501
                tlsName    string
1,984✔
3502
                tlsTimeout float64
1,984✔
3503
        )
1,984✔
3504

1,984✔
3505
        remote.RLock()
1,984✔
3506
        defer remote.RUnlock()
1,984✔
3507

1,984✔
3508
        tlsRequired := remote.TLS || remote.TLSConfig != nil
1,984✔
3509
        if tlsRequired {
2,057✔
3510
                if remote.TLSConfig != nil {
124✔
3511
                        tlsConfig = remote.TLSConfig.Clone()
51✔
3512
                } else {
73✔
3513
                        tlsConfig = &tls.Config{MinVersion: tls.VersionTLS12}
22✔
3514
                }
22✔
3515
                tlsName = remote.tlsName
73✔
3516
                tlsTimeout = remote.TLSTimeout
73✔
3517
                if tlsTimeout == 0 {
112✔
3518
                        tlsTimeout = float64(TLS_TIMEOUT / time.Second)
39✔
3519
                }
39✔
3520
        }
3521

3522
        return tlsRequired, tlsConfig, tlsName, tlsTimeout
1,984✔
3523
}
3524

3525
// Initiates the LeafNode Websocket connection by:
3526
// - doing the TLS handshake if needed
3527
// - sending the HTTP request
3528
// - waiting for the HTTP response
3529
//
3530
// Since some bufio reader is used to consume the HTTP response, this function
3531
// returns the slice of buffered bytes (if any) so that the readLoop that will
3532
// be started after that consume those first before reading from the socket.
3533
// The boolean
3534
//
3535
// Lock held on entry.
3536
func (c *client) leafNodeSolicitWSConnection(opts *Options, rURL *url.URL, remote *leafNodeCfg) ([]byte, ClosedState, error) {
54✔
3537
        remote.RLock()
54✔
3538
        compress := remote.Websocket.Compression
54✔
3539
        // By default the server will mask outbound frames, but it can be disabled with this option.
54✔
3540
        noMasking := remote.Websocket.NoMasking
54✔
3541
        infoTimeout := remote.FirstInfoTimeout
54✔
3542
        remote.RUnlock()
54✔
3543
        // Will do the client-side TLS handshake if needed.
54✔
3544
        tlsRequired, err := c.leafClientHandshakeIfNeeded(remote, opts)
54✔
3545
        if err != nil {
58✔
3546
                // 0 will indicate that the connection was already closed
4✔
3547
                return nil, 0, err
4✔
3548
        }
4✔
3549

3550
        // For http request, we need the passed URL to contain either http or https scheme.
3551
        scheme := "http"
50✔
3552
        if tlsRequired {
58✔
3553
                scheme = "https"
8✔
3554
        }
8✔
3555
        // We will use the `/leafnode` path to tell the accepting WS server that it should
3556
        // create a LEAF connection, not a CLIENT.
3557
        // In case we use the user's URL path in the future, make sure we append the user's
3558
        // path to our `/leafnode` path.
3559
        lpath := leafNodeWSPath
50✔
3560
        if curPath := rURL.EscapedPath(); curPath != _EMPTY_ {
71✔
3561
                if curPath[0] == '/' {
42✔
3562
                        curPath = curPath[1:]
21✔
3563
                }
21✔
3564
                lpath = path.Join(curPath, lpath)
21✔
3565
        } else {
29✔
3566
                lpath = lpath[1:]
29✔
3567
        }
29✔
3568
        ustr := fmt.Sprintf("%s://%s/%s", scheme, rURL.Host, lpath)
50✔
3569
        u, _ := url.Parse(ustr)
50✔
3570
        req := &http.Request{
50✔
3571
                Method:     "GET",
50✔
3572
                URL:        u,
50✔
3573
                Proto:      "HTTP/1.1",
50✔
3574
                ProtoMajor: 1,
50✔
3575
                ProtoMinor: 1,
50✔
3576
                Header:     make(http.Header),
50✔
3577
                Host:       u.Host,
50✔
3578
        }
50✔
3579
        wsKey, err := wsMakeChallengeKey()
50✔
3580
        if err != nil {
50✔
3581
                return nil, WriteError, err
×
3582
        }
×
3583

3584
        req.Header["Upgrade"] = []string{"websocket"}
50✔
3585
        req.Header["Connection"] = []string{"Upgrade"}
50✔
3586
        req.Header["Sec-WebSocket-Key"] = []string{wsKey}
50✔
3587
        req.Header["Sec-WebSocket-Version"] = []string{"13"}
50✔
3588
        if compress {
61✔
3589
                req.Header.Add("Sec-WebSocket-Extensions", wsPMCReqHeaderValue)
11✔
3590
        }
11✔
3591
        if noMasking {
60✔
3592
                req.Header.Add(wsNoMaskingHeader, wsNoMaskingValue)
10✔
3593
        }
10✔
3594
        c.nc.SetDeadline(time.Now().Add(infoTimeout))
50✔
3595
        if err := req.Write(c.nc); err != nil {
50✔
3596
                return nil, WriteError, err
×
3597
        }
×
3598

3599
        var resp *http.Response
50✔
3600

50✔
3601
        br := bufio.NewReaderSize(c.nc, MAX_CONTROL_LINE_SIZE)
50✔
3602
        resp, err = http.ReadResponse(br, req)
50✔
3603
        if err == nil &&
50✔
3604
                (resp.StatusCode != 101 ||
50✔
3605
                        !strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") ||
50✔
3606
                        !strings.EqualFold(resp.Header.Get("Connection"), "upgrade") ||
50✔
3607
                        resp.Header.Get("Sec-Websocket-Accept") != wsAcceptKey(wsKey)) {
51✔
3608

1✔
3609
                err = fmt.Errorf("invalid websocket connection")
1✔
3610
        }
1✔
3611
        // Check compression extension...
3612
        if err == nil && c.ws.compress {
61✔
3613
                // Check that not only permessage-deflate extension is present, but that
11✔
3614
                // we also have server and client no context take over.
11✔
3615
                srvCompress, noCtxTakeover := wsPMCExtensionSupport(resp.Header, false)
11✔
3616

11✔
3617
                // If server does not support compression, then simply disable it in our side.
11✔
3618
                if !srvCompress {
16✔
3619
                        c.ws.compress = false
5✔
3620
                } else if !noCtxTakeover {
11✔
3621
                        err = fmt.Errorf("compression negotiation error")
×
3622
                }
×
3623
        }
3624
        // Same for no masking...
3625
        if err == nil && noMasking {
60✔
3626
                // Check if server accepts no masking
10✔
3627
                if resp.Header.Get(wsNoMaskingHeader) != wsNoMaskingValue {
11✔
3628
                        // Nope, need to mask our writes as any client would do.
1✔
3629
                        c.ws.maskwrite = true
1✔
3630
                }
1✔
3631
        }
3632
        if resp != nil {
84✔
3633
                resp.Body.Close()
34✔
3634
        }
34✔
3635
        if err != nil {
67✔
3636
                return nil, ReadError, err
17✔
3637
        }
17✔
3638
        c.Debugf("Leafnode compression=%v masking=%v", c.ws.compress, c.ws.maskwrite)
33✔
3639

33✔
3640
        var preBuf []byte
33✔
3641
        // We have to slurp whatever is in the bufio reader and pass that to the readloop.
33✔
3642
        if n := br.Buffered(); n != 0 {
34✔
3643
                preBuf, _ = br.Peek(n)
1✔
3644
        }
1✔
3645
        return preBuf, 0, nil
33✔
3646
}
3647

3648
const connectProcessTimeout = 2 * time.Second
3649

3650
// This is invoked for remote LEAF remote connections after processing the INFO
3651
// protocol.
3652
func (s *Server) leafNodeResumeConnectProcess(c *client) {
703✔
3653
        clusterName := s.ClusterName()
703✔
3654

703✔
3655
        c.mu.Lock()
703✔
3656
        if c.isClosed() {
703✔
3657
                c.mu.Unlock()
×
3658
                return
×
3659
        }
×
3660
        if err := c.sendLeafConnect(clusterName, c.headers); err != nil {
705✔
3661
                c.mu.Unlock()
2✔
3662
                c.closeConnection(WriteError)
2✔
3663
                return
2✔
3664
        }
2✔
3665

3666
        // Spin up the write loop.
3667
        s.startGoRoutine(func() { c.writeLoop() })
1,402✔
3668

3669
        // timeout leafNodeFinishConnectProcess
3670
        c.ping.tmr = time.AfterFunc(connectProcessTimeout, func() {
701✔
3671
                c.mu.Lock()
×
3672
                // check if leafNodeFinishConnectProcess was called and prevent later leafNodeFinishConnectProcess
×
3673
                if !c.flags.setIfNotSet(connectProcessFinished) {
×
3674
                        c.mu.Unlock()
×
3675
                        return
×
3676
                }
×
3677
                clearTimer(&c.ping.tmr)
×
3678
                closed := c.isClosed()
×
3679
                c.mu.Unlock()
×
3680
                if !closed {
×
3681
                        c.sendErrAndDebug("Stale Leaf Node Connection - Closing")
×
3682
                        c.closeConnection(StaleConnection)
×
3683
                }
×
3684
        })
3685
        c.mu.Unlock()
701✔
3686
        c.Debugf("Remote leafnode connect msg sent")
701✔
3687
}
3688

3689
// This is invoked for remote LEAF connections after processing the INFO
3690
// protocol and leafNodeResumeConnectProcess.
3691
// This will send LS+ the CONNECT protocol and register the leaf node.
3692
func (s *Server) leafNodeFinishConnectProcess(c *client) {
666✔
3693
        c.mu.Lock()
666✔
3694
        if !c.flags.setIfNotSet(connectProcessFinished) {
666✔
3695
                c.mu.Unlock()
×
3696
                return
×
3697
        }
×
3698
        if c.isClosed() {
666✔
3699
                c.mu.Unlock()
×
3700
                s.removeLeafNodeConnection(c)
×
3701
                return
×
3702
        }
×
3703
        remote := c.leaf.remote
666✔
3704
        if remote == nil || c.acc == nil {
667✔
3705
                c.mu.Unlock()
1✔
3706
                c.sendErr("Authorization Violation")
1✔
3707
                c.closeConnection(ProtocolViolation)
1✔
3708
                return
1✔
3709
        }
1✔
3710
        // Check if we will need to send the system connect event.
3711
        remote.RLock()
665✔
3712
        sendSysConnectEvent := remote.Hub
665✔
3713
        remote.RUnlock()
665✔
3714

665✔
3715
        // Capture account before releasing lock
665✔
3716
        acc := c.acc
665✔
3717
        // cancel connectProcessTimeout
665✔
3718
        clearTimer(&c.ping.tmr)
665✔
3719
        c.mu.Unlock()
665✔
3720

665✔
3721
        // Make sure we register with the account here.
665✔
3722
        if err := c.registerWithAccount(acc); err != nil {
667✔
3723
                if err == ErrTooManyAccountConnections {
2✔
3724
                        c.maxAccountConnExceeded()
×
3725
                        return
×
3726
                } else if err == ErrLeafNodeLoop {
4✔
3727
                        c.handleLeafNodeLoop(true)
2✔
3728
                        return
2✔
3729
                }
2✔
3730
                c.Errorf("Registering leaf with account %s resulted in error: %v", acc.Name, err)
×
3731
                c.closeConnection(ProtocolViolation)
×
3732
                return
×
3733
        }
3734
        if !s.addLeafNodeConnection(c, _EMPTY_, _EMPTY_, false) {
663✔
3735
                // Was not added, could be because the remote configuration has been removed.
×
3736
                c.closeConnection(ClientClosed)
×
3737
                return
×
3738
        }
×
3739
        s.initLeafNodeSmapAndSendSubs(c)
663✔
3740
        if sendSysConnectEvent {
681✔
3741
                s.sendLeafNodeConnect(acc)
18✔
3742
        }
18✔
3743
        s.accountConnectEvent(c)
663✔
3744

663✔
3745
        // The above functions are not running under the client lock, so it is
663✔
3746
        // possible that between the time we have started the read/write loops
663✔
3747
        // and now, that the connection was closed. This would leave the closed
663✔
3748
        // LN connection possibly registered with the account and/or the server's
663✔
3749
        // leafs map. So check if connection is closed, and if so, manually cleanup.
663✔
3750
        c.mu.Lock()
663✔
3751
        closed := c.isClosed()
663✔
3752
        if !closed {
1,326✔
3753
                c.setFirstPingTimer()
663✔
3754
        }
663✔
3755
        c.mu.Unlock()
663✔
3756
        if closed {
663✔
3757
                s.removeLeafNodeConnection(c)
×
3758
                if prev := acc.removeClient(c); prev == 1 {
×
3759
                        s.decActiveAccounts()
×
3760
                }
×
3761
        }
3762
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc