-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathconfiguration.go
258 lines (236 loc) · 6.56 KB
/
configuration.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
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 planner
import (
"fmt"
api "github.com/samba-in-kubernetes/samba-operator/api/v1alpha1"
"github.com/samba-in-kubernetes/samba-operator/internal/smbcc"
)
func (pl *Planner) instanceID() smbcc.Key {
return smbcc.Key(pl.InstanceName())
}
func (pl *Planner) shareName() string {
// todo: make sure this is smb-conf clean, otherwise we need to
// fix up the name value(s).
if pl.SmbShare.Spec.ShareName != "" {
return pl.SmbShare.Spec.ShareName
}
// It was not named explicitly. Name it after the CR.
// todo: may need massaging too.
return pl.SmbShare.Name
}
func (pl *Planner) idmapOptions() smbcc.SmbOptions {
if pl.SecurityConfig == nil || len(pl.SecurityConfig.Spec.Domains) == 0 {
// default idmap config
return smbcc.SmbOptions{
"idmap config * : backend": "autorid",
"idmap config * : range": "2000-9999999",
}
}
// this is hacky and needs both config to support user supplied ID
// ranges as well as a decent algo to deal with ID map ranges.
// for now we're just punting though (call it prototyping) :-)
o := smbcc.SmbOptions{}
doms := []api.SmbSecurityDomainSpec{}
userDefault := false
for _, d := range pl.SecurityConfig.Spec.Domains {
doms = append(doms, d)
if d.Name == "*" {
userDefault = true
}
}
if !userDefault {
doms = append(doms, api.SmbSecurityDomainSpec{
Name: "*",
Backend: "autorid",
})
}
step := 10000
for i, d := range doms {
pfx := fmt.Sprintf("idmap config %s : ", d.Name)
if d.Backend == "autorid" {
o[pfx+"backend"] = "autorid"
} else {
o[pfx+"backend"] = "ad"
o[pfx+"schema_mode"] = "rfc2307"
}
rs := (i * step) + 2000
o[pfx+"range"] = fmt.Sprintf("%d-%d", rs, rs+step-1)
}
return o
}
// Update the held configuration based on the state of the instance
// configuration.
func (pl *Planner) Update() (changed bool, err error) {
_, found := pl.ConfigState.Globals[smbcc.Globals]
if !found {
globalOptions := smbcc.NewGlobalOptions()
globalOptions.SmbPort = pl.GlobalConfig.SmbdPort
globals := smbcc.NewGlobals(globalOptions)
pl.ConfigState.Globals[smbcc.Globals] = globals
changed = true
}
shareKey := smbcc.Key(pl.shareName())
share, found := pl.ConfigState.Shares[shareKey]
if !found {
share = smbcc.NewSimpleShare(pl.Paths().Share())
pl.ConfigState.Shares[shareKey] = share
changed = true
}
if pl.CommonConfig != nil {
if c := applyCustomGlobal(pl.ConfigState.Globals[smbcc.Globals],
pl.CommonConfig.Spec); c {
changed = true
}
}
if c := applyCustomShare(share, pl.SmbShare.Spec); c {
changed = true
}
if c := applyShareValues(share, pl.SmbShare.Spec); c {
changed = true
}
cfgKey := pl.instanceID()
cfg, found := pl.ConfigState.Configs[cfgKey]
if !found {
cfg = smbcc.ConfigSection{
Shares: []smbcc.Key{shareKey},
Globals: []smbcc.Key{smbcc.Globals},
InstanceName: pl.InstanceName(),
Permissions: smbcc.NewPermissionsConfig(),
}
if pl.SecurityMode() == ADMode {
realmKey := smbcc.Key(pl.Realm())
cfg.Globals = append(cfg.Globals, realmKey)
}
if pl.IsClustered() {
cfg.InstanceFeatures = []smbcc.FeatureFlag{smbcc.CTDB}
}
changed = true
}
if !hasShare(&cfg, shareKey) {
cfg.Shares = append(cfg.Shares, shareKey)
changed = true
}
if changed {
pl.ConfigState.Configs[cfgKey] = cfg
}
if len(pl.ConfigState.Users) == 0 {
pl.ConfigState.Users = smbcc.NewDefaultUsers()
changed = true
}
if pl.SecurityMode() == ADMode {
realmKey := smbcc.Key(pl.Realm())
_, found := pl.ConfigState.Globals[realmKey]
if !found {
opts := pl.idmapOptions()
// security mode
opts["security"] = "ads"
// workgroup and realm
opts["workgroup"] = pl.Workgroup()
opts["realm"] = pl.Realm()
pl.ConfigState.Globals[realmKey] = smbcc.GlobalConfig{
Options: opts,
}
changed = true
}
}
return
}
// Prune the target share from the configuration.
func (pl *Planner) Prune() (changed bool, err error) {
cfgKey := pl.instanceID()
shareKey := smbcc.Key(pl.shareName())
if cfg, found := pl.ConfigState.Configs[cfgKey]; found {
if removeShare(&cfg, shareKey) {
pl.ConfigState.Configs[cfgKey] = cfg
changed = true
}
}
if _, found := pl.ConfigState.Shares[shareKey]; found {
delete(pl.ConfigState.Shares, shareKey)
changed = true
}
return
}
func applyCustomGlobal(globals smbcc.GlobalConfig, spec api.SmbCommonConfigSpec) bool {
changed := false
if spec.CustomGlobalConfig != nil {
// check if the user wants to use custom configs
// if not, just return
if !spec.CustomGlobalConfig.UseUnsafeCustomConfig {
return changed
}
for k, v := range spec.CustomGlobalConfig.Configs {
oriValue, ok := globals.Options[k]
if !ok || (ok && oriValue != v) {
globals.Options[k] = v
changed = true
}
}
}
return changed
}
func applyCustomShare(share smbcc.ShareConfig, spec api.SmbShareSpec) bool {
changed := false
if spec.CustomShareConfig != nil {
// check if the user wants to use custom configs
// if not, just return
if !spec.CustomShareConfig.UseUnsafeCustomConfig {
return changed
}
for k, v := range spec.CustomShareConfig.Configs {
oriValue, ok := share.Options[k]
if !ok || (ok && oriValue != v) {
share.Options[k] = v
changed = true
}
}
}
return changed
}
func applyShareValues(share smbcc.ShareConfig, spec api.SmbShareSpec) bool {
changed := false
setBrowsable := smbcc.Yes
if !spec.Browseable {
setBrowsable = smbcc.No
}
setReadOnly := smbcc.No
if spec.ReadOnly {
setReadOnly = smbcc.Yes
}
if share.Options[smbcc.BrowseableParam] != setBrowsable {
share.Options[smbcc.BrowseableParam] = setBrowsable
changed = true
}
if share.Options[smbcc.ReadOnlyParam] != setReadOnly {
share.Options[smbcc.ReadOnlyParam] = setReadOnly
changed = true
}
return changed
}
func hasShare(cfg *smbcc.ConfigSection, k smbcc.Key) bool {
for i := range cfg.Shares {
if cfg.Shares[i] == k {
return true
}
}
return false
}
func removeShare(cfg *smbcc.ConfigSection, k smbcc.Key) bool {
for i := range cfg.Shares {
if cfg.Shares[i] == k {
cfg.Shares = append(cfg.Shares[:i], cfg.Shares[i+1:]...)
return true
}
}
return false
}