/
directoryserver.go
199 lines (163 loc) · 5.02 KB
/
directoryserver.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
package lohpi
import (
"crypto/x509/pkix"
"errors"
"github.com/arcsecc/lohpi/core/comm"
"github.com/arcsecc/lohpi/core/datasetmanager"
"github.com/arcsecc/lohpi/core/membershipmanager"
"github.com/arcsecc/lohpi/core/directoryserver"
"github.com/go-redis/redis"
"time"
"fmt"
)
type DirectoryServerConfig struct {
// The address of the CA. Default value is "127.0.0.1:8301"
CaAddress string
// The name of this node
Name string
// The database connection string. Default value is "". If it is not set, the database connection
// will not be used. This means that only the in-memory maps will be used for storage.
SQLConnectionString string
// Backup retention time. Default value is 0. If it is zero, backup retentions will not be issued.
// NOT USED
BackupRetentionTime time.Time
// Hostname of the node. Default value is "127.0.1.1".
HostName string
// Output directory of gossip observation unit. Default value is the current working directory.
PolicyObserverWorkingDirectory string
// HTTP port used by the server. Default value is 8080.
HTTPPort int
// TCP port used by the gRPC server. Default value is 8081.
GRPCPort int
// Path used to store X.509 certificate and private key
CryptoUnitWorkingDirectory string
// Ifrit's TCP port. Default value is 5000.
IfritTCPPort int
// Ifrit's UDP port. Default value is 6000.
IfritUDPPort int
}
type DirectoryServer struct {
dsCore *directoryserver.DirectoryServerCore
conf *directoryserver.Config
}
// Returns a new DirectoryServer using the given directory server options. Returns a non-nil error, if any.
func NewDirectoryServer(config *DirectoryServerConfig, new bool) (*DirectoryServer, error) {
if config == nil {
return nil, errors.New("Directory server configuration is nil")
}
if config.Name == "" {
config.Name = "Lohpi directory server"
}
if config.CaAddress == "" {
config.CaAddress = "127.0.1.1:8301"
}
if config.HostName == "" {
config.HostName = "127.0.1.1"
}
if config.PolicyObserverWorkingDirectory == "" {
config.PolicyObserverWorkingDirectory = "."
}
if config.HTTPPort == 0 {
config.HTTPPort = 8080
}
if config.GRPCPort == 0 {
config.GRPCPort = 8081
}
if config.CryptoUnitWorkingDirectory == "" {
config.CryptoUnitWorkingDirectory = "./crypto/lohpi"
}
if config.IfritTCPPort == 0 {
config.IfritTCPPort = 5000
}
if config.IfritUDPPort == 0 {
config.IfritUDPPort = 6000
}
ds := &DirectoryServer{
conf: &directoryserver.Config{
Name: config.Name,
Hostname: config.HostName,
HTTPPort: config.HTTPPort,
GRPCPort: config.GRPCPort,
CaAddress: config.CaAddress,
SQLConnectionString: config.SQLConnectionString,
IfritTCPPort: config.IfritTCPPort,
IfritUDPPort: config.IfritUDPPort,
},
}
// Crypto manager
var cu *comm.CryptoUnit
var err error
if new {
// Create a new crypto unit
cryptoUnitConfig := &comm.CryptoUnitConfig{
Identity: pkix.Name{
Country: []string{"NO"},
CommonName: ds.conf.Name,
Locality: []string{
fmt.Sprintf("%s:%d", config.HostName, config.HTTPPort),
fmt.Sprintf("%s:%d", config.HostName, config.GRPCPort),
},
},
CaAddr: config.CaAddress,
Hostnames: []string{config.HostName},
}
cu, err = comm.NewCu(config.CryptoUnitWorkingDirectory, cryptoUnitConfig)
if err != nil {
return nil, err
}
if err := cu.SaveState(); err != nil {
return nil, err
}
} else {
cu, err = comm.LoadCu(config.CryptoUnitWorkingDirectory)
if err != nil {
return nil, err
}
}
// Dataset manager
datasetLookupServiceConfig := &datasetmanager.DatasetLookupServiceConfig{
SQLConnectionString: config.SQLConnectionString,
RedisClientOptions: &redis.Options{
Network: "tcp",
Addr: fmt.Sprintf("%s:%d", "127.0.1.1", 6379),
Password: "",
DB: 0,
},
}
datasetLookupService, err := datasetmanager.NewDatasetLookupService("directoryserver", datasetLookupServiceConfig)
if err != nil {
return nil, err
}
// Membership manager
memManagerConfig := &membershipmanager.MembershipManagerUnitConfig{
SQLConnectionString: config.SQLConnectionString,
UseDB: true,
}
memManager, err := membershipmanager.NewMembershipManager(memManagerConfig)
if err != nil {
return nil, err
}
// Checkout manager
dsCheckoutManagerConfig := &datasetmanager.DatasetCheckoutServiceUnitConfig{
SQLConnectionString: config.SQLConnectionString,
}
dsCheckoutManager, err := datasetmanager.NewDatasetCheckoutServiceUnit("directoryserver", dsCheckoutManagerConfig)
if err != nil {
return nil, err
}
dsCore, err := directoryserver.NewDirectoryServerCore(cu, datasetLookupService, memManager, dsCheckoutManager, ds.conf)
if err != nil {
return nil, err
}
ds.dsCore = dsCore
return ds, nil
}
// Starts the directory server by running the Ifrit server, gRPC server and HTTP server. The call will return when
// these services have been started.
func (d *DirectoryServer) Start() {
d.dsCore.Start()
}
// Performs a graceful shutdown of the directory server.
func (d *DirectoryServer) Stop() {
d.dsCore.Stop()
}