From af47951cde650bdff407b076f3e4e8ac50fd7f01 Mon Sep 17 00:00:00 2001 From: Kai Hudalla Date: Tue, 30 Aug 2022 07:31:24 +0200 Subject: [PATCH] Fix potential NPEs Signed-off-by: Kai Hudalla --- .../tracing/MessageAnnotationsInjectAdapter.java | 6 +++--- .../client/util/DownstreamMessageProperties.java | 5 ++--- .../org/eclipse/hono/config/AbstractConfig.java | 5 +++++ .../org/eclipse/hono/util/CredentialsObject.java | 9 +++++++-- .../hono/service/base/jdbc/store/Statement.java | 4 ++-- .../jdbc/impl/ClasspathSchemaCreator.java | 14 ++++++++++---- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/clients/amqp-connection/src/main/java/org/eclipse/hono/client/amqp/tracing/MessageAnnotationsInjectAdapter.java b/clients/amqp-connection/src/main/java/org/eclipse/hono/client/amqp/tracing/MessageAnnotationsInjectAdapter.java index c46b506517..5e5a233c31 100644 --- a/clients/amqp-connection/src/main/java/org/eclipse/hono/client/amqp/tracing/MessageAnnotationsInjectAdapter.java +++ b/clients/amqp-connection/src/main/java/org/eclipse/hono/client/amqp/tracing/MessageAnnotationsInjectAdapter.java @@ -58,11 +58,11 @@ public void put(final String key, final String value) { @SuppressWarnings("unchecked") private Map getPropertiesMap() { final MessageAnnotations messageAnnotations; - if (message.getMessageAnnotations() == null || message.getMessageAnnotations().getValue() == null) { + if (message.getMessageAnnotations() != null && message.getMessageAnnotations().getValue() != null) { + messageAnnotations = message.getMessageAnnotations(); + } else { messageAnnotations = new MessageAnnotations(new HashMap<>()); message.setMessageAnnotations(messageAnnotations); - } else { - messageAnnotations = message.getMessageAnnotations(); } final Map propertiesMap; final Object annotationValue = messageAnnotations.getValue().get(Symbol.getSymbol(propertiesMapName)); diff --git a/clients/client-common/src/main/java/org/eclipse/hono/client/util/DownstreamMessageProperties.java b/clients/client-common/src/main/java/org/eclipse/hono/client/util/DownstreamMessageProperties.java index 1797594031..2fe4fb83ad 100644 --- a/clients/client-common/src/main/java/org/eclipse/hono/client/util/DownstreamMessageProperties.java +++ b/clients/client-common/src/main/java/org/eclipse/hono/client/util/DownstreamMessageProperties.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Contributors to the Eclipse Foundation + * Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -48,7 +48,7 @@ public final class DownstreamMessageProperties { private static final Logger LOG = LoggerFactory.getLogger(DownstreamMessageProperties.class); - private final Map props; + private final Map props = new HashMap<>(); private final ResourceLimits resourceLimits; private final String endpointName; @@ -78,7 +78,6 @@ public DownstreamMessageProperties( this.endpointName = Objects.requireNonNull(endpointName); this.resourceLimits = resourceLimits; - this.props = new HashMap<>(); Optional.ofNullable(tenantLevelDefaults).ifPresent(props::putAll); Optional.ofNullable(deviceLevelDefaults).ifPresent(props::putAll); diff --git a/core/src/main/java/org/eclipse/hono/config/AbstractConfig.java b/core/src/main/java/org/eclipse/hono/config/AbstractConfig.java index 879c23e651..2a0c244087 100644 --- a/core/src/main/java/org/eclipse/hono/config/AbstractConfig.java +++ b/core/src/main/java/org/eclipse/hono/config/AbstractConfig.java @@ -417,6 +417,11 @@ private KeyCertOptions createKeyCertOptions() { final FileFormat format = FileFormat.orDetect(this.keyFormat, this.keyStorePath); + if (format == null) { + LOG.warn("Unable to detect keystore file format for: {}", keyStorePath); + return null; + } + // construct result switch (format) { diff --git a/core/src/main/java/org/eclipse/hono/util/CredentialsObject.java b/core/src/main/java/org/eclipse/hono/util/CredentialsObject.java index cbe80306ff..d19991149c 100644 --- a/core/src/main/java/org/eclipse/hono/util/CredentialsObject.java +++ b/core/src/main/java/org/eclipse/hono/util/CredentialsObject.java @@ -287,12 +287,17 @@ public void checkSecrets(final BiConsumer secretValidator) { } else { + final var credentialsType = getType(); + if (credentialsType == null) { + throw new IllegalStateException("credentials object must have a type"); + } + try { - switch (getType()) { + switch (credentialsType) { case CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD: checkSecrets(secrets, secret -> { checkHashedPassword(secret); - secretValidator.accept(getType(), secret); + secretValidator.accept(credentialsType, secret); }); break; default: diff --git a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/Statement.java b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/Statement.java index 9b0f14d390..8e3a05efdd 100644 --- a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/Statement.java +++ b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/store/Statement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020 Contributors to the Eclipse Foundation + * Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -173,7 +173,7 @@ public String toString() { * @param sql The SQL statement to process. This is a formatted string according to * {@link String#format(String, Object...)}. * @param values The values to replace in the parameter {@code sql}. - * @return The statement, or {@code null} if the provided SQL as {@code null}. + * @return The statement, or {@code null} if the provided SQL is {@code null}. */ public static Statement statement(final String sql, final Object... values) { if (sql == null) { diff --git a/services/device-registry-jdbc/src/main/java/org/eclipse/hono/deviceregistry/jdbc/impl/ClasspathSchemaCreator.java b/services/device-registry-jdbc/src/main/java/org/eclipse/hono/deviceregistry/jdbc/impl/ClasspathSchemaCreator.java index fe64b72569..fb442daa4c 100644 --- a/services/device-registry-jdbc/src/main/java/org/eclipse/hono/deviceregistry/jdbc/impl/ClasspathSchemaCreator.java +++ b/services/device-registry-jdbc/src/main/java/org/eclipse/hono/deviceregistry/jdbc/impl/ClasspathSchemaCreator.java @@ -107,10 +107,16 @@ private Future runScript(final JdbcProperties jdbcProperties, final String SQL.runTransactionally(jdbcClient, tracer, ctx, (connection, context) -> { final var expanded = Statement.statement(script).expand(); - log.debug("Creating database schema in [{}] with script: {}", jdbcProperties.getUrl(), expanded); - return expanded - .query(jdbcClient) - .recover(SQL::translateException); + if (expanded == null) { + log.warn("cannot create database schema in [{}]: script can not be expanded to SQL statement", + jdbcProperties.getUrl()); + return Future.failedFuture("cannot create database schema using script"); + } else { + log.debug("creating database schema in [{}] using script: {}", jdbcProperties.getUrl(), expanded); + return expanded + .query(jdbcClient) + .recover(SQL::translateException); + } }) .onComplete(ar -> jdbcClient.close(clientCloseTracker)); return clientCloseTracker.future();