-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
194 lines (163 loc) · 6.11 KB
/
main.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"fmt"
"os"
"regexp"
"strings"
"github.com/ory/oathkeeper-maester/internal/validation"
oathkeeperv1alpha1 "github.com/ory/oathkeeper-maester/api/v1alpha1"
"github.com/ory/oathkeeper-maester/controllers"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
apiv1.AddToScheme(scheme)
oathkeeperv1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var rulesConfigmapName string
var rulesConfigmapNamespace string
var rulesFileName string
var rulesFilePath string
var operator controllers.OperatorMode
controllerCommand := flag.NewFlagSet("controller", flag.ExitOnError)
sidecarCommand := flag.NewFlagSet("sidecar", flag.ExitOnError)
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
controllerCommand.StringVar(&rulesConfigmapName, "rulesConfigmapName", "oathkeeper-rules", "Name of the Configmap that stores Oathkeeper rules.")
controllerCommand.StringVar(&rulesConfigmapNamespace, "rulesConfigmapNamespace", "oathkeeper-maester-system", "Namespace of the Configmap that stores Oathkeeper rules.")
controllerCommand.StringVar(&rulesFileName, "rulesFileName", "access-rules.json", "Name of the key in ConfigMap containing the rules.json")
sidecarCommand.StringVar(&rulesFilePath, "rulesFilePath", "/etc/config/access-rules.json", "Path to the file with converted Oathkeeper rules")
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
sideCarMode, err := selectMode(flag.Args(), controllerCommand, sidecarCommand)
if err != nil {
setupLog.Error(err, "problem parsing flags")
os.Exit(1)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
// Defaults to "" which means all namespaces
Namespace: os.Getenv("NAMESPACE"),
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if err := validateRulesFileName(rulesFileName); err != nil {
setupLog.Error(err, "Validation error")
os.Exit(1)
}
validationConfig := initValidationConfig()
if sideCarMode {
operator = &controllers.FilesOperator{
Log: ctrl.Log.WithName("controllers").WithName("Rule"),
RulesFilePath: rulesFilePath,
}
} else {
operator = &controllers.ConfigMapOperator{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Rule"),
DefaultConfigMap: types.NamespacedName{
Name: rulesConfigmapName,
Namespace: rulesConfigmapNamespace,
},
RulesFileName: rulesFileName,
}
}
ruleReconciler := &controllers.RuleReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Rule"),
ValidationConfig: validationConfig,
OperatorMode: operator,
}
if err := ruleReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Rule")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func parseListOrDefault(list string, defaultArr []string, name string) []string {
if list == "" {
setupLog.Info(fmt.Sprintf("using default values for %s", name))
return defaultArr
}
return parseList(list)
}
func parseList(list string) []string {
return removeEmptyStrings(strings.Split(list, ","))
}
func removeEmptyStrings(list []string) []string {
result := make([]string, 0)
for _, s := range list {
ts := strings.TrimSpace(s)
if ts != "" {
result = append(result, ts)
}
}
return result
}
func initValidationConfig() validation.Config {
authenticatorsAvailable := os.Getenv(oathkeeperv1alpha1.AuthenticatorsAvailableEnv)
authorizersAvailable := os.Getenv(oathkeeperv1alpha1.AuthorizersAvailableEnv)
mutatorsAvailable := os.Getenv(oathkeeperv1alpha1.MutatorsAvailableEnv)
errorsAvailable := os.Getenv(oathkeeperv1alpha1.ErrorsAvailableEnv)
return validation.Config{
AuthenticatorsAvailable: parseListOrDefault(authenticatorsAvailable, oathkeeperv1alpha1.DefaultAuthenticatorsAvailable[:], oathkeeperv1alpha1.AuthenticatorsAvailableEnv),
AuthorizersAvailable: parseListOrDefault(authorizersAvailable, oathkeeperv1alpha1.DefaultAuthorizersAvailable[:], oathkeeperv1alpha1.AuthorizersAvailableEnv),
MutatorsAvailable: parseListOrDefault(mutatorsAvailable, oathkeeperv1alpha1.DefaultMutatorsAvailable[:], oathkeeperv1alpha1.MutatorsAvailableEnv),
ErrorsAvailable: parseListOrDefault(errorsAvailable, oathkeeperv1alpha1.DefaultErrorsAvailable[:], oathkeeperv1alpha1.ErrorsAvailableEnv),
}
}
func validateRulesFileName(rfn string) error {
match, _ := regexp.MatchString(oathkeeperv1alpha1.RulesFileNameRegexp, rfn)
if match {
return nil
}
return fmt.Errorf("rulesFileName: %s is not a valid name", rfn)
}
func selectMode(args []string, controllerCommand *flag.FlagSet, sidecarCommand *flag.FlagSet) (bool, error) {
if len(args) < 1 {
setupLog.Info("running in controller mode")
return false, nil
}
switch args[0] {
case "controller":
if err := controllerCommand.Parse(args[1:]); err != nil {
return false, err
}
setupLog.Info("running in controller mode")
return false, nil
case "sidecar":
if err := sidecarCommand.Parse(args[1:]); err != nil {
return false, err
}
setupLog.Info("running in sidecar mode")
return true, nil
default:
return false, fmt.Errorf(`modes "controller" and "sidecar" are supported but got: %s`, args[0])
}
}