Skip to content

Commit

Permalink
create log/report utility object
Browse files Browse the repository at this point in the history
  • Loading branch information
rjpmestre committed Oct 25, 2024
1 parent 137544a commit 1dbcc5c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
49 changes: 49 additions & 0 deletions java/code/src/com/redhat/rhn/common/ErrorReportingStrategies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.redhat.rhn.common;

import static com.redhat.rhn.common.ExceptionMessage.NOT_INSTANTIABLE;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.Supplier;

public class ErrorReportingStrategies {

private ErrorReportingStrategies() {
throw new UnsupportedOperationException(NOT_INSTANTIABLE);
}

private static final Map<Object, Logger> OBJ_LOGGER = Collections.synchronizedMap(new WeakHashMap<>());

/**
* Raise and log an exception
* @param obj Object to log
* @param message Message to log
* @return Supplier of RhnRuntimeException that logs the message and throw the exception
*/
public static Supplier<RhnRuntimeException> raiseAndLog(Object obj, String message) {
Logger logger = OBJ_LOGGER.computeIfAbsent(obj, (key) -> LogManager.getLogger(obj.getClass().getName()));
return () -> {
logger.error(message);
return new RhnRuntimeException(message);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.redhat.rhn.manager.system.proxycontainerconfig;

import static com.redhat.rhn.common.ErrorReportingStrategies.raiseAndLog;
import static com.suse.utils.Predicates.allProvided;
import static com.suse.utils.Predicates.isAbsent;

Expand Down Expand Up @@ -46,7 +47,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
Expand All @@ -63,10 +63,10 @@ public void handle(ProxyContainerConfigCreateContext context) {

// Generate SSH keys for proxy
MgrUtilRunner.SshKeygenResult proxySshKey = saltApi.generateSSHKey(null, null)
.orElseThrow(raiseAndLog("Could not generate proxy salt-ssh SSH keys."));
.orElseThrow(raiseAndLog(this, "Could not generate proxy salt-ssh SSH keys."));

if (!(proxySshKey.getReturnCode() == 0 || proxySshKey.getReturnCode() == -1)) {
throw raiseAndLog("Generating proxy salt-ssh SSH keys failed: " + proxySshKey.getStderr()).get();
raiseAndLog(this, "Generating proxy salt-ssh SSH keys failed: " + proxySshKey.getStderr()).get();
}

context.setProxySshKey(proxySshKey);
Expand Down Expand Up @@ -107,7 +107,7 @@ public void handle(ProxyContainerConfigCreateContext context) {
context.setClientCertificate(SystemManager.createClientCertificate(proxySystem));
}
catch (InstantiationException e) {
throw raiseAndLog("Failed creating client certificate: " + e.getMessage()).get();
raiseAndLog(this, "Failed creating client certificate: " + e.getMessage()).get();
}

// Check the SSL files using mgr-ssl-cert-setup
Expand Down Expand Up @@ -207,44 +207,33 @@ private Server getOrCreateProxySystem(
private String getServerSshPublicKey(User user, String serverFqdn) {
String localManagerFqdn = Config.get().getString(ConfigDefaults.SERVER_HOSTNAME);
if (isAbsent(localManagerFqdn)) {
throw raiseAndLog("Could not determine the local SUSE Manager FQDN.").get();
raiseAndLog(this, "Could not determine the local SUSE Manager FQDN.").get();
}

if (localManagerFqdn.equals(serverFqdn)) {
MgrUtilRunner.SshKeygenResult serverSshKey =
saltApi.generateSSHKey(SaltSSHService.SSH_KEY_PATH, SaltSSHService.SUMA_SSH_PUB_KEY)
.orElseThrow(raiseAndLog("Could not generate salt-ssh public key."));
.orElseThrow(raiseAndLog(this, "Could not generate salt-ssh public key."));

if (!(serverSshKey.getReturnCode() == 0 || serverSshKey.getReturnCode() == -1)) {
throw raiseAndLog("Generating salt-ssh public key failed: " + serverSshKey.getStderr()).get();
raiseAndLog(this, "Generating salt-ssh public key failed: " + serverSshKey.getStderr()).get();
}

return serverSshKey.getPublicKey();
}

Server serverServer = ServerFactory.lookupProxiesByOrg(user).stream()
.filter(proxy -> serverFqdn.equals(proxy.getName())).findFirst()
.orElseThrow(raiseAndLog("Could not find specified server named " + serverFqdn +
.orElseThrow(raiseAndLog(this, "Could not find specified server named " + serverFqdn +
" in the organization."));

if (!allProvided(serverServer.getProxyInfo(), serverServer.getProxyInfo().getSshPublicKey())) {
throw raiseAndLog("Could not find the public SSH key for the server.").get();
raiseAndLog(this, "Could not find the public SSH key for the server.").get();
}

return new String(serverServer.getProxyInfo().getSshPublicKey());
}

/**
* Raises and logs an exception
*
* @param message exception message
* @return exception supplier
*/
private Supplier<RhnRuntimeException> raiseAndLog(String message) {
LOG.error(message);
return () -> new RhnRuntimeException(message);
}

private Optional<Server> findByAnyFqdn(Set<String> fqdns) {
for (String fqdn : fqdns) {
Optional<Server> server = ServerFactory.findByFqdn(fqdn);
Expand Down

0 comments on commit 1dbcc5c

Please sign in to comment.