/
state.go
188 lines (168 loc) · 4.72 KB
/
state.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
package app
import (
"os"
"path/filepath"
"strings"
"github.com/patrickmn/go-cache"
dt "github.com/xops-infra/go-dingtalk-sdk-wrapper"
"github.com/xops-infra/jms/core/db"
"github.com/xops-infra/jms/utils"
"github.com/xops-infra/multi-cloud-sdk/pkg/io"
"github.com/xops-infra/multi-cloud-sdk/pkg/model"
server "github.com/xops-infra/multi-cloud-sdk/pkg/service"
"github.com/xops-infra/noop/log"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"github.com/xops-infra/jms/config"
)
var App *Application
type Application struct {
Debug bool
HomeDir, SSHDir string // /opt/jms/
Version string
RobotClient *dt.RobotClient // 钉钉机器人
DingTalkClient *dt.DingTalkClient // 钉钉APP使用审批流
Ldap *utils.Ldap
Config *config.Config // 支持数据库和配置文件两种方式载入配置
Cache *cache.Cache
DBService *db.DBService
McsServer model.CommonContract
}
// Manager,Agent,Worker need to be initialized
func NewSshdApplication(debug bool, version string) *Application {
if version == "" {
version = "unknown"
}
App = &Application{
HomeDir: "/opt/jms/",
SSHDir: "/opt/jms/.ssh/",
Version: version,
Debug: debug,
Config: config.Conf,
Cache: cache.New(cache.NoExpiration, cache.NoExpiration),
}
// mkdir
err := os.MkdirAll(App.SSHDir, 0755)
if err != nil {
panic(err)
}
// 判断文件hostAuthorizedKeys是否存在,不存在则创建
hostAuthorizedKeys := App.SSHDir + "authorized_keys"
if !utils.FileExited(hostAuthorizedKeys) {
// 600权限
os.Create(hostAuthorizedKeys)
os.Chmod(hostAuthorizedKeys, 0600)
}
return App
}
func NewApiApplication() *Application {
App = &Application{
Config: config.Conf,
}
return App
}
// withLdap
func (app *Application) WithLdap() *Application {
ldap, err := utils.NewLdap(App.Config.WithLdap)
if err != nil {
panic(err)
}
app.Ldap = ldap
return app
}
// withMcs
func (app *Application) WithMcs() *Application {
if len(App.Config.Profiles) == 0 {
panic("请配置 profiles")
}
profiles := DBProfilesToMcsProfiles(app.Config.Profiles)
cloudIo := io.NewCloudClient(profiles)
serverTencent := io.NewTencentClient(cloudIo)
serverAws := io.NewAwsClient(cloudIo)
App.McsServer = server.NewCommonService(profiles, serverAws, serverTencent)
return app
}
func (app *Application) WithRobot() *Application {
app.RobotClient = dt.NewRobotClient()
return app
}
func (app *Application) WithDingTalk() *Application {
client, _ := dt.NewDingTalkClient(&dt.DingTalkConfig{
AppKey: app.Config.WithDingtalk.AppKey,
AppSecret: app.Config.WithDingtalk.AppSecret,
})
client.WithWorkflowClientV2().WithDepartClient().WithUserClient()
app.DingTalkClient = client
return app
}
// 启用 Policy 规则的情况下,使用数据库记录规则信息
func (app *Application) WithPolicy() *Application {
// 优先匹配 pg
var dialector gorm.Dialector
if app.Config.WithDB.PG.Database != "" {
log.Debugf("with policy pg database: %s", app.Config.WithDB.PG.GetUrl())
dialector = postgres.Open(app.Config.WithDB.PG.GetUrl())
} else {
dbFile := config.Conf.WithDB.DBFile
if !strings.HasSuffix(dbFile, ".db") {
panic("db file must be end with .db")
}
if dbFile == "" {
dbFile = "jms.db"
}
// show dbfile directory
log.Infof("db file path: %s/%s", filepath.Dir(dbFile), dbFile)
dialector = sqlite.Open(dbFile)
}
gormConfig := &gorm.Config{}
if !app.Debug {
gormConfig.Logger = logger.Default.LogMode(logger.Silent)
}
rdb, err := gorm.Open(dialector, gormConfig)
if err != nil {
panic("无法连接到数据库")
}
// 初始化数据库
rdb.AutoMigrate(
&db.Policy{}, &db.User{}, &db.AuthorizedKey{},
&db.Key{}, &db.Profile{}, &db.Proxy{}, // 配置
&db.SSHLoginRecord{}, &db.ScpRecord{}, // 审计
&db.Broadcast{},
)
App.DBService = db.NewDbService(rdb)
app.LoadFromDB()
return app
}
// 抽出来在初始化用以及定时热加载数据库
func (app *Application) LoadFromDB() {
log.Debugf("load from db")
profiles, err := App.DBService.LoadProfile()
if err != nil {
panic(err)
}
App.Config.Profiles = profiles
resp, err := App.DBService.InternalLoad()
if err != nil {
log.Panicf("load keys failed: %v", err)
}
App.Config.Keys = resp
proxys, err := App.DBService.ListProxy()
if err != nil {
log.Panicf("load proxy failed: %v", err)
}
App.Config.Proxys = proxys
}
func DBProfilesToMcsProfiles(profiles []db.CreateProfileRequest) []model.ProfileConfig {
var mcsProfiles []model.ProfileConfig
for _, profile := range profiles {
mcsProfiles = append(mcsProfiles, model.ProfileConfig{
Name: *profile.Name,
AK: *profile.AK,
SK: *profile.SK,
Cloud: model.Cloud(*profile.Cloud),
})
}
return mcsProfiles
}