Skip to content

Commit

Permalink
IKC-410 Policy create, view, edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Belke authored and Piotr Belke committed Oct 14, 2024
1 parent 9a8f41b commit 3cd182a
Show file tree
Hide file tree
Showing 48 changed files with 1,184 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private static void setClusterConfigBrokers(ClusterConfig clusterConfig, Cluster
clusterDto.getBrokers().forEach(brokerDto -> {
BrokerConfig brokerConfig = new BrokerConfig();
String[] hostPort = brokerDto.getBootstrapServer().split(":");
//todo walidacja na FE na poprawność
brokerConfig.setHost(hostPort[0]);
brokerConfig.setPort(Integer.parseInt(hostPort[1]));
brokerConfig.setJmxUser(brokerDto.getJmxUser());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.consdata.kouncil.datamasking;

import com.consdata.kouncil.datamasking.dto.PolicyDto;
import com.consdata.kouncil.model.admin.SystemFunctionName.Fields;
import javax.annotation.security.RolesAllowed;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -16,6 +21,24 @@ public class PolicyController {

private final PolicyService policyService;

@RolesAllowed({Fields.POLICY_DETAILS, Fields.POLICY_UPDATE})
@GetMapping(path = "/{policyId}")
public PolicyDto getPolicyById(@PathVariable("policyId") Long id) {
return policyService.getPolicyById(id);
}

@RolesAllowed(Fields.POLICY_CREATE)
@PostMapping()
public void addNewPolicy(@RequestBody PolicyDto policyDto) {
policyService.savePolicy(policyDto);
}

@RolesAllowed(Fields.POLICY_UPDATE)
@PutMapping()
public void updatePolicy(@RequestBody PolicyDto policyDto) {
policyService.savePolicy(policyDto);
}

@RolesAllowed(Fields.POLICY_DELETE)
@DeleteMapping(path = "/{id}")
public void deletePolicy(@PathVariable("id") Long id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.consdata.kouncil.datamasking;

import com.consdata.kouncil.datamasking.converter.PolicyConverter;
import com.consdata.kouncil.datamasking.converter.PolicyDtoConverter;
import com.consdata.kouncil.datamasking.dto.PolicyDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -9,7 +12,16 @@ public class PolicyService {

private final PolicyRepository policyRepository;

public void savePolicy(PolicyDto policyDto) {
policyRepository.save(PolicyConverter.convert(policyDto));
}

public void deletePolicy(Long id) {
policyRepository.deleteById(id);
}

public PolicyDto getPolicyById(Long id) {
return policyRepository.findById(id).map(PolicyDtoConverter::convertToDto)
.orElseThrow(() -> new IllegalArgumentException(String.format("Policy with id=%s not found", id)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.consdata.kouncil.datamasking.converter;

import com.consdata.kouncil.datamasking.dto.PolicyDto;
import com.consdata.kouncil.model.datamasking.Policy;
import com.consdata.kouncil.model.datamasking.PolicyField;
import com.consdata.kouncil.model.datamasking.PolicyResource;
import java.util.HashSet;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class PolicyConverter {

public static Policy convert(PolicyDto policyDto) {
Policy policy = new Policy();
BeanUtils.copyProperties(policyDto, policy);

policy.setFields(new HashSet<>());

policyDto.getFields().forEach(field -> {
PolicyField policyField = new PolicyField();
BeanUtils.copyProperties(field, policyField);
policy.getFields().add(policyField);
});

policy.setResources(new HashSet<>());
policyDto.getResources().forEach(resource -> {
PolicyResource policyResource = new PolicyResource();
BeanUtils.copyProperties(resource, policyResource);
policy.getResources().add(policyResource);
});

return policy;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.consdata.kouncil.datamasking.converter;

import com.consdata.kouncil.datamasking.dto.PolicyDto;
import com.consdata.kouncil.datamasking.dto.PolicyFieldDto;
import com.consdata.kouncil.datamasking.dto.PolicyResourceDto;
import com.consdata.kouncil.model.datamasking.Policy;
import java.util.HashSet;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
Expand All @@ -12,6 +15,22 @@ public final class PolicyDtoConverter {
public static PolicyDto convertToDto(Policy policy) {
PolicyDto policyDto = new PolicyDto();
BeanUtils.copyProperties(policy, policyDto);

policyDto.setFields(new HashSet<>());
policy.getFields().forEach(field -> {
PolicyFieldDto policyFieldDto = new PolicyFieldDto();
BeanUtils.copyProperties(field, policyFieldDto);
policyDto.getFields().add(policyFieldDto);
});


policyDto.setResources(new HashSet<>());
policy.getResources().forEach(resource -> {
PolicyResourceDto policyResource = new PolicyResourceDto();
BeanUtils.copyProperties(resource, policyResource);
policyDto.getResources().add(policyResource);
});

return policyDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class PolicyDto {

private Long id;
private String name;
private MaskingType type;
private Set<String> fields;
private MaskingType maskingType;
private Boolean applyToAllResources;
private Set<PolicyFieldDto> fields;
private Set<PolicyResourceDto> resources;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.consdata.kouncil.datamasking.dto;

import com.consdata.kouncil.model.datamasking.FieldFindRule;
import lombok.Data;

@Data
public class PolicyFieldDto {

private Long id;
private FieldFindRule findRule;
private String field;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.consdata.kouncil.datamasking.dto;

import com.consdata.kouncil.clusters.dto.ClusterDto;
import lombok.Data;

@Data
public class PolicyResourceDto {

private Long id;
private ClusterDto cluster;
private Long cluster;
private String topic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.consdata.kouncil.model.datamasking;

public enum FieldFindRule {

ANY_LEVEL,
EXACT_PATH
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
Expand Down Expand Up @@ -36,12 +34,11 @@ public class Policy {

@Column(name = "MASKING_TYPE")
@Enumerated(EnumType.STRING)
private MaskingType type;
private MaskingType maskingType;

@ElementCollection
@CollectionTable(name = "POLICY_FIELDS", joinColumns = @JoinColumn(name = "POLICY_ID"))
@Column(name = "FIELD")
private Set<String> fields;
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "POLICY_ID")
private Set<PolicyField> fields;

@Column(name = "APPLY_TO_ALL_RESOURCES")
private Boolean applyToAllResources;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.consdata.kouncil.model.datamasking;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "POLICY_FIELD")
@Getter
@Setter
public class PolicyField {

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_POLICY_FIELD_GEN")
@SequenceGenerator(name = "SEQ_POLICY_FIELD_GEN", sequenceName = "SEQ_POLICY_FIELD", initialValue = 1, allocationSize = 1)
private Long id;

@Column(name = "FIELD")
private String field;

@Column(name = "FIND_RULE")
@Enumerated(EnumType.STRING)
private FieldFindRule findRule;

@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "POLICY_ID", insertable = false, updatable = false)
private Policy policy;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.consdata.kouncil.model.datamasking;

import com.consdata.kouncil.model.cluster.Cluster;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -26,9 +25,8 @@ public class PolicyResource {
@SequenceGenerator(name = "SEQ_POLICY_RESOURCE_GEN", sequenceName = "SEQ_POLICY_RESOURCE", initialValue = 1, allocationSize = 1)
private Long id;

@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "CLUSTER_ID", insertable = false, updatable = false)
private Cluster cluster;
@Column(name = "CLUSTER_ID")
private Long cluster;

@Column(name = "TOPIC")
private String topic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
create table policy
(
id bigint not null primary key,
name varchar(255),
masking_type varchar(255),
apply_to_all_resources boolean
id bigint not null primary key,
apply_to_all_resources boolean,
name varchar(255),
masking_type varchar(255)
);
create table policy_fields

create table policy_field
(
policy_id bigint not null
constraint fkq4ltt72s1qqgpolry1fnmy67b references policy,
field varchar(255)
id bigint not null primary key,
field varchar(255),
find_rule varchar(255),
policy_id bigint
constraint fkppv2kuelp29jag142kqgvober references policy
);

create table policy_resource
Expand All @@ -24,6 +27,7 @@ create table policy_resource

CREATE SEQUENCE SEQ_POLICY MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 10;
CREATE SEQUENCE SEQ_POLICY_RESOURCE MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 10;
CREATE SEQUENCE SEQ_POLICY_FIELD MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 10;

insert into system_function(id, name, label, function_group)
VALUES (nextval('SEQ_SYSTEM_FUNCTION'), 'POLICY_LIST', 'Policies list', 'DATA_MASKING'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.consdata.kouncil.datamasking.converter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import com.consdata.kouncil.datamasking.dto.PolicyDto;
import com.consdata.kouncil.datamasking.dto.PolicyFieldDto;
import com.consdata.kouncil.datamasking.dto.PolicyResourceDto;
import com.consdata.kouncil.model.datamasking.MaskingType;
import com.consdata.kouncil.model.datamasking.Policy;
import java.util.HashSet;
import org.junit.jupiter.api.Test;

class PolicyConverterTest {

@Test
void should_convert_to_entity() {
//given
PolicyDto policyDto = new PolicyDto();
policyDto.setId(1L);
policyDto.setName("test");
policyDto.setApplyToAllResources(false);
policyDto.setMaskingType(MaskingType.ALL);
policyDto.setFields(new HashSet<>());
policyDto.getFields().add(createField(1L));
policyDto.getFields().add(createField(2L));
policyDto.getFields().add(createField(3L));
policyDto.setResources(new HashSet<>());
policyDto.getResources().add(createResource(1L));
policyDto.getResources().add(createResource(2L));
//when
Policy policy = PolicyConverter.convert(policyDto);
//then
assertAll(
() -> assertThat(policy.getId()).isEqualTo(policyDto.getId()),
() -> assertThat(policy.getName()).isEqualTo(policyDto.getName()),
() -> assertThat(policy.getApplyToAllResources()).isEqualTo(policyDto.getApplyToAllResources()),
() -> assertThat(policy.getMaskingType()).isEqualTo(policyDto.getMaskingType()),
() -> assertThat(policy.getFields()).hasSize(policyDto.getFields().size()),
() -> assertThat(policy.getResources()).hasSize(policyDto.getResources().size())
);
}

private PolicyResourceDto createResource(long id) {
PolicyResourceDto policyResource = new PolicyResourceDto();
policyResource.setId(id);
return policyResource;
}

private PolicyFieldDto createField(long id) {
PolicyFieldDto policyField = new PolicyFieldDto();
policyField.setId(id);
return policyField;
}
}
Loading

0 comments on commit 3cd182a

Please sign in to comment.