diff options
Diffstat (limited to 'models/asymkey/ssh_key.go')
-rw-r--r-- | models/asymkey/ssh_key.go | 36 |
1 files changed, 13 insertions, 23 deletions
diff --git a/models/asymkey/ssh_key.go b/models/asymkey/ssh_key.go index 74d2411dd9..10220ea937 100644 --- a/models/asymkey/ssh_key.go +++ b/models/asymkey/ssh_key.go @@ -75,7 +75,7 @@ func (key *PublicKey) AuthorizedString() string { return AuthorizedStringForKey(key) } -func addKey(e db.Engine, key *PublicKey) (err error) { +func addKey(ctx context.Context, key *PublicKey) (err error) { if len(key.Fingerprint) == 0 { key.Fingerprint, err = calcFingerprint(key.Content) if err != nil { @@ -84,7 +84,7 @@ func addKey(e db.Engine, key *PublicKey) (err error) { } // Save SSH key. - if _, err = e.Insert(key); err != nil { + if err = db.Insert(ctx, key); err != nil { return err } @@ -105,14 +105,13 @@ func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*Pub return nil, err } defer committer.Close() - sess := db.GetEngine(ctx) - if err := checkKeyFingerprint(sess, fingerprint); err != nil { + if err := checkKeyFingerprint(ctx, fingerprint); err != nil { return nil, err } // Key name of same user cannot be duplicated. - has, err := sess. + has, err := db.GetEngine(ctx). Where("owner_id = ? AND name = ?", ownerID, name). Get(new(PublicKey)) if err != nil { @@ -130,7 +129,7 @@ func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*Pub Type: KeyTypeUser, LoginSourceID: authSourceID, } - if err = addKey(sess, key); err != nil { + if err = addKey(ctx, key); err != nil { return nil, fmt.Errorf("addKey: %v", err) } @@ -151,9 +150,11 @@ func GetPublicKeyByID(keyID int64) (*PublicKey, error) { return key, nil } -func searchPublicKeyByContentWithEngine(e db.Engine, content string) (*PublicKey, error) { +// SearchPublicKeyByContent searches content as prefix (leak e-mail part) +// and returns public key found. +func SearchPublicKeyByContent(ctx context.Context, content string) (*PublicKey, error) { key := new(PublicKey) - has, err := e. + has, err := db.GetEngine(ctx). Where("content like ?", content+"%"). Get(key) if err != nil { @@ -164,15 +165,11 @@ func searchPublicKeyByContentWithEngine(e db.Engine, content string) (*PublicKey return key, nil } -// SearchPublicKeyByContent searches content as prefix (leak e-mail part) +// SearchPublicKeyByContentExact searches content // and returns public key found. -func SearchPublicKeyByContent(content string) (*PublicKey, error) { - return searchPublicKeyByContentWithEngine(db.GetEngine(db.DefaultContext), content) -} - -func searchPublicKeyByContentExactWithEngine(e db.Engine, content string) (*PublicKey, error) { +func SearchPublicKeyByContentExact(ctx context.Context, content string) (*PublicKey, error) { key := new(PublicKey) - has, err := e. + has, err := db.GetEngine(ctx). Where("content = ?", content). Get(key) if err != nil { @@ -183,12 +180,6 @@ func searchPublicKeyByContentExactWithEngine(e db.Engine, content string) (*Publ return key, nil } -// SearchPublicKeyByContentExact searches content -// and returns public key found. -func SearchPublicKeyByContentExact(content string) (*PublicKey, error) { - return searchPublicKeyByContentExactWithEngine(db.GetEngine(db.DefaultContext), content) -} - // SearchPublicKey returns a list of public keys matching the provided arguments. func SearchPublicKey(uid int64, fingerprint string) ([]*PublicKey, error) { keys := make([]*PublicKey, 0, 5) @@ -335,12 +326,11 @@ func deleteKeysMarkedForDeletion(keys []string) (bool, error) { return false, err } defer committer.Close() - sess := db.GetEngine(ctx) // Delete keys marked for deletion var sshKeysNeedUpdate bool for _, KeyToDelete := range keys { - key, err := searchPublicKeyByContentWithEngine(sess, KeyToDelete) + key, err := SearchPublicKeyByContent(ctx, KeyToDelete) if err != nil { log.Error("SearchPublicKeyByContent: %v", err) continue |