Skip to content

Commit

Permalink
feat: allow to export only specific client secrets (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcScheib committed Mar 2, 2024
1 parent 881a14a commit e4cf4ce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -51,7 +53,8 @@ public void init() {
public void export() throws IOException, ParseException, URISyntaxException {
final Collection<Template> templates = VelocityUtils.loadTemplates(loadTemplateFiles());
final Map<String, ClientRepresentation> clients = loadClients();
for (final ClientRepresentation client : clients.values()) {
final Collection<ClientRepresentation> filteredClients = getFilteredClients(clients);
for (final ClientRepresentation client : filteredClients) {
// skip all clients without a secret
if (StringUtil.isBlank(client.getSecret())) {
continue;
Expand All @@ -77,6 +80,18 @@ private Map<String, ClientRepresentation> loadClients() {
.collect(Collectors.toMap(ClientRepresentation::getClientId, Function.identity()));
}

private Collection<ClientRepresentation> getFilteredClients(
final Map<String, ClientRepresentation> clients) {
if (StringUtil.isBlank(configuration.getClientIds())) {
return clients.values();
}
final String[] split = configuration.getClientIds().split(",");
return Arrays.stream(split)
.map(clients::get)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}

/**
* Derives all templates that must be expanded for the given client.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class ExportSecretsCommand implements Runnable {
@CommandLine.Option(required = true, names = { "-c", "--config" },
description = "Directory containing templates for secret output files.")
String configDirectory;
@CommandLine.Option(names = { "-n", "--client-ids" },
description = "Comma separated list of client IDs to export secrets for.")
String clientIds;
@CommandLine.Option(names = { "-o", "--output" }, defaultValue = "./",
description = "Output directory for generate files.")
String outputDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
public class ExportSecretsCommandConfiguration extends KeycloakConfiguration {
private final String realmName;
private final String configDirectory;
private final String clientIds;
private final String outputDirectory;

public ExportSecretsCommandConfiguration(final ParseResult parseResult) {
super(parseResult);
realmName = getMatchedOption(parseResult, "-r");
configDirectory = getMatchedOption(parseResult, "-c");
clientIds = getMatchedOption(parseResult, "-n");
outputDirectory = getMatchedOption(parseResult, "-o");
}
}

0 comments on commit e4cf4ce

Please sign in to comment.