Skip to content

Commit 5665daa

Browse files
committed
conversion from proto to json for laudit loggers
1 parent 92b53a7 commit 5665daa

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package audit
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
v3rbacpb "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v3"
8+
)
9+
10+
//TODO we should have somewhere logic related to
11+
//1. checking if logger type is known
12+
//2. if yes, check if it's config is valid (LoggerBuilder.ParseLoggerConfig)
13+
//IMHO it belongs to json parsing layer but not here
14+
15+
func ConvertXdsAuditLoggerConfig(loggerCfg *v3rbacpb.RBAC_AuditLoggingOptions_AuditLoggerConfig) (json.RawMessage, error) {
16+
if loggerCfg == nil {
17+
return nil, fmt.Errorf("rbac audit logging: nil AuditLoggerConfig message provided")
18+
}
19+
if loggerCfg.AuditLogger == nil {
20+
return nil, fmt.Errorf("rbac audit logging: AuditLogger type is mandatory")
21+
}
22+
jsonBytes, err := json.Marshal(loggerCfg)
23+
if err != nil {
24+
return nil, fmt.Errorf("rbac audit logging: failed to marshal AuditLoggerConfig to json: %v", err)
25+
}
26+
return jsonBytes, nil
27+
}
28+
29+
func ExtractXdsAuditLoggersConfig(optionsCfg *v3rbacpb.RBAC_AuditLoggingOptions) ([]json.RawMessage, error) {
30+
if optionsCfg == nil {
31+
fmt.Println("rbac audit logging: nil AuditLoggingOptions message provided, audit is disabled")
32+
return nil, nil
33+
}
34+
if optionsCfg.LoggerConfigs == nil || len(optionsCfg.LoggerConfigs) == 0 {
35+
fmt.Println("rbac audit logging: no AuditLoggerConfigs found, audit is disabled")
36+
return nil, nil
37+
}
38+
validConfigs := make([]json.RawMessage, 0)
39+
for _, v := range optionsCfg.LoggerConfigs {
40+
jsonBytes, err := ConvertXdsAuditLoggerConfig(v)
41+
if err != nil {
42+
continue
43+
}
44+
validConfigs = append(validConfigs, jsonBytes)
45+
}
46+
47+
return validConfigs, nil
48+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package audit
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
8+
v3rbacpb "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v3"
9+
"github.com/google/go-cmp/cmp"
10+
"google.golang.org/protobuf/testing/protocmp"
11+
"google.golang.org/protobuf/types/known/anypb"
12+
"google.golang.org/protobuf/types/known/structpb"
13+
)
14+
15+
func TestExtractXdsAuditLoggersConfig(t *testing.T) {
16+
tests := map[string]struct {
17+
auditLoggingOptions *v3rbacpb.RBAC_AuditLoggingOptions
18+
wantErr string
19+
wantJsonCfg string
20+
}{
21+
"valid std_out cfg": {
22+
auditLoggingOptions: &v3rbacpb.RBAC_AuditLoggingOptions{
23+
AuditCondition: v3rbacpb.RBAC_AuditLoggingOptions_NONE,
24+
LoggerConfigs: []*v3rbacpb.RBAC_AuditLoggingOptions_AuditLoggerConfig{
25+
{AuditLogger: &v3corepb.TypedExtensionConfig{
26+
Name: "stdout_logger", TypedConfig: anyPbHelper(t, map[string]interface{}{})},
27+
IsOptional: true,
28+
},
29+
},
30+
},
31+
wantJsonCfg: "{\"audit_logger\":{\"name\":\"stdout_logger\",\"typed_config\":{\"type_url\":\"type.googleapis.com/google.protobuf.Struct\"}},\"is_optional\":true}",
32+
},
33+
}
34+
35+
for name, test := range tests {
36+
t.Run(name, func(t *testing.T) {
37+
gotJsonCfg, gotErr := ExtractXdsAuditLoggersConfig(test.auditLoggingOptions)
38+
if gotErr != nil && !strings.HasPrefix(gotErr.Error(), test.wantErr) {
39+
t.Fatalf("unexpected error\nwant:%v\ngot:%v", test.wantErr, gotErr)
40+
}
41+
if diff := cmp.Diff(string(gotJsonCfg[0]), test.wantJsonCfg, protocmp.Transform()); diff != "" {
42+
t.Fatalf("unexpected jsonconfig\ndiff (-want +got):\n%s", diff)
43+
}
44+
})
45+
}
46+
}
47+
48+
func anyPbHelper(t *testing.T, in map[string]interface{}) *anypb.Any {
49+
t.Helper()
50+
pb, err := structpb.NewStruct(in)
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
ret, err := anypb.New(pb)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
return ret
59+
}

0 commit comments

Comments
 (0)