GoCD 基于文件的秘钥管理插件,可以将秘钥进行加密存储!
系统自带的默认插件就已经可以帮助我们完成隐私数据的加密和使用了,所以如果没有额外需求的话(比如部署在 aws
或 k8s
集群系统之上),就不需要额外安装其他插件工具了。
1. 功能介绍
虽然这个插件日常基本不太会使用,但是还是有其用武之地的!
我们都知道,秘钥管理是非常困难的一件事情!在理想的情况下,秘钥应该被安全的妥善存储起来,并且秘钥还需要严格的访问控制以及定期更换。市场上有一些秘钥管理工具,专门解决围绕秘钥处理的一些问题。
利用这些解决方案,GoCD
提供了插件的方式来查找在外部 Secrets Manager
中定义的秘密。这使 GoCD
用户可以灵活地使用他们选择的 Secret Manager
来存储和管理机密。
外部秘钥不会存储在 GoCD
中,只有在需要时才会查找这些秘密,例如,如果一个作业被配置为使用外部秘密,那么在将作业分配给代理并安全传输给代理之前查找这些秘密。
2. 安装方式
需要先创建秘钥数据库,才可以配置!
在服务器端安装 GoCD
的时候,服务器端默认就帮我们安装了一个简单版本的秘钥配置管理插件,如果没有的话,可以在 官网地址 下载到插件存储目录(go-server/plugins/bundled
)下面。
已经存在上述插件之后,我们首先需要执行 init
命令来初始化秘钥数据库(file
),这里的文件需要我们自行定义其文件名称。同时,建议将其配置文件保存在 CONFIG_DIR
环境变量的对应目录下面。这样在备份服务端数据的时候,会将其一并备份,防止数据丢失的出现。
# 创建
$ mkdir -pv /data/go-server/secrets
# 初始化秘钥文件
$ cd /data/go-server/secrets
$ java -jar \
../plugins/bundled/gocd-file-based-secrets-plugin.jar \
init -f github-secret.json
Initialized secret database file in github-secret.json
执行上述命令,查看创建出来的文件内容,对应的 valule
是加密存储的。
$ cat github-secret.json
{
"secret_key": "...",
"secrets": {
"my-password": "AES:..."
}
}
有了上述的秘钥数据库文件之后,我们就可以对其进行增删改查了。
# 增加/更新(key/value)
$ java -jar \
../plugins/bundled/gocd-file-based-secrets-plugin.jar \
add -f github-secret.json \
-n github-name -v escape
# 查看(value)
$ java -jar \
../plugins/bundled/gocd-file-based-secrets-plugin.jar \
show -f github-secret.json \
-n github-name
# 查看(keys)
$ java -jar \
../plugins/bundled/gocd-file-based-secrets-plugin.jar \
keys -f github-secret.json
# 删除
$ java -jar \
../plugins/bundled/gocd-file-based-secrets-plugin.jar \
remove -f github-secret.json \
-n github-name
以下,是该秘钥插件的所有命令行参数。
$ java -jar ./gocd-file-based-secrets-plugin.jar --help
Usage: java -jar <plugin.jar.file> [options] [command] [command options]
Options:
--help, -h
Prints this help.
Commands:
init Initialize the secret database file.
Usage: init [options]
Options:
* --file, -f
The path to the secret database file.
add Adds a secret.
Usage: add [options]
Options:
* --file, -f
The path to the secret database file.
* --name, -n
The name of the secret to remove.
* --value, -v
The value of the secret.
remove Removes given secret.
Usage: remove [options]
Options:
* --file, -f
The path to the secret database file.
* --name, -n
The name of the secret to remove.
show Returns value for given secret.
Usage: show [options]
Options:
* --file, -f
The path to the secret database file.
* --name, -n
The name of the secret to remove.
keys Returns all secret keys.
Usage: keys [options]
Options:
* --file, -f
The path to the secret database file.
3. 配置方式
需要先配置,才可以使用!
常见完成上述秘钥数据库文件之后,就需要在服务器配置文件中加入对应配置。
- [1] 通过配置文件配置
<secretConfigs>
<secretConfig id="Env1Secrets" pluginId="cd.go.secrets.file-based-plugin">
<description>All secrets for env1</description>
<configuration>
<property>
<key>SecretsFilePath</key>
<value>/godata/config/secrets.json</value>
</property>
</configuration>
<rules>
<allow action="refer" type="environment">env_*</allow>
<deny action="refer" type="pipeline_group">my_group</deny>
<allow action="refer" type="pipeline_group">other_group</allow>
</rules>
</secretConfig>
</secretConfigs>
- [2] 通过 UI 界面配置
- [3] 密码使用方式
可以在支持的实体中,通过以下语法来使用该秘钥数据库定义的内容。
# 语法格式
{{secret:[Env1Secrets][github-name]}}
4. 使用方式
定义和引用
配置了 Secret Manager
之后,我们就可以通过秘钥对应的语法格式,进行引用,以及使用了。以下展示了七种使用方式,如果不在其范围的其他地方定义的话,将会被自动忽略掉,并将其按照字符串进行处理。
# Secret Param
# secret_config_id: 需要和secret_id保持一致
# secret_key: 就是在数据库定义的key值
{{SECRET:[secret_config_id][secret_key]}}
- [1] Password field for an SCM Material
- [2] Value field of an environment variable
- [3] Any configuration field of a Pluggable SCM
- [4] Any configuration field of a Package Repository
- [5] Any configuration field of a Package
- [6] Any configuration field for Cluster Profile
- [7] Any configuration field for Elastic Agent Profile
5. 参考链接
送人玫瑰,手有余香!