You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keys.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/private"
  10. "github.com/urfave/cli"
  11. )
  12. // CmdKeys represents the available keys sub-command
  13. var CmdKeys = cli.Command{
  14. Name: "keys",
  15. Usage: "This command queries the Gitea database to get the authorized command for a given ssh key fingerprint",
  16. Before: PrepareConsoleLoggerLevel(log.FATAL),
  17. Action: runKeys,
  18. Flags: []cli.Flag{
  19. cli.StringFlag{
  20. Name: "expected, e",
  21. Value: "git",
  22. Usage: "Expected user for whom provide key commands",
  23. },
  24. cli.StringFlag{
  25. Name: "username, u",
  26. Value: "",
  27. Usage: "Username trying to log in by SSH",
  28. },
  29. cli.StringFlag{
  30. Name: "type, t",
  31. Value: "",
  32. Usage: "Type of the SSH key provided to the SSH Server (requires content to be provided too)",
  33. },
  34. cli.StringFlag{
  35. Name: "content, k",
  36. Value: "",
  37. Usage: "Base64 encoded content of the SSH key provided to the SSH Server (requires type to be provided too)",
  38. },
  39. },
  40. }
  41. func runKeys(c *cli.Context) error {
  42. if !c.IsSet("username") {
  43. return errors.New("No username provided")
  44. }
  45. // Check username matches the expected username
  46. if strings.TrimSpace(c.String("username")) != strings.TrimSpace(c.String("expected")) {
  47. return nil
  48. }
  49. content := ""
  50. if c.IsSet("type") && c.IsSet("content") {
  51. content = fmt.Sprintf("%s %s", strings.TrimSpace(c.String("type")), strings.TrimSpace(c.String("content")))
  52. }
  53. if content == "" {
  54. return errors.New("No key type and content provided")
  55. }
  56. ctx, cancel := installSignals()
  57. defer cancel()
  58. setup(ctx, false)
  59. authorizedString, extra := private.AuthorizedPublicKeyByContent(ctx, content)
  60. // do not use handleCliResponseExtra or cli.NewExitError, if it exists immediately, it breaks some tests like Test_CmdKeys
  61. if extra.Error != nil {
  62. return extra.Error
  63. }
  64. fmt.Println(strings.TrimSpace(authorizedString))
  65. return nil
  66. }