Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

authz: Stdout logger #6230

Merged
merged 30 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
065089d
Draft of StdoutLogger
erm-g Apr 25, 2023
e17b64e
Fitting StdoutLogger to lb patterns
erm-g Apr 25, 2023
92b53a7
Merge branch 'grpc:master' into AuditLoggerRegistry
erm-g Apr 27, 2023
5665daa
conversion from proto to json for laudit loggers
erm-g Apr 28, 2023
95ebc88
Tests for multiple loggers and empty Options
erm-g Apr 28, 2023
bcf4256
Added LoggerConfig impl
erm-g May 1, 2023
b918c0a
Switched to grpcLogger and added a unit test comparing log with os.St…
erm-g May 4, 2023
861d9f2
Minor fix in exception handling wording
erm-g May 4, 2023
270948b
Added timestamp for logging statement
erm-g May 4, 2023
8859707
Changed format to json and added custom marshalling
erm-g May 7, 2023
c073753
Migration to log.go and additional test for a full event
erm-g May 7, 2023
c6dd904
Migration of stdout logger to a separate package
erm-g May 7, 2023
e72bfa2
migration to grpcLogger, unit test fix
erm-g May 8, 2023
9866f61
Delete xds parsing functionality. Will be done in a separate PR
erm-g May 8, 2023
0d9a56e
Delete xds parsing functionality. Will be done in a separate PR
erm-g May 8, 2023
8c24380
Address PR comments (embedding interface, table test, pointer optimiz…
erm-g May 9, 2023
317f501
vet.sh fixes
erm-g May 9, 2023
e0800ba
Address PR comments
erm-g May 10, 2023
e0c53d8
Commit for go tidy changes
erm-g May 10, 2023
da716bb
vet.sh fix for buf usage
erm-g May 10, 2023
7c36609
Address PR comments
erm-g May 11, 2023
c903401
Address PR comments
erm-g May 11, 2023
9bcb689
Address PR comments (easwars)
erm-g May 12, 2023
a1e3e7a
Address PR comments (luwei)
erm-g May 14, 2023
c74d21b
Migrate printing to standard out from log package level func to a Log…
erm-g May 15, 2023
e6c450b
Changed event Timestamp format back to RFC3339
erm-g May 15, 2023
f60c376
Address PR comments
erm-g May 16, 2023
8f0ad3c
Address PR comments
erm-g May 16, 2023
6ef31dd
Address PR comments
erm-g May 16, 2023
d663a6d
Address PR comments
erm-g May 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions authz/audit/stdout_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
*
* Copyright 2023 gRPC authors.
*
* 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 audit

import (
"encoding/json"
"fmt"
)

type StdOutLogger struct{}
erm-g marked this conversation as resolved.
Show resolved Hide resolved
erm-g marked this conversation as resolved.
Show resolved Hide resolved

func (logger *StdOutLogger) Log(event *Event) {
jsonBytes, err := json.Marshal(event)
erm-g marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Errorf("failed to marshal log data to JSON: %v", err)
erm-g marked this conversation as resolved.
Show resolved Hide resolved
}

fmt.Println(string(jsonBytes))
erm-g marked this conversation as resolved.
Show resolved Hide resolved
}

func (logger *StdOutLogger) ToJSON() ([]byte, error) {
erm-g marked this conversation as resolved.
Show resolved Hide resolved
jsonBytes, err := json.Marshal(logger)
if err != nil {
return nil, fmt.Errorf("failed to marshal logger to JSON: %v", err)
}

return jsonBytes, nil
}

const (
stdName = "stdout"
)

type StdOutLoggerBuilder struct{}

func (StdOutLoggerBuilder) Name() string {
return stdName
}

func (StdOutLoggerBuilder) Build(LoggerConfig) Logger {
return &StdOutLogger{}
}

func (StdOutLoggerBuilder) ParseLoggerConfig(config json.RawMessage) (LoggerConfig, error) {
fmt.Println("Config value ignored, " +
"StdOutLogger doesn't support any configs")
erm-g marked this conversation as resolved.
Show resolved Hide resolved
erm-g marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil
}
50 changes: 50 additions & 0 deletions authz/audit/stdout_logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* Copyright 2023 gRPC authors.
*
* 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 audit

import (
"testing"
)

var (
builder = &StdOutLoggerBuilder{}
logger = builder.Build(nil)
)

func TestStdOutLogger_Log(t *testing.T) {
event := &Event{PolicyName: "test policy", Principal: "test principal"}
logger.Log(event)
erm-g marked this conversation as resolved.
Show resolved Hide resolved
}

//func TestMyLogger_ToJSON(t *testing.T) {
erm-g marked this conversation as resolved.
Show resolved Hide resolved
// jsonBytes, err := logger.ToJSON()
// if err != nil {
// t.Fatalf("Failed to marshal logger to JSON: %v", err)
// }
//
// var restored StdOutLogger
// err = json.Unmarshal(jsonBytes, &restored)
// if err != nil {
// t.Fatalf("Failed to unmarshal logger back from JSON: %v", err)
// }
//
// if !reflect.DeepEqual(logger, &restored) {
// t.Errorf("ToJSON() test failed, restored = %v, want %v", restored, logger)
// }
//}