/
bootstrap.go
212 lines (183 loc) · 6.12 KB
/
bootstrap.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
// Package ctrl converts Settings CRD into a bootstrap config
package ctrl
import (
"context"
"encoding/base64"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/pomerium/pomerium/config"
"github.com/pomerium/ingress-controller/internal/filemgr"
"github.com/pomerium/ingress-controller/model"
)
// Apply prepares a minimal bootstrap configuration for Pomerium
func Apply(ctx context.Context, dst *config.Options, src *model.Config) error {
for _, apply := range []struct {
name string
fn func(context.Context, *config.Options, *model.Config) error
}{
{"authenticate", applyAuthenticate},
{"secrets", applySecrets},
{"storage", applyStorage},
} {
if err := apply.fn(ctx, dst, src); err != nil {
return fmt.Errorf("%s: %w", apply.name, err)
}
}
if err := dst.Validate(); err != nil {
return fmt.Errorf("validate: %w", err)
}
return nil
}
var storageFiles = filemgr.New(filepath.Join(os.TempDir(), "pomerium-storage-files"))
func applyAuthenticate(_ context.Context, dst *config.Options, src *model.Config) error {
if src.Spec.IdentityProvider == nil {
return nil
}
// if IdP is set, the authenticate is managed locally,
// and we need to set the internal URL to the service in order to be able to fetch keys
// as public URL is not accessible from inside the container due to port remapping
host, port, err := net.SplitHostPort(dst.Addr)
if err != nil {
return fmt.Errorf("parsing server addr: %w", err)
}
if host == "" {
host = "localhost"
}
dst.AuthenticateInternalURLString = (&url.URL{
Scheme: "https",
Host: net.JoinHostPort(host, port),
}).String()
return nil
}
func applyStorage(ctx context.Context, dst *config.Options, src *model.Config) error {
if err := storageFiles.DeleteFiles(); err != nil {
log.FromContext(ctx).V(1).Error(err, "failed to delete existing files")
}
if src.Spec.Storage == nil {
return nil
}
if err := src.StorageSecrets.Validate(); err != nil {
return err
}
if src.Spec.Storage.Postgres != nil {
return applyStoragePostgres(dst, src)
}
if src.Spec.Storage.Redis != nil {
return applyStorageRedis(dst, src)
}
return fmt.Errorf("if storage is specified, it must contain either redis or postgresql config. omit storage key for in-memory")
}
func applyStorageRedis(dst *config.Options, src *model.Config) error {
conn, ok := src.StorageSecrets.Secret.Data[model.StorageConnectionStringKey]
if !ok {
return fmt.Errorf("storage secret must have %s key", model.StorageConnectionStringKey)
}
dst.DataBrokerStorageConnectionString = string(conn)
dst.DataBrokerStorageCertSkipVerify = src.Spec.Storage.Redis.TLSSkipVerify
if src.StorageSecrets.CA != nil {
ca, err := storageFiles.CreateFile("ca.pem", src.StorageSecrets.Secret.Data[model.CAKey])
if err != nil {
return fmt.Errorf("ca: %w", err)
}
dst.DataBrokerStorageCAFile = ca
}
if src.StorageSecrets.TLS != nil {
cert, err := storageFiles.CreateFile("cert.pem", src.StorageSecrets.TLS.Data[corev1.TLSCertKey])
if err != nil {
return fmt.Errorf("tls cert: %w", err)
}
key, err := storageFiles.CreateFile("key.pem", src.StorageSecrets.TLS.Data[corev1.TLSPrivateKeyKey])
if err != nil {
return fmt.Errorf("tls key: %w", err)
}
dst.DataBrokerStorageCertFile = cert
dst.DataBrokerStorageCertKeyFile = key
}
return nil
}
func applyStoragePostgres(dst *config.Options, src *model.Config) error {
const (
sslMode = "sslmode"
sslRootCert = "sslrootcert"
sslCert = "sslcert"
sslKey = "sslkey"
)
conn, ok := src.StorageSecrets.Secret.Data[model.StorageConnectionStringKey]
if !ok {
return fmt.Errorf("storage secret must have %s key", model.StorageConnectionStringKey)
}
dst.DataBrokerStorageType = config.StoragePostgresName
if src.StorageSecrets.CA == nil && src.StorageSecrets.TLS == nil {
dst.DataBrokerStorageConnectionString = string(conn)
return nil
}
u, err := url.Parse(string(conn))
if err != nil {
return fmt.Errorf("parse connection string: %w", err)
}
param := u.Query()
if !param.Has(sslMode) {
return fmt.Errorf("%s must be set in a connection string if TLS/CA options are provided", sslMode)
}
if src.StorageSecrets.CA != nil {
// in principle, one may customize that externally and provide secrets mounted directly to a pod
if param.Has(sslRootCert) {
return fmt.Errorf("%s should not be set in a connection string if CA secret is provided", sslRootCert)
}
f, err := storageFiles.CreateFile(sslRootCert, src.StorageSecrets.CA.Data[model.CAKey])
if err != nil {
return fmt.Errorf("ca: %w", err)
}
param.Set(sslRootCert, f)
}
if src.StorageSecrets.TLS != nil {
if param.Has(sslCert) || param.Has(sslKey) {
return fmt.Errorf("%s or %s should not be set in a connection string if TLS secret is provided", sslCert, sslKey)
}
cert, err := storageFiles.CreateFile(sslCert, src.StorageSecrets.TLS.Data[corev1.TLSCertKey])
if err != nil {
return fmt.Errorf("tls cert: %w", err)
}
key, err := storageFiles.CreateFile(sslKey, src.StorageSecrets.TLS.Data[corev1.TLSPrivateKeyKey])
if err != nil {
return fmt.Errorf("tls key: %w", err)
}
param.Set(sslCert, cert)
param.Set(sslKey, key)
}
u.RawQuery = param.Encode()
dst.DataBrokerStorageConnectionString = u.String()
return nil
}
func applySecrets(_ context.Context, dst *config.Options, src *model.Config) error {
if src.Secrets == nil {
return fmt.Errorf("secrets missing, this is a bug")
}
name := types.NamespacedName{Name: src.Secrets.Name, Namespace: src.Secrets.Namespace}
for _, secret := range []struct {
key string
len int
sp *string
}{
{"shared_secret", 32, &dst.SharedKey},
{"cookie_secret", 32, &dst.CookieSecret},
{"signing_key", -1, &dst.SigningKey},
} {
data, ok := src.Secrets.Data[secret.key]
if !ok && secret.len > 0 {
return fmt.Errorf("secret %s is missing a key %s", name, secret.key)
}
if secret.len > 0 && len(data) != secret.len {
return fmt.Errorf("secret %s, key %s should be %d bytes, got %d", name, secret.key, secret.len, len(data))
}
txt := base64.StdEncoding.EncodeToString(data)
*secret.sp = txt
}
return nil
}