-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
206 lines (187 loc) · 5.08 KB
/
registry.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/kelseyhightower/envconfig"
crud "github.com/tg123/sshpiper/sshpiperd/upstream/mysql/crud"
"go.uber.org/zap"
)
type Registry struct {
database *sql.DB
}
type Config struct {
Port int `default:"3306"`
Host string `default:"localhost"`
User string `default:"root"`
Password string `default:""`
Database string `default:"sshpiper"`
}
type Upstream struct {
Name string
Username string
Address string
SSHPiperPrivateKey string
DownstreamPublicKey []string
}
func NewRegistry() *Registry {
return &Registry{}
}
func (r *Registry) ConnectDatabase() error {
var conf Config
var err error
err = envconfig.Process("KSCE_MYSQL", &conf)
if err != nil {
return err
}
logger.Info("MySQL Config", zap.String("user", conf.User), zap.String("host", conf.Host), zap.Int("port", conf.Port), zap.String("database", conf.Database))
source := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", conf.User, conf.Password, conf.Host, conf.Port, conf.Database)
r.database, err = sql.Open("mysql", source)
return err
}
func (r *Registry) IsConnected() bool {
return r.database != nil
}
func (r *Registry) truncate(table string, hasForeignKey bool, ignoreForeignKeyChecks bool) error {
var tx *sql.Tx
var err error
if tx, err = r.database.Begin(); err != nil {
return err
}
if hasForeignKey && ignoreForeignKeyChecks {
if _, err = tx.Exec("set foreign_key_checks = 0;"); err != nil {
return err
}
}
if _, err = tx.Exec(fmt.Sprintf("truncate table %s;", table)); err != nil {
return err
}
if hasForeignKey && ignoreForeignKeyChecks {
if _, err = tx.Exec("set foreign_key_checks = 1;"); err != nil {
return err
}
}
return tx.Commit()
}
func (r *Registry) TruncateAll() error {
var err error
type table struct {
name string
hasForeignKey bool
}
tables := []table{
{"pubkey_prikey_map", false},
{"pubkey_upstream_map", false},
{"user_upstream_map", false},
{"private_keys", true},
{"public_keys", true},
{"server", true},
{"upstream", true},
}
for _, table := range tables {
if err = r.truncate(table.name, table.hasForeignKey, table.hasForeignKey); err != nil {
return err
}
}
logger.Info("Database truncated")
return nil
}
func (r *Registry) RegisterUpstream(upstream *Upstream) (*Upstream, error) {
var err error
var serverID int64
var upstreamID int64
var privateKeyID int64
var publicKeyID int64
s := crud.NewServer(r.database)
if rec, err := s.GetFirstByAddress(upstream.Address); err == nil {
if rec != nil {
serverID = rec.Id
} else {
if serverID, err = s.Post(&crud.ServerRecord{Name: upstream.Name, Address: upstream.Address}); err == nil {
err = s.Commit()
} else {
err = s.Rollback()
}
}
}
if err != nil {
return nil, err
}
u := crud.NewUpstream(r.database)
if rec, err := u.GetFirstByServerId(serverID); err == nil {
if rec != nil {
upstreamID = rec.Id
} else {
if upstreamID, err = u.Post(&crud.UpstreamRecord{Name: upstream.Name, ServerId: serverID, Username: upstream.Name}); err == nil {
err = u.Commit()
} else {
err = u.Rollback()
}
}
}
if err != nil {
return nil, err
}
uum := crud.NewUserUpstreamMap(r.database)
if rec, err := uum.GetFirstByUpstreamId(upstreamID); err == nil && rec == nil {
if _, err = uum.Post(&crud.UserUpstreamMapRecord{UpstreamId: upstreamID, Username: upstream.Username}); err == nil {
err = uum.Commit()
} else {
err = uum.Rollback()
}
}
if err != nil {
return nil, err
}
prv := crud.NewPrivateKeys(r.database)
if rec, err := prv.GetFirstByName(upstream.Name); err == nil {
if rec != nil {
privateKeyID = rec.Id
} else {
if privateKeyID, err = prv.Post(&crud.PrivateKeysRecord{Name: upstream.Name, Data: upstream.SSHPiperPrivateKey}); err == nil {
err = prv.Commit()
} else {
err = prv.Rollback()
}
}
}
if err != nil {
return nil, err
}
for i := range upstream.DownstreamPublicKey {
logger.Info("New DownstreamPublicKey")
pub := crud.NewPublicKeys(r.database)
// if rec, err := pub.GetFirstByName(upstream.Name); err == nil {
// if rec != nil {
// publicKeyID = rec.Id
// logger.Info("Key Fetched")
// } else {
if publicKeyID, err = pub.Post(&crud.PublicKeysRecord{Name: upstream.Name, Data: upstream.DownstreamPublicKey[i]}); err == nil {
err = pub.Commit()
logger.Info("Key Added")
logger.Info(upstream.DownstreamPublicKey[i])
} else {
err = pub.Rollback()
}
// }
// }
if err != nil {
return nil, err
}
ppm := crud.NewPubkeyPrikeyMap(r.database)
// if rec, err := ppm.GetFirstByPrivateKeyId(privateKeyID); err == nil && rec == nil {
if _, err = ppm.Post(&crud.PubkeyPrikeyMapRecord{PrivateKeyId: privateKeyID, PubkeyId: publicKeyID}); err == nil {
err = ppm.Commit()
logger.Info("Key Map Added")
} else {
err = ppm.Rollback()
}
// }
// if err != nil {
// return nil, err
// }
logger.Info("########")
}
logger.Info("Upstream registered", zap.String("name", upstream.Name), zap.String("username", upstream.Username))
return nil, err
}