Skip to content

Commit 4043c95

Browse files
lunnydelvh
authored andcommitted
Allow options to disable user ssh keys configuration from the interface on app.ini (#29447)
Follow #29275 Extract from #20549 Fix #24716 --------- Co-authored-by: delvh <dev.lh@web.de>
1 parent c937e69 commit 4043c95

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed

custom/conf/app.example.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -1480,8 +1480,9 @@ LEVEL = Info
14801480
;;
14811481
;; Default configuration for email notifications for users (user configurable). Options: enabled, onmention, disabled
14821482
;DEFAULT_EMAIL_NOTIFICATIONS = enabled
1483-
;; Disabled features for users, could be "deletion","manage_gpg_keys" more features can be disabled in future
1483+
;; Disabled features for users, could be "deletion", "manage_ssh_keys","manage_gpg_keys" more features can be disabled in future
14841484
;; - deletion: a user cannot delete their own account
1485+
;; - manage_ssh_keys: a user cannot configure ssh keys
14851486
;; - manage_gpg_keys: a user cannot configure gpg keys
14861487
;USER_DISABLED_FEATURES =
14871488

docs/content/administration/config-cheat-sheet.en-us.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,10 @@ And the following unique queues:
518518

519519
- `DEFAULT_EMAIL_NOTIFICATIONS`: **enabled**: Default configuration for email notifications for users (user configurable). Options: enabled, onmention, disabled
520520
- `DISABLE_REGULAR_ORG_CREATION`: **false**: Disallow regular (non-admin) users from creating organizations.
521-
- `USER_DISABLED_FEATURES`: **_empty_** Disabled features for users, could be `deletion`, `manage_gpg_keys` and more features can be added in future.
521+
- `USER_DISABLED_FEATURES`: **_empty_** Disabled features for users, could be `deletion`, `manage_ssh_keys`, `manage_gpg_keys` and more features can be added in future.
522522
- `deletion`: User cannot delete their own account.
523-
- `manage_gpg_keys`: User cannot configure gpg keys
523+
- `manage_ssh_keys`: User cannot configure ssh keys.
524+
- `manage_gpg_keys`: User cannot configure gpg keys.
524525

525526
## Security (`security`)
526527

docs/content/administration/config-cheat-sheet.zh-cn.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,10 @@ Gitea 创建以下非唯一队列:
497497

498498
- `DEFAULT_EMAIL_NOTIFICATIONS`: **enabled**:用户电子邮件通知的默认配置(用户可配置)。选项:enabled、onmention、disabled
499499
- `DISABLE_REGULAR_ORG_CREATION`: **false**:禁止普通(非管理员)用户创建组织。
500-
- `USER_DISABLED_FEATURES`:**_empty_** 禁用的用户特性,当前允许为空或者 `deletion``manage_gpg_keys` 未来可以增加更多设置。
500+
- `USER_DISABLED_FEATURES`:**_empty_** 禁用的用户特性,当前允许为空或者 `deletion``manage_ssh_keys``manage_gpg_keys` 未来可以增加更多设置。
501501
- `deletion`: 用户不能通过界面或者API删除他自己。
502-
- `manage_gpg_keys`: 用户不能配置 GPG 密钥
502+
- `manage_ssh_keys`: 用户不能通过界面或者API配置SSH Keys。
503+
- `manage_gpg_keys`: 用户不能配置 GPG 密钥。
503504

504505
## 安全性 (`security`)
505506

modules/setting/admin.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ func loadAdminFrom(rootCfg ConfigProvider) {
2121

2222
const (
2323
UserFeatureDeletion = "deletion"
24+
UserFeatureManageSSHKeys = "manage_ssh_keys"
2425
UserFeatureManageGPGKeys = "manage_gpg_keys"
2526
)

routers/api/v1/user/key.go

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package user
55

66
import (
77
std_ctx "context"
8+
"fmt"
89
"net/http"
910

1011
asymkey_model "code.gitea.io/gitea/models/asymkey"
@@ -198,6 +199,11 @@ func GetPublicKey(ctx *context.APIContext) {
198199

199200
// CreateUserPublicKey creates new public key to given user by ID.
200201
func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) {
202+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageSSHKeys) {
203+
ctx.NotFound("Not Found", fmt.Errorf("ssh keys setting is not allowed to be visited"))
204+
return
205+
}
206+
201207
content, err := asymkey_model.CheckPublicKeyString(form.Key)
202208
if err != nil {
203209
repo.HandleCheckKeyStringError(ctx, err)
@@ -263,6 +269,11 @@ func DeletePublicKey(ctx *context.APIContext) {
263269
// "404":
264270
// "$ref": "#/responses/notFound"
265271

272+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageSSHKeys) {
273+
ctx.NotFound("Not Found", fmt.Errorf("ssh keys setting is not allowed to be visited"))
274+
return
275+
}
276+
266277
id := ctx.ParamsInt64(":id")
267278
externallyManaged, err := asymkey_model.PublicKeyIsExternallyManaged(ctx, id)
268279
if err != nil {

routers/web/user/setting/keys.go

+16
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ func KeysPost(ctx *context.Context) {
159159
ctx.Flash.Success(ctx.Tr("settings.verify_gpg_key_success", keyID))
160160
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
161161
case "ssh":
162+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageSSHKeys) {
163+
ctx.NotFound("Not Found", fmt.Errorf("ssh keys setting is not allowed to be visited"))
164+
return
165+
}
166+
162167
content, err := asymkey_model.CheckPublicKeyString(form.Content)
163168
if err != nil {
164169
if db.IsErrSSHDisabled(err) {
@@ -198,6 +203,11 @@ func KeysPost(ctx *context.Context) {
198203
ctx.Flash.Success(ctx.Tr("settings.add_key_success", form.Title))
199204
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
200205
case "verify_ssh":
206+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageSSHKeys) {
207+
ctx.NotFound("Not Found", fmt.Errorf("ssh keys setting is not allowed to be visited"))
208+
return
209+
}
210+
201211
token := asymkey_model.VerificationToken(ctx.Doer, 1)
202212
lastToken := asymkey_model.VerificationToken(ctx.Doer, 0)
203213

@@ -240,6 +250,11 @@ func DeleteKey(ctx *context.Context) {
240250
ctx.Flash.Success(ctx.Tr("settings.gpg_key_deletion_success"))
241251
}
242252
case "ssh":
253+
if setting.Admin.UserDisabledFeatures.Contains(setting.UserFeatureManageSSHKeys) {
254+
ctx.NotFound("Not Found", fmt.Errorf("ssh keys setting is not allowed to be visited"))
255+
return
256+
}
257+
243258
keyID := ctx.FormInt64("id")
244259
external, err := asymkey_model.PublicKeyIsExternallyManaged(ctx, keyID)
245260
if err != nil {
@@ -318,4 +333,5 @@ func loadKeysData(ctx *context.Context) {
318333

319334
ctx.Data["VerifyingID"] = ctx.FormString("verify_gpg")
320335
ctx.Data["VerifyingFingerprint"] = ctx.FormString("verify_ssh")
336+
ctx.Data["UserDisabledFeatures"] = &setting.Admin.UserDisabledFeatures
321337
}

templates/user/settings/keys.tmpl

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{{template "user/settings/layout_head" (dict "ctxData" . "pageClass" "user settings sshkeys")}}
22
<div class="user-setting-content">
3-
{{template "user/settings/keys_ssh" .}}
3+
{{if not ($.UserDisabledFeatures.Contains "manage_ssh_keys")}}
4+
{{template "user/settings/keys_ssh" .}}
5+
{{end}}
46
{{template "user/settings/keys_principal" .}}
57
{{if not ($.UserDisabledFeatures.Contains "manage_gpg_keys")}}
68
{{template "user/settings/keys_gpg" .}}

0 commit comments

Comments
 (0)