Skip to content

fix(serviceaccount reset-credentials): validate serviceaccount ID in prompt#720

Merged
rkpattnaik780 merged 3 commits intodevelopfrom
svc_acc_validation
Jun 16, 2021
Merged

fix(serviceaccount reset-credentials): validate serviceaccount ID in prompt#720
rkpattnaik780 merged 3 commits intodevelopfrom
svc_acc_validation

Conversation

@rkpattnaik780
Copy link
Contributor

Client side validation for service account ID in interactive mode.

Closes #716

Verification Steps

  1. Run command to reset credentials for service account in interactive mode.
go run ./cmd/rhoas serviceaccount reset-credentials
  1. Input any random string that is not an UUID, e.g "kafka". It should throw the error:
Sorry, your reply was invalid: "kafka" is not a valid UUID

Screenshot from 2021-06-15 18-53-53

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Other (please specify)

Checklist

  • Documentation added for the feature
  • CI and all relevant tests are passing
  • Code Review completed
  • Verified independently by reviewer

@rkpattnaik780
Copy link
Contributor Author

Should client side validation be extended to non-interactive mode as well?

validateIDFlag := validation.ValidateID(opts.localizer)
validID := validateIDFlag(opts.id)
if validID != nil {
return validID
}

Also, it seems bit complex to write tests for higher order functions being used as validators.

Copy link
Contributor

@craicoverflow craicoverflow left a comment

Choose a reason for hiding this comment

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

Should client side validation be extended to non-interactive mode as well?

Yes, we should. Any client-side validation improves UX/DX so this would be nice to have.

Also, it seems bit complex to write tests for higher order functions being used as validators.

Yeah, I see what you mean! Now that we are needing to pass localizer to validators they are no longer simple functions.

What would be better (and you can do this in this PR) is to create a Validator type which contains all the validator functions for service accounts.

}

// ValidateID validates if ID is a valid UUID
func ValidateID(localizer localize.Localizer) func(v interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func ValidateID(localizer localize.Localizer) func(v interface{}) error {
func ValidateUUID(localizer localize.Localizer) func(v interface{}) error {

legalNameChars = "^[a-z]([-a-z0-9]*[a-z0-9])?$"
maxNameLength = 50
minNameLength = 1
leagalUUID = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
leagalUUID = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
legalUUID = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"

}

// ValidateUUID validates if ID is a valid UUID
func ValidateUUID(localizer localize.Localizer) func(v interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it will be a good idea for readibility if we do not use anonymous return functions anymore for all validators. I'll explain in a follow up issue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PS: It would also be nice to validate the flag for UUId like you had suggested.

It has been done in reset_credentials.go. Did you expect something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it will be a good idea for readibility if we do not use anonymous return functions anymore for all validators. I'll explain in a follow up issue

Maked #721 as WIP, it has localizations for validators using anonymous functions, should this PR wait for the issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I had not installed the binary :)

Copy link
Contributor

Choose a reason for hiding this comment

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

No you can merge this PR beforehand if you like.

}
}

func TestValidateUUID(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

@craicoverflow
Copy link
Contributor

PS: It would also be nice to validate the flag for UUId like you had suggested.

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.

2 participants