-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
179 lines (146 loc) · 4.95 KB
/
main.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
/*
Copyright 2017 Atos
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"SLALite/assessment"
"SLALite/assessment/monitor"
"SLALite/assessment/monitor/cimiadapter"
"SLALite/assessment/notifier"
"SLALite/mf2c"
"SLALite/model"
"SLALite/repositories/cimi"
"SLALite/repositories/memrepository"
"SLALite/repositories/mongodb"
"SLALite/repositories/validation"
"SLALite/utils"
"flag"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var cimirepo cimi.Repository
var policies mf2c.PoliciesConnecter
// version and date are defined on compilation (see makefile)
var version string
var date string
func main() {
// TODO: Add windows path
configPath := flag.String("d", utils.UnixConfigPath, "Directories where to search config files")
configBasename := flag.String("b", utils.ConfigName, "Filename (w/o extension) of config file")
configFile := flag.String("f", "", "Path of configuration file. Overrides -b and -d")
flag.Parse()
log.Infof("Running SLALite %s compiled on %s", version, date)
config := createMainConfig(configFile, configPath, configBasename)
logMainConfig(config)
singlefile := config.GetBool(utils.SingleFilePropertyName)
checkPeriod := config.GetDuration(utils.CheckPeriodPropertyName)
repoType := config.GetString(utils.RepositoryTypePropertyName)
utils.AddTrustedCAs(config)
var repoconfig *viper.Viper
if singlefile {
repoconfig = config
}
var repo model.IRepository
var errRepo error
switch repoType {
case utils.DefaultRepositoryType:
repo, errRepo = memrepository.New(repoconfig)
case "mongodb":
repo, errRepo = mongodb.New(repoconfig)
case "cimi":
cimirepo, errRepo = cimi.New(repoconfig)
repo = cimirepo
}
if errRepo != nil {
log.Fatal("Error creating repository: ", errRepo.Error())
}
var mF2C mf2c.Mf2c
var err error
mF2C, err = mf2c.New(config)
policies = mF2C.Policies
if err != nil {
log.Fatal("Error creating Policies: ", err.Error())
}
validater := model.NewDefaultValidator(config.GetBool(utils.ExternalIDsPropertyName), false)
repo, _ = validation.New(repo, validater)
if repo != nil {
a, _ := NewApp(config, repo, validater)
go createValidationThread(repo, nil, nil, checkPeriod)
a.Run()
}
}
//
// Creates the main Viper configuration.
// file: if set, is the path to a configuration file. If not set, paths and basename will be used
// paths: colon separated paths where to search a config file
// basename: basename of a configuration file accepted by Viper (extension is automatic)
//
func createMainConfig(file *string, paths *string, basename *string) *viper.Viper {
config := viper.New()
config.SetEnvPrefix(utils.ConfigPrefix) // Env vars start with 'SLA_'
config.AutomaticEnv()
config.SetDefault(utils.CheckPeriodPropertyName, utils.DefaultCheckPeriod)
config.SetDefault(utils.RepositoryTypePropertyName, utils.DefaultRepositoryType)
config.SetDefault(utils.ExternalIDsPropertyName, utils.DefaultExternalIDs)
if *file != "" {
config.SetConfigFile(*file)
} else {
config.SetConfigName(*basename)
for _, path := range strings.Split(*paths, ":") {
config.AddConfigPath(path)
}
}
errConfig := config.ReadInConfig()
if errConfig != nil {
log.Println("Can't find configuration file: " + errConfig.Error())
log.Println("Using defaults")
}
return config
}
func logMainConfig(config *viper.Viper) {
checkPeriod := config.GetDuration(utils.CheckPeriodPropertyName)
repoType := config.GetString(utils.RepositoryTypePropertyName)
externalIDs := config.GetBool(utils.ExternalIDsPropertyName)
log.Infof("SLALite initialization\n"+
"\tConfigfile: %s\n"+
"\tRepository type: %s\n"+
"\tExternal IDs: %v\n"+
"\tCheck period:%d\n",
config.ConfigFileUsed(), repoType, externalIDs, checkPeriod)
caPath := config.GetString(utils.CAPathPropertyName)
if caPath != "" {
log.Infof("SLALite intialization. Trusted CAs file: %s", caPath)
}
}
func createValidationThread(repo model.IRepository, ma monitor.MonitoringAdapter,
not notifier.ViolationNotifier, checkPeriod time.Duration) {
ticker := time.NewTicker(checkPeriod * time.Second)
for {
<-ticker.C
assessMf2cAgreements(repo)
}
}
func validateProviders(repo model.IRepository) {
providers, err := repo.GetAllProviders()
if err == nil {
log.Println("There are " + strconv.Itoa(len(providers)) + " providers")
} else {
log.Println("Error: " + err.Error())
}
}
func assessMf2cAgreements(repo model.IRepository) {
ma := cimiadapter.New(cimirepo)
assessment.AssessMf2cAgreements(repo, cimirepo, ma, policies)
}