Skip to content

Commit

Permalink
Add the recurring scap policy type
Browse files Browse the repository at this point in the history
  • Loading branch information
admd committed Nov 29, 2024
1 parent bafb1f3 commit f5042e7
Show file tree
Hide file tree
Showing 13 changed files with 715 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.redhat.rhn.domain.recurringactions.state.RecurringConfigChannel;
import com.redhat.rhn.domain.recurringactions.state.RecurringInternalState;
import com.redhat.rhn.domain.recurringactions.type.RecurringHighstate;
import com.redhat.rhn.domain.recurringactions.type.RecurringScapPolicy;
import com.redhat.rhn.domain.recurringactions.type.RecurringState;
import com.redhat.rhn.domain.rhnpackage.PackageBreaks;
import com.redhat.rhn.domain.rhnpackage.PackageConflicts;
Expand Down Expand Up @@ -208,7 +209,8 @@ private AnnotationRegistry() {
TokenChannelAppStream.class,
PaygDimensionResult.class,
TailoringFile.class,
ScapPolicy.class
ScapPolicy.class,
RecurringScapPolicy.class
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public abstract class RecurringActionType {

public enum ActionType {
HIGHSTATE("Highstate"),
CUSTOMSTATE("Custom state");
CUSTOMSTATE("Custom state"),
SCAPPOLICY("Scap Policy");
private final String description;
ActionType(String descriptionIn) {
this.description = descriptionIn;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2023 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.domain.recurringactions.type;

import com.redhat.rhn.domain.audit.ScapPolicy;
import org.hibernate.annotations.Type;

import javax.persistence.*;

/**
* Recurring Action type for scap policy implementation
*/

@Entity
@Table(name = "suseRecurringScapPolicy")
public class RecurringScapPolicy extends RecurringActionType {

private boolean testMode;
private ScapPolicy scapPolicy;

/**
* Standard constructor
*/
public RecurringScapPolicy() {
}

/**
* Constructor
*
* @param testModeIn if action is in testMode
*/
public RecurringScapPolicy(boolean testModeIn) {
super();
this.testMode = testModeIn;
}

/**
* Constructor
*
* @param scapPolicyIn the scap policy
* @param testModeIn if action is in testMode
*/
public RecurringScapPolicy(ScapPolicy scapPolicyIn, boolean testModeIn) {
super();
this.testMode = testModeIn;
this.scapPolicy = scapPolicyIn;
}

@Override
@Transient
public ActionType getActionType() {
return ActionType.SCAPPOLICY;
}

/**
* Gets if action is in testMode.
*
* @return testMode - if action is testMode
*/
@Column(name = "test_mode")
@Type(type = "yes_no")
public boolean isTestMode() {
return this.testMode;
}

/**
* Sets testMode.
*
* @param testModeIn - testMode
*/
public void setTestMode(boolean testModeIn) {
this.testMode = testModeIn;
}

/**
* Gets the related Scap policy
*
* @return the Scap Policy
*/
@ManyToOne
@JoinColumn(name = "scap_policy_id", nullable = false)
public ScapPolicy getScapPolicy() {
return this.scapPolicy;
}

/**
* Set the related Scap Policy
*
* @param scapPolicyIn the scap policy
*/
public void setScapPolicy(ScapPolicy scapPolicyIn) {
this.scapPolicy = scapPolicyIn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.redhat.rhn.domain.recurringactions.RecurringActionFactory;
import com.redhat.rhn.domain.recurringactions.type.RecurringActionType;
import com.redhat.rhn.domain.recurringactions.type.RecurringHighstate;
import com.redhat.rhn.domain.recurringactions.type.RecurringScapPolicy;
import com.redhat.rhn.domain.recurringactions.type.RecurringState;
import com.redhat.rhn.domain.role.RoleFactory;
import com.redhat.rhn.domain.server.MinionServer;
Expand Down Expand Up @@ -125,6 +126,8 @@ private static RecurringActionType createRecurringActionType(RecurringActionType
return new RecurringHighstate(false);
case CUSTOMSTATE:
return new RecurringState(false);
case SCAPPOLICY:
return new RecurringScapPolicy(false);
default:
throw new UnsupportedOperationException("type not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,30 @@
import static spark.Spark.get;
import static spark.Spark.post;

import com.google.gson.JsonObject;
import com.redhat.rhn.common.db.datasource.DataResult;
import com.redhat.rhn.common.hibernate.HibernateFactory;
import com.redhat.rhn.common.localization.LocalizationService;
import com.redhat.rhn.common.util.RecurringEventPicker;
import com.redhat.rhn.common.validator.ValidatorException;
import com.redhat.rhn.domain.action.Action;
import com.redhat.rhn.domain.action.ActionFactory;
import com.redhat.rhn.domain.audit.ScapFactory;
import com.redhat.rhn.domain.audit.ScapPolicy;
import com.redhat.rhn.domain.config.ConfigChannel;
import com.redhat.rhn.domain.recurringactions.RecurringAction;
import com.redhat.rhn.domain.recurringactions.RecurringAction.TargetType;
import com.redhat.rhn.domain.recurringactions.RecurringActionFactory;
import com.redhat.rhn.domain.recurringactions.state.RecurringStateConfig;
import com.redhat.rhn.domain.recurringactions.type.RecurringActionType;
import com.redhat.rhn.domain.recurringactions.type.RecurringHighstate;
import com.redhat.rhn.domain.recurringactions.type.RecurringScapPolicy;
import com.redhat.rhn.domain.recurringactions.type.RecurringState;
import com.redhat.rhn.domain.user.User;
import com.redhat.rhn.frontend.listview.PageControl;
import com.redhat.rhn.manager.action.ActionManager;
import com.redhat.rhn.manager.configuration.ConfigurationManager;
import com.redhat.rhn.manager.org.OrgManager;
import com.redhat.rhn.manager.recurringactions.RecurringActionManager;
import com.redhat.rhn.manager.recurringactions.StateConfigFactory;
import com.redhat.rhn.taskomatic.TaskomaticApi;
Expand Down Expand Up @@ -79,6 +84,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import spark.ModelAndView;
import spark.Request;
Expand Down Expand Up @@ -111,6 +117,7 @@ public static void initRoutes(JadeTemplateEngine jade) {
get("/manager/api/recurringactions/:id/details", asJson(withUser(RecurringActionController::getDetails)));
get("/manager/api/recurringactions/:type/:id", asJson(withUser(RecurringActionController::listByEntity)));
get("/manager/api/recurringactions/states", asJson(withUser(RecurringActionController::getStatesConfig)));
get("/manager/api/recurringactions/policies", asJson(withUser(RecurringActionController::listScapPolicies)));
post("/manager/api/recurringactions/save", asJson(withUser(RecurringActionController::save)));
post("/manager/api/recurringactions/custom/execute",
asJson(withUser(RecurringActionController::executeCustom)));
Expand Down Expand Up @@ -301,7 +308,33 @@ else if (RecurringActionType.ActionType.CUSTOMSTATE.equals(action.getActionType(
}
return dto;
}

/**
* Processes a GET request to get a list of all the scap policies
*
* @param req the request object
* @param res the response object
* @param user the authorized user
* @return the result JSON object
*/
public static String listScapPolicies(Request req, Response res, User user) {
Map<String, Object> data = new HashMap<>();
List<ScapPolicy> scapPolicies = ScapFactory.listScapPolicies(user.getOrg());
List<JsonObject> scapPoliciesJson = scapPolicies.stream()
.map(policy -> {
JsonObject json = new JsonObject();
json.addProperty("id", policy.getId());
json.addProperty("policyName", policy.getPolicyName());
json.addProperty("dataStreamName", policy.getDataStreamName());
json.addProperty("xccdfProfileId", policy.getXccdfProfileId());
json.addProperty("tailoringFileName", policy.getTailoringFile().getName());
json.addProperty("tailoringFileProfileId", policy.getTailoringProfileId());
return json;
})
.collect(Collectors.toList());
//data.put("tailoringFiles", Json.GSON.toJson(tailoringFiles.));
data.put("scapPolicies", scapPoliciesJson);
return json(res, scapPoliciesJson, new TypeToken<>() { });
}

/**
* Creates a new Recurring Action Schedule
Expand All @@ -319,7 +352,7 @@ public static String save(Request request, Response response, User user) {
try {
RecurringAction action = createOrGetAction(user, json);
HibernateFactory.getSession().evict(action); // entity -> detached, prevent hibernate flushes
mapJsonToAction(json, action);
mapJsonToAction(json, action, user);
RecurringActionManager.saveAndSchedule(action, user);
}
catch (ValidatorException e) {
Expand Down Expand Up @@ -437,7 +470,7 @@ private static Set<RecurringStateConfig> getStateConfigFromJson(Set<StateConfigJ
return stateConfig;
}

private static void mapJsonToAction(RecurringActionScheduleJson json, RecurringAction action) {
private static void mapJsonToAction(RecurringActionScheduleJson json, RecurringAction action, User user) {
action.setName(json.getScheduleName());
action.setActive(json.isActive());

Expand All @@ -456,6 +489,13 @@ else if (action.getRecurringActionType() instanceof RecurringState stateType) {
Set<RecurringStateConfig> newConfig = getStateConfigFromJson(details.getStates(), action.getCreator());
((RecurringState) action.getRecurringActionType()).saveStateConfig(newConfig);
}
} else if (action.getRecurringActionType() instanceof RecurringScapPolicy recurringScapPolicy) {

details.getPolicies()
.stream()
.findFirst()
.flatMap(policyJson -> ScapFactory.lookupScapPolicyByIdAndOrg(policyJson.getId(), user.getOrg()))
.ifPresent(recurringScapPolicy::setScapPolicy);
}

String cron = json.getCron();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ script(type='text/javascript').
window.csrfToken = "#{csrf_token}";
window.scapDataStreams = !{scapDataStreams};
window.tailoringFiles = !{tailoringFiles};
window.selectedPolicy = !{selectedPolicy};

script(type='text/javascript').
spaImportReactPage('audit/create-scap-policy')
Expand Down
Loading

0 comments on commit f5042e7

Please sign in to comment.