-
Notifications
You must be signed in to change notification settings - Fork 0
/
userBackend.go
143 lines (129 loc) · 4.24 KB
/
userBackend.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
package validation
import (
"fmt"
"strconv"
"github.com/cloudogu/k8s-ces-setup/app/context"
)
const (
DsTypeEmbedded = "embedded"
DsTypeExternal = "external"
searchFilter = "(objectClass=person)"
)
type userBackendValidator struct {
}
// NewUserBackendValidator creates a new validator for the user backend section of the setup configuration
func NewUserBackendValidator() *userBackendValidator {
return &userBackendValidator{}
}
// ValidateUserBackend validates all properties of the user backend section from a setup json
// see: https://docs.cloudogu.com/docs/system-components/ces-setup/operations/setup-json_de/
func (ubv *userBackendValidator) ValidateUserBackend(backend context.UserBackend) error {
dsType := backend.DsType
if dsType != DsTypeEmbedded && dsType != DsTypeExternal {
return getInvalidOptionError("dsType", DsTypeEmbedded, DsTypeExternal)
}
var result error
if dsType == DsTypeExternal {
result = ubv.validateExternalBackend(backend)
}
if dsType == DsTypeEmbedded {
result = ubv.validateEmbeddedBackend(backend)
}
return result
}
func (ubv *userBackendValidator) validateActiveDirectoryServer(backend context.UserBackend) error {
if backend.AttributeID != "sAMAccountName" {
return getInvalidOptionError("attributeID", "sAMAccountName")
}
if backend.AttributeFullname != "cn" {
return getInvalidOptionError("attributeFullName", "cn")
}
if backend.AttributeMail != "mail" {
return getInvalidOptionError("attributeMail", "mail")
}
if backend.AttributeGroup != "memberOf" {
return getInvalidOptionError("attributeGroup", "memberOf")
}
if backend.SearchFilter != searchFilter {
return getInvalidOptionError("searchFilter", searchFilter)
}
return nil
}
func (ubv *userBackendValidator) validateExternalBackend(backend context.UserBackend) error {
if backend.Server != "activeDirectory" && backend.Server != "custom" {
return getInvalidOptionError("server", "activeDirectory", "custom")
}
if backend.Server == "activeDirectory" {
err := ubv.validateActiveDirectoryServer(backend)
if err != nil {
return err
}
}
if backend.AttributeGivenName == "" {
return getPropertyNotSetError("attributeGivenName")
}
if backend.AttributeSurname == "" {
return getPropertyNotSetError("attributeSurName")
}
if backend.BaseDN == "" {
return getPropertyNotSetError("baseDn")
}
if backend.ConnectionDN == "" {
return getPropertyNotSetError("connectionDn")
}
if backend.Password == "" {
return getPropertyNotSetError("password")
}
if backend.Host == "" {
return getPropertyNotSetError("host")
}
if backend.Port == "" {
return getPropertyNotSetError("port")
}
if _, err := strconv.Atoi(backend.Port); err != nil {
return fmt.Errorf("failed to validate property port: the given value is not a number")
}
if backend.Encryption != "none" && backend.Encryption != "ssl" && backend.Encryption != "sslAny" && backend.Encryption != "startTLS" && backend.Encryption != "startTLSAny" {
return getInvalidOptionError("encryption", "none", "ssl", "sslAny", "startTLS", "startTLSAny")
}
if backend.GroupBaseDN == "" {
return getPropertyNotSetError("groupBaseDN")
}
if backend.GroupSearchFilter == "" {
return getPropertyNotSetError("groupSearchFilter")
}
if backend.GroupAttributeName == "" {
return getPropertyNotSetError("groupAttributeName")
}
if backend.GroupAttributeDescription == "" {
return getPropertyNotSetError("groupAttributeDescription")
}
if backend.GroupAttributeMember == "" {
return getPropertyNotSetError("groupAttributeMember")
}
return nil
}
func (ubv *userBackendValidator) validateEmbeddedBackend(backend context.UserBackend) error {
if backend.AttributeID != "uid" {
return getInvalidOptionError("attributeID", "uid")
}
if backend.AttributeFullname != "cn" {
return getInvalidOptionError("attributeFullName", "cn")
}
if backend.AttributeMail != "mail" {
return getInvalidOptionError("attributeMail", "mail")
}
if backend.AttributeGroup != "memberOf" {
return getInvalidOptionError("attributeGroup", "memberOf")
}
if backend.SearchFilter != searchFilter {
return getInvalidOptionError("searchFilter", searchFilter)
}
if backend.Host != "ldap" {
return getInvalidOptionError("host", "ldap")
}
if backend.Port != "389" {
return getInvalidOptionError("port", "389")
}
return nil
}