Skip to content

Commit 469ab99

Browse files
vtemianlafriks
authored andcommitted
Delete a user's public key via admin api (closes #3014) (#3059)
* Delete a user's public key via admin api * Test admin ssh endpoint for creating a new ssh key * Adapt public ssh key test to also test the delete operation * Test that deleting a missing key will result in a 404 * Test that a normal user can't delete another user's ssh key * Make DeletePublicKey return err * Update swagger doc
1 parent c7fb6e3 commit 469ab99

File tree

6 files changed

+324
-11
lines changed

6 files changed

+324
-11
lines changed

integrations/api_admin_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
"testing"
11+
12+
"code.gitea.io/gitea/models"
13+
api "code.gitea.io/sdk/gitea"
14+
)
15+
16+
func TestAPIAdminCreateAndDeleteSSHKey(t *testing.T) {
17+
prepareTestEnv(t)
18+
// user1 is an admin user
19+
session := loginUser(t, "user1")
20+
keyOwner := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User)
21+
22+
urlStr := fmt.Sprintf("/api/v1/admin/users/%s/keys", keyOwner.Name)
23+
req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
24+
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n",
25+
"title": "test-key",
26+
})
27+
resp := session.MakeRequest(t, req, http.StatusCreated)
28+
29+
var newPublicKey api.PublicKey
30+
DecodeJSON(t, resp, &newPublicKey)
31+
models.AssertExistsAndLoadBean(t, &models.PublicKey{
32+
ID: newPublicKey.ID,
33+
Name: newPublicKey.Title,
34+
Content: newPublicKey.Key,
35+
Fingerprint: newPublicKey.Fingerprint,
36+
OwnerID: keyOwner.ID,
37+
})
38+
39+
req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d",
40+
keyOwner.Name, newPublicKey.ID)
41+
session.MakeRequest(t, req, http.StatusNoContent)
42+
models.AssertNotExistsBean(t, &models.PublicKey{ID: newPublicKey.ID})
43+
}
44+
45+
func TestAPIAdminDeleteMissingSSHKey(t *testing.T) {
46+
prepareTestEnv(t)
47+
// user1 is an admin user
48+
session := loginUser(t, "user1")
49+
50+
req := NewRequestf(t, "DELETE", "/api/v1/admin/users/user1/keys/%d", models.NonexistentID)
51+
session.MakeRequest(t, req, http.StatusNotFound)
52+
}
53+
54+
func TestAPIAdminDeleteUnauthorizedKey(t *testing.T) {
55+
prepareTestEnv(t)
56+
adminUsername := "user1"
57+
normalUsername := "user2"
58+
session := loginUser(t, adminUsername)
59+
60+
urlStr := fmt.Sprintf("/api/v1/admin/users/%s/keys", adminUsername)
61+
req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
62+
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n",
63+
"title": "test-key",
64+
})
65+
resp := session.MakeRequest(t, req, http.StatusCreated)
66+
var newPublicKey api.PublicKey
67+
DecodeJSON(t, resp, &newPublicKey)
68+
69+
session = loginUser(t, normalUsername)
70+
req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d",
71+
adminUsername, newPublicKey.ID)
72+
session.MakeRequest(t, req, http.StatusForbidden)
73+
}

models/ssh_key.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,7 @@ func deletePublicKeys(e *xorm.Session, keyIDs ...int64) error {
506506
func DeletePublicKey(doer *User, id int64) (err error) {
507507
key, err := GetPublicKeyByID(id)
508508
if err != nil {
509-
if IsErrKeyNotExist(err) {
510-
return nil
511-
}
512-
return fmt.Errorf("GetPublicKeyByID: %v", err)
509+
return err
513510
}
514511

515512
// Check if user has access to delete this key.

0 commit comments

Comments
 (0)