Drone CI 中文文档

Secret(第三方密钥托管)

你可以使用 secret 扩展从自定义的第三方源提供密钥到你的流水线中。例如,我们创建了一个参考的 secret 扩展,从 Vault 中获取密钥。

以下是一些参考扩展:

扩展配置

你可以通过提供以下配置参数来向你的 Runner 注册 secret 扩展:

  • DRONE_SECRET_PLUGIN_ENDPOINT
    提供用于向 secret 扩展发起 HTTP 请求的端点(endpoint)。
  • DRONE_SECRET_PLUGIN_TOKEN
    提供用于验证对扩展的 http 请求的令牌。这个令牌在服务器和扩展之间共享。
与 Drone Server 的集中式管理设计不同,secret 扩展是向 Runner 注册的。这种设计是有意的。例如,不同地区或数据中心的 Runner 可能具有访问不同密钥的权限。

扩展工作原理

你可以在 YAML 配置文件中定义一个 外部密钥 资源。当你定义一个外部密钥时,Runner 会向 secret 扩展发起 HTTP POST 请求以检索外部密钥。

以下是带有外部密钥的 YAML 配置示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
kind: secret
name: username
get:
  path: secrets/data/docker
  name: username

---
kind: secret
name: password
get:
  path: secrets/data/docker
  name: password

扩展请求响应

Secret 扩展将收到一个 HTTP 请求,以返回命名的密钥。请求正文以 JSON 编码,包括请求的密钥名称,以及存储库和构建信息。

以下是请求体的定义:

1
2
3
4
5
6
class Request {
    name: string;
    path: string;
    repo: Repository;
    build: Build;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Repository {
    id: int64;
    uid: int64;
    user_id: int64;
    namespace: string;
    name: string;
    slug: string;
    scm: string;
    git_http_url: string;
    git_ssh_url: string;
    link: string;
    default_branch: string;
    private: boolean;
    visibility: string;
    active: boolean;
    config: string;
    trusted: boolean;
    protected: boolean;
    ignore_forks: boolean;
    ignore_pulls: boolean;
    cancel_pulls: boolean;
    timeout: int64;
    counter: int64;
    synced: int64;
    created: int64;
    updated: int64;
    version: int64;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Build {
    id: int64;
    repo_id: int64;
    number: int64;
    parent: int64;
    status: string;
    error: string
    event: string;
    action: string;
    link: string;
    timestamp: int64;
    title: string;
    message: string;
    before: string;
    after: string;
    ref: string;
    source_repo: string;
    source: string;
    target: string;
    author_login: string;
    author_name: string;
    author_email: string;
    author_avatar: string;
    sender: string;
    params: [string][string];
    cron: string;
    deploy_to: string;
    deploy_id: int64;
    started: int64;
    finished: int64;
    created: int64;
    updated: int64;
    version: int64;
}

扩展请求响应

Secret 扩展应该以 HTTP 200 响应代码响应请求,并将密钥以 JSON 格式进行编码返回。如果无法授权请求的密钥访问,扩展应该返回 204 状态码。

以下是响应体的定义:

1
2
3
4
class Secret {
  name: string
  data: string
}

以下是响应体的示例:

{
  "name": "password",
  "data": "correct-horse-battery-staple"
}

扩展请求鉴权

HTTP 请求根据 HTTP 签名规范草案使用共享 Secret 进行签名。请求接收者应该使用签名来验证 WebHook 的真实性和完整性。

起始项目 (模板)

如果你有兴趣创建一个 Secret 扩展,我们建议使用我们的起始项目模板作为基础来启动开发。