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

fix/refactor(broker): Fix JSON generation of returned authentication data (userinfo and message) #43

Merged
merged 2 commits into from
Jul 3, 2024

Conversation

denisonbarbosa
Copy link
Member

Some errors were causing our previous strategy of generating the JSON on the go to fail, as some error messages could have double quotes. To avoid this, it's better to use the native marshaling of strings that the encode/json package provides.

UDENG-3428

@denisonbarbosa denisonbarbosa marked this pull request as ready for review July 1, 2024 16:27
@denisonbarbosa denisonbarbosa requested a review from a team as a code owner July 1, 2024 16:27
Copy link
Member

@didrocks didrocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

I think this is the right approach with the existing code. Thanks for tackling this!
As you saw, I have some improvements requests in the PR, but nothing changing the structure of the overall PR.

Also, shouldn’t we add a test where the error message was not producing a valid json previously on purpose? Because this is what is supposively fixed in the PR :D

internal/broker/broker.go Show resolved Hide resolved
internal/broker/broker.go Outdated Show resolved Hide resolved
internal/broker/broker.go Outdated Show resolved Hide resolved
internal/broker/data.go Outdated Show resolved Hide resolved
internal/broker/broker.go Show resolved Hide resolved
internal/broker/data.go Outdated Show resolved Hide resolved
internal/broker/data.go Outdated Show resolved Hide resolved
internal/broker/data.go Outdated Show resolved Hide resolved
internal/broker/data.go Outdated Show resolved Hide resolved
@3v1n0
Copy link
Contributor

3v1n0 commented Jul 2, 2024

I think it would be worth adding a test to ensure we properly do the encoding (something maybe inspired to this, but done with the proper abstraction which at the time I didn't do since it was mostly a PoC).

Indeed internal/broker/testdata/TestIsAuthenticated/golden/error_when_mode_is_password_and_can_not_fetch_user_info/first_call does already some check, but it's not very close to the issue we had in the past IMHO.

Maybe also making the tests to-remarshall the output to a native go map, just to ensure that's valid?

Copy link
Contributor

@3v1n0 3v1n0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed nicer now, thanks.

Some further nits :)

internal/broker/broker.go Outdated Show resolved Hide resolved
internal/broker/broker.go Outdated Show resolved Hide resolved
internal/broker/broker_test.go Show resolved Hide resolved
Copy link
Member

@didrocks didrocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s readable and easier to parse now!

So, I only have one remaining requests to ensure that we don’t end up with having invalid json sent back to the caller without noticing on the testing side, otherwise good!

internal/broker/broker_test.go Show resolved Hide resolved
internal/broker/data.go Outdated Show resolved Hide resolved
Copy link
Contributor

@3v1n0 3v1n0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see validation was actually needed :)

@denisonbarbosa
Copy link
Member Author

Nice to see validation was actually needed :)

Yeah, it wouldn't break anything (as we don't care about the message in the cases that the validation was failing, and unmarshaling an empty string is also fine). However, it's still good to keep things as standardized as possible and ensure we always have a valid JSON-encoded message in non-error cases.

internal/broker/broker.go Outdated Show resolved Hide resolved
Copy link
Member

@didrocks didrocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA failed due Tuesday go release vulnerability notice. Thanks for doing the change!

To prepare for the json marshaling changes, we need to turn the userInfo
into its own type and add json annotations to the struct so that it can
be marshaled correctly through json.Marshal.
We had some problems when error messages had double quotes in them, as
it would break the JSON unmarshaling. To avoid this kind of errors
again, we now use separate types for the returned data and marshal them
through json.Marshal, which prevents mistakes that could happen when
"manually" generating the message.
@3v1n0
Copy link
Contributor

3v1n0 commented Jul 3, 2024

All good here after the final commit :)

@denisonbarbosa denisonbarbosa merged commit 34577ad into main Jul 3, 2024
3 checks passed
@denisonbarbosa denisonbarbosa deleted the fix-json-messaging branch July 3, 2024 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants