Skip to content

Commit cb940c4

Browse files
noerwtechknowlogickzeripathlunny
authored
Encrypt migration credentials at rest (#15895)
* encrypt migration credentials in task persistence Not sure this is the best approach, we could encrypt the entire `PayloadContent` instead. Also instead of clearing individual fields in payload content, we could just delete the task once it has (successfully) finished..? * remove credentials of past migrations * only run DB migration for completed tasks * fix binding * add omitempty * never serialize unencrypted credentials * fix import order Co-authored-by: techknowlogick <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 256b1a3 commit cb940c4

File tree

5 files changed

+145
-5
lines changed

5 files changed

+145
-5
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ var migrations = []Migration{
309309
NewMigration("Add LFS columns to Mirror", addLFSMirrorColumns),
310310
// v179 -> v180
311311
NewMigration("Convert avatar url to text", convertAvatarURLToText),
312+
// v180 -> v181
313+
NewMigration("Delete credentials from past migrations", deleteMigrationCredentials),
312314
}
313315

314316
// GetCurrentDBVersion returns the current db version

models/migrations/v180.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2021 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 migrations
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/migrations/base"
10+
"code.gitea.io/gitea/modules/structs"
11+
"code.gitea.io/gitea/modules/util"
12+
13+
jsoniter "github.com/json-iterator/go"
14+
"xorm.io/builder"
15+
"xorm.io/xorm"
16+
)
17+
18+
func deleteMigrationCredentials(x *xorm.Engine) (err error) {
19+
const batchSize = 100
20+
21+
// only match migration tasks, that are not pending or running
22+
cond := builder.Eq{
23+
"type": structs.TaskTypeMigrateRepo,
24+
}.And(builder.Gte{
25+
"status": structs.TaskStatusStopped,
26+
})
27+
28+
sess := x.NewSession()
29+
defer sess.Close()
30+
31+
for start := 0; ; start += batchSize {
32+
tasks := make([]*models.Task, 0, batchSize)
33+
if err = sess.Limit(batchSize, start).Where(cond, 0).Find(&tasks); err != nil {
34+
return
35+
}
36+
if len(tasks) == 0 {
37+
break
38+
}
39+
if err = sess.Begin(); err != nil {
40+
return
41+
}
42+
for _, t := range tasks {
43+
if t.PayloadContent, err = removeCredentials(t.PayloadContent); err != nil {
44+
return
45+
}
46+
if _, err = sess.ID(t.ID).Cols("payload_content").Update(t); err != nil {
47+
return
48+
}
49+
}
50+
if err = sess.Commit(); err != nil {
51+
return
52+
}
53+
}
54+
return
55+
}
56+
57+
func removeCredentials(payload string) (string, error) {
58+
var opts base.MigrateOptions
59+
json := jsoniter.ConfigCompatibleWithStandardLibrary
60+
err := json.Unmarshal([]byte(payload), &opts)
61+
if err != nil {
62+
return "", err
63+
}
64+
65+
opts.AuthPassword = ""
66+
opts.AuthToken = ""
67+
opts.CloneAddr = util.SanitizeURLCredentials(opts.CloneAddr, true)
68+
69+
confBytes, err := json.Marshal(opts)
70+
if err != nil {
71+
return "", err
72+
}
73+
return string(confBytes), nil
74+
}

models/task.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
"fmt"
99

1010
migration "code.gitea.io/gitea/modules/migrations/base"
11+
"code.gitea.io/gitea/modules/secret"
12+
"code.gitea.io/gitea/modules/setting"
1113
"code.gitea.io/gitea/modules/structs"
1214
"code.gitea.io/gitea/modules/timeutil"
15+
"code.gitea.io/gitea/modules/util"
1316
jsoniter "github.com/json-iterator/go"
1417

1518
"xorm.io/builder"
@@ -110,6 +113,24 @@ func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) {
110113
if err != nil {
111114
return nil, err
112115
}
116+
117+
// decrypt credentials
118+
if opts.CloneAddrEncrypted != "" {
119+
if opts.CloneAddr, err = secret.DecryptSecret(setting.SecretKey, opts.CloneAddrEncrypted); err != nil {
120+
return nil, err
121+
}
122+
}
123+
if opts.AuthPasswordEncrypted != "" {
124+
if opts.AuthPassword, err = secret.DecryptSecret(setting.SecretKey, opts.AuthPasswordEncrypted); err != nil {
125+
return nil, err
126+
}
127+
}
128+
if opts.AuthTokenEncrypted != "" {
129+
if opts.AuthToken, err = secret.DecryptSecret(setting.SecretKey, opts.AuthTokenEncrypted); err != nil {
130+
return nil, err
131+
}
132+
}
133+
113134
return &opts, nil
114135
}
115136
return nil, fmt.Errorf("Task type is %s, not Migrate Repo", task.Type.Name())
@@ -205,12 +226,31 @@ func createTask(e Engine, task *Task) error {
205226
func FinishMigrateTask(task *Task) error {
206227
task.Status = structs.TaskStatusFinished
207228
task.EndTime = timeutil.TimeStampNow()
229+
230+
// delete credentials when we're done, they're a liability.
231+
conf, err := task.MigrateConfig()
232+
if err != nil {
233+
return err
234+
}
235+
conf.AuthPassword = ""
236+
conf.AuthToken = ""
237+
conf.CloneAddr = util.SanitizeURLCredentials(conf.CloneAddr, true)
238+
conf.AuthPasswordEncrypted = ""
239+
conf.AuthTokenEncrypted = ""
240+
conf.CloneAddrEncrypted = ""
241+
json := jsoniter.ConfigCompatibleWithStandardLibrary
242+
confBytes, err := json.Marshal(conf)
243+
if err != nil {
244+
return err
245+
}
246+
task.PayloadContent = string(confBytes)
247+
208248
sess := x.NewSession()
209249
defer sess.Close()
210250
if err := sess.Begin(); err != nil {
211251
return err
212252
}
213-
if _, err := sess.ID(task.ID).Cols("status", "end_time").Update(task); err != nil {
253+
if _, err := sess.ID(task.ID).Cols("status", "end_time", "payload_content").Update(task); err != nil {
214254
return err
215255
}
216256

modules/migrations/base/options.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import "code.gitea.io/gitea/modules/structs"
1111
// this is for internal usage by migrations module and func who interact with it
1212
type MigrateOptions struct {
1313
// required: true
14-
CloneAddr string `json:"clone_addr" binding:"Required"`
15-
AuthUsername string `json:"auth_username"`
16-
AuthPassword string `json:"auth_password"`
17-
AuthToken string `json:"auth_token"`
14+
CloneAddr string `json:"clone_addr" binding:"Required"`
15+
CloneAddrEncrypted string `json:"clone_addr_encrypted,omitempty"`
16+
AuthUsername string `json:"auth_username"`
17+
AuthPassword string `json:"-"`
18+
AuthPasswordEncrypted string `json:"auth_password_encrypted,omitempty"`
19+
AuthToken string `json:"-"`
20+
AuthTokenEncrypted string `json:"auth_token_encrypted,omitempty"`
1821
// required: true
1922
UID int `json:"uid" binding:"Required"`
2023
// required: true

modules/task/task.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import (
1313
"code.gitea.io/gitea/modules/migrations/base"
1414
"code.gitea.io/gitea/modules/queue"
1515
repo_module "code.gitea.io/gitea/modules/repository"
16+
"code.gitea.io/gitea/modules/secret"
17+
"code.gitea.io/gitea/modules/setting"
1618
"code.gitea.io/gitea/modules/structs"
1719
"code.gitea.io/gitea/modules/timeutil"
20+
"code.gitea.io/gitea/modules/util"
1821
jsoniter "github.com/json-iterator/go"
1922
)
2023

@@ -65,6 +68,24 @@ func MigrateRepository(doer, u *models.User, opts base.MigrateOptions) error {
6568

6669
// CreateMigrateTask creates a migrate task
6770
func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models.Task, error) {
71+
// encrypt credentials for persistence
72+
var err error
73+
opts.CloneAddrEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.CloneAddr)
74+
if err != nil {
75+
return nil, err
76+
}
77+
opts.CloneAddr = util.SanitizeURLCredentials(opts.CloneAddr, true)
78+
opts.AuthPasswordEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.AuthPassword)
79+
if err != nil {
80+
return nil, err
81+
}
82+
opts.AuthPassword = ""
83+
opts.AuthTokenEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.AuthToken)
84+
if err != nil {
85+
return nil, err
86+
}
87+
opts.AuthToken = ""
88+
6889
json := jsoniter.ConfigCompatibleWithStandardLibrary
6990
bs, err := json.Marshal(&opts)
7091
if err != nil {

0 commit comments

Comments
 (0)