Skip to content

Commit

Permalink
Adds the same check for valid stream name for Mirror
Browse files Browse the repository at this point in the history
Fix test using invalid stream names

Signed-off-by: Jean-Noël Moyne <jnmoyne@gmail.com>
  • Loading branch information
jnmoyne committed Jun 8, 2023
1 parent bd6c15d commit 7ff1141
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
10 changes: 10 additions & 0 deletions server/errors.json
Expand Up @@ -1398,5 +1398,15 @@
"help": "",
"url": "",
"deprecates": ""
},
{
"constant": "JSMirrorInvalidStreamName",
"code": 400,
"error_code": 10142,
"description": "source stream name is invalid",
"comment": "",
"help": "",
"url": "",
"deprecates": ""
}
]
14 changes: 14 additions & 0 deletions server/jetstream_errors_generated.go
Expand Up @@ -224,6 +224,9 @@ const (
// JSMirrorConsumerSetupFailedErrF generic mirror consumer setup failure string ({err})
JSMirrorConsumerSetupFailedErrF ErrorIdentifier = 10029

// JSMirrorInvalidStreamName source stream name is invalid
JSMirrorInvalidStreamName ErrorIdentifier = 10142

// JSMirrorMaxMessageSizeTooBigErr stream mirror must have max message size >= source
JSMirrorMaxMessageSizeTooBigErr ErrorIdentifier = 10030

Expand Down Expand Up @@ -501,6 +504,7 @@ var (
JSMaximumStreamsLimitErr: {Code: 400, ErrCode: 10027, Description: "maximum number of streams reached"},
JSMemoryResourcesExceededErr: {Code: 500, ErrCode: 10028, Description: "insufficient memory resources available"},
JSMirrorConsumerSetupFailedErrF: {Code: 500, ErrCode: 10029, Description: "{err}"},
JSMirrorInvalidStreamName: {Code: 400, ErrCode: 10142, Description: "source stream name is invalid"},
JSMirrorMaxMessageSizeTooBigErr: {Code: 400, ErrCode: 10030, Description: "stream mirror must have max message size >= source"},
JSMirrorWithSourcesErr: {Code: 400, ErrCode: 10031, Description: "stream mirrors can not also contain other sources"},
JSMirrorWithStartSeqAndTimeErr: {Code: 400, ErrCode: 10032, Description: "stream mirrors can not have both start seq and start time configured"},
Expand Down Expand Up @@ -1389,6 +1393,16 @@ func NewJSMirrorConsumerSetupFailedError(err error, opts ...ErrorOption) *ApiErr
}
}

// NewJSMirrorInvalidStreamNameError creates a new JSMirrorInvalidStreamName error: "source stream name is invalid"
func NewJSMirrorInvalidStreamNameError(opts ...ErrorOption) *ApiError {
eopts := parseOpts(opts)
if ae, ok := eopts.err.(*ApiError); ok {
return ae
}

return ApiErrors[JSMirrorInvalidStreamName]
}

// NewJSMirrorMaxMessageSizeTooBigError creates a new JSMirrorMaxMessageSizeTooBigErr error: "stream mirror must have max message size >= source"
func NewJSMirrorMaxMessageSizeTooBigError(opts ...ErrorOption) *ApiError {
eopts := parseOpts(opts)
Expand Down
4 changes: 2 additions & 2 deletions server/jetstream_super_cluster_test.go
Expand Up @@ -550,7 +550,7 @@ func TestJetStreamSuperClusterConnectionCount(t *testing.T) {
require_NoError(t, err)
_, err = js.AddStream(&nats.StreamConfig{
Name: "src",
Sources: []*nats.StreamSource{{Name: "foo.1"}, {Name: "foo.2"}},
Sources: []*nats.StreamSource{{Name: "foo1"}, {Name: "foo2"}},
Replicas: 3})
require_NoError(t, err)
}()
Expand All @@ -561,7 +561,7 @@ func TestJetStreamSuperClusterConnectionCount(t *testing.T) {
require_NoError(t, err)
_, err = js.AddStream(&nats.StreamConfig{
Name: "mir",
Mirror: &nats.StreamSource{Name: "foo.2"},
Mirror: &nats.StreamSource{Name: "foo2"},
Replicas: 3})
require_NoError(t, err)
}()
Expand Down
3 changes: 3 additions & 0 deletions server/stream.go
Expand Up @@ -1164,6 +1164,9 @@ func (s *Server) checkStreamCfg(config *StreamConfig, acc *Account) (StreamConfi
// Do not perform checks if External is provided, as it could lead to
// checking against itself (if sourced stream name is the same on different JetStream)
if cfg.Mirror.External == nil {
if !isValidName(cfg.Mirror.Name) {
return StreamConfig{}, NewJSMirrorInvalidStreamNameError()
}
// We do not require other stream to exist anymore, but if we can see it check payloads.
exists, maxMsgSize, subs := hasStream(cfg.Mirror.Name)
if len(subs) > 0 {
Expand Down

0 comments on commit 7ff1141

Please sign in to comment.