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

Simplified Proxy Onboarding #9703

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 SUSE LLC
* Copyright (c) 2024--2025 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
Expand All @@ -25,13 +25,35 @@
import java.util.WeakHashMap;
import java.util.function.Supplier;

/**
* This class provides various strategies for error reporting and logging.
* Base validation reporting strategy throws a {@link RhnGeneralException} if there are errors.
*/
public class ErrorReportingStrategies {

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

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

static {
VALIDATION_REPORT_STRATEGY = errors -> {
if (!errors.isEmpty()) {
throw new RhnGeneralException(errors);
}
};
}


/**
* Returns a default validation reporting strategy
* @return RhnReportStrategy
*/
public static RhnReportStrategy<RhnError> validationReportingStrategy() {
return VALIDATION_REPORT_STRATEGY;
}

/**
* Raise and log an exception
Expand Down
37 changes: 37 additions & 0 deletions java/code/src/com/redhat/rhn/common/RhnError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2025 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 java.io.Serializable;

/**
* Represents a generic error in scope of RHN
*/
public class RhnError implements Serializable {
private final String message;

/**
* Constructor
* @param messageIn the error message
*/
public RhnError(String messageIn) {
this.message = messageIn;
}

public String getMessage() {
return message;
}
}
69 changes: 69 additions & 0 deletions java/code/src/com/redhat/rhn/common/RhnErrorReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2025 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 java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Represents a collection of errors that can be reported.
*/
public class RhnErrorReport {
private final List<RhnError> errors = Collections.synchronizedList(new ArrayList<>());

/**
* Registers a new error in the error report.
*
* @param message The error message.
*/
public void register(String message) {
errors.add(new RhnError(message));
}

/**
* Checks if any errors have been registered.
*
* @return true if there are errors; false otherwise.
*/
public boolean hasErrors() {
return !errors.isEmpty();
}

/**
* Returns a copy of the current list of errors.
*
* @return A copy of the errors list.
*/
public List<RhnError> getErrors() {
return new ArrayList<>(errors);
}

/**
* Logs the errors following a RhnReportStrategy.
* @param strategy The reporting strategy.
*/
public void report(RhnReportStrategy<RhnError> strategy) {
strategy.report(errors);
}

/**
* Logs the errors using the default validation reporting strategy.
*/
public void report() {
ErrorReportingStrategies.validationReportingStrategy().report(errors);
}
}
47 changes: 47 additions & 0 deletions java/code/src/com/redhat/rhn/common/RhnGeneralException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2025 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 java.io.Serializable;
import java.util.List;

/**
* Represents a RHN general exception
*/
public class RhnGeneralException extends RuntimeException implements Serializable {
private final List<RhnError> errors;

/**
* Constructor with a list of errors
* @param errorsIn the list of errors
*/
public RhnGeneralException(List<RhnError> errorsIn) {
this.errors = errorsIn;
}

public List<RhnError> getErrors() {
return errors;
}

/**
* Returns all error messages as a string array
* @return String array of error messages
*/
public String[] getErrorMessages() {
return errors.stream().map(RhnError::getMessage).toList().toArray(new String[0]);
}

}
30 changes: 30 additions & 0 deletions java/code/src/com/redhat/rhn/common/RhnReportStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2025 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 java.util.List;

/**
* Interface for strategy reporting errors
* @param <E> the type of errors
*/
public interface RhnReportStrategy<E> {

/**
* Report a list of errors
* @param errors the list of errors
*/
void report(List<E> errors);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,7 @@ SELECT 1
WHEN 'monitoring_entitled' then 'Monitoring'
WHEN 'ansible_control_node' then 'Ansible Control Node'
WHEN 'peripheral_server' then 'Peripheral Server'
WHEN 'proxy_entitled' then 'Proxy'
END)
</query>
</write-mode>
Expand Down
25 changes: 25 additions & 0 deletions java/code/src/com/redhat/rhn/common/security/acl/Access.java
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,29 @@ public boolean aclIsPeripheral(Map<String, Object> ctx, String[] params) {
HubFactory factory = new HubFactory();
return factory.isISSPeripheral();
}

/**
* Uses the sid param to decide if a system is a proxy
* @param ctx Context Map to pass in
* @param params Parameters to use (unused)
* @return true if a system is a proxy, false otherwise
*/
public boolean aclSystemIsConvertibleToProxy(Map<String, Object> ctx, String[] params) {
Long sid = getAsLong(ctx.get("sid"));
User user = (User) ctx.get("user");
Server lookedUp = SystemManager.lookupByIdAndUser(sid, user);

return lookedUp.isConvertibleToProxy();
}

/**
* Checks is server has a proxy entitlement
*
* @param ctx Context map to pass in.
* @param params Parameters to use to fetch from context.
* @return True if system has proxy entitlement, false otherwise.
*/
public boolean aclSystemHasProxyEntitlement(Map<String, Object> ctx, String[] params) {
return SystemManager.serverHasProxyEntitlement(getAsLong(ctx.get("sid")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2025 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.test;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.redhat.rhn.common.ErrorReportingStrategies;
import com.redhat.rhn.common.RhnError;
import com.redhat.rhn.common.RhnGeneralException;
import com.redhat.rhn.common.RhnReportStrategy;
import com.redhat.rhn.common.RhnRuntimeException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.Supplier;

public class ErrorReportingStrategiesTest {

@Test
public void testValidationReportingStrategyThrowsExceptionOnErrors() {
RhnReportStrategy<RhnError> strategy = ErrorReportingStrategies.validationReportingStrategy();
List<RhnError> errors = List.of(new RhnError("Test error"));

assertThrows(RhnGeneralException.class, () -> strategy.report(errors));
}

@Test
public void testRaiseAndLog() {
String testMessage = "Test error message";
Supplier<RhnRuntimeException> exceptionSupplier = ErrorReportingStrategies.raiseAndLog(this, testMessage);

RhnRuntimeException exception = exceptionSupplier.get();
assertTrue(exception.getMessage().contains(testMessage));
}

@Test
public void testRaiseAndLogLogsMessage() {
String testMessage = "Test error message";
Supplier<RhnRuntimeException> exceptionSupplier = ErrorReportingStrategies.raiseAndLog(this, testMessage);

// Capture the log output
Logger logger = LogManager.getLogger(this.getClass().getName());
logger.error(testMessage);

RhnRuntimeException exception = exceptionSupplier.get();
assertTrue(exception.getMessage().contains(testMessage));
}
}
Loading
Loading