简体   繁体   English

如何使用Groovy存储秘密文本或文件

[英]How to store secret text or file using groovy

I have found how to store an Username/Password or SSH Username/PrivateKey using the groovy-based APIs for Jenkins. 我发现了如何使用基于groovy的Jenkins API存储用户名/密码或SSH用户名/ PrivateKey。

https://gist.github.com/iocanel/9de5c976cc0bd5011653 https://gist.github.com/iocanel/9de5c976cc0bd5011653

domain = Domain.global()
store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()

priveteKey = new BasicSSHUserPrivateKey(
CredentialsScope.GLOBAL,
"jenkins-slave-key",
"root",
new BasicSSHUserPrivateKey.UsersPrivateKeySource(),
"",
""
)

usernameAndPassword = new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL,
"jenkins-slave-password", "Jenkis Slave with Password Configuration",
"root",
"jenkins"
)

store.addCredentials(domain, priveteKey)
store.addCredentials(domain, usernameAndPassword)

There are more kinds of credentials that can be stored. 可以存储更多种类的凭证。 How do I do: 我该怎么办:

  • Secret file 秘密档案
  • Secret text 秘密文字

After some research, I found that the plain-credentials plugin implements the Secret Text and Secret File credentials. 经过研究后,我发现纯凭证插件实现了Secret Text和Secret File凭证。 I forked the gist above and added code for these two types (see the gist for the reqwuired imports). 我在上面列出了要点,并添加了这两种类型的代码(请参阅要点,以了解所需的导入内容)。

https://gist.github.com/chrisvire/383a2c7b7cfb3f55df6a https://gist.github.com/chrisvire/383a2c7b7cfb3f55df6a

secretText = new StringCredentialsImpl(
CredentialsScope.GLOBAL,
"secret-text",
"Secret Text Description",
Secret.fromString("some secret text goes here"))

file = new File("/path/to/some/file")
noFileItem = [ getName: { return "" } ] as FileItem
FileCredentailsImpl can take a file from a do
secretFile = new FileCredentialsImpl(
CredentialsScope.GLOBAL,
"secret-file",
"Secret File Description"
noFileItem, // Don't use FileItem
file.getName(),
file.text)

store.addCredentials(domain, secretText)
store.addCredentials(domain, secretFile)

Implementation using another FileCredentialsImpl constructor: 使用另一个FileCredentialsImpl构造函数的实现:

import com.cloudbees.plugins.credentials.*;
import com.cloudbees.plugins.credentials.domains.Domain;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;

def secret = '''Hi
there,
only
test'''

def secretBytes = SecretBytes.fromBytes(secret.getBytes())
def credentials = new FileCredentialsImpl(CredentialsScope.GLOBAL, 'my test file', 'description', 'file.txt', secretBytes)

SystemCredentialsProvider.instance.store.addCredentials(Domain.global(), credentials)

import com.cloudbees.plugins.credentials.*;
import com.cloudbees.plugins.credentials.domains.Domain;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;
import java.nio.file.*;

Path fileLocation = Paths.get("/path/to/some/file.txt");

def secretBytes = SecretBytes.fromBytes(Files.readAllBytes(fileLocation))
def credentials = new FileCredentialsImpl(CredentialsScope.GLOBAL, 'my test file', 'description', 'file.txt', secretBytes)

SystemCredentialsProvider.instance.store.addCredentials(Domain.global(), credentials)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM