-
Notifications
You must be signed in to change notification settings - Fork 31
/
option.go
111 lines (88 loc) · 2.89 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
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
package puppetsec
import (
"fmt"
"os"
"runtime"
"github.com/choria-io/go-choria/tlssetup"
"github.com/choria-io/go-choria/config"
"github.com/sirupsen/logrus"
)
// Option is a function that can configure the Puppet Security Provider
type Option func(*PuppetSecurity) error
// WithChoriaConfig optionally configures the Puppet Security Provider from settings found in a typical Choria configuration
func WithChoriaConfig(bi BuildInfoProvider, c *config.Config) Option {
return func(p *PuppetSecurity) error {
cfg := Config{
AllowList: c.Choria.CertnameWhitelist,
DisableTLSVerify: c.DisableTLSVerify,
PrivilegedUsers: c.Choria.PrivilegedUsers,
SSLDir: c.Choria.SSLDir,
PuppetCAHost: c.Choria.PuppetCAHost,
PuppetCAPort: c.Choria.PuppetCAPort,
Identity: c.Identity,
AlwaysOverwriteCache: c.Choria.SecurityAlwaysOverwriteCache,
RemoteSignerURL: c.Choria.RemoteSignerURL,
RemoteSignerTokenFile: c.Choria.RemoteSignerTokenFile,
RemoteSignerTokenEnvironment: c.Choria.RemoteSignerTokenEnvironment,
TLSConfig: tlssetup.TLSConfig(c),
IdentitySuffix: bi.ClientIdentitySuffix(),
}
if cfg.IdentitySuffix == "" {
cfg.IdentitySuffix = "mcollective"
}
if c.Choria.NetworkClientAdvertiseName != "" {
cfg.AltNames = append(cfg.AltNames, c.Choria.NetworkClientAdvertiseName)
}
if c.HasOption("plugin.choria.puppetca_host") || c.HasOption("plugin.choria.puppetca_port") {
cfg.DisableSRV = true
}
if c.OverrideCertname == "" {
if cn, ok := os.LookupEnv("MCOLLECTIVE_CERTNAME"); ok {
c.OverrideCertname = cn
}
}
if c.OverrideCertname != "" {
cfg.Identity = c.OverrideCertname
} else if !c.InitiatedByServer {
userEnvVar := "USER"
if runtime.GOOS == "windows" {
userEnvVar = "USERNAME"
}
u, ok := os.LookupEnv(userEnvVar)
if !ok {
return fmt.Errorf("could not determine client identity, ensure %s environment variable is set", userEnvVar)
}
cfg.Identity = fmt.Sprintf("%s.%s", u, cfg.IdentitySuffix)
}
if cfg.SSLDir == "" {
d, err := userSSlDir()
if err != nil {
return err
}
cfg.SSLDir = d
}
p.conf = &cfg
return nil
}
}
// WithConfig optionally configures the Puppet Security Provider using its native configuration format
func WithConfig(c *Config) Option {
return func(p *PuppetSecurity) error {
p.conf = c
return nil
}
}
// WithLog configures a logger for the Puppet Security Provider
func WithLog(l *logrus.Entry) Option {
return func(p *PuppetSecurity) error {
p.log = l.WithFields(logrus.Fields{"ssl": "puppet"})
return nil
}
}
// WithResolver configures a SRV resolver for the Puppet Security Provider
func WithResolver(r Resolver) Option {
return func(p *PuppetSecurity) error {
p.res = r
return nil
}
}