Skip to content

Commit 6bd8fb1

Browse files
committed
Add ability to search keys by fingerprint
This commit adds the functionality to search ssh-keys by fingerprint of the ssh-key. Deploy keys per repository can also be searched. There is no current clear API point to allow search of all deploy keys by fingerprint or keyID. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 8b85fee commit 6bd8fb1

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

models/ssh_key.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"code.gitea.io/gitea/modules/util"
2525

2626
"github.com/Unknwon/com"
27+
"github.com/go-xorm/builder"
2728
"github.com/go-xorm/xorm"
2829
"golang.org/x/crypto/ssh"
2930
)
@@ -465,6 +466,19 @@ func SearchPublicKeyByContent(content string) (*PublicKey, error) {
465466
return key, nil
466467
}
467468

469+
// SearchPublicKey returns a list of public keys matching the provided arguments.
470+
func SearchPublicKey(uid int64, fingerprint string) ([]*PublicKey, error) {
471+
keys := make([]*PublicKey, 0, 5)
472+
cond := builder.NewCond()
473+
if uid != 0 {
474+
cond = cond.And(builder.Eq{"owner_id": uid})
475+
}
476+
if fingerprint != "" {
477+
cond = cond.And(builder.Eq{"fingerprint": fingerprint})
478+
}
479+
return keys, x.Where(cond).Find(&keys)
480+
}
481+
468482
// ListPublicKeys returns a list of public keys belongs to given user.
469483
func ListPublicKeys(uid int64) ([]*PublicKey, error) {
470484
keys := make([]*PublicKey, 0, 5)
@@ -833,3 +847,19 @@ func ListDeployKeys(repoID int64) ([]*DeployKey, error) {
833847
Where("repo_id = ?", repoID).
834848
Find(&keys)
835849
}
850+
851+
// SearchDeployKeys returns a list of deploy keys matching the provided arguments.
852+
func SearchDeployKeys(repoID int64, keyID int64, fingerprint string) ([]*DeployKey, error) {
853+
keys := make([]*DeployKey, 0, 5)
854+
cond := builder.NewCond()
855+
if repoID != 0 {
856+
cond = cond.And(builder.Eq{"repo_id": repoID})
857+
}
858+
if keyID != 0 {
859+
cond = cond.And(builder.Eq{"key_id": keyID})
860+
}
861+
if fingerprint != "" {
862+
cond = cond.And(builder.Eq{"fingerprint": fingerprint})
863+
}
864+
return keys, x.Where(cond).Find(&keys)
865+
}

routers/api/v1/repo/key.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,28 @@ func ListDeployKeys(ctx *context.APIContext) {
5252
// description: name of the repo
5353
// type: string
5454
// required: true
55+
// - name: key_id
56+
// in: query
57+
// description: the key_id to search for
58+
// type: integer
59+
// - name: fingerprint
60+
// in: query
61+
// description: fingerprint of the key
62+
// type: string
5563
// responses:
5664
// "200":
5765
// "$ref": "#/responses/DeployKeyList"
58-
keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
66+
var keys []*models.DeployKey
67+
var err error
68+
69+
fingerprint := ctx.Query("fingerprint")
70+
keyID := ctx.QueryInt64("key_id")
71+
if fingerprint != "" || keyID != 0 {
72+
keys, err = models.SearchDeployKeys(ctx.Repo.Repository.ID, keyID, fingerprint)
73+
} else {
74+
keys, err = models.ListDeployKeys(ctx.Repo.Repository.ID)
75+
}
76+
5977
if err != nil {
6078
ctx.Error(500, "ListDeployKeys", err)
6179
return

routers/api/v1/user/key.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,25 @@ func composePublicKeysAPILink() string {
6161
}
6262

6363
func listPublicKeys(ctx *context.APIContext, user *models.User) {
64-
keys, err := models.ListPublicKeys(user.ID)
64+
var keys []*models.PublicKey
65+
var err error
66+
67+
fingerprint := ctx.Query("fingerprint")
68+
username := ctx.Params("username")
69+
70+
if fingerprint != "" {
71+
// Querying not just listing
72+
if username != "" {
73+
// Restrict to provided uid
74+
keys, err = models.SearchPublicKey(user.ID, fingerprint)
75+
} else {
76+
// Unrestricted
77+
keys, err = models.SearchPublicKey(0, fingerprint)
78+
}
79+
} else {
80+
// Use ListPublicKeys
81+
keys, err = models.ListPublicKeys(user.ID)
82+
}
6583

6684
if err != nil {
6785
ctx.Error(500, "ListPublicKeys", err)
@@ -85,6 +103,11 @@ func ListMyPublicKeys(ctx *context.APIContext) {
85103
// swagger:operation GET /user/keys user userCurrentListKeys
86104
// ---
87105
// summary: List the authenticated user's public keys
106+
// parameters:
107+
// - name: fingerprint
108+
// in: query
109+
// description: fingerprint of the key
110+
// type: string
88111
// produces:
89112
// - application/json
90113
// responses:
@@ -106,6 +129,10 @@ func ListPublicKeys(ctx *context.APIContext) {
106129
// description: username of user
107130
// type: string
108131
// required: true
132+
// - name: fingerprint
133+
// in: query
134+
// description: fingerprint of the key
135+
// type: string
109136
// responses:
110137
// "200":
111138
// "$ref": "#/responses/PublicKeyList"

templates/swagger/v1_json.tmpl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,18 @@
26822682
"name": "repo",
26832683
"in": "path",
26842684
"required": true
2685+
},
2686+
{
2687+
"type": "integer",
2688+
"description": "the key_id to search for",
2689+
"name": "key_id",
2690+
"in": "query"
2691+
},
2692+
{
2693+
"type": "string",
2694+
"description": "fingerprint of the key",
2695+
"name": "fingerprint",
2696+
"in": "query"
26852697
}
26862698
],
26872699
"responses": {
@@ -4976,6 +4988,14 @@
49764988
],
49774989
"summary": "List the authenticated user's public keys",
49784990
"operationId": "userCurrentListKeys",
4991+
"parameters": [
4992+
{
4993+
"type": "string",
4994+
"description": "fingerprint of the key",
4995+
"name": "fingerprint",
4996+
"in": "query"
4997+
}
4998+
],
49794999
"responses": {
49805000
"200": {
49815001
"$ref": "#/responses/PublicKeyList"
@@ -5540,6 +5560,12 @@
55405560
"name": "username",
55415561
"in": "path",
55425562
"required": true
5563+
},
5564+
{
5565+
"type": "string",
5566+
"description": "fingerprint of the key",
5567+
"name": "fingerprint",
5568+
"in": "query"
55435569
}
55445570
],
55455571
"responses": {

0 commit comments

Comments
 (0)