-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig_admin.go
104 lines (83 loc) · 2.25 KB
/
config_admin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gitdir
import (
"github.com/belak/go-gitdir/internal/git"
"github.com/belak/go-gitdir/models"
)
func (c *Config) ensureAdminConfig(repo *git.Repository) error {
return newMultiError(
c.ensureAdminConfigYaml(repo),
c.ensureAdminEd25519Key(repo),
c.ensureAdminRSAKey(repo),
)
}
func (c *Config) ensureAdminConfigYaml(repo *git.Repository) error {
return repo.UpdateFile("config.yml", ensureSampleConfig)
}
func (c *Config) ensureAdminUser(repo *git.Repository, user string, pubKey *models.PublicKey) error {
return repo.UpdateFile("config.yml", func(data []byte) ([]byte, error) {
return ensureAdminUser(data, user, pubKey.MarshalAuthorizedKey())
})
}
func (c *Config) ensureAdminEd25519Key(repo *git.Repository) error {
return repo.UpdateFile("ssh/id_ed25519", func(data []byte) ([]byte, error) {
if data != nil {
return data, nil
}
pk, err := models.GenerateEd25519PrivateKey()
if err != nil {
return nil, err
}
return pk.MarshalPrivateKey()
})
}
func (c *Config) ensureAdminRSAKey(repo *git.Repository) error {
return repo.UpdateFile("ssh/id_rsa", func(data []byte) ([]byte, error) {
if data != nil {
return data, nil
}
pk, err := models.GenerateRSAPrivateKey()
if err != nil {
return nil, err
}
return pk.MarshalPrivateKey()
})
}
func (c *Config) loadAdminConfig(adminRepo *git.Repository) error {
configData, err := adminRepo.GetFile("config.yml")
if err != nil {
return err
}
adminConfig, err := models.ParseAdminConfig(configData)
if err != nil {
return err
}
// Merge the adminConfig with the base config. Note that this will reset all
// values.
c.Invites = adminConfig.Invites
c.Groups = adminConfig.Groups
c.Orgs = adminConfig.Orgs
c.Users = adminConfig.Users
c.Repos = adminConfig.Repos
c.Options = adminConfig.Options
// Load the private keys
c.PrivateKeys = nil
keyData, err := adminRepo.GetFile("ssh/id_ed25519")
if err != nil {
return err
}
pk, err := models.ParseEd25519PrivateKey(keyData)
if err != nil {
return err
}
c.PrivateKeys = append(c.PrivateKeys, pk)
keyData, err = adminRepo.GetFile("ssh/id_rsa")
if err != nil {
return err
}
pk, err = models.ParseRSAPrivateKey(keyData)
if err != nil {
return err
}
c.PrivateKeys = append(c.PrivateKeys, pk)
return nil
}