From bb13d415c5205384df270baeda5c185747c2a1f9 Mon Sep 17 00:00:00 2001 From: Marco Fargetta Date: Wed, 27 Sep 2023 15:39:22 +0200 Subject: [PATCH] Add profile filter to the CLI The command ca-profile-find has 3 additional filters: - "--visible" shows only visible profiles (this is the default behaviour for non admin users); - "--enable" shows only the enabled profiles; - "--enableBy " shows the profiles enabled by the provided user. --- .../server/ca/rest/CertRequestService.java | 10 +++---- .../server/ca/rest/ProfileService.java | 30 ++++++++----------- .../com/netscape/cms/servlet/test/CATest.java | 2 +- .../certsrv/profile/ProfileClient.java | 5 +++- .../certsrv/profile/ProfileResource.java | 3 +- .../cmstools/profile/ProfileFindCLI.java | 18 ++++++++++- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/base/ca/src/main/java/org/dogtagpki/server/ca/rest/CertRequestService.java b/base/ca/src/main/java/org/dogtagpki/server/ca/rest/CertRequestService.java index 009666c54f0..ea286769b6b 100644 --- a/base/ca/src/main/java/org/dogtagpki/server/ca/rest/CertRequestService.java +++ b/base/ca/src/main/java/org/dogtagpki/server/ca/rest/CertRequestService.java @@ -265,7 +265,6 @@ public Response listEnrollmentTemplates(Integer start, Integer size) { } ProfileDataInfos infos = new ProfileDataInfos(); - boolean visibleOnly = true; Enumeration e = ps.getProfileIds(); if (e == null) return createOKResponse(infos); @@ -275,12 +274,13 @@ public Response listEnrollmentTemplates(Integer start, Integer size) { while (e.hasMoreElements()) { try { String id = e.nextElement(); - ProfileDataInfo info = ProfileService.createProfileDataInfo(id, visibleOnly, uriInfo, getLocale(headers)); - if (info == null) continue; + ProfileDataInfo info = ProfileService.createProfileDataInfo(id, uriInfo, getLocale(headers)); + if (info == null || !info.getProfileVisible().booleanValue()) { + continue; + } results.add(info); } catch (EBaseException ex) { - logger.warn("CertRequestService: " + ex.getMessage()); - continue; + logger.warn("CertRequestService: {}", ex.getMessage()); } } diff --git a/base/ca/src/main/java/org/dogtagpki/server/ca/rest/ProfileService.java b/base/ca/src/main/java/org/dogtagpki/server/ca/rest/ProfileService.java index ca1ed289989..43dcee4fb8d 100644 --- a/base/ca/src/main/java/org/dogtagpki/server/ca/rest/ProfileService.java +++ b/base/ca/src/main/java/org/dogtagpki/server/ca/rest/ProfileService.java @@ -110,7 +110,7 @@ public ProfileService() { } @Override - public Response listProfiles(Integer start, Integer size, Boolean visible, Boolean enable) { + public Response listProfiles(Integer start, Integer size, Boolean visible, Boolean enable, String enableBy) { start = start == null ? 0 : start; size = size == null ? DEFAULT_SIZE : size; @@ -145,15 +145,16 @@ public Response listProfiles(Integer start, Integer size, Boolean visible, Boole while (e.hasMoreElements()) { try { String id = e.nextElement(); - ProfileDataInfo info = createProfileDataInfo(id, visibleOnly, uriInfo, getLocale(headers)); - if (info == null) continue; - if (visible != null && info.getProfileVisible() != null && !info.getProfileVisible().equals(visible)) - continue; - if (enable != null && info.getProfileEnable() != null && !info.getProfileEnable().equals(enable)) + ProfileDataInfo info = createProfileDataInfo(id, uriInfo, getLocale(headers)); + if (info == null || + (visibleOnly && !info.getProfileVisible().booleanValue()) || + (visible != null && !visible.equals(info.getProfileVisible())) || + (enable != null && !enable.equals(info.getProfileEnable())) || + (enableBy != null && !enableBy.equals(info.getProfileEnableBy()))) continue; results.add(info); } catch (EBaseException ex) { - continue; + logger.warn("ProfileService: {}", ex.getMessage()); } } @@ -363,7 +364,7 @@ public static ProfileOutput createProfileOutput(Profile profile, String outputId return output; } - public static ProfileDataInfo createProfileDataInfo(String profileId, boolean visibleOnly, UriInfo uriInfo, + public static ProfileDataInfo createProfileDataInfo(String profileId, UriInfo uriInfo, Locale locale) throws EBaseException { CAEngine engine = CAEngine.getInstance(); @@ -381,21 +382,16 @@ public static ProfileDataInfo createProfileDataInfo(String profileId, boolean vi return null; } - if (visibleOnly && !profile.isVisible()) { - return null; - } - ret = new ProfileDataInfo(); ret.setProfileId(profileId); ret.setProfileName(profile.getName(locale)); ret.setProfileDescription(profile.getDescription(locale)); - if (!visibleOnly) { - ret.setProfileVisible(Boolean.valueOf(profile.isVisible())); - ret.setProfileEnable(Boolean.valueOf(profile.isEnable())); - ret.setProfileEnableBy(profile.getApprovedBy()); - } + ret.setProfileVisible(profile.isVisible()); + ret.setProfileEnable(profile.isEnable()); + ret.setProfileEnableBy(profile.getApprovedBy()); + UriBuilder profileBuilder = uriInfo.getBaseUriBuilder(); URI uri = profileBuilder.path(ProfileResource.class).path("{id}"). build(profileId); diff --git a/base/ca/src/test/java/com/netscape/cms/servlet/test/CATest.java b/base/ca/src/test/java/com/netscape/cms/servlet/test/CATest.java index 8919d81add5..5d75711014f 100644 --- a/base/ca/src/test/java/com/netscape/cms/servlet/test/CATest.java +++ b/base/ca/src/test/java/com/netscape/cms/servlet/test/CATest.java @@ -275,7 +275,7 @@ public static void main(String args[]) throws Exception { //Get a list of Profiles - ProfileDataInfos pInfos = profileClient.listProfiles(null, null); + ProfileDataInfos pInfos = profileClient.listProfiles(null, null, null, null, null); printProfileInfos(pInfos); diff --git a/base/common/src/main/java/com/netscape/certsrv/profile/ProfileClient.java b/base/common/src/main/java/com/netscape/certsrv/profile/ProfileClient.java index 935ae3c46c6..00784c496de 100644 --- a/base/common/src/main/java/com/netscape/certsrv/profile/ProfileClient.java +++ b/base/common/src/main/java/com/netscape/certsrv/profile/ProfileClient.java @@ -42,10 +42,13 @@ public byte[] retrieveProfileRaw(String id) throws Exception { return get(id + "/raw", byte[].class); } - public ProfileDataInfos listProfiles(Integer start, Integer size) throws Exception { + public ProfileDataInfos listProfiles(Integer start, Integer size, Boolean visible, Boolean enable, String enableBy) throws Exception { Map params = new HashMap<>(); if (start != null) params.put("start", start); if (size != null) params.put("size", size); + if (visible != null) params.put("visible", visible); + if (enable != null) params.put("enable", enable); + if (enableBy != null) params.put("enableBy", enableBy); return get(null, params, ProfileDataInfos.class); } diff --git a/base/common/src/main/java/com/netscape/certsrv/profile/ProfileResource.java b/base/common/src/main/java/com/netscape/certsrv/profile/ProfileResource.java index 33f5d42bf81..e56f5a4bf2b 100644 --- a/base/common/src/main/java/com/netscape/certsrv/profile/ProfileResource.java +++ b/base/common/src/main/java/com/netscape/certsrv/profile/ProfileResource.java @@ -22,7 +22,8 @@ public Response listProfiles( @QueryParam("start") Integer start, @QueryParam("size") Integer size, @QueryParam("visible") Boolean visible, - @QueryParam("enable") Boolean enable); + @QueryParam("enable") Boolean enable, + @QueryParam("enableBy") String enableBy); @GET @Path("{id}") diff --git a/base/tools/src/main/java/com/netscape/cmstools/profile/ProfileFindCLI.java b/base/tools/src/main/java/com/netscape/cmstools/profile/ProfileFindCLI.java index c1b0e69e798..e6b90d8fe6d 100644 --- a/base/tools/src/main/java/com/netscape/cmstools/profile/ProfileFindCLI.java +++ b/base/tools/src/main/java/com/netscape/cmstools/profile/ProfileFindCLI.java @@ -38,6 +38,18 @@ public void createOptions() { option = new Option(null, "size", true, "Page size"); option.setArgName("size"); options.addOption(option); + + option = new Option(null, "visible", true, "Profile with visible value"); + option.setArgName("true/false"); + options.addOption(option); + + option = new Option(null, "enable", true, "Profile with enable value"); + option.setArgName("true/false"); + options.addOption(option); + + option = new Option(null, "enableBy", true, "Only enabled by the user"); + option.setArgName("user"); + options.addOption(option); } @Override @@ -55,11 +67,15 @@ public void execute(CommandLine cmd) throws Exception { s = cmd.getOptionValue("size"); Integer size = s == null ? null : Integer.valueOf(s); + Boolean visible = cmd.hasOption("visible") ? Boolean.valueOf(cmd.getOptionValue("visible")) : null; + Boolean enable = cmd.hasOption("enable") ? Boolean.valueOf(cmd.getOptionValue("enable")) : null; + String enableBy = cmd.getOptionValue("enableBy"); + MainCLI mainCLI = (MainCLI) getRoot(); mainCLI.init(); ProfileClient profileClient = profileCLI.getProfileClient(); - ProfileDataInfos response = profileClient.listProfiles(start, size); + ProfileDataInfos response = profileClient.listProfiles(start, size, visible, enable, enableBy); MainCLI.printMessage(response.getTotal() + " entries matched"); if (response.getTotal() == 0) return;