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

Refactor and unite structure of platform enums #375

Merged
merged 4 commits into from
Apr 19, 2023
Merged
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
Expand Up @@ -10,7 +10,6 @@
import com.czertainly.api.model.core.certificate.*;
import com.czertainly.api.model.core.location.LocationDto;
import com.czertainly.api.model.core.search.SearchFieldDataByGroupDto;
import com.czertainly.api.model.core.search.SearchFieldDataDto;
import com.czertainly.api.model.core.v2.ClientCertificateRequestDto;
import com.czertainly.core.dao.entity.Certificate;
import com.czertainly.core.security.authz.SecuredUUID;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/czertainly/core/api/web/EnumControllerImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.czertainly.core.api.web;

import com.czertainly.api.interfaces.core.web.EnumController;
import com.czertainly.api.model.common.enums.IPlatformEnum;
import com.czertainly.api.model.common.enums.PlatformEnum;
import com.czertainly.api.model.core.enums.EnumItemDto;
import com.czertainly.core.service.EnumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class EnumControllerImpl implements EnumController {

private EnumService enumService;

@Autowired
public void setEnumService(EnumService enumService) {
this.enumService = enumService;
}


@Override
public Map<PlatformEnum, Map<String, EnumItemDto>> getPlatformEnums() {
return enumService.getPlatformEnums();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.czertainly.api.interfaces.core.web.SettingController;
import com.czertainly.api.model.core.settings.PlatformSettingsDto;
import com.czertainly.api.model.core.settings.Section;
import com.czertainly.api.model.core.settings.SettingsSection;
import com.czertainly.core.service.SettingService;
import com.czertainly.core.util.converter.SectionCodeConverter;
import com.czertainly.core.util.converter.SettingsSectionCodeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
Expand All @@ -22,7 +22,7 @@ public void setSettingService(SettingService settingService) {

@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(Section.class, new SectionCodeConverter());
webdataBinder.registerCustomEditor(SettingsSection.class, new SettingsSectionCodeConverter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void setUsage(List<KeyUsage> usage) {
",",
usage.stream().map(
i -> String.valueOf(
i.getId()
i.getBitmask()
)
).collect(
Collectors.toList()
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/czertainly/core/dao/entity/Setting.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.czertainly.core.dao.entity;

import com.czertainly.api.model.core.settings.Section;
import com.czertainly.api.model.core.settings.SettingsSection;
import jakarta.persistence.*;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
Expand All @@ -13,7 +13,7 @@ public class Setting extends UniquelyIdentifiedAndAudited {

@Column(name = "section", nullable = false)
@Enumerated(EnumType.STRING)
private Section section;
private SettingsSection section;

@Column(name = "category")
private String category;
Expand All @@ -24,11 +24,11 @@ public class Setting extends UniquelyIdentifiedAndAudited {
@Column(name = "value", length = Integer.MAX_VALUE)
private String value;

public Section getSection() {
public SettingsSection getSection() {
return section;
}

public void setSection(Section section) {
public void setSection(SettingsSection section) {
this.section = section;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void setUsage(List<KeyUsage> usage) {
",",
usage.stream().map(
i -> String.valueOf(
i.getId()
i.getBitmask()
)
).collect(
Collectors.toList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.czertainly.core.dao.repository;

import com.czertainly.api.model.core.settings.Section;
import com.czertainly.api.model.core.settings.SettingsSection;
import com.czertainly.core.dao.entity.Setting;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Repository;
Expand All @@ -15,7 +15,7 @@ public interface SettingRepository extends SecurityFilterRepository<Setting, UUI

Optional<Setting> findByUuid(UUID uuid);

List<Setting> findBySection(Section section);
List<Setting> findBySection(SettingsSection section);

Optional<Setting> findBySectionAndName(Section section, String name);
Optional<Setting> findBySectionAndName(SettingsSection section, String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.czertainly.api.model.core.certificate.*;
import com.czertainly.api.model.core.location.LocationDto;
import com.czertainly.api.model.core.search.SearchFieldDataByGroupDto;
import com.czertainly.api.model.core.search.SearchFieldDataDto;
import com.czertainly.core.dao.entity.Certificate;
import com.czertainly.core.dao.entity.RaProfile;
import com.czertainly.core.security.authz.SecuredUUID;
Expand All @@ -22,7 +21,6 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/czertainly/core/service/EnumService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.czertainly.core.service;

import com.czertainly.api.model.common.enums.PlatformEnum;
import com.czertainly.api.model.core.enums.EnumItemDto;

import java.util.Map;

public interface EnumService {

/**
* Get platform enums
* @return map of platform enums and their items
*/
Map<PlatformEnum, Map<String, EnumItemDto>> getPlatformEnums();

}
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ public List<SearchFieldDataByGroupDto> getSearchableFieldInformationByGroup() {

final List<SearchFieldObject> metadataSearchFieldObject = getSearchFieldObjectForMetadata();
if (metadataSearchFieldObject.size() > 0) {
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(metadataSearchFieldObject), SearchGroup.META.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(metadataSearchFieldObject), SearchGroup.META));
}

final List<SearchFieldObject> customAttrSearchFieldObject = getSearchFieldObjectForCustomAttributes();
if (customAttrSearchFieldObject.size() > 0) {
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(customAttrSearchFieldObject), SearchGroup.CUSTOM.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(customAttrSearchFieldObject), SearchGroup.CUSTOM));
}

List<SearchFieldDataDto> fields = List.of(
Expand Down Expand Up @@ -409,7 +409,7 @@ public List<SearchFieldDataByGroupDto> getSearchableFieldInformationByGroup() {
fields = fields.stream().collect(Collectors.toList());
fields.sort(new SearchFieldDataComparator());

searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(fields, SearchGroup.PROPERTY.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(fields, SearchGroup.PROPERTY));

logger.debug("Searchable Fields by Groups: {}", searchFieldDataByGroupDtos);
return searchFieldDataByGroupDtos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ private void updateKeyUsages(UUID uuid, List<KeyUsage> usages, boolean evaluateT
usages = new ArrayList<>(usages);
if (!new HashSet<>(List.of(PERMITTED_USAGES.get(content.getType()))).containsAll(usages)) {
usages.removeAll(List.of(PERMITTED_USAGES.get(content.getType())));
String nonAllowedUsages = String.join(", ", usages.stream().map(KeyUsage::getName).collect(Collectors.toList()));
String nonAllowedUsages = String.join(", ", usages.stream().map(KeyUsage::getCode).collect(Collectors.toList()));
keyEventHistoryService.addEventHistory(KeyEvent.UPDATE_USAGE, KeyEventStatus.FAILED,
"Unsupported Key usages: " + nonAllowedUsages, null, content);
throw new ValidationException(
Expand All @@ -1236,10 +1236,10 @@ private void updateKeyUsages(UUID uuid, List<KeyUsage> usages, boolean evaluateT
)
);
}
String oldUsage = String.join(", ", content.getUsage().stream().map(KeyUsage::getName).collect(Collectors.toList()));
String oldUsage = String.join(", ", content.getUsage().stream().map(KeyUsage::getCode).collect(Collectors.toList()));
content.setUsage(usages);
cryptographicKeyItemRepository.save(content);
String newUsage = String.join(", ", usages.stream().map(KeyUsage::getName).collect(Collectors.toList()));
String newUsage = String.join(", ", usages.stream().map(KeyUsage::getCode).collect(Collectors.toList()));
keyEventHistoryService.addEventHistory(KeyEvent.UPDATE_USAGE, KeyEventStatus.SUCCESS,
"Update Key Usage from " + oldUsage + " to " + newUsage, null, content);
}
Expand Down Expand Up @@ -1296,30 +1296,30 @@ private List<SearchFieldDataByGroupDto> getSearchableFieldsMap() {

final List<SearchFieldObject> metadataSearchFieldObject = getSearchFieldObjectForMetadata();
if (metadataSearchFieldObject.size() > 0) {
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(metadataSearchFieldObject), SearchGroup.META.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(metadataSearchFieldObject), SearchGroup.META));
}

final List<SearchFieldObject> customAttrSearchFieldObject = getSearchFieldObjectForCustomAttributes();
if (customAttrSearchFieldObject.size() > 0) {
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(customAttrSearchFieldObject), SearchGroup.CUSTOM.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(customAttrSearchFieldObject), SearchGroup.CUSTOM));
}

List<SearchFieldDataDto> fields = List.of(
SearchHelper.prepareSearch(SearchFieldNameEnum.NAME),
SearchHelper.prepareSearch(SearchFieldNameEnum.CK_GROUP, groupRepository.findAll().stream().map(Group::getName).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.CK_OWNER),
SearchHelper.prepareSearch(SearchFieldNameEnum.CK_KEY_USAGE, Arrays.stream((KeyUsage.values())).map(KeyUsage::getName).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.CK_KEY_USAGE, Arrays.stream((KeyUsage.values())).map(KeyUsage::getCode).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_LENGTH),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_STATE, Arrays.stream((KeyState.values())).map(KeyState::getCode).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_FORMAT, Arrays.stream((KeyFormat.values())).map(KeyFormat::getName).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_TYPE, Arrays.stream((KeyType.values())).map(KeyType::getName).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_CRYPTOGRAPHIC_ALGORITHM, Arrays.stream((CryptographicAlgorithm.values())).map(CryptographicAlgorithm::getName).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_FORMAT, Arrays.stream((KeyFormat.values())).map(KeyFormat::getCode).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_TYPE, Arrays.stream((KeyType.values())).map(KeyType::getCode).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_CRYPTOGRAPHIC_ALGORITHM, Arrays.stream((CryptographicAlgorithm.values())).map(CryptographicAlgorithm::getCode).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_TOKEN_PROFILE, tokenProfileRepository.findAll().stream().map(TokenProfile::getName).collect(Collectors.toList())),
SearchHelper.prepareSearch(SearchFieldNameEnum.KEY_TOKEN_INSTANCE_LABEL, tokenInstanceReferenceRepository.findAll().stream().map(TokenInstanceReference::getName).collect(Collectors.toList()))
);
fields = fields.stream().collect(Collectors.toList());
fields.sort(new SearchFieldDataComparator());
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(fields, SearchGroup.PROPERTY.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(fields, SearchGroup.PROPERTY));

logger.debug("Searchable CryptographicKey Fields groups: {}", searchFieldDataByGroupDtos);
return searchFieldDataByGroupDtos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public String generateCsr(X500Principal principal, String key, CryptographicKeyI
// Build bouncy castle p10 builder
PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(
principal,
CsrUtil.publicKeyObjectFromString(key, publicKeyItem.getCryptographicAlgorithm().getName())
CsrUtil.publicKeyObjectFromString(key, publicKeyItem.getCryptographicAlgorithm().getCode())
);

// Assign the custom signer to sign the CSR with the private key from the cryptography provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,12 @@ public List<SearchFieldDataByGroupDto> getSearchableFieldInformationByGroup() {

final List<SearchFieldObject> metadataSearchFieldObject = getSearchFieldObjectForMetadata();
if (metadataSearchFieldObject.size() > 0) {
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(metadataSearchFieldObject), SearchGroup.META.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(metadataSearchFieldObject), SearchGroup.META));
}

final List<SearchFieldObject> customAttrSearchFieldObject = getSearchFieldObjectForCustomAttributes();
if (customAttrSearchFieldObject.size() > 0) {
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(customAttrSearchFieldObject), SearchGroup.CUSTOM.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(customAttrSearchFieldObject), SearchGroup.CUSTOM));
}

List<SearchFieldDataDto> fields = List.of(
Expand All @@ -480,7 +480,7 @@ public List<SearchFieldDataByGroupDto> getSearchableFieldInformationByGroup() {
fields = fields.stream().collect(Collectors.toList());
fields.sort(new SearchFieldDataComparator());

searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(fields, SearchGroup.PROPERTY.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(fields, SearchGroup.PROPERTY));

logger.debug("Searchable Fields by Groups: {}", searchFieldDataByGroupDtos);
return searchFieldDataByGroupDtos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,12 @@ public List<SearchFieldDataByGroupDto> getSearchableFieldInformationByGroup() {

final List<SearchFieldObject> metadataSearchFieldObject = getSearchFieldObjectForMetadata();
if (metadataSearchFieldObject.size() > 0) {
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(metadataSearchFieldObject), SearchGroup.META.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(metadataSearchFieldObject), SearchGroup.META));
}

final List<SearchFieldObject> customAttrSearchFieldObject = getSearchFieldObjectForCustomAttributes();
if (customAttrSearchFieldObject.size() > 0) {
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(customAttrSearchFieldObject), SearchGroup.CUSTOM.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(SearchHelper.prepareSearchForJSON(customAttrSearchFieldObject), SearchGroup.CUSTOM));
}

List<SearchFieldDataDto> fields = List.of(
Expand All @@ -345,7 +345,7 @@ public List<SearchFieldDataByGroupDto> getSearchableFieldInformationByGroup() {
fields = fields.stream().collect(Collectors.toList());
fields.sort(new SearchFieldDataComparator());

searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(fields, SearchGroup.PROPERTY.getLabel()));
searchFieldDataByGroupDtos.add(new SearchFieldDataByGroupDto(fields, SearchGroup.PROPERTY));

logger.debug("Searchable Fields by Groups: {}", searchFieldDataByGroupDtos);
return searchFieldDataByGroupDtos;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.czertainly.core.service.impl;

import com.czertainly.api.model.common.enums.IPlatformEnum;
import com.czertainly.api.model.common.enums.PlatformEnum;
import com.czertainly.api.model.core.enums.EnumItemDto;
import com.czertainly.core.service.EnumService;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Transactional
public class EnumServiceImpl implements EnumService {

private static final Logger logger = LoggerFactory.getLogger(EnumServiceImpl.class);

@Override
public Map<PlatformEnum, Map<String, EnumItemDto>> getPlatformEnums() {
Map<PlatformEnum, Map<String, EnumItemDto>> enumsMap = new HashMap<>();

for (PlatformEnum platformEnum: PlatformEnum.values()) {
Map<String, EnumItemDto> enumItemsMap = new HashMap<>();

IPlatformEnum[] enumConstants = platformEnum.getEnumClass().getEnumConstants();
for (IPlatformEnum enumConstant : enumConstants) {
EnumItemDto enumItem = new EnumItemDto();
enumItem.setCode(enumConstant.getCode());
enumItem.setLabel(enumConstant.getLabel());
enumItem.setDescription(enumConstant.getDescription());
enumItemsMap.put(enumConstant.getCode(), enumItem);
}
enumsMap.put(platformEnum, enumItemsMap);
}

return enumsMap;
}
}
Loading