Skip to content

Commit fb51749

Browse files
authored
Merge pull request #134 from authzed/improve-logging
support setting a logger programatically
2 parents 69452ad + 0771df4 commit fb51749

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

pkg/authz/authz.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func WithAuthorization(handler, failed http.Handler, restMapper meta.RESTMapper,
3636

3737
matchingRules := (*matcher).Match(input.Request)
3838
if len(matchingRules) == 0 {
39-
klog.V(3).InfoSDepth(1,
39+
klog.FromContext(ctx).V(3).Info(
4040
"request did not match any authorization rule",
4141
"verb", input.Request.Verb,
4242
"APIGroup", input.Request.APIGroup,
@@ -49,13 +49,13 @@ func WithAuthorization(handler, failed http.Handler, restMapper meta.RESTMapper,
4949
// Apply CEL condition filtering
5050
filteredRules, err := rules.FilterRulesWithCELConditions(matchingRules, input)
5151
if err != nil {
52-
klog.V(2).ErrorS(err, "error evaluating CEL conditions", "input", input)
52+
klog.FromContext(ctx).V(2).Error(err, "error evaluating CEL conditions", "input", input)
5353
handleError(w, failed, req, err)
5454
return
5555
}
5656

5757
if len(filteredRules) == 0 {
58-
klog.V(3).InfoSDepth(1,
58+
klog.FromContext(ctx).V(3).Info(
5959
"request matched authorization rule/s but failed CEL conditions",
6060
"verb", input.Request.Verb,
6161
"APIGroup", input.Request.APIGroup,
@@ -65,40 +65,40 @@ func WithAuthorization(handler, failed http.Handler, restMapper meta.RESTMapper,
6565
return
6666
}
6767

68-
klog.V(3).InfoSDepth(1,
68+
klog.FromContext(ctx).V(3).Info(
6969
"request matched authorization rule/s and passed CEL conditions",
7070
"verb", input.Request.Verb,
7171
"APIGroup", input.Request.APIGroup,
7272
"APIVersion", input.Request.APIVersion,
7373
"Resource", input.Request.Resource)
74-
klog.V(4).InfoSDepth(1, "authorization input details", "input", input)
74+
klog.FromContext(ctx).V(4).Info("authorization input details", "input", input)
7575

7676
// run all checks for this request
7777
if err := runAllMatchingChecks(ctx, filteredRules, input, permissionsClient); err != nil {
78-
klog.V(2).ErrorS(err, "input failed authorization checks", "input", input)
78+
klog.FromContext(ctx).V(2).Error(err, "input failed authorization checks", "input", input)
7979
handleError(w, failed, req, err)
8080
return
8181
}
82-
klog.V(3).InfoSDepth(1, "input passed all authorization checks", "input", input)
82+
klog.FromContext(ctx).V(3).Info("input passed all authorization checks", "input", input)
8383

8484
// if this request is a write, perform the dual write and return
8585
rule, err := getSingleUpdateRule(filteredRules)
8686
if err != nil {
87-
klog.V(2).ErrorS(err, "unable to get single update rule", "input", input)
87+
klog.FromContext(ctx).V(2).Error(err, "unable to get single update rule", "input", input)
8888
handleError(w, failed, req, err)
8989
return
9090
}
9191

9292
if rule != nil {
93-
klog.V(4).InfoSDepth(1, "single update rule", "rule", rule)
93+
klog.FromContext(ctx).V(4).Info("single update rule", "rule", rule)
9494
if err := performUpdate(ctx, w, rule, input, req.RequestURI, workflowClient); err != nil {
95-
klog.V(2).ErrorS(err, "failed to perform update", "input", input)
95+
klog.FromContext(ctx).V(2).Error(err, "failed to perform update", "input", input)
9696
handleError(w, failed, req, err)
9797
return
9898
}
9999
return
100100
} else {
101-
klog.V(4).InfoSDepth(1, "no update rule found for request")
101+
klog.FromContext(ctx).V(4).Info("no update rule found for request")
102102
}
103103

104104
// all other requests are filtered by matching rules
@@ -186,12 +186,12 @@ func createPostCheckHandler(handler, failed http.Handler, ctx context.Context, f
186186
if recorder.statusCode >= 200 && recorder.statusCode < 300 {
187187
// Run PostChecks
188188
if err := runAllMatchingPostChecks(ctx, filteredRules, input, permissionsClient); err != nil {
189-
klog.V(2).ErrorS(err, "input failed post-authorization checks", "input", input)
189+
klog.FromContext(ctx).V(2).Error(err, "input failed post-authorization checks", "input", input)
190190
// Return the original error handler instead of the successful response
191191
failed.ServeHTTP(w, req)
192192
return
193193
}
194-
klog.V(3).InfoSDepth(1, "input passed all post-authorization checks", "input", input)
194+
klog.FromContext(ctx).V(3).Info("input passed all post-authorization checks", "input", input)
195195

196196
// Only write the successful response if PostChecks passed
197197
recorder.emitResponseToWriter(w)
@@ -216,7 +216,7 @@ func createPostFilterHandler(handler, failed http.Handler, ctx context.Context,
216216
if input.Request.Verb == "list" {
217217
// Handle list operations
218218
if err := filterListResponse(ctx, recorder, filteredRules, input, permissionsClient); err != nil {
219-
klog.V(2).ErrorS(err, "failed to filter list response", "input", input)
219+
klog.FromContext(ctx).V(2).Error(err, "failed to filter list response", "input", input)
220220
failed.ServeHTTP(w, req)
221221
return
222222
}
@@ -288,7 +288,7 @@ func (r *responseRecorder) emitResponseToWriter(w http.ResponseWriter) {
288288
// Write body
289289
if len(r.body) > 0 {
290290
if _, err := w.Write(r.body); err != nil {
291-
klog.ErrorS(err, "failed to write response body", "status_code", statusCode, "body_length", len(r.body))
291+
klog.Error(err, "failed to write response body", "status_code", statusCode, "body_length", len(r.body))
292292
}
293293
}
294294
}

pkg/proxy/options.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
1717
"github.com/authzed/grpcutil"
1818
"github.com/authzed/spicedb/pkg/cmd/server"
19+
"github.com/go-logr/logr"
1920
"github.com/spf13/pflag"
2021
"google.golang.org/grpc"
2122
"google.golang.org/grpc/backoff"
@@ -47,6 +48,7 @@ type Options struct {
4748
SecureServing apiserveroptions.SecureServingOptionsWithLoopback `debugmap:"hidden"`
4849
Authentication Authentication `debugmap:"hidden"`
4950
Logs *logs.Options `debugmap:"hidden"`
51+
CustomLogger logr.Logger
5052

5153
// TODO: use genericclioptions.ConfigFlags instead of this?
5254
BackendKubeconfigPath string `debugmap:"visible"`
@@ -151,7 +153,9 @@ type CompletedConfig struct {
151153
}
152154

153155
func (o *Options) Complete(ctx context.Context) (*CompletedConfig, error) {
154-
if err := logsv1.ValidateAndApply(o.Logs, utilfeature.DefaultFeatureGate); err != nil {
156+
if (o.CustomLogger != logr.Logger{}) {
157+
klog.SetLoggerWithOptions(o.CustomLogger, klog.ContextualLogger(true))
158+
} else if err := logsv1.ValidateAndApply(o.Logs, utilfeature.DefaultFeatureGate); err != nil {
155159
return nil, err
156160
}
157161

pkg/proxy/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func NewServer(ctx context.Context, c *CompletedConfig) (*Server, error) {
7474
}
7575

7676
clusterHost = restConfig.Host
77-
klog.FromContext(ctx).WithValues("host", clusterHost).Error(err, "created upstream client")
77+
klog.FromContext(ctx).WithValues("host", clusterHost).Info("created upstream client")
7878

7979
mux := http.NewServeMux()
8080

0 commit comments

Comments
 (0)