Skip to content

Commit

Permalink
Prevent configuring first_seq on mirrors (#4345)
Browse files Browse the repository at this point in the history
This prevents configurations where mirrors are mixed with `first_seq`s
of greater than zero.

Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
neilalexander committed Jul 31, 2023
2 parents 3b2231a + 3b9e8b9 commit d780da6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions server/errors.json
Expand Up @@ -299,6 +299,16 @@
"url": "",
"deprecates": ""
},
{
"constant": "JSMirrorWithFirstSeqErr",
"code": 400,
"error_code": 10143,
"description": "stream mirrors can not have first sequence configured",
"comment": "",
"help": "",
"url": "",
"deprecates": ""
},
{
"constant": "JSNotEnabledErr",
"code": 503,
Expand Down
14 changes: 14 additions & 0 deletions server/jetstream_errors_generated.go
Expand Up @@ -230,6 +230,9 @@ const (
// JSMirrorMaxMessageSizeTooBigErr stream mirror must have max message size >= source
JSMirrorMaxMessageSizeTooBigErr ErrorIdentifier = 10030

// JSMirrorWithFirstSeqErr stream mirrors can not have first sequence configured
JSMirrorWithFirstSeqErr ErrorIdentifier = 10143

// JSMirrorWithSourcesErr stream mirrors can not also contain other sources
JSMirrorWithSourcesErr ErrorIdentifier = 10031

Expand Down Expand Up @@ -506,6 +509,7 @@ var (
JSMirrorConsumerSetupFailedErrF: {Code: 500, ErrCode: 10029, Description: "{err}"},
JSMirrorInvalidStreamName: {Code: 400, ErrCode: 10142, Description: "mirrored stream name is invalid"},
JSMirrorMaxMessageSizeTooBigErr: {Code: 400, ErrCode: 10030, Description: "stream mirror must have max message size >= source"},
JSMirrorWithFirstSeqErr: {Code: 400, ErrCode: 10143, Description: "stream mirrors can not have first sequence configured"},
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"},
JSMirrorWithSubjectFiltersErr: {Code: 400, ErrCode: 10033, Description: "stream mirrors can not contain filtered subjects"},
Expand Down Expand Up @@ -1413,6 +1417,16 @@ func NewJSMirrorMaxMessageSizeTooBigError(opts ...ErrorOption) *ApiError {
return ApiErrors[JSMirrorMaxMessageSizeTooBigErr]
}

// NewJSMirrorWithFirstSeqError creates a new JSMirrorWithFirstSeqErr error: "stream mirrors can not have first sequence configured"
func NewJSMirrorWithFirstSeqError(opts ...ErrorOption) *ApiError {
eopts := parseOpts(opts)
if ae, ok := eopts.err.(*ApiError); ok {
return ae
}

return ApiErrors[JSMirrorWithFirstSeqErr]
}

// NewJSMirrorWithSourcesError creates a new JSMirrorWithSourcesErr error: "stream mirrors can not also contain other sources"
func NewJSMirrorWithSourcesError(opts ...ErrorOption) *ApiError {
eopts := parseOpts(opts)
Expand Down
16 changes: 16 additions & 0 deletions server/jetstream_test.go
Expand Up @@ -18134,6 +18134,22 @@ func TestJetStreamMirrorUpdatesNotSupported(t *testing.T) {
require_Error(t, err, NewJSStreamMirrorNotUpdatableError())
}

func TestJetStreamMirrorFirstSeqNotSupported(t *testing.T) {
s := RunBasicJetStreamServer(t)
defer s.Shutdown()

_, err := s.gacc.addStream(&StreamConfig{Name: "SOURCE"})
require_NoError(t, err)

cfg := &StreamConfig{
Name: "M",
Mirror: &StreamSource{Name: "SOURCE"},
FirstSeq: 123,
}
_, err = s.gacc.addStream(cfg)
require_Error(t, err, NewJSMirrorWithFirstSeqError())
}

func TestJetStreamDirectGetBySubject(t *testing.T) {
conf := createConfFile(t, []byte(`
listen: 127.0.0.1:-1
Expand Down
3 changes: 3 additions & 0 deletions server/stream.go
Expand Up @@ -1159,6 +1159,9 @@ func (s *Server) checkStreamCfg(config *StreamConfig, acc *Account) (StreamConfi

// Do some pre-checking for mirror config to avoid cycles in clustered mode.
if cfg.Mirror != nil {
if cfg.FirstSeq > 0 {
return StreamConfig{}, NewJSMirrorWithFirstSeqError()
}
if len(cfg.Subjects) > 0 {
return StreamConfig{}, NewJSMirrorWithSubjectsError()

Expand Down

0 comments on commit d780da6

Please sign in to comment.