Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SubscribeSync should return error on permissions violation #1583

Open
chadglaser4682 opened this issue Mar 12, 2024 · 1 comment
Open

SubscribeSync should return error on permissions violation #1583

chadglaser4682 opened this issue Mar 12, 2024 · 1 comment
Labels
defect Suspected defect such as a bug or regression

Comments

@chadglaser4682
Copy link

Observed behavior

I am writing error handling logic. When I introduce in issue with the nkey I Get an error
nats: Permissions Violation for Subscription to "my..my." on connection [267]
But I can't handle the error from SubscribeSync (or Subscribe, I've tried both)

subscriptions[name], err = nc.SubscribeSync(prefix)
	if err != nil {
		errString := fmt.Sprintf("Message bus error: Subscription failed: subject=%s, prefix=%s, error=%v", "subject", prefix, err)
		return errString, "Not Connected"
	}
	time.Sleep(100 * time.Millisecond) ----> Added so I can catch the errorin nc.LastError(), otherwise the error is never caught. 
	lastError := nc.LastError()
	if lastError != nil {
		errString := fmt.Sprintf("Message bus error: %s", lastError.Error())
		return errString, "Not Connected"
	}

Expected behavior

Expected behavior would be the error being returned here.

subscriptions[name], err = nc.SubscribeSync(prefix)
if err != nil {
errString := fmt.Sprintf("Message bus error: Subscription failed: subject=%s, prefix=%s, error=%v", "subject", prefix, err)
return errString, "Not Connected"
}

Error
nats: Permissions Violation for Subscription to "agent.c4b976da-5cbf-4628-b46d-5d794c929b07.agent.c4b976da-5cbf-4628-b46d-5d794c929b07" on connection [267]
Should be returned and handled.

Server and client version

Golang code I am writing is
github.com/nats-io/nats.go v1.31.0

I think this is what the server side is getting, but I don't have access to the nats-server to run the nats-server --version command.

go install github.com/nats-io/nats-server/v2@latest 

Host environment

Client is running in
Linux 6.2.0-1019-azure x86_64

go version go1.21.7 linux/amd64

Steps to reproduce

When everything works there is no issue at all.
But when the myID is changed to something it isn't suppose to be, there is an error that if the time.sleep isn't introduced, does not get caught. The error should be returned by the call to nc.SubscribeSync(prefix). lastError can't catch the error unless the sleep is introduced. After the time.sleep the error comes up
nats: Permissions Violation for Subscription to "my..my." on connection [295]

func checkNatsConnection(config *config)(string, string)
natsConnector := msgbus.NatsConnection{}
var subscriptions = make(map[string]*nats.Subscription)
mySubject := "my." + config.myID
name := "my." + config.myID

nc, err := natsConnector.Connect(
	config.NatsConfig.EndpointUri,
	nats.UserJWTAndSeed(config.NatsConfig.Jwt, config.NatsConfig.PrivNkey),
)
if err != nil {
	errString := fmt.Sprintf("Message bus error: connection failed: %v", err)
	return errString, "Not Connected"
}
defer nc.Close()

prefix := mySubject + "." + name
subscriptions[name], err = nc.SubscribeSync(prefix)
if err != nil {
	errString := fmt.Sprintf("Message bus error: Subscription failed: subject=%s, prefix=%s, error=%v", "subject", prefix, err)
	return errString, "Not Connected"
}

time.Sleep(100 * time.Millisecond)
lastError := nc.LastError()
if lastError != nil {
	errString := fmt.Sprintf("Message bus error: %s", lastError.Error())
	return errString, "Not Connected"
}

subscriptions[name].Drain()

return "None", "Connected"

}

@chadglaser4682 chadglaser4682 added the defect Suspected defect such as a bug or regression label Mar 12, 2024
@piotrpio
Copy link
Collaborator

Hey @chadglaser4682, thanks for reporting the issue. I agree that handling subscription errors on nc isn't ideal, especially since you have to introduce delays to catch it. We are working of a way to localize the errors to be coupled with the subscription, I'll let you know here when I have something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect Suspected defect such as a bug or regression
Projects
None yet
Development

No branches or pull requests

2 participants