publickey.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // Copyright 2014 The Gogs 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. package models
  5. import (
  6. "bufio"
  7. "encoding/base64"
  8. "encoding/binary"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "io/ioutil"
  13. "os"
  14. "os/exec"
  15. "path"
  16. "path/filepath"
  17. "strings"
  18. "sync"
  19. "time"
  20. "github.com/Unknwon/com"
  21. "github.com/go-xorm/xorm"
  22. "github.com/gogits/gogs/modules/log"
  23. "github.com/gogits/gogs/modules/process"
  24. "github.com/gogits/gogs/modules/setting"
  25. )
  26. const (
  27. // "### autogenerated by gitgos, DO NOT EDIT\n"
  28. _TPL_PUBLICK_KEY = `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n"
  29. )
  30. var (
  31. ErrKeyUnableVerify = errors.New("Unable to verify public key")
  32. )
  33. var sshOpLocker = sync.Mutex{}
  34. var (
  35. SSHPath string // SSH directory.
  36. appPath string // Execution(binary) path.
  37. )
  38. // exePath returns the executable path.
  39. func exePath() (string, error) {
  40. file, err := exec.LookPath(os.Args[0])
  41. if err != nil {
  42. return "", err
  43. }
  44. return filepath.Abs(file)
  45. }
  46. // homeDir returns the home directory of current user.
  47. func homeDir() string {
  48. home, err := com.HomeDir()
  49. if err != nil {
  50. log.Fatal(4, "Fail to get home directory: %v", err)
  51. }
  52. return home
  53. }
  54. func init() {
  55. var err error
  56. if appPath, err = exePath(); err != nil {
  57. log.Fatal(4, "fail to get app path: %v\n", err)
  58. }
  59. appPath = strings.Replace(appPath, "\\", "/", -1)
  60. // Determine and create .ssh path.
  61. SSHPath = filepath.Join(homeDir(), ".ssh")
  62. if err = os.MkdirAll(SSHPath, 0700); err != nil {
  63. log.Fatal(4, "fail to create '%s': %v", SSHPath, err)
  64. }
  65. }
  66. type KeyType int
  67. const (
  68. KEY_TYPE_USER = iota + 1
  69. KEY_TYPE_DEPLOY
  70. )
  71. // PublicKey represents a SSH or deploy key.
  72. type PublicKey struct {
  73. ID int64 `xorm:"pk autoincr"`
  74. OwnerID int64 `xorm:"INDEX NOT NULL"`
  75. Name string `xorm:"NOT NULL"`
  76. Fingerprint string `xorm:"NOT NULL"`
  77. Content string `xorm:"TEXT NOT NULL"`
  78. Mode AccessMode `xorm:"NOT NULL DEFAULT 2"`
  79. Type KeyType `xorm:"NOT NULL DEFAULT 1"`
  80. Created time.Time `xorm:"CREATED"`
  81. Updated time.Time // Note: Updated must below Created for AfterSet.
  82. HasRecentActivity bool `xorm:"-"`
  83. HasUsed bool `xorm:"-"`
  84. }
  85. func (k *PublicKey) AfterSet(colName string, _ xorm.Cell) {
  86. switch colName {
  87. case "created":
  88. k.HasUsed = k.Updated.After(k.Created)
  89. k.HasRecentActivity = k.Updated.Add(7 * 24 * time.Hour).After(time.Now())
  90. }
  91. }
  92. // OmitEmail returns content of public key but without e-mail address.
  93. func (k *PublicKey) OmitEmail() string {
  94. return strings.Join(strings.Split(k.Content, " ")[:2], " ")
  95. }
  96. // GetAuthorizedString generates and returns formatted public key string for authorized_keys file.
  97. func (key *PublicKey) GetAuthorizedString() string {
  98. return fmt.Sprintf(_TPL_PUBLICK_KEY, appPath, key.ID, setting.CustomConf, key.Content)
  99. }
  100. var minimumKeySizes = map[string]int{
  101. "(ED25519)": 256,
  102. "(ECDSA)": 256,
  103. "(NTRU)": 1087,
  104. "(MCE)": 1702,
  105. "(McE)": 1702,
  106. "(RSA)": 1024,
  107. "(DSA)": 1024,
  108. }
  109. func extractTypeFromBase64Key(key string) (string, error) {
  110. b, err := base64.StdEncoding.DecodeString(key)
  111. if err != nil || len(b) < 4 {
  112. return "", errors.New("Invalid key format")
  113. }
  114. keyLength := int(binary.BigEndian.Uint32(b))
  115. if len(b) < 4+keyLength {
  116. return "", errors.New("Invalid key format")
  117. }
  118. return string(b[4 : 4+keyLength]), nil
  119. }
  120. // parseKeyString parses any key string in openssh or ssh2 format to clean openssh string (rfc4253)
  121. func parseKeyString(content string) (string, error) {
  122. // Transform all legal line endings to a single "\n"
  123. s := strings.Replace(strings.Replace(strings.TrimSpace(content), "\r\n", "\n", -1), "\r", "\n", -1)
  124. lines := strings.Split(s, "\n")
  125. var keyType, keyContent, keyComment string
  126. if len(lines) == 1 {
  127. // Parse openssh format
  128. parts := strings.Fields(lines[0])
  129. switch len(parts) {
  130. case 0:
  131. return "", errors.New("Empty key")
  132. case 1:
  133. keyContent = parts[0]
  134. case 2:
  135. keyType = parts[0]
  136. keyContent = parts[1]
  137. default:
  138. keyType = parts[0]
  139. keyContent = parts[1]
  140. keyComment = parts[2]
  141. }
  142. // If keyType is not given, extract it from content. If given, validate it
  143. if len(keyType) == 0 {
  144. if t, err := extractTypeFromBase64Key(keyContent); err == nil {
  145. keyType = t
  146. } else {
  147. return "", err
  148. }
  149. } else {
  150. if t, err := extractTypeFromBase64Key(keyContent); err != nil || keyType != t {
  151. return "", err
  152. }
  153. }
  154. } else {
  155. // Parse SSH2 file format.
  156. continuationLine := false
  157. for _, line := range lines {
  158. // Skip lines that:
  159. // 1) are a continuation of the previous line,
  160. // 2) contain ":" as that are comment lines
  161. // 3) contain "-" as that are begin and end tags
  162. if continuationLine || strings.ContainsAny(line, ":-") {
  163. continuationLine = strings.HasSuffix(line, "\\")
  164. } else {
  165. keyContent = keyContent + line
  166. }
  167. }
  168. if t, err := extractTypeFromBase64Key(keyContent); err == nil {
  169. keyType = t
  170. } else {
  171. return "", err
  172. }
  173. }
  174. return keyType + " " + keyContent + " " + keyComment, nil
  175. }
  176. // CheckPublicKeyString checks if the given public key string is recognized by SSH.
  177. func CheckPublicKeyString(content string) (_ string, err error) {
  178. content, err = parseKeyString(content)
  179. if err != nil {
  180. return "", err
  181. }
  182. content = strings.TrimRight(content, "\n\r")
  183. if strings.ContainsAny(content, "\n\r") {
  184. return "", errors.New("only a single line with a single key please")
  185. }
  186. // write the key to a file…
  187. tmpFile, err := ioutil.TempFile(os.TempDir(), "keytest")
  188. if err != nil {
  189. return "", err
  190. }
  191. tmpPath := tmpFile.Name()
  192. defer os.Remove(tmpPath)
  193. tmpFile.WriteString(content)
  194. tmpFile.Close()
  195. // Check if ssh-keygen recognizes its contents.
  196. stdout, stderr, err := process.Exec("CheckPublicKeyString", "ssh-keygen", "-l", "-f", tmpPath)
  197. if err != nil {
  198. return "", errors.New("ssh-keygen -l -f: " + stderr)
  199. } else if len(stdout) < 2 {
  200. return "", errors.New("ssh-keygen returned not enough output to evaluate the key: " + stdout)
  201. }
  202. // The ssh-keygen in Windows does not print key type, so no need go further.
  203. if setting.IsWindows {
  204. return content, nil
  205. }
  206. sshKeygenOutput := strings.Split(stdout, " ")
  207. if len(sshKeygenOutput) < 4 {
  208. return content, ErrKeyUnableVerify
  209. }
  210. // Check if key type and key size match.
  211. if !setting.Service.DisableMinimumKeySizeCheck {
  212. keySize := com.StrTo(sshKeygenOutput[0]).MustInt()
  213. if keySize == 0 {
  214. return "", errors.New("cannot get key size of the given key")
  215. }
  216. keyType := strings.TrimSpace(sshKeygenOutput[len(sshKeygenOutput)-1])
  217. if minimumKeySize := minimumKeySizes[keyType]; minimumKeySize == 0 {
  218. return "", errors.New("sorry, unrecognized public key type")
  219. } else if keySize < minimumKeySize {
  220. return "", fmt.Errorf("the minimum accepted size of a public key %s is %d", keyType, minimumKeySize)
  221. }
  222. }
  223. return content, nil
  224. }
  225. // saveAuthorizedKeyFile writes SSH key content to authorized_keys file.
  226. func saveAuthorizedKeyFile(keys ...*PublicKey) error {
  227. sshOpLocker.Lock()
  228. defer sshOpLocker.Unlock()
  229. fpath := filepath.Join(SSHPath, "authorized_keys")
  230. f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
  231. if err != nil {
  232. return err
  233. }
  234. defer f.Close()
  235. fi, err := f.Stat()
  236. if err != nil {
  237. return err
  238. }
  239. // FIXME: following command does not support in Windows.
  240. if !setting.IsWindows {
  241. // .ssh directory should have mode 700, and authorized_keys file should have mode 600.
  242. if fi.Mode().Perm() > 0600 {
  243. log.Error(4, "authorized_keys file has unusual permission flags: %s - setting to -rw-------", fi.Mode().Perm().String())
  244. if err = f.Chmod(0600); err != nil {
  245. return err
  246. }
  247. }
  248. }
  249. for _, key := range keys {
  250. if _, err = f.WriteString(key.GetAuthorizedString()); err != nil {
  251. return err
  252. }
  253. }
  254. return nil
  255. }
  256. func checkKeyContent(content string) error {
  257. // Same key can only be added once.
  258. has, err := x.Where("content=?", content).Get(new(PublicKey))
  259. if err != nil {
  260. return err
  261. } else if has {
  262. return ErrKeyAlreadyExist{0, content}
  263. }
  264. return nil
  265. }
  266. func addKey(e Engine, key *PublicKey) (err error) {
  267. // Calculate fingerprint.
  268. tmpPath := strings.Replace(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
  269. "id_rsa.pub"), "\\", "/", -1)
  270. os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
  271. if err = ioutil.WriteFile(tmpPath, []byte(key.Content), 0644); err != nil {
  272. return err
  273. }
  274. stdout, stderr, err := process.Exec("AddPublicKey", "ssh-keygen", "-l", "-f", tmpPath)
  275. if err != nil {
  276. return errors.New("ssh-keygen -l -f: " + stderr)
  277. } else if len(stdout) < 2 {
  278. return errors.New("not enough output for calculating fingerprint: " + stdout)
  279. }
  280. key.Fingerprint = strings.Split(stdout, " ")[1]
  281. // Save SSH key.
  282. if _, err = e.Insert(key); err != nil {
  283. return err
  284. }
  285. return saveAuthorizedKeyFile(key)
  286. }
  287. // AddPublicKey adds new public key to database and authorized_keys file.
  288. func AddPublicKey(ownerID int64, name, content string) (err error) {
  289. if err = checkKeyContent(content); err != nil {
  290. return err
  291. }
  292. // Key name of same user cannot be duplicated.
  293. has, err := x.Where("owner_id=? AND name=?", ownerID, name).Get(new(PublicKey))
  294. if err != nil {
  295. return err
  296. } else if has {
  297. return ErrKeyNameAlreadyUsed{ownerID, name}
  298. }
  299. sess := x.NewSession()
  300. defer sessionRelease(sess)
  301. if err = sess.Begin(); err != nil {
  302. return err
  303. }
  304. key := &PublicKey{
  305. OwnerID: ownerID,
  306. Name: name,
  307. Content: content,
  308. Mode: ACCESS_MODE_WRITE,
  309. Type: KEY_TYPE_USER,
  310. }
  311. if err = addKey(sess, key); err != nil {
  312. return fmt.Errorf("addKey: %v", err)
  313. }
  314. return sess.Commit()
  315. }
  316. // GetPublicKeyByID returns public key by given ID.
  317. func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
  318. key := new(PublicKey)
  319. has, err := x.Id(keyID).Get(key)
  320. if err != nil {
  321. return nil, err
  322. } else if !has {
  323. return nil, ErrKeyNotExist{keyID}
  324. }
  325. return key, nil
  326. }
  327. // ListPublicKeys returns a list of public keys belongs to given user.
  328. func ListPublicKeys(uid int64) ([]*PublicKey, error) {
  329. keys := make([]*PublicKey, 0, 5)
  330. return keys, x.Where("owner_id=?", uid).Find(&keys)
  331. }
  332. // rewriteAuthorizedKeys finds and deletes corresponding line in authorized_keys file.
  333. func rewriteAuthorizedKeys(key *PublicKey, p, tmpP string) error {
  334. fr, err := os.Open(p)
  335. if err != nil {
  336. return err
  337. }
  338. defer fr.Close()
  339. fw, err := os.OpenFile(tmpP, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
  340. if err != nil {
  341. return err
  342. }
  343. defer fw.Close()
  344. isFound := false
  345. keyword := fmt.Sprintf("key-%d", key.ID)
  346. buf := bufio.NewReader(fr)
  347. for {
  348. line, errRead := buf.ReadString('\n')
  349. line = strings.TrimSpace(line)
  350. if errRead != nil {
  351. if errRead != io.EOF {
  352. return errRead
  353. }
  354. // Reached end of file, if nothing to read then break,
  355. // otherwise handle the last line.
  356. if len(line) == 0 {
  357. break
  358. }
  359. }
  360. // Found the line and copy rest of file.
  361. if !isFound && strings.Contains(line, keyword) && strings.Contains(line, key.Content) {
  362. isFound = true
  363. continue
  364. }
  365. // Still finding the line, copy the line that currently read.
  366. if _, err = fw.WriteString(line + "\n"); err != nil {
  367. return err
  368. }
  369. if errRead == io.EOF {
  370. break
  371. }
  372. }
  373. return nil
  374. }
  375. // UpdatePublicKey updates given public key.
  376. func UpdatePublicKey(key *PublicKey) error {
  377. _, err := x.Id(key.ID).AllCols().Update(key)
  378. return err
  379. }
  380. func deletePublicKey(e *xorm.Session, key *PublicKey) error {
  381. sshOpLocker.Lock()
  382. defer sshOpLocker.Unlock()
  383. has, err := e.Get(key)
  384. if err != nil {
  385. return err
  386. } else if !has {
  387. return nil
  388. }
  389. if _, err = e.Id(key.ID).Delete(key); err != nil {
  390. return err
  391. }
  392. fpath := filepath.Join(SSHPath, "authorized_keys")
  393. tmpPath := filepath.Join(SSHPath, "authorized_keys.tmp")
  394. if err = rewriteAuthorizedKeys(key, fpath, tmpPath); err != nil {
  395. return err
  396. } else if err = os.Remove(fpath); err != nil {
  397. return err
  398. }
  399. return os.Rename(tmpPath, fpath)
  400. }
  401. // DeletePublicKey deletes SSH key information both in database and authorized_keys file.
  402. func DeletePublicKey(id int64) (err error) {
  403. key := &PublicKey{ID: id}
  404. has, err := x.Id(key.ID).Get(key)
  405. if err != nil {
  406. return err
  407. } else if !has {
  408. return nil
  409. }
  410. sess := x.NewSession()
  411. defer sessionRelease(sess)
  412. if err = sess.Begin(); err != nil {
  413. return err
  414. }
  415. if err = deletePublicKey(sess, key); err != nil {
  416. return err
  417. }
  418. return sess.Commit()
  419. }
  420. // RewriteAllPublicKeys removes any authorized key and rewrite all keys from database again.
  421. func RewriteAllPublicKeys() error {
  422. sshOpLocker.Lock()
  423. defer sshOpLocker.Unlock()
  424. tmpPath := filepath.Join(SSHPath, "authorized_keys.tmp")
  425. f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  426. if err != nil {
  427. return err
  428. }
  429. defer os.Remove(tmpPath)
  430. err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
  431. _, err = f.WriteString((bean.(*PublicKey)).GetAuthorizedString())
  432. return err
  433. })
  434. f.Close()
  435. if err != nil {
  436. return err
  437. }
  438. fpath := filepath.Join(SSHPath, "authorized_keys")
  439. if com.IsExist(fpath) {
  440. if err = os.Remove(fpath); err != nil {
  441. return err
  442. }
  443. }
  444. if err = os.Rename(tmpPath, fpath); err != nil {
  445. return err
  446. }
  447. return nil
  448. }
  449. // ________ .__ ____ __.
  450. // \______ \ ____ ______ | | ____ ___.__.| |/ _|____ ___.__.
  451. // | | \_/ __ \\____ \| | / _ < | || <_/ __ < | |
  452. // | ` \ ___/| |_> > |_( <_> )___ || | \ ___/\___ |
  453. // /_______ /\___ > __/|____/\____// ____||____|__ \___ > ____|
  454. // \/ \/|__| \/ \/ \/\/
  455. // DeployKey represents deploy key information and its relation with repository.
  456. type DeployKey struct {
  457. ID int64 `xorm:"pk autoincr"`
  458. KeyID int64 `xorm:"UNIQUE(s) INDEX"`
  459. RepoID int64 `xorm:"UNIQUE(s) INDEX"`
  460. Name string
  461. Fingerprint string
  462. Created time.Time `xorm:"CREATED"`
  463. Updated time.Time // Note: Updated must below Created for AfterSet.
  464. HasRecentActivity bool `xorm:"-"`
  465. HasUsed bool `xorm:"-"`
  466. }
  467. func (k *DeployKey) AfterSet(colName string, _ xorm.Cell) {
  468. switch colName {
  469. case "created":
  470. k.HasUsed = k.Updated.After(k.Created)
  471. k.HasRecentActivity = k.Updated.Add(7 * 24 * time.Hour).After(time.Now())
  472. }
  473. }
  474. func checkDeployKey(e Engine, keyID, repoID int64, name string) error {
  475. // Note: We want error detail, not just true or false here.
  476. has, err := e.Where("key_id=? AND repo_id=?", keyID, repoID).Get(new(DeployKey))
  477. if err != nil {
  478. return err
  479. } else if has {
  480. return ErrDeployKeyAlreadyExist{keyID, repoID}
  481. }
  482. has, err = e.Where("repo_id=? AND name=?", repoID, name).Get(new(DeployKey))
  483. if err != nil {
  484. return err
  485. } else if has {
  486. return ErrDeployKeyNameAlreadyUsed{repoID, name}
  487. }
  488. return nil
  489. }
  490. // addDeployKey adds new key-repo relation.
  491. func addDeployKey(e *xorm.Session, keyID, repoID int64, name, fingerprint string) (err error) {
  492. if err = checkDeployKey(e, keyID, repoID, name); err != nil {
  493. return err
  494. }
  495. _, err = e.Insert(&DeployKey{
  496. KeyID: keyID,
  497. RepoID: repoID,
  498. Name: name,
  499. Fingerprint: fingerprint,
  500. })
  501. return err
  502. }
  503. // HasDeployKey returns true if public key is a deploy key of given repository.
  504. func HasDeployKey(keyID, repoID int64) bool {
  505. has, _ := x.Where("key_id=? AND repo_id=?", keyID, repoID).Get(new(DeployKey))
  506. return has
  507. }
  508. // AddDeployKey add new deploy key to database and authorized_keys file.
  509. func AddDeployKey(repoID int64, name, content string) (err error) {
  510. if err = checkKeyContent(content); err != nil {
  511. return err
  512. }
  513. key := &PublicKey{
  514. Content: content,
  515. Mode: ACCESS_MODE_READ,
  516. Type: KEY_TYPE_DEPLOY,
  517. }
  518. has, err := x.Get(key)
  519. if err != nil {
  520. return err
  521. }
  522. sess := x.NewSession()
  523. defer sessionRelease(sess)
  524. if err = sess.Begin(); err != nil {
  525. return err
  526. }
  527. // First time use this deploy key.
  528. if !has {
  529. if err = addKey(sess, key); err != nil {
  530. return nil
  531. }
  532. }
  533. if err = addDeployKey(sess, key.ID, repoID, name, key.Fingerprint); err != nil {
  534. return err
  535. }
  536. return sess.Commit()
  537. }
  538. // GetDeployKeyByRepo returns deploy key by given public key ID and repository ID.
  539. func GetDeployKeyByRepo(keyID, repoID int64) (*DeployKey, error) {
  540. key := &DeployKey{
  541. KeyID: keyID,
  542. RepoID: repoID,
  543. }
  544. _, err := x.Get(key)
  545. return key, err
  546. }
  547. // UpdateDeployKey updates deploy key information.
  548. func UpdateDeployKey(key *DeployKey) error {
  549. _, err := x.Id(key.ID).AllCols().Update(key)
  550. return err
  551. }
  552. // DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed.
  553. func DeleteDeployKey(id int64) error {
  554. key := &DeployKey{ID: id}
  555. has, err := x.Id(key.ID).Get(key)
  556. if err != nil {
  557. return err
  558. } else if !has {
  559. return nil
  560. }
  561. sess := x.NewSession()
  562. defer sessionRelease(sess)
  563. if err = sess.Begin(); err != nil {
  564. return err
  565. }
  566. if _, err = sess.Id(key.ID).Delete(key); err != nil {
  567. return fmt.Errorf("delete deploy key[%d]: %v", key.ID, err)
  568. }
  569. // Check if this is the last reference to same key content.
  570. has, err = sess.Where("key_id=?", key.KeyID).Get(new(DeployKey))
  571. if err != nil {
  572. return err
  573. } else if !has {
  574. if err = deletePublicKey(sess, &PublicKey{ID: key.KeyID}); err != nil {
  575. return err
  576. }
  577. }
  578. return sess.Commit()
  579. }
  580. // ListDeployKeys returns all deploy keys by given repository ID.
  581. func ListDeployKeys(repoID int64) ([]*DeployKey, error) {
  582. keys := make([]*DeployKey, 0, 5)
  583. return keys, x.Where("repo_id=?", repoID).Find(&keys)
  584. }