Skip to content

[#5356] Delete GPG keys with user deletion #5357

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions models/gpg_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
}, nil
}

// deleteGPGKeys does the actual key deletion but does not update authorized_keys file.
Copy link
Member

@sapk sapk Nov 19, 2018

Choose a reason for hiding this comment

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

Can you add for information that this method will not delete any related (subkey) compared to deleteGPGKey ? And you should loose the authorized_keys file part since it not related to GPG.
Something like :

Suggested change
// deleteGPGKeys does the actual key deletion but does not update authorized_keys file.
// deleteGPGKeys does the actual key deletion but does not delete any related key (subkey).

or add :
_, err := e.In("primary_key_id", keyIDs).Delete(new(GPGKey))
each solution is fine as the list passed should already contained any related keys in the original use case.

Copy link
Author

Choose a reason for hiding this comment

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

But deleteGPGKeys does also delete all subkeys of a master key which are assigned to a user. More specifically: deleteGPGKeys deletes all keys of a specified user.

Copy link
Member

@sapk sapk Nov 20, 2018

Choose a reason for hiding this comment

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

This is just in case for later use in other part in code. Currently when you call this method here you pass all keys (and subkeys) owned by user. But if this method is used somewhere else there could be orphaned subkeys if not all the key are passed as argument. This is just to add a warning for later use (or add the delete of any sub key).

func deleteGPGKeys(e *xorm.Session, keyIDs ...int64) error {
if len(keyIDs) == 0 {
return nil
}

_, err := e.In("id", keyIDs).Delete(new(GPGKey))
Copy link
Member

Choose a reason for hiding this comment

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

Why not just e.Where("owner_id =?", userID).Delete(new(GPGKey))?

Copy link
Author

@JohnnyLeone JohnnyLeone Nov 19, 2018

Choose a reason for hiding this comment

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

This is a nice solution and makes this code a lot smaller. Also this check can simplify the public key deletion.

return err
}

// deleteGPGKey does the actual key deletion
func deleteGPGKey(e *xorm.Session, keyID string) (int64, error) {
if keyID == "" {
Expand Down
15 changes: 15 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,21 @@ func deleteUser(e *xorm.Session, u *User) error {
}
// ***** END: PublicKey *****

// ***** START: GPGPublicKey *****
GPGkeys := make([]*GPGKey, 0, 10)
if err = e.Find(&GPGkeys, &GPGKey{OwnerID: u.ID}); err != nil {
return fmt.Errorf("get all gpg keys: %v", err)
}

GPGkeyIDs := make([]int64, len(GPGkeys))
for i := range GPGkeys {
GPGkeyIDs[i] = GPGkeys[i].ID
}
if err = deleteGPGKeys(e, GPGkeyIDs...); err != nil {
return fmt.Errorf("deleteGPGKeys: %v", err)
}
// ***** END: GPGPublicKey *****

// Clear assignee.
if err = clearAssigneeByUserID(e, u.ID); err != nil {
return fmt.Errorf("clear assignee: %v", err)
Expand Down