我正在尝试将凭据从Jenkins迁移到另一个凭据存储。
我想从Jenkins商店中读取凭据,并找到了该脚本(https://github.com/tkrzeminski/jenkins-groovy-scripts/blob/master/show-all-credentials.groovy
脚本为根级别的全局域的SystemCredentialsProvider凭据完成了工作。
但是我的凭据存储在文件夹中,因此该脚本对我不起作用。
我正在使用Jenkins脚本控制台执行脚本。
[如果我导航到Jenkins凭据配置页面,并将鼠标悬停在我的一个凭据条目的图标上,则工具提示将显示“文件夹凭据提供程序”。
================================================ ====
问题:如何从Jenkins中的文件夹中读取所有凭据?
================================================ ====
请参见下面的脚本:
import jenkins.model.*
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
def showRow = { credentialType, secretId, username = null, password = null, description = null ->
println("${credentialType} : ".padLeft(20) + secretId?.padRight(38)+" | " +username?.padRight(20)+" | " +password?.padRight(40) + " | " +description)
}
// set Credentials domain name (null means is it global)
domainName = null
credentialsStore = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0]?.getStore()
domain = new Domain(domainName, null, Collections.<DomainSpecification>emptyList())
credentialsStore?.getCredentials(domain).each{
if(it instanceof UsernamePasswordCredentialsImpl)
showRow("user/password", it.id, it.username, it.password?.getPlainText(), it.description)
else if(it instanceof BasicSSHUserPrivateKey)
showRow("ssh priv key", it.id, it.passphrase?.getPlainText(), it.privateKeySource?.getPrivateKey(), it.description)
else if(it instanceof AWSCredentialsImpl)
showRow("aws", it.id, it.accessKey, it.secretKey?.getPlainText(), it.description)
else if(it instanceof StringCredentials)
showRow("secret text", it.id, it.secret?.getPlainText(), '', it.description)
else if(it instanceof FileCredentialsImpl)
showRow("secret file", it.id, it.content?.text, '', it.description)
else
showRow("something else", it.id, '', '', '')
}
return
我只是创建了一个类来从全局或文件夹范围检索凭证:
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.hudson.plugins.folder.Folder
class JenkinsCredentials
{
public static StandardUsernamePasswordCredentials getCredentialsByUsername(String userName) throws Exception
{
List credsList = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class, Jenkins.getInstance());
if(credsList == null || credsList.size() == 0)
{
credsList = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class, getFolderItem("MyFolder"));
}
return credsList.findResult { it.username == userName ? it : null };
}
public static StandardUsernamePasswordCredentials getCredentialsByID(String userID) throws Exception
{
List credsList = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class, Jenkins.getInstance());
if(credsList == null || credsList.size() == 0)
{
credsList = CredentialsProvider.lookupCredentials(StandardUsernamePasswordCredentials.class, getFolderItem("MyFolder"));
}
return credsList.findResult { it.id == userID ? it : null };
}
public static Folder getFolderItem(String folderName)
{
def allJenkinsItems = Jenkins.getInstance().getItems();
for (currentJenkinsItem in allJenkinsItems)
{
if(currentJenkinsItem instanceof Folder)
{
if(((Folder)currentJenkinsItem).getFullName().contains(folderName))
{
return (Folder)currentJenkinsItem;
}
}
}
}
}