1

我是使用 spring 云配置服务器的新手。我已经通过 Spring Initlzr 设置了一个服务器作为 Spring Boot Maven 项目。我想在自托管 GitLab 服务器上使用私有存储库。

TL;博士

在任何响应中都会返回包含部署令牌(凭据)的已配置 GitLab 存储库 uri。我怎样才能防止这种情况?我不想使用 ssh 密钥。

环境

Component       Version
Spring Boot     2.4.0
Spring Cloud    2020.0.0-M5
GitLab CE       13.5.3
Java VM         openjdk 11

长版

GitLab 允许使用部署或访问令牌读取(拉取)存储库。我找不到任何说明如何在 Spring Cloud 配置服务器中进行配置的文档。根据 spring.io 上的官方文档,服务器使用 JGit 与远程 git 存储库进行通信。

因此,我查阅了 JGit 文档并找到了有关如何在 GitLab 中使用部署令牌的说明。您使用基本的 http 身份验证指定 GitLab 存储库 uri,并另外指定用户名和密码。 https://www.codeaffine.com/2014/12/09/jgit-authentication/(认证@GitLab)

我为 spring 云配置服务器采用了它并且它可以工作,但是在提供内容时,GitLab 存储库 uri 中配置的凭据也会暴露。当我删除 uri 或用户名/密码属性中的凭据时,服务器无法连接到存储库。

应用程序属性:

spring.cloud.config.server.git.uri=https://<deploy-token-name>:<token>@gitlab.home.local/spring-cloud-config
spring.cloud.config.server.git.username=<deploy-token-name>
spring.cloud.config.server.git.password=<token>
spring.cloud.config.server.git.clone-on-start=true
server.port=8888

读取服务的属性会返回如下响应:
GET https://cloud-config.home.local:8888/some-service/development
响应:

{
  "name": "some-service",
  "profiles": [
    "development"
  ],
  "label": null,
  "version": "b87f905c28cc911b056c5ecf6aef6724bfbbbe58",
  "state": null,
  "propertySources": [
    {
      "name": "https://<deploy-token-name>:<token>@gitlab.home.local/spring-cloud-config.git/some-service/application-development.properties",
      "source": {
        "my.property": "Hello from development",
      }
    },
    {
      "name": "https://<deploy-token-name>:<token>@gitlab.home.local/spring-cloud-config.git/application.properties",
      "source": {
        "my.property": "Hello from default"
      }
    }
  ]
}

最好的问候,
大卫

更新

根据 GitLab 文档,您也可以使用私有访问令牌读取存储库。见链接:https ://docs.gitlab.com/ee/api/README.html#personalproject-access-tokens

我在 GitLab 服务器上创建了一个私有访问令牌,并尝试在 spring 云配置服务器中对其进行配置。当我添加?private-token=<token>参数时,服务器不会启动,因为 JGit 似乎将路径附加到配置的 uri,这会导致无效的 url。
Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'https://gitlab.home.local/spring-cloud-config.git?private_token=<token>/info/refs&service=git-upload-pack'

我还尝试将令牌添加为 application.properties 中的标头字段。但是此时该属性似乎被忽略或至少没有被评估。
spring.cloud.config.headers.PRIVATE-TOKEN=<token>或者
spring.cloud.config.headers.Authorization=Bearer <token>

4

0 回答 0