/
atmcfg.go
188 lines (166 loc) · 5.76 KB
/
atmcfg.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
// Copyright 2020 MongoDB Inc
//
// 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 atmcfg provides helpers for updating Ops Manager automation config.
package atmcfg // import "go.mongodb.org/ops-manager/atmcfg"
import (
"errors"
"fmt"
"go.mongodb.org/ops-manager/opsmngr"
"go.mongodb.org/ops-manager/search"
)
func setDisabledByClusterName(out *opsmngr.AutomationConfig, name string, disabled bool) {
// This value may not be present and is mandatory
if out.Auth.DeploymentAuthMechanisms == nil {
out.Auth.DeploymentAuthMechanisms = make([]string, 0)
}
setDisabledByReplicaSetName(out, name, disabled)
setDisabledByShardName(out, name, disabled)
}
func setDisabledByReplicaSetName(out *opsmngr.AutomationConfig, name string, disabled bool) {
i, found := search.ReplicaSets(out.ReplicaSets, func(rs *opsmngr.ReplicaSet) bool {
return rs.ID == name
})
if found {
rs := out.ReplicaSets[i]
for _, m := range rs.Members {
for k, p := range out.Processes {
if p.Name == m.Host {
out.Processes[k].Disabled = disabled
}
}
}
}
}
func setDisabledByShardName(out *opsmngr.AutomationConfig, name string, disabled bool) {
i, found := search.ShardingConfig(out.Sharding, func(s *opsmngr.ShardingConfig) bool {
return s.Name == name
})
if found {
s := out.Sharding[i]
for _, rs := range s.Shards {
setDisabledByReplicaSetName(out, rs.ID, disabled)
}
}
}
// Shutdown disables all processes of the given cluster name
func Shutdown(out *opsmngr.AutomationConfig, name string) {
setDisabledByClusterName(out, name, true)
}
// Startup enables all processes of the given cluster name
func Startup(out *opsmngr.AutomationConfig, name string) {
setDisabledByClusterName(out, name, false)
}
// AddUser adds a opsmngr.MongoDBUser to the opsmngr.AutomationConfi
func AddUser(out *opsmngr.AutomationConfig, u *opsmngr.MongoDBUser) {
out.Auth.Users = append(out.Auth.Users, u)
}
// AddIndexConfig adds an opsmngr.IndexConfig to the opsmngr.AutomationConfig
func AddIndexConfig(out *opsmngr.AutomationConfig, newIndex *opsmngr.IndexConfig) error {
if out == nil {
return errors.New("the Automation Config has not been initialized")
}
_, exists := search.MongoDBIndexes(out.IndexConfigs, compareIndexConfig(newIndex))
if exists {
return errors.New("index already exists")
}
out.IndexConfigs = append(out.IndexConfigs, newIndex)
return nil
}
// compareIndexConfig returns a function that compares two indexConfig struts
func compareIndexConfig(newIndex *opsmngr.IndexConfig) func(index *opsmngr.IndexConfig) bool {
return func(index *opsmngr.IndexConfig) bool {
if newIndex.RSName == index.RSName && newIndex.CollectionName == index.CollectionName && newIndex.DBName == index.DBName && len(newIndex.Key) == len(index.Key) {
// if keys are equal the two indexes are considered to be the same
for i := 0; i < len(newIndex.Key); i++ {
if newIndex.Key[i][0] != index.Key[i][0] || newIndex.Key[i][1] != index.Key[i][1] {
return false
}
}
return true
}
return false
}
}
// RemoveUser removes a MongoDBUser from the authentication config
func RemoveUser(out *opsmngr.AutomationConfig, username, database string) error {
pos, found := search.MongoDBUsers(out.Auth.Users, func(p *opsmngr.MongoDBUser) bool {
return p.Username == username && p.Database == database
})
if !found {
return fmt.Errorf("user '%s' not found for '%s'", username, database)
}
out.Auth.Users = append(out.Auth.Users[:pos], out.Auth.Users[pos+1:]...)
return nil
}
const (
automationAgentName = "mms-automation"
keyLength = 500
cr = "MONGODB-CR"
sha256 = "SCRAM-SHA-256"
atmAgentWindowsKeyFilePath = "%SystemDrive%\\MMSAutomation\\versions\\keyfile"
atmAgentKeyFilePathInContainer = "/var/lib/mongodb-mms-automation/keyfile"
)
// EnableMechanism allows you to enable a given set of authentication mechanisms to an opsmngr.AutomationConfig.
// This method currently only supports MONGODB-CR, and SCRAM-SHA-256
func EnableMechanism(out *opsmngr.AutomationConfig, m []string) error {
out.Auth.Disabled = false
for _, v := range m {
if v != cr && v != sha256 {
return fmt.Errorf("unsupported mechanism %s", v)
}
if v == sha256 {
out.Auth.AutoAuthMechanism = v
}
if !stringInSlice(out.Auth.DeploymentAuthMechanisms, v) {
out.Auth.DeploymentAuthMechanisms = append(out.Auth.DeploymentAuthMechanisms, v)
}
if !stringInSlice(out.Auth.AutoAuthMechanisms, v) {
out.Auth.AutoAuthMechanisms = append(out.Auth.AutoAuthMechanisms, v)
}
}
if out.Auth.AutoUser == "" {
if err := setAutoUser(out); err != nil {
return err
}
}
var err error
if out.Auth.Key == "" {
if out.Auth.Key, err = generateRandomBase64String(keyLength); err != nil {
return err
}
}
if out.Auth.KeyFile == "" {
out.Auth.KeyFile = atmAgentKeyFilePathInContainer
}
if out.Auth.KeyFileWindows == "" {
out.Auth.KeyFileWindows = atmAgentWindowsKeyFilePath
}
return nil
}
func setAutoUser(out *opsmngr.AutomationConfig) error {
var err error
out.Auth.AutoUser = automationAgentName
if out.Auth.AutoPwd, err = generateRandomASCIIString(500); err != nil {
return err
}
return nil
}
func stringInSlice(a []string, x string) bool {
for _, b := range a {
if b == x {
return true
}
}
return false
}