-
Notifications
You must be signed in to change notification settings - Fork 53
/
security.go
179 lines (161 loc) · 4.81 KB
/
security.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
package opniopensearch
import (
"bytes"
"fmt"
"text/template"
"github.com/rancher/opni/pkg/util"
"golang.org/x/crypto/bcrypt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
)
const (
bcryptCost = 12
internalUsersKey = "internal_users.yml"
securityConfigKey = "config.yml"
)
var (
internalUsersTemplate = template.Must(template.New("natsconn").Parse(`_meta:
type: "internalusers"
config_version: 2
# Define your internal users here
{{ .AdminUser }}:
hash: "{{ .Admin }}"
reserved: true
backend_roles:
- "admin"
description: "Internal admin user"
kibanaserver:
hash: "{{ .Dashboards }}"
reserved: true
description: "Demo OpenSearch Dashboards user"`))
securityConfig = `
---
_meta:
type: "config"
config_version: 2
config:
dynamic:
http:
anonymous_auth_enabled: false
xff:
enabled: false
internalProxies: '192\.168\.0\.10|192\.168\.0\.11' # regex pattern
#internalProxies: '.*' # trust all internal proxies, regex pattern
#remoteIpHeader: 'x-forwarded-for'
###### see https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html for regex help
###### more information about XFF https://en.wikipedia.org/wiki/X-Forwarded-For
###### and here https://tools.ietf.org/html/rfc7239
###### and https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Remote_IP_Valve
authc:
basic_internal_auth_domain:
description: "Authenticate via HTTP Basic against internal users database"
http_enabled: true
transport_enabled: true
order: 2
http_authenticator:
type: basic
challenge: true
authentication_backend:
type: intern
clientcert_auth_domain:
description: "Authenticate via SSL client certificates"
http_enabled: true
transport_enabled: false
order: 1
http_authenticator:
type: clientcert
config:
username_attribute: cn #optional, if omitted DN becomes username
challenge: false
authentication_backend:
type: noop
`
)
type internalUsersHashes struct {
AdminUser string
Admin string
Dashboards string
}
type internalUsersPasswords struct {
admin []byte
dashboards []byte
}
func (r *Reconciler) generatePasswordObjects() (retObjects []runtime.Object, retErr error) {
adminPassword := util.GenerateRandomString(16)
dashboardsPassword := util.GenerateRandomString(16)
securityconfig, retErr := r.generateInternalUsers(internalUsersPasswords{
admin: adminPassword,
dashboards: dashboardsPassword,
})
if retErr != nil {
return
}
retObjects = append(retObjects, securityconfig)
retObjects = append(retObjects, r.generateAuthSecret(adminPassword))
retObjects = append(retObjects, r.generateDashboardsSecret(dashboardsPassword))
return
}
func (r *Reconciler) generateInternalUsers(passwords internalUsersPasswords) (runtime.Object, error) {
lg := log.FromContext(r.ctx)
lg.Info("generating bcrypt hash, this is slow")
adminHash, err := bcrypt.GenerateFromPassword(passwords.admin, bcryptCost)
if err != nil {
return nil, err
}
dashHash, err := bcrypt.GenerateFromPassword(passwords.dashboards, bcryptCost)
if err != nil {
return nil, err
}
var buffer bytes.Buffer
err = internalUsersTemplate.Execute(&buffer, internalUsersHashes{
AdminUser: internalUsername,
Admin: string(adminHash),
Dashboards: string(dashHash),
})
if err != nil {
return nil, err
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-securityconfig", r.instance.Name),
Namespace: r.instance.Namespace,
},
Data: map[string][]byte{
internalUsersKey: buffer.Bytes(),
securityConfigKey: []byte(securityConfig),
},
}
ctrl.SetControllerReference(r.instance, secret, r.client.Scheme())
return secret, nil
}
func (r *Reconciler) generateAuthSecret(password []byte) runtime.Object {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-internal-auth", r.instance.Name),
Namespace: r.instance.Namespace,
},
Data: map[string][]byte{
"username": []byte("internalopni"),
"password": password,
},
}
ctrl.SetControllerReference(r.instance, secret, r.client.Scheme())
return secret
}
func (r *Reconciler) generateDashboardsSecret(password []byte) runtime.Object {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-dashboards-auth", r.instance.Name),
Namespace: r.instance.Namespace,
},
Data: map[string][]byte{
"username": []byte("kibanaserver"),
"password": password,
},
}
ctrl.SetControllerReference(r.instance, secret, r.client.Scheme())
return secret
}