From b461a9f5a46b408c260b0086dc6cae766ca732f9 Mon Sep 17 00:00:00 2001 From: John Casey Date: Fri, 5 Apr 2019 14:55:10 -0500 Subject: [PATCH 1/5] Refactor CustomJsonLayout to use default logback layout configuration for envars This will skip the period when CDI isn't started, when it's still trying to log things. It also skips the need for CDI.current() to lookup the environment configuration, putting the envar extraction config directly in logback.xml Conflicts: deployments/launcher/src/main/etc/indy/logging/logback.xml --- api/pom.xml | 8 ++ .../org/commonjava/indy/CustomJsonLayout.java | 74 +++++++++++++++++++ .../src/main/etc/indy/logging/logback.xml | 56 ++------------ 3 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 api/src/main/java/org/commonjava/indy/CustomJsonLayout.java diff --git a/api/pom.xml b/api/pom.xml index b1dff324cf..dc952d41b6 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -68,6 +68,14 @@ com.fasterxml.jackson.core jackson-databind + + ch.qos.logback.contrib + logback-jackson + + + ch.qos.logback.contrib + logback-json-classic + diff --git a/api/src/main/java/org/commonjava/indy/CustomJsonLayout.java b/api/src/main/java/org/commonjava/indy/CustomJsonLayout.java new file mode 100644 index 0000000000..045f9fe2e3 --- /dev/null +++ b/api/src/main/java/org/commonjava/indy/CustomJsonLayout.java @@ -0,0 +1,74 @@ +/** + * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy) + * + * 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 org.commonjava.indy; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.contrib.json.classic.JsonLayout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; + +/** + * Created by yma on 2019/3/26. + */ +public class CustomJsonLayout + extends JsonLayout +{ + public static final String ENVIRONMENT = "environment"; + + private final Logger logger = LoggerFactory.getLogger( getClass() ); + + private String environmentMappings; + + private Map envars; + + public String getEnvironmentMappings() + { + return environmentMappings; + } + + public void setEnvironmentMappings( final String environmentMappings ) + { + this.environmentMappings = environmentMappings; + + String[] mappings = environmentMappings == null ? new String[0] : environmentMappings.split( "\\s*,\\s*" ); + envars = new HashMap<>(); + Stream.of(mappings).forEach( kv ->{ + String[] keyAlias = kv.split( "\\s*=\\s*" ); + if ( keyAlias.length > 1 ) + { + String value = System.getenv( keyAlias[0].trim() ); + if ( value == null ) + { + value = "Unknown"; + } + + envars.put( keyAlias[1].trim(), value ); + } + } ); + } + + @Override + protected void addCustomDataToJsonMap( Map map, ILoggingEvent iLoggingEvent ) + { + super.addCustomDataToJsonMap( map, iLoggingEvent ); + + map.put( ENVIRONMENT, envars ); + } +} diff --git a/deployments/launcher/src/main/etc/indy/logging/logback.xml b/deployments/launcher/src/main/etc/indy/logging/logback.xml index e95738566f..72e04618fa 100644 --- a/deployments/launcher/src/main/etc/indy/logging/logback.xml +++ b/deployments/launcher/src/main/etc/indy/logging/logback.xml @@ -82,43 +82,6 @@ - - - @@ -134,9 +97,16 @@ - @@ -193,14 +161,6 @@ - - - - From 41f6b6ae351ec346309cd0cabb9164ae6a19fc89 Mon Sep 17 00:00:00 2001 From: John Casey Date: Fri, 5 Apr 2019 15:01:48 -0500 Subject: [PATCH 2/5] Remove old CustomJsonLayout class --- .../indy/bind/jaxrs/CustomJsonLayout.java | 61 ------------------- 1 file changed, 61 deletions(-) delete mode 100644 subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/CustomJsonLayout.java diff --git a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/CustomJsonLayout.java b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/CustomJsonLayout.java deleted file mode 100644 index c910ab3ced..0000000000 --- a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/CustomJsonLayout.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy) - * - * 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 org.commonjava.indy.bind.jaxrs; - -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.contrib.json.classic.JsonLayout; -import com.fasterxml.jackson.core.JsonProcessingException; -import org.commonjava.indy.conf.EnvironmentConfig; -import org.commonjava.indy.model.core.io.IndyObjectMapper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.enterprise.inject.spi.CDI; -import java.util.Map; - -import static org.commonjava.indy.bind.jaxrs.RequestContextConstants.ENVIRONMENT; - -/** - * Created by yma on 2019/3/26. - */ -public class CustomJsonLayout - extends JsonLayout -{ - private final Logger logger = LoggerFactory.getLogger( getClass() ); - - @Override - protected void addCustomDataToJsonMap( Map map, ILoggingEvent iLoggingEvent ) - { - IndyObjectMapper objectMapper = new IndyObjectMapper( true ); - super.addCustomDataToJsonMap( map, iLoggingEvent ); - - if ( !iLoggingEvent.getMDCPropertyMap().isEmpty() ) - { - Map mdcs = (Map) map.get( MDC_ATTR_NAME ); - try - { - Map envars = CDI.current().select( EnvironmentConfig.class ).get().getEnvars(); - mdcs.put( ENVIRONMENT, objectMapper.writeValueAsString( envars ) ); - } - catch ( JsonProcessingException e ) - { - mdcs.put( ENVIRONMENT, "{error: \"Envars could not be processed by Jackson.\"}" ); - logger.error( String.format( "Failed to create environment mdc. Reason: %s", e.getMessage() ), e ); - } - map.put( MDC_ATTR_NAME, mdcs ); - } - } -} From 3c69c08fbd69e445093e439889bdbbeac3ce84d6 Mon Sep 17 00:00:00 2001 From: John Casey Date: Fri, 5 Apr 2019 15:03:41 -0500 Subject: [PATCH 3/5] remove unused constant --- .../commonjava/indy/bind/jaxrs/RequestContextConstants.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/RequestContextConstants.java b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/RequestContextConstants.java index 33e6985c15..b702556c0b 100644 --- a/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/RequestContextConstants.java +++ b/subsys/jaxrs/src/main/java/org/commonjava/indy/bind/jaxrs/RequestContextConstants.java @@ -60,7 +60,4 @@ public class RequestContextConstants @Thread @MDC public static final String PREFERRED_ID = "preferred-id"; - - @Thread @MDC - public static final String ENVIRONMENT = "environment"; } From d75dfc470739e967c620e100c18afde06777f6f0 Mon Sep 17 00:00:00 2001 From: John Casey Date: Wed, 10 Apr 2019 12:28:52 -0500 Subject: [PATCH 4/5] Be more careful about empty strings when pulling envars --- api/src/main/java/org/commonjava/indy/CustomJsonLayout.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/org/commonjava/indy/CustomJsonLayout.java b/api/src/main/java/org/commonjava/indy/CustomJsonLayout.java index 045f9fe2e3..13fd677b8d 100644 --- a/api/src/main/java/org/commonjava/indy/CustomJsonLayout.java +++ b/api/src/main/java/org/commonjava/indy/CustomJsonLayout.java @@ -17,6 +17,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.contrib.json.classic.JsonLayout; +import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,7 +55,7 @@ public void setEnvironmentMappings( final String environmentMappings ) if ( keyAlias.length > 1 ) { String value = System.getenv( keyAlias[0].trim() ); - if ( value == null ) + if ( StringUtils.isEmpty( value ) ) { value = "Unknown"; } From 692b7c753f6c869bf64bbe7854f6a8057e8d3dde Mon Sep 17 00:00:00 2001 From: John Casey Date: Wed, 10 Apr 2019 12:33:10 -0500 Subject: [PATCH 5/5] adjust example kafka config to include SSL params --- deployments/launcher/src/main/etc/indy/logging/logback.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/deployments/launcher/src/main/etc/indy/logging/logback.xml b/deployments/launcher/src/main/etc/indy/logging/logback.xml index 72e04618fa..7bb312d41f 100644 --- a/deployments/launcher/src/main/etc/indy/logging/logback.xml +++ b/deployments/launcher/src/main/etc/indy/logging/logback.xml @@ -115,6 +115,13 @@ bootstrap.servers=localhost:9092 + + security.protocol=SSL + ssl.keystore.location=/path/to/secret/keystore.jks + ssl.keystore.password=changeit + ssl.truststore.location=/path/to/secret/truststore.jks + ssl.truststore.password=changeit + acks=0 linger.ms=1000 max.block.ms=0