Last active
September 20, 2023 17:24
-
-
Save fabienrenaud/349764873855533abc71b7fadafdf29e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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