Skip to content

[public-api] Implement regenerate token #14867

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

Merged
merged 1 commit into from
Nov 23, 2022
Merged

Conversation

jeanp413
Copy link
Member

@jeanp413 jeanp413 commented Nov 22, 2022

Description

Implements regenerate token

Related Issue(s)

Fixes #14611

Release Notes

NONE

Documentation

Werft options:

  • /werft with-local-preview
    If enabled this will build install/preview
  • /werft with-preview
  • /werft with-large-vm
  • /werft with-integration-tests=all
    Valid options are all, workspace, webapp, ide, jetbrains, vscode, ssh

@jeanp413 jeanp413 changed the title [public-api] Implement regenerate [public-api] Implement regenerate token Nov 23, 2022
@jeanp413 jeanp413 marked this pull request as ready for review November 23, 2022 03:25
@jeanp413 jeanp413 requested a review from a team November 23, 2022 03:25
@jeanp413
Copy link
Member Author

jeanp413 commented Nov 23, 2022

/werft run

👍 started the job as gitpod-build-jp-public-api-regenerate.2
(with .werft/ from main)

@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label Nov 23, 2022
@jeanp413 jeanp413 force-pushed the jp/public-api-regenerate branch from a796b47 to 5c62016 Compare November 23, 2022 03:51
Copy link
Member

@easyCZ easyCZ left a comment

Choose a reason for hiding this comment

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

Looking good. Just gonna investigate the DB query simplification.

Comment on lines 107 to 123
err := db.Transaction(func(tx *gorm.DB) error {
txErr := tx.
Where("id = ?", tokenID).
Where("userId = ?", userID).
Where("deleted = ?", 0).
Select("hash", "expirationTime").Updates(PersonalAccessToken{Hash: hash, ExpirationTime: expirationTime}).Error
if txErr != nil {
return txErr
}

txErr = tx.Where("id = ?", tokenID).Where("userId = ?", userID).Where("deleted = ?", 0).First(&token).Error
if txErr != nil {
return txErr
}

return nil
})
Copy link
Member

Choose a reason for hiding this comment

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

I believe we can write this query using https://gorm.io/docs/update.html#Returning-Data-From-Modified-Rows

This would remove the need for

  • The explicit transaction
  • The lookup afterwards

I'm just spinning up a workspace to test this out, will comment if I can make it work.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, looks like it doesn't work because it won't return the full PersonalAccessToken object, but only the rows which have actually changed. It's also dependant on the DB supporting Returning values.

Copy link
Contributor

@mustard-mh mustard-mh Nov 23, 2022

Choose a reason for hiding this comment

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

Okay, looks like it doesn't work because it won't return the full PersonalAccessToken object, but only the rows which have actually changed. It's also dependant on the DB supporting Returning values.

Yep, we tried it (clause.Returning{}) yesterday, and it was not worked as expected, so we switched to update and select to avoid transaction.

Update:

I saw JP switched it to transaction 😂, I will make it back to update and select only

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, looks like it doesn't work because it won't return the full PersonalAccessToken object, but only the rows which have actually changed. It's also dependant on the DB supporting Returning values.

yep it's because mysql doesn't support returning sql clause

Comment on lines +422 to +425
require.Equal(t, origResponse.Msg.Token.Id, response.Msg.Token.Id)
require.NotEqual(t, "", response.Msg.Token.Value)
require.Equal(t, origResponse.Msg.Token.Name, response.Msg.Token.Name)
require.Equal(t, origResponse.Msg.Token.Description, response.Msg.Token.Description)
require.Equal(t, origResponse.Msg.Token.Scopes, response.Msg.Token.Scopes)
require.Equal(t, newTimestamp.AsTime(), response.Msg.Token.ExpirationTime.AsTime())
require.Equal(t, origResponse.Msg.Token.CreatedAt, response.Msg.Token.CreatedAt)
Copy link
Member

Choose a reason for hiding this comment

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

As a follow-up (in another PR), we can add a helper method which compares the token and performs a custom check for the Value, ensuring it exists (and possibly trying to parse it) without matching on specific contents.

The alternative to be able to use require.Equal(t, struct, struct) would also be to have a seeded implementation of the Signer.

@mustard-mh
Copy link
Contributor

/hold

Co-authored-by: Milan Pavlik <[email protected]>
Co-authored-by: mustard <[email protected]>
@mustard-mh mustard-mh force-pushed the jp/public-api-regenerate branch from 6c8676b to 4917f6e Compare November 23, 2022 12:55
Copy link
Contributor

@mustard-mh mustard-mh left a comment

Choose a reason for hiding this comment

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

LGTM

/unhold

@roboquat roboquat merged commit afac3c9 into main Nov 23, 2022
@roboquat roboquat deleted the jp/public-api-regenerate branch November 23, 2022 13:01
@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed: webapp Meta team change is running in production deployed Change is completely running in production release-note-none size/L team: webapp Issue belongs to the WebApp team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement RegeneratePersonalAccessToken RPC
4 participants