Skip to content

Instantly share code, notes, and snippets.

@fabienrenaud
Last active September 20, 2023 17:24
Show Gist options
  • Save fabienrenaud/349764873855533abc71b7fadafdf29e to your computer and use it in GitHub Desktop.
Save fabienrenaud/349764873855533abc71b7fadafdf29e to your computer and use it in GitHub Desktop.
import jenkins.model.*
import hudson.model.ModelObject
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.impl.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey
import com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl
import org.jenkinsci.plugins.plaincredentials.StringCredentials
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl
import com.cloudbees.hudson.plugins.folder.Folder
class DeepCredentialsPrinter {
private static final boolean DEBUG = false;
private final out;
private final Set<CredentialsStore> visitedStores = new HashSet<>();
DeepCredentialsPrinter(out) {
this.out = out;
}
private void start() {
out.println("Folder,Credentials Type,Credentials ID") // header
process(Jenkins.getInstance())
}
private void process(ItemGroup group) {
printCreds(group);
List<ItemGroup> items = group.getItems();
if (items == null || items.isEmpty()) {
return;
}
for (item in items) {
if (item instanceof ItemGroup) {
process(item);
} else if (item instanceof Item) {
printCreds(item)
} else {
if (DEBUG) {
out.println("[DEBUG] unsupported item type: " + item.getClass().getCanonicalName());
}
}
}
}
private void printCreds(ModelObject model) {
for (store in CredentialsProvider.lookupStores(model)) {
if (visitedStores.add(store)) { // only visit new stores
print(model.getFullName(), store.getCredentials(Domain.global()));
}
}
}
private void print(String fullname, List<Credentials> creds) {
if (creds.isEmpty()) {
if (DEBUG) {
out.println("[DEBUG] No credentials in /" + fullname);
}
} else {
for (c in creds) {
out.printf("/%s,%s,%s\n", fullname, c.getClass().getSimpleName(), c.id)
}
}
}
}
new DeepCredentialsPrinter(getBinding().out).start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment