Skip to content

Commit

Permalink
Add profile filter to the CLI
Browse files Browse the repository at this point in the history
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 <username>" shows the profiles enabled by the provided
  user.
  • Loading branch information
fmarco76 committed Sep 27, 2023
1 parent 7a933c0 commit bb13d41
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ public Response listEnrollmentTemplates(Integer start, Integer size) {
}

ProfileDataInfos infos = new ProfileDataInfos();
boolean visibleOnly = true;

Enumeration<String> e = ps.getProfileIds();
if (e == null) return createOKResponse(infos);
Expand All @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit bb13d41

Please sign in to comment.