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

MM-56823: Validate license only when deploying app nodes #822

Merged

Conversation

agarciamontoro
Copy link
Member

Summary

If we are not deploying app nodes (to load-test an already existing Mattermost installation), we don't need to validate the license (first commit). Actually, we don't even need to specify a file in the config at all (second commit).

Ticket Link

https://mattermost.atlassian.net/browse/MM-56823

When deploying no app nodes, the license file setting may be empty,
since we don't need a license to install anywhere.
@agarciamontoro agarciamontoro added the 2: Dev Review Requires review by a core committer label Oct 8, 2024
Copy link
Contributor

@streamer45 streamer45 left a comment

Choose a reason for hiding this comment

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

Nice, and great test coverage!

@@ -123,6 +123,14 @@ func validate(validation, fieldName string, p, v reflect.Value) error {
if s == "" || !isAlphanumeric(s) {
return fmt.Errorf("%s is not alphanumeric", fieldName)
}
case "emptyorfile":
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the license is the only use of the file tag I wonder if we could more simply make it accept empty values. It would be cool to allow multiple subtags, so one could define something like validate:"notempty,file".

Comment on lines 407 to 418
err := Validate(&cfg)
require.Error(t, err)
})

t.Run("not a path for emptyorfile", func(t *testing.T) {
var cfg serverConfiguration
Set(&cfg)

cfg.LicenseFile = "not a file"

err := Validate(&cfg)
require.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I usually find value in using EqualError rather than simply checking if it errored. It saved me from a few bugs in the past :)

Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

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

I really like the validate:"notempty,file" idea, but non-blocking. Great improvements.

@agarciamontoro agarciamontoro removed the request for review from isacikgoz October 9, 2024 07:32
Add a new "empty" validation as well, that verifies the value is
empty (useful only for multiple validations, like in empty;file).
@agarciamontoro
Copy link
Member Author

I added an OR clause: simply concatenate multiple validations separated by a colon. If one of those validations pass, it is considered valid. What we need for the license in this case is "empty;file", since we want it either to be empty, or to be a valid, existing file.

@agarciamontoro
Copy link
Member Author

Re-requesting review just to double-check this is what was on your minds. I do like this solution myself :)

return nil
}

merr.Append(err)
Copy link
Member

Choose a reason for hiding this comment

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

You can just use the standard library: https://pkg.go.dev/errors#Join

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, didn't know about that one, thanks!

Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

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

Didn't mean my previous comment to be blocking.

Copy link
Contributor

@streamer45 streamer45 left a comment

Choose a reason for hiding this comment

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

Great work!

Comment on lines 97 to 99
// Multiple validations separated by a colon (as in "empty;file") behave like an OR clause
if strings.Contains(validation, ";") {
individualValidations := strings.Split(validation, ";")
Copy link
Contributor

Choose a reason for hiding this comment

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

I am thinking that calling Split directly and checking if we get more than a single result would be enough?

Copy link
Member Author

@agarciamontoro agarciamontoro Oct 9, 2024

Choose a reason for hiding this comment

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

Mmmm we could, but I feel like

if individualValidations := strings.Split(validation, "|"); len(individualValidations) > 1 {

is a bit less readable than what we have now, and

individualValidations := strings.Split(validation, "|")
if len(individualValidations) > 1 {

is basically the same, but it makes my brain slightly uncomfortable, it's like it's the other way around 😂

If you feel strongly about this, or if you were thinking about something else, please let me know!

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope, I just prefer to keep the scope level down when feasible. It's a preference really so not at all blocking.

@@ -18,6 +19,8 @@ func TestValidate(t *testing.T) {
MaxUsers int `default:"1000" validate:"range:(0,]"`
LogLevel string `default:"ERROR" validate:"oneof:{TRACE, INFO, WARN, ERROR}"`
S3URI string `default:"" validate:"s3uri"`
LicenseFile string `default:"" validate:"empty;file"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend | instead of ;. Then we could add & for improved boolean logic :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oooh, I like it! Let's do that :)

Copy link
Contributor

Choose a reason for hiding this comment

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

that was half a joke you know :p

Copy link
Member Author

Choose a reason for hiding this comment

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

You have to be careful with your jokes!

Copy link
Contributor

@streamer45 streamer45 left a comment

Choose a reason for hiding this comment

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

Merge this please :D

@streamer45 streamer45 added 4: Reviews Complete All reviewers have approved the pull request and removed 2: Dev Review Requires review by a core committer labels Oct 9, 2024
@agarciamontoro agarciamontoro merged commit 782ea07 into master Oct 9, 2024
1 check passed
@agarciamontoro agarciamontoro deleted the MM-56823.remove.license.check.when.no.app.deployed branch October 9, 2024 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4: Reviews Complete All reviewers have approved the pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants