Skip to content

Commit

Permalink
Add SCAP policy create and list pages
Browse files Browse the repository at this point in the history
  • Loading branch information
admd committed Nov 27, 2024
1 parent dc050ef commit 69aaf35
Show file tree
Hide file tree
Showing 14 changed files with 1,137 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package com.redhat.rhn.common.hibernate;

import com.redhat.rhn.domain.audit.ScapPolicy;
import com.redhat.rhn.domain.channel.AppStream;
import com.redhat.rhn.domain.channel.AppStreamApi;
import com.redhat.rhn.domain.channel.ChannelSyncFlag;
Expand Down Expand Up @@ -206,7 +207,8 @@ private AnnotationRegistry() {
AppStreamApi.class,
TokenChannelAppStream.class,
PaygDimensionResult.class,
TailoringFile.class
TailoringFile.class,
ScapPolicy.class
);

/**
Expand Down
63 changes: 63 additions & 0 deletions java/code/src/com/redhat/rhn/domain/audit/ScapFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,69 @@ public static void saveTailoringFile(TailoringFile tailoringFile) {
singleton.saveObject(tailoringFile);
}

/**
* List all SCAP polices objects in the database
* @param org the organization
* @return Returns a list of tailoring files
*/
public static List<ScapPolicy> listScapPolicies(Org org) {
CriteriaBuilder builder = getSession().getCriteriaBuilder();
CriteriaQuery<ScapPolicy> criteria = builder.createQuery(ScapPolicy.class);
Root<ScapPolicy> root = criteria.from(ScapPolicy.class);
criteria.where(builder.equal(root.get("org"), org));
return getSession().createQuery(criteria).getResultList();
}

/**
* Lookup for Scap policies by an id list and organization
* @param ids image profile id list
* @param org the organization
* @return Returns a list of tailoring files
* inside the organization
*/
public static List<ScapPolicy> lookupScapPoliciesByIds(List<Integer> ids, Org org) {
CriteriaBuilder builder = getSession().getCriteriaBuilder();
CriteriaQuery<ScapPolicy> criteria = builder.createQuery(ScapPolicy.class);
Root<ScapPolicy> root = criteria.from(ScapPolicy.class);
criteria.where(builder.and(
root.get("id").in(ids),
builder.equal(root.get("org"), org)));
return getSession().createQuery(criteria).getResultList();
}
/**
* Lookup for a tailoring file object based on the id and organization
* @param id tailoring file ID
* @param org the organization
* @return optional of tailoring file object
*/
public static Optional<ScapPolicy> lookupScapPolicyByIdAndOrg(Integer id, Org org) {

if (Objects.isNull(id)) {
return Optional.empty();
}
CriteriaBuilder builder = getSession().getCriteriaBuilder();
CriteriaQuery<ScapPolicy> select = builder.createQuery(ScapPolicy.class);
Root<ScapPolicy> root = select.from(ScapPolicy.class);
select.where(builder.and(
builder.equal(root.get("id"), id),
builder.equal(root.get("org"), org)));
return getSession().createQuery(select).uniqueResultOptional();
}
/**
* Deletes the Scap Policy object from the database
* @param scapPolicy ScapPolicy object
*/
public static void deleteScapPolicy(ScapPolicy scapPolicy) {
getSession().delete(scapPolicy);
}
/**
* Save the scapPolicy object to the database
* @param scapPolicy object
*/
public static void saveScapPolicy(ScapPolicy scapPolicy) {
scapPolicy.setModified(new Date());
singleton.saveObject(scapPolicy);
}
/**
* Get the Logger for the derived class so log messages
* show up on the correct class.
Expand Down
212 changes: 212 additions & 0 deletions java/code/src/com/redhat/rhn/domain/audit/ScapPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package com.redhat.rhn.domain.audit;


import com.google.gson.annotations.Expose;
import com.redhat.rhn.domain.BaseDomainHelper;
import com.redhat.rhn.domain.org.Org;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name = "suseScapPolicy")
public class ScapPolicy extends BaseDomainHelper {

// Unique identifier for the SCAP policy
private Integer id;

// The name of the SCAP policy
@Expose
private String policyName;

// The name of the data stream associated with the SCAP policy
@Expose
private String dataStreamName;

// The XCCDF profile ID used by this SCAP policy
private String xccdfProfileId;

// Reference to the TailoringFile associated with the SCAP policy (optional)
private TailoringFile tailoringFile;

// The profile ID used for tailoring the SCAP policy
private String tailoringProfileId;

// The organization (Org) to which this policy belongs
private Org org;

/**
* Default constructor for the ScapPolicy entity.
* This is required by JPA for entity instantiation.
*/
public ScapPolicy() {
// No-op constructor
}

/**
* Constructor for creating a ScapPolicy with the essential details.
* @param policyName the name of the policy
* @param dataStreamName the name of the data stream
* @param xccdfProfileId the XCCDF profile ID
*/
public ScapPolicy(String policyName, String dataStreamName, String xccdfProfileId) {
this.policyName = policyName;
this.dataStreamName = dataStreamName;
this.xccdfProfileId = xccdfProfileId;
}

/**
* Get the unique identifier for this SCAP policy.
* @return the unique identifier (ID)
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}

public void setId(Integer idIn) {
id = idIn;
}

/**
* Get the name of this SCAP policy.
* @return the name of the policy
*/
@Column(name = "policy_name")
public String getPolicyName() {
return policyName;
}

/**
* Sets the name of the SCAP policy.
* @param policyName the policy name to set
*/
public void setPolicyName(String policyName) {
this.policyName = policyName;
}

/**
* Get the data stream name for this SCAP policy.
* @return the data stream name
*/
@Column(name = "data_stream_name")
public String getDataStreamName() {
return dataStreamName;
}

/**
* Set the data stream name for this SCAP policy.
* @param dataStreamName the data stream name to set
*/
public void setDataStreamName(String dataStreamName) {
this.dataStreamName = dataStreamName;
}

/**
* Get the XCCDF profile ID for this SCAP policy.
* @return the XCCDF profile ID
*/
@Column(name = "xccdf_profile_id")
public String getXccdfProfileId() {
return xccdfProfileId;
}

/**
* Set the XCCDF profile ID for this SCAP policy.
* @param xccdfProfileId the XCCDF profile ID to set
*/
public void setXccdfProfileId(String xccdfProfileId) {
this.xccdfProfileId = xccdfProfileId;
}

/**
* Get the TailoringFile associated with this SCAP policy.
* TailoringFile is optional, so it can be null.
* @return the TailoringFile, or null if not set
*/
@ManyToOne
@JoinColumn(name = "tailoring_file") // Ensuring the TailoringFile relationship is properly joined
public TailoringFile getTailoringFile() {
return tailoringFile;
}

/**
* Set the TailoringFile for this SCAP policy.
* @param tailoringFile the TailoringFile to associate with this policy
*/
public void setTailoringFile(TailoringFile tailoringFile) {
this.tailoringFile = tailoringFile;
}

/**
* Get the tailoring profile ID used for customizing this SCAP policy.
* @return the tailoring profile ID
*/
@Column(name = "tailoring_profile_id")
public String getTailoringProfileId() {
return tailoringProfileId;
}

/**
* Set the tailoring profile ID for this SCAP policy.
* @param tailoringProfileId the tailoring profile ID to set
*/
public void setTailoringProfileId(String tailoringProfileId) {
this.tailoringProfileId = tailoringProfileId;
}

/**
* Get the organization (Org) associated with this SCAP policy.
* @return the organization
*/
@ManyToOne
@JoinColumn(name = "org_id") // Ensuring the Org relationship is properly joined
public Org getOrg() {
return org;
}

/**
* Set the organization (Org) for this SCAP policy.
* @param org the Org to set
*/
public void setOrg(Org org) {
this.org = org;
}

/**
* Compares this SCAP policy with another object for equality.
* Two SCAP policies are considered equal if their policy name, data stream name,
* and XCCDF profile ID are the same.
* @param o the object to compare with
* @return true if this SCAP policy is equal to the provided object
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ScapPolicy that = (ScapPolicy) o;
return policyName.equals(that.policyName) && dataStreamName.equals(that.dataStreamName) && xccdfProfileId.equals(that.xccdfProfileId);
}

/**
* Generates a hash code for this SCAP policy based on policy name, data stream name,
* and XCCDF profile ID. This is important for using this object in hash-based collections.
* @return the hash code of this SCAP policy
*/
@Override
public int hashCode() {
return Objects.hash(policyName, dataStreamName, xccdfProfileId);
}

/**
* Returns a string representation of this SCAP policy.
* @return the string representation
*/
@Override
public String toString() {
return super.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@
<context context-type="sourcefile">Navigation Menu</context>
</context-group>
</trans-unit>
<trans-unit id="Scap Policies" xml:space="preserve">
<source>Scap Policies</source>
<context-group name="ctx">
<context context-type="sourcefile">Navigation Menu</context>
</context-group>
</trans-unit>
<trans-unit id="Tailoring Files" xml:space="preserve">
<source>Tailoring Files</source>
<context-group name="ctx">
Expand Down
Loading

0 comments on commit 69aaf35

Please sign in to comment.