/
option.go
75 lines (58 loc) · 1.59 KB
/
option.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
// Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package certmanagersec
import (
"context"
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/choria-io/go-choria/config"
)
type Option func(*CertManagerSecurity) error
func WithChoriaConfig(c *config.Config) Option {
return func(p *CertManagerSecurity) error {
cfg := Config{
apiVersion: c.Choria.CertManagerAPIVersion,
sslDir: c.Choria.SSLDir,
privilegedUsers: c.Choria.PrivilegedUsers,
namespace: c.Choria.CertManagerSecurityNamespace,
issuer: c.Choria.CertManagerSecurityIssuer,
replace: c.Choria.CertManagerSecurityReplaceCSR,
altnames: c.Choria.CertManagerSecurityAltNames,
identity: c.Identity,
legacyCerts: c.Choria.SecurityAllowLegacyCerts,
}
if c.OverrideCertname == "" {
if cn, ok := os.LookupEnv("MCOLLECTIVE_CERTNAME"); ok {
c.OverrideCertname = cn
}
}
if c.OverrideCertname != "" {
cfg.identity = c.OverrideCertname
}
if cfg.sslDir == "" {
return fmt.Errorf("plugin.choria.ssldir is required")
}
if cfg.identity == "" {
return fmt.Errorf("identity could not be established")
}
if cfg.apiVersion == "" {
cfg.apiVersion = "v1"
}
p.conf = &cfg
return nil
}
}
func WithLog(l *logrus.Entry) Option {
return func(p *CertManagerSecurity) error {
p.log = l.WithFields(logrus.Fields{"ssl": "certmanager"})
return nil
}
}
func WithContext(ctx context.Context) Option {
return func(p *CertManagerSecurity) error {
p.ctx = ctx
return nil
}
}