forked from ory/oathkeeper-maester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (179 loc) · 6.72 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
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")
defaultAuthenticatorsAvailable = [...]string{"noop", "unauthorized", "anonymous", "cookie_session", "oauth2_client_credentials", "oauth2_introspection", "jwt"}
defaultAuthorizersAvailable = [...]string{"allow", "deny", "keto_engine_acp_ory"}
defaultMutatorsAvailable = [...]string{"noop", "id_token", "header", "cookie", "hydrator"}
)
const (
authenticatorsAvailableEnv = "authenticatorsAvailable"
authorizersAvailableEnv = "authorizersAvailable"
mutatorsAvailableEnv = "mutatorsAvailable"
rulesFileNameRegexp = "\\A[-._a-zA-Z0-9]+\\z"
)
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.Logger(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,
})
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(authenticatorsAvailableEnv)
authorizersAvailable := os.Getenv(authorizersAvailableEnv)
mutatorsAvailable := os.Getenv(mutatorsAvailableEnv)
return validation.Config{
AuthenticatorsAvailable: parseListOrDefault(authenticatorsAvailable, defaultAuthenticatorsAvailable[:], authenticatorsAvailableEnv),
AuthorizersAvailable: parseListOrDefault(authorizersAvailable, defaultAuthorizersAvailable[:], authorizersAvailableEnv),
MutatorsAvailable: parseListOrDefault(mutatorsAvailable, defaultMutatorsAvailable[:], mutatorsAvailableEnv),
}
}
func validateRulesFileName(rfn string) error {
match, _ := regexp.MatchString(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) {
sidecar := true
controller := false
if len(args) < 1 {
setupLog.Info("running in controller mode")
return controller, nil
}
switch args[0] {
case "controller":
if err := controllerCommand.Parse(args[1:]); err != nil {
return false, err
}
setupLog.Info("running in controller mode")
return controller, nil
case "sidecar":
if err := sidecarCommand.Parse(args[1:]); err != nil {
return false, err
}
setupLog.Info("running in sidecar mode")
return sidecar, nil
default:
return false, fmt.Errorf(`modes "controller" and "sidecar" are supported but got: %s`, args[0])
}
}