Skip to content

Commit 7d9a191

Browse files
zeripathtechknowlogick
authored andcommitted
Create AuthorizedKeysCommand (go-gitea#5236)
1 parent 00533d3 commit 7d9a191

File tree

7 files changed

+136
-19
lines changed

7 files changed

+136
-19
lines changed

cmd/cmd.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ func argsSet(c *cli.Context, args ...string) error {
2727
}
2828

2929
func initDB() error {
30+
return initDBDisableConsole(false)
31+
}
32+
33+
func initDBDisableConsole(disableConsole bool) error {
3034
setting.NewContext()
3135
models.LoadConfigs()
3236

33-
setting.NewXORMLogService(false)
37+
setting.NewXORMLogService(disableConsole)
3438
if err := models.SetEngine(); err != nil {
3539
return fmt.Errorf("models.SetEngine: %v", err)
3640
}

cmd/keys.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2018 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 cmd
6+
7+
import (
8+
"errors"
9+
"fmt"
10+
"strings"
11+
12+
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/setting"
14+
15+
"github.com/urfave/cli"
16+
)
17+
18+
// CmdKeys represents the available keys sub-command
19+
var CmdKeys = cli.Command{
20+
Name: "keys",
21+
Usage: "This command queries the Gitea database to get the authorized command for a given ssh key fingerprint",
22+
Action: runKeys,
23+
Flags: []cli.Flag{
24+
cli.StringFlag{
25+
Name: "expected, e",
26+
Value: "git",
27+
Usage: "Expected user for whom provide key commands",
28+
},
29+
cli.StringFlag{
30+
Name: "username, u",
31+
Value: "",
32+
Usage: "Username trying to log in by SSH",
33+
},
34+
cli.StringFlag{
35+
Name: "type, t",
36+
Value: "",
37+
Usage: "Type of the SSH key provided to the SSH Server (requires content to be provided too)",
38+
},
39+
cli.StringFlag{
40+
Name: "content, k",
41+
Value: "",
42+
Usage: "Base64 encoded content of the SSH key provided to the SSH Server (requires type to be provided too)",
43+
},
44+
cli.StringFlag{
45+
Name: "config, c",
46+
Value: "custom/conf/app.ini",
47+
Usage: "Custom configuration file path",
48+
},
49+
},
50+
}
51+
52+
func runKeys(c *cli.Context) error {
53+
if c.IsSet("config") {
54+
setting.CustomConf = c.String("config")
55+
}
56+
57+
if !c.IsSet("username") {
58+
return errors.New("No username provided")
59+
}
60+
// Check username matches the expected username
61+
if strings.TrimSpace(c.String("username")) != strings.TrimSpace(c.String("expected")) {
62+
return nil
63+
}
64+
65+
content := ""
66+
67+
if c.IsSet("type") && c.IsSet("content") {
68+
content = fmt.Sprintf("%s %s", strings.TrimSpace(c.String("type")), strings.TrimSpace(c.String("content")))
69+
}
70+
71+
if content == "" {
72+
return errors.New("No key type and content provided")
73+
}
74+
75+
if err := initDBDisableConsole(true); err != nil {
76+
return err
77+
}
78+
79+
publicKey, err := models.SearchPublicKeyByContent(content)
80+
if err != nil {
81+
return err
82+
}
83+
fmt.Println(publicKey.AuthorizedString())
84+
return nil
85+
}

custom/conf/app.ini.sample

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ SSH_PORT = 22
154154
SSH_LISTEN_PORT = %(SSH_PORT)s
155155
; Root path of SSH directory, default is '~/.ssh', but you have to use '/home/git/.ssh'.
156156
SSH_ROOT_PATH =
157+
; Gitea will create a authorized_keys file by default when it is not using the internal ssh server
158+
; If you intend to use the AuthorizedKeysCommand functionality then you should turn this off.
159+
SSH_CREATE_AUTHORIZED_KEYS_FILE = true
157160
; For the built-in SSH server, choose the ciphers to support for SSH connections,
158161
; for system SSH this setting has no effect
159162
SSH_SERVER_CIPHERS = aes128-ctr, aes192-ctr, aes256-ctr, [email protected], arcfour256, arcfour128

docs/content/doc/usage/command-line.md

+21
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,24 @@ for automatic deployments.
163163
- `gitea generate secret INTERNAL_TOKEN`
164164
- `gitea generate secret LFS_JWT_SECRET`
165165
- `gitea generate secret SECRET_KEY`
166+
167+
#### keys
168+
169+
Provides an SSHD AuthorizedKeysCommand. Needs to be configured in the sshd config file:
170+
171+
```ini
172+
...
173+
# The value of -e and the AuthorizedKeysCommandUser should match the
174+
# username running gitea
175+
AuthorizedKeysCommandUser git
176+
AuthorizedKeysCommand /path/to/gitea keys -e git -u %u -t %t -k %k
177+
```
178+
179+
The command will return the appropriate authorized_keys line for the
180+
provided key. You should also set the value
181+
`SSH_CREATE_AUTHORIZED_KEYS_FILE=false` in the `[server]` section of
182+
`app.ini`.
183+
184+
NB: opensshd requires the gitea program to be owned by root and not
185+
writable by group or others. The program must be specified by an absolute
186+
path.

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/cmd"
1414
"code.gitea.io/gitea/modules/log"
1515
"code.gitea.io/gitea/modules/setting"
16+
1617
// register supported doc types
1718
_ "code.gitea.io/gitea/modules/markup/csv"
1819
_ "code.gitea.io/gitea/modules/markup/markdown"
@@ -48,6 +49,7 @@ arguments - which can alternatively be run by running the subcommand web.`
4849
cmd.CmdAdmin,
4950
cmd.CmdGenerate,
5051
cmd.CmdMigrate,
52+
cmd.CmdKeys,
5153
}
5254
app.Flags = append(app.Flags, cmd.CmdWeb.Flags...)
5355
app.Action = cmd.CmdWeb.Action

models/ssh_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ func DeletePublicKey(doer *User, id int64) (err error) {
558558
// outside any session scope independently.
559559
func RewriteAllPublicKeys() error {
560560
//Don't rewrite key if internal server
561-
if setting.SSH.StartBuiltinServer {
561+
if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile {
562562
return nil
563563
}
564564

modules/setting/setting.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,24 @@ var (
118118
LetsEncryptEmail string
119119

120120
SSH = struct {
121-
Disabled bool `ini:"DISABLE_SSH"`
122-
StartBuiltinServer bool `ini:"START_SSH_SERVER"`
123-
BuiltinServerUser string `ini:"BUILTIN_SSH_SERVER_USER"`
124-
Domain string `ini:"SSH_DOMAIN"`
125-
Port int `ini:"SSH_PORT"`
126-
ListenHost string `ini:"SSH_LISTEN_HOST"`
127-
ListenPort int `ini:"SSH_LISTEN_PORT"`
128-
RootPath string `ini:"SSH_ROOT_PATH"`
129-
ServerCiphers []string `ini:"SSH_SERVER_CIPHERS"`
130-
ServerKeyExchanges []string `ini:"SSH_SERVER_KEY_EXCHANGES"`
131-
ServerMACs []string `ini:"SSH_SERVER_MACS"`
132-
KeyTestPath string `ini:"SSH_KEY_TEST_PATH"`
133-
KeygenPath string `ini:"SSH_KEYGEN_PATH"`
134-
AuthorizedKeysBackup bool `ini:"SSH_AUTHORIZED_KEYS_BACKUP"`
135-
MinimumKeySizeCheck bool `ini:"-"`
136-
MinimumKeySizes map[string]int `ini:"-"`
137-
ExposeAnonymous bool `ini:"SSH_EXPOSE_ANONYMOUS"`
121+
Disabled bool `ini:"DISABLE_SSH"`
122+
StartBuiltinServer bool `ini:"START_SSH_SERVER"`
123+
BuiltinServerUser string `ini:"BUILTIN_SSH_SERVER_USER"`
124+
Domain string `ini:"SSH_DOMAIN"`
125+
Port int `ini:"SSH_PORT"`
126+
ListenHost string `ini:"SSH_LISTEN_HOST"`
127+
ListenPort int `ini:"SSH_LISTEN_PORT"`
128+
RootPath string `ini:"SSH_ROOT_PATH"`
129+
ServerCiphers []string `ini:"SSH_SERVER_CIPHERS"`
130+
ServerKeyExchanges []string `ini:"SSH_SERVER_KEY_EXCHANGES"`
131+
ServerMACs []string `ini:"SSH_SERVER_MACS"`
132+
KeyTestPath string `ini:"SSH_KEY_TEST_PATH"`
133+
KeygenPath string `ini:"SSH_KEYGEN_PATH"`
134+
AuthorizedKeysBackup bool `ini:"SSH_AUTHORIZED_KEYS_BACKUP"`
135+
MinimumKeySizeCheck bool `ini:"-"`
136+
MinimumKeySizes map[string]int `ini:"-"`
137+
CreateAuthorizedKeysFile bool `ini:"SSH_CREATE_AUTHORIZED_KEYS_FILE"`
138+
ExposeAnonymous bool `ini:"SSH_EXPOSE_ANONYMOUS"`
138139
}{
139140
Disabled: false,
140141
StartBuiltinServer: false,
@@ -863,6 +864,7 @@ func NewContext() {
863864
}
864865
}
865866
SSH.AuthorizedKeysBackup = sec.Key("SSH_AUTHORIZED_KEYS_BACKUP").MustBool(true)
867+
SSH.CreateAuthorizedKeysFile = sec.Key("SSH_CREATE_AUTHORIZED_KEYS_FILE").MustBool(true)
866868
SSH.ExposeAnonymous = sec.Key("SSH_EXPOSE_ANONYMOUS").MustBool(false)
867869

868870
sec = Cfg.Section("server")

0 commit comments

Comments
 (0)