/
web.go
147 lines (130 loc) · 3.68 KB
/
web.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
package web
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"sync"
"github.com/JIexa24/chef-webapi/database"
"github.com/JIexa24/chef-webapi/logging"
"github.com/go-chef/chef"
)
// App is a global application object.
var App *Application
// NewApplication set empty application structure.
func NewApplication(env string, reloadChannel chan struct{}) {
App = &Application{
Client: nil,
DB: nil,
AppKey: nil,
Logger: nil,
WorkerDirectory: "/app/worker/data",
SSHKeyPath: "",
ChefKeyPath: "",
chefClientName: "",
chefURL: "",
chefMux: sync.Mutex{},
Env: env,
SessionExpire: 86400,
LDAP: nil,
UsersLastSeen: make(map[string]string),
ReloadChannel: reloadChannel,
}
}
// ConfigureLDAP configures ldap.
func (App *Application) ConfigureLDAP(baseDN, bindAddress, bindPrefix, bindSuffix string) {
App.LDAP = &LDAPData{
BaseDN: baseDN,
BindAddress: bindAddress,
BindPrefix: bindPrefix,
BindSuffix: bindSuffix,
}
}
// ConfigureLogger configures logger.
func (App *Application) ConfigureLogger(l logging.Logger) {
App.Logger = l
}
// GetChefClientConfig return existing chef client config.
func (App *Application) GetChefClientConfig() *chef.Client {
App.chefMux.Lock()
defer App.chefMux.Unlock()
return App.Client
}
// CreateChefClientConfig creates config for chef client.
func (App *Application) CreateChefClientConfig() error {
App.chefMux.Lock()
defer App.chefMux.Unlock()
// read a client key
key, err := os.ReadFile(App.ChefKeyPath)
if err != nil {
return err
}
App.Client, err = chef.NewClient(&chef.Config{
Name: App.chefClientName,
Key: string(key),
BaseURL: App.chefURL,
})
return err
}
// ConfigureChefClient configures chef client.
func (App *Application) ConfigureChefClient(name, url, keyPath string) error {
App.ChefKeyPath = keyPath
App.chefClientName = name
App.chefURL = url
return App.CreateChefClientConfig()
}
// ConfigureDatabase configures database.
func (App *Application) ConfigureDatabase(databaseProvider, sessionProvider,
databaseName, databaseUser, databasePassword,
databaseHost, databasePort string) error {
var err error
databaseParams := database.NewParams()
databaseParams.DatabaseProvider = databaseProvider
databaseParams.SessionProvider = sessionProvider
databaseParams.User = databaseUser
databaseParams.Password = databasePassword
databaseParams.Host = databaseHost
databaseParams.Port = databasePort
databaseParams.Name = databaseName
App.DB, err = database.NewDatabaseConnector(App.Env, databaseParams, App.Logger)
return err
}
// ConfigureApp configures App.
func (App *Application) ConfigureApp(workerDir, appKeyPath, sshKeyPath string, sessionExpire int64) {
App.SessionExpire = sessionExpire
App.WorkerDirectory = workerDir
App.SSHKeyPath = sshKeyPath
data, err := os.ReadFile(appKeyPath)
if err != nil {
App.Logger.Fatal(err.Error())
}
App.AppKey, err = parseRsaPrivateKey(data)
if err != nil {
App.Logger.Fatal(err.Error())
}
}
func parseRsaPrivateKey(str []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(str)
if block == nil {
return nil, fmt.Errorf("failed to parse PEM block containing the key")
}
pub, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return pub, nil
}
// ConcatenateStringWithDelimeter constructing string like
// 'names[0],names[1], ... ,names[n]' if delimeter as ","
func ConcatenateStringWithDelimeter(delimeter string, names []string) string {
var result string
var length = len(names)
for i, v := range names {
result = result + v
if i < length-1 {
result = result + delimeter
}
}
return result
}