diff options
author | 2020-10-30 17:06:04 +0100 | |
---|---|---|
committer | 2020-11-05 13:53:05 +0100 | |
commit | 5fe9e1b7c43fc1a34b38899a2a055540cc30ac81 (patch) | |
tree | 5a83d0219fa03f0d280db61ed44f64d377a04138 /certServiceK8sExternalProvider/src/cmpv2controller/logger | |
parent | 15446c2ccfc117a0feb5a5249c2e62714b2cd3d7 (diff) |
[OOM-K8S-CERT-EXTERNAL-PROVIDER] Change logger implementation provider
Issue-ID: OOM-2559
Signed-off-by: Joanna Jeremicz <joanna.jeremicz@nokia.com>
Change-Id: I717dfcfd74a01ccb81b2eb03c52be2c90fc3a9c5
Diffstat (limited to 'certServiceK8sExternalProvider/src/cmpv2controller/logger')
3 files changed, 61 insertions, 42 deletions
diff --git a/certServiceK8sExternalProvider/src/cmpv2controller/logger/certificate_request_logger.go b/certServiceK8sExternalProvider/src/cmpv2controller/logger/certificate_request_logger.go index 0aaf48d3..786557c1 100644 --- a/certServiceK8sExternalProvider/src/cmpv2controller/logger/certificate_request_logger.go +++ b/certServiceK8sExternalProvider/src/cmpv2controller/logger/certificate_request_logger.go @@ -26,8 +26,8 @@ import ( "net/url" "strconv" - "github.com/go-logr/logr" cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1" + "onap.org/oom-certservice/k8s-external-provider/src/leveledlogger" ) const ( @@ -35,13 +35,13 @@ const ( CMPv2ServerName = "CMPv2 Server" ) -func LogCertRequestProperties(log logr.Logger, request *cmapi.CertificateRequest, csr *x509.CertificateRequest) { +func LogCertRequestProperties(log leveledlogger.Logger, request *cmapi.CertificateRequest, csr *x509.CertificateRequest) { logSupportedProperties(log, request, csr) logPropertiesNotSupportedByCertService(log, request, csr) logPropertiesOverriddenByCMPv2Server(log, request) } -func logSupportedProperties(log logr.Logger, request *cmapi.CertificateRequest, csr *x509.CertificateRequest) { +func logSupportedProperties(log leveledlogger.Logger, request *cmapi.CertificateRequest, csr *x509.CertificateRequest) { logSupportedProperty(log, csr.Subject.Organization, "organization") logSupportedProperty(log, csr.Subject.OrganizationalUnit, "organization unit") logSupportedProperty(log, csr.Subject.Country, "country") @@ -50,13 +50,13 @@ func logSupportedProperties(log logr.Logger, request *cmapi.CertificateRequest, logSupportedProperty(log, csr.DNSNames, "dns names") } -func logSupportedProperty(log logr.Logger, values []string, propertyName string) { +func logSupportedProperty(log leveledlogger.Logger, values []string, propertyName string) { if len(values) > 0 { log.Info(getSupportedMessage(propertyName, extractStringArray(values))) } } -func logPropertiesOverriddenByCMPv2Server(log logr.Logger, request *cmapi.CertificateRequest) { +func logPropertiesOverriddenByCMPv2Server(log leveledlogger.Logger, request *cmapi.CertificateRequest) { if request.Spec.Duration != nil && len(request.Spec.Duration.String()) > 0 { log.Info(getOverriddenMessage("duration", request.Spec.Duration.Duration.String())) } @@ -73,36 +73,36 @@ func extractUsages(usages []cmapi.KeyUsage) string { return values } -func logPropertiesNotSupportedByCertService(log logr.Logger, request *cmapi.CertificateRequest, csr *x509.CertificateRequest) { +func logPropertiesNotSupportedByCertService(log leveledlogger.Logger, request *cmapi.CertificateRequest, csr *x509.CertificateRequest) { //IP addresses in SANs if len(csr.IPAddresses) > 0 { - log.Info(getNotSupportedMessage("ipAddresses", extractIPAddresses(csr.IPAddresses))) + log.Warning(getNotSupportedMessage("ipAddresses", extractIPAddresses(csr.IPAddresses))) } //URIs in SANs if len(csr.URIs) > 0 { - log.Info(getNotSupportedMessage("uris", extractURIs(csr.URIs))) + log.Warning(getNotSupportedMessage("uris", extractURIs(csr.URIs))) } //Email addresses in SANs if len(csr.EmailAddresses) > 0 { - log.Info(getNotSupportedMessage("emailAddresses", extractStringArray(csr.EmailAddresses))) + log.Warning(getNotSupportedMessage("emailAddresses", extractStringArray(csr.EmailAddresses))) } if request.Spec.IsCA == true { - log.Info(getNotSupportedMessage("isCA", strconv.FormatBool(request.Spec.IsCA))) + log.Warning(getNotSupportedMessage("isCA", strconv.FormatBool(request.Spec.IsCA))) } if len(csr.Subject.StreetAddress) > 0 { - log.Info(getNotSupportedMessage("subject.streetAddress", extractStringArray(csr.Subject.StreetAddress))) + log.Warning(getNotSupportedMessage("subject.streetAddress", extractStringArray(csr.Subject.StreetAddress))) } if len(csr.Subject.PostalCode) > 0 { - log.Info(getNotSupportedMessage("subject.postalCodes", extractStringArray(csr.Subject.PostalCode))) + log.Warning(getNotSupportedMessage("subject.postalCodes", extractStringArray(csr.Subject.PostalCode))) } if len(csr.Subject.SerialNumber) > 0 { - log.Info(getNotSupportedMessage("subject.serialNumber", csr.Subject.SerialNumber)) + log.Warning(getNotSupportedMessage("subject.serialNumber", csr.Subject.SerialNumber)) } } diff --git a/certServiceK8sExternalProvider/src/cmpv2controller/logger/certificate_request_logger_test.go b/certServiceK8sExternalProvider/src/cmpv2controller/logger/certificate_request_logger_test.go index ea1076dc..e6a4d0e6 100644 --- a/certServiceK8sExternalProvider/src/cmpv2controller/logger/certificate_request_logger_test.go +++ b/certServiceK8sExternalProvider/src/cmpv2controller/logger/certificate_request_logger_test.go @@ -22,7 +22,9 @@ package logger import ( "bytes" - "flag" + + "io/ioutil" + "log" "os" "strings" "testing" @@ -31,10 +33,9 @@ import ( cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/klog/v2" - "k8s.io/klog/v2/klogr" x509utils "onap.org/oom-certservice/k8s-external-provider/src/x509" + "onap.org/oom-certservice/k8s-external-provider/src/leveledlogger" ) var checkedLogMessages = [7]string{"Property 'duration'", "Property 'usages'", "Property 'ipAddresses'", @@ -44,22 +45,17 @@ var checkedLogMessages = [7]string{"Property 'duration'", "Property 'usages'", " var supportedProperties = [7]string{"Property 'organization'", "Property 'organization unit'", "Property 'country'", "Property 'state'", "Property 'location'", "Property 'dns names'"} +const RESULT_LOG = "testdata/test_result.log" func TestMain(m *testing.M) { - klog.InitFlags(nil) - flag.CommandLine.Set("v", "10") - flag.CommandLine.Set("skip_headers", "true") - flag.CommandLine.Set("logtostderr", "false") - flag.CommandLine.Set("alsologtostderr", "false") - flag.Parse() + leveledlogger.SetConfigFileName("testdata/test_logger_config.json") os.Exit(m.Run()) } func TestLogShouldNotProvideInformationAboutSkippedPropertiesIfNotExistInCSR(t *testing.T) { //given - logger := klogr.New() + logger := leveledlogger.GetLoggerWithName("test") request := getCertificateRequestWithoutSkippedProperties() - tmpWriteBuffer := getLogBuffer() csr, err := x509utils.DecodeCSR(request.Spec.Request) if err != nil { @@ -68,19 +64,19 @@ func TestLogShouldNotProvideInformationAboutSkippedPropertiesIfNotExistInCSR(t * //when LogCertRequestProperties(logger, request, csr) - closeLogBuffer() - logsArray := convertBufferToStringArray(tmpWriteBuffer) + logsArray := convertLogFileToStringArray(RESULT_LOG) + //then for _, logMsg := range checkedLogMessages { assert.False(t, logsContainExpectedMessage(logsArray, logMsg), "Logs contain: "+logMsg+", but should not") } + removeTemporaryFile(RESULT_LOG) } func TestLogShouldProvideInformationAboutSkippedPropertiesIfExistInCSR(t *testing.T) { //given - logger := klogr.New() + logger := leveledlogger.GetLoggerWithName("test") request := getCertificateRequestWithSkippedProperties() - tmpWriteBuffer := getLogBuffer() csr, err := x509utils.DecodeCSR(request.Spec.Request) if err != nil { @@ -89,20 +85,19 @@ func TestLogShouldProvideInformationAboutSkippedPropertiesIfExistInCSR(t *testin //when LogCertRequestProperties(logger, request, csr) - closeLogBuffer() - logsArray := convertBufferToStringArray(tmpWriteBuffer) + logsArray := convertLogFileToStringArray(RESULT_LOG) //then for _, logMsg := range checkedLogMessages { assert.True(t, logsContainExpectedMessage(logsArray, logMsg), "Logs not contain: "+logMsg) } + removeTemporaryFile(RESULT_LOG) } func TestLogShouldListSupportedProperties(t *testing.T) { //given - logger := klogr.New() + logger := leveledlogger.GetLoggerWithName("test") request := getCertificateRequestWithoutSkippedProperties() - tmpWriteBuffer := getLogBuffer() csr, err := x509utils.DecodeCSR(request.Spec.Request) if err != nil { @@ -111,13 +106,13 @@ func TestLogShouldListSupportedProperties(t *testing.T) { //when LogCertRequestProperties(logger, request, csr) - closeLogBuffer() - logsArray := convertBufferToStringArray(tmpWriteBuffer) + logsArray := convertLogFileToStringArray(RESULT_LOG) //then for _, logMsg := range supportedProperties { assert.True(t, logsContainExpectedMessage(logsArray, logMsg), "Logs not contain: "+logMsg) } + removeTemporaryFile(RESULT_LOG) } func getCertificateRequestWithoutSkippedProperties() *cmapi.CertificateRequest { @@ -135,18 +130,31 @@ func getCertificateRequestWithSkippedProperties() *cmapi.CertificateRequest { return request } -func getLogBuffer() *bytes.Buffer { - tmpWriteBuffer := bytes.NewBuffer(nil) - klog.SetOutput(tmpWriteBuffer) - return tmpWriteBuffer +func convertBufferToStringArray(buffer *bytes.Buffer) []string { + return strings.Split(buffer.String(), "\n") } -func closeLogBuffer() { - klog.Flush() +func convertLogFileToStringArray(filename string) []string { + buffer := bytes.NewBuffer(make([]byte, 0)) + buffer.Write(readFile(filename)) + return convertBufferToStringArray(buffer) } -func convertBufferToStringArray(buffer *bytes.Buffer) []string { - return strings.Split(buffer.String(), "\n") +func readFile(filename string) []byte { + certRequest, err := ioutil.ReadFile(filename) + if err != nil { + log.Fatal(err) + } + return certRequest +} + +func removeTemporaryFile(fileName string) { + if _, err := os.Stat(fileName); err == nil { + e := os.Remove(fileName) + if e != nil { + log.Fatal(e) + } + } } func logsContainExpectedMessage(array []string, expectedMsg string) bool { diff --git a/certServiceK8sExternalProvider/src/cmpv2controller/logger/testdata/test_logger_config.json b/certServiceK8sExternalProvider/src/cmpv2controller/logger/testdata/test_logger_config.json new file mode 100644 index 00000000..02030b6e --- /dev/null +++ b/certServiceK8sExternalProvider/src/cmpv2controller/logger/testdata/test_logger_config.json @@ -0,0 +1,11 @@ +{ + "level": "debug", + "encoding": "json", + "outputPaths": ["stdout", "testdata/test_result.log"], + "encoderConfig": { + "messageKey": "message", + "levelKey": "level", + "nameKey": "name", + "levelEncoder": "capital" + } +} |