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

Introduce pkg/pagination #613

Merged
merged 3 commits into from
Sep 7, 2018
Merged

Introduce pkg/pagination #613

merged 3 commits into from
Sep 7, 2018

Conversation

jzelinskie
Copy link
Contributor

This change refactors a lot of the code dealing with pagination so that
fernet implementation details do not leak.

  • Deletes database/pgsql/token
  • Introduces a pagination package
  • Renames idPageNumber to Page and add a constructor and method.

@jzelinskie jzelinskie added kind/cleanup no functional changes, only reorganization component/database labels Sep 6, 2018
Copy link
Contributor

@KeyboardNerd KeyboardNerd left a comment

Choose a reason for hiding this comment

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

A few nitpicks about the error handling. Otherwise, it looks good.

var key *fernet.Key
key, err = fernet.DecodeKey(keyString)
if err != nil {
err = errors.New("invalid pagination key string: must be 32-bit URL-safe base64")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this intended to be un-exported error?

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'll export it. I also just noticed that it's 32 bytes not bits. Woops

func MustGenerateNewKey() Key {
var key fernet.Key
if err := key.Generate(); err != nil {
panic("pagination: failed to read enough randomness to generate a new key")
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that by current implementation, it'll only return error because of insufficient randomness. But will this error be the only error that key.Generate function will generate in the future?

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 went down the rabbit hole and talked to a bunch of the people working on Go crypto about this. I think it's safe to assume that fernet probably isn't going to change anything in this function and that any errors from crypto/rand.Read() should panic.

// UnmarshalToken decrypts a token using provided key and decodes the result
// into the provided interface.
func (k Key) UnmarshalToken(token string, v interface{}) error {
decoded, _ := fernet.DecodeKey(string(k))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really want to suppress the error here? Yes, it was not handled before. If there's a valid reason, I think it makes sense to comment it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you only ever make Keys from the constructors, they'd always be valid.

However, I'll change this so that the Key type actually keeps the decoded bytes in memory rather than the string representation.

This change refactors a lot of the code dealing with pagination so that
fernet implementation details do not leak.

- Deletes database/pgsql/token
- Introduces a pagination package
- Renames idPageNumber to Page and add a constructor and method.
This change pulls as much pagination logic out of the database
implementation as possible. Database implementations should now be able
to marshal whatever state they need into opaque tokens with the
utilities in the pagination package.
Copy link
Contributor

@KeyboardNerd KeyboardNerd left a comment

Choose a reason for hiding this comment

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

LGTM

@jzelinskie jzelinskie merged commit e5c2e37 into quay:master Sep 7, 2018
@jzelinskie jzelinskie deleted the pkg-pagination branch September 7, 2018 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/cleanup no functional changes, only reorganization
Development

Successfully merging this pull request may close these issues.

None yet

2 participants