This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcfgreader.go
155 lines (129 loc) · 3.62 KB
/
cfgreader.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
package ldapsync
import (
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/go-yaml/yaml"
)
// Config object
type Config struct {
Common struct {
Configpath string
Logpath string
}
Directory struct {
User string
Password string
Host string
Port int64
Groups map[string][]string
Roles map[string][]string
Attrmap map[string]map[string]string
Frozen []string
Allusers string
}
Spacewalk struct {
Url string
User string
Password string
Checkssl bool
}
}
// NewConfig creates new object instance
func NewConfig() *Config {
cfg := new(Config)
cfg.Directory.Groups = make(map[string][]string)
cfg.Directory.Roles = make(map[string][]string)
cfg.Directory.Attrmap = make(map[string]map[string]string)
return cfg
}
// ConfigReader object
type ConfigReader struct {
path string
config *Config
}
// NewConfigReader creates new object instance
func NewConfigReader(path string) *ConfigReader {
cfg := new(ConfigReader)
cfg.path = path
cfg.config = NewConfig()
cfg.loadFromPath()
return cfg.validate()
}
// Load configuration from the path
func (cfg *ConfigReader) loadFromPath() {
fh, err := os.Open(cfg.path)
if err != nil {
Log.Fatal(err)
}
defer fh.Close()
cfgBytes, err := ioutil.ReadAll(fh)
if err != nil {
Log.Fatal(err)
}
if err := yaml.Unmarshal(cfgBytes, &cfg.config); err != nil {
Log.Fatal(err)
} else {
cfg.setDefaults()
}
}
// Set defaults if they were not configured
func (cfg *ConfigReader) setDefaults() {
if cfg.Config().Common.Configpath == "" {
cfg.config.Common.Configpath = "/etc/rhn/ldapsync.conf"
}
if cfg.Config().Common.Logpath == "" {
cfg.config.Common.Logpath = "/var/log/rhn/ldapsync.log"
}
if cfg.Config().Directory.Port == 0 {
cfg.config.Directory.Port = 389
}
}
func (cfg *ConfigReader) validateAggregate(aggr map[string][]string) error {
if len(aggr) == 0 {
return errors.New("Block is empty")
}
for dn := range aggr {
if len(dn) == 0 {
return fmt.Errorf("DN '%s' contains no mapped roles", dn)
}
}
return nil
}
// Validate the configuration, if it is eligible to proceed with the syncing
func (cfg *ConfigReader) validate() *ConfigReader {
for errmsg, attr := range map[string]interface{}{
// Directory
"DN for LDAP user is not specified": cfg.config.Directory.User,
"Password for LDAP user is not specified": cfg.config.Directory.Password,
"Fully qualified domain name for LDAP server is not specified": cfg.config.Directory.Host,
"DN for all LDAP users is not specified": cfg.config.Directory.Allusers,
// Uyuni
"Uyuni RPC-API URL is not specified": cfg.config.Spacewalk.Url,
"Uyuni user is not specified": cfg.config.Spacewalk.User,
"The password for the Uyuni user is not specified": cfg.config.Spacewalk.Password} {
if attr == "" {
Log.Fatal(errmsg)
}
}
// Look if at least one frozen dude has this role
if len(cfg.config.Directory.Frozen) == 0 {
Log.Fatal("You have to regiser at least one frozen account with Organisation Manager role for emergency purposes")
}
// Look if at least one frozen dude has this role
if len(cfg.config.Directory.Groups) == 0 && len(cfg.config.Directory.Roles) == 0 {
Log.Fatal("Either Directory/Groups or Directory/Roles needs to be specified")
}
for _, aggr := range []map[string][]string{cfg.config.Directory.Groups, cfg.config.Directory.Roles} {
err := cfg.validateAggregate(aggr)
if err != nil {
Log.Fatal(err)
}
}
return cfg
}
// Config returns the configuration object
func (cfg *ConfigReader) Config() *Config {
return cfg.config
}