Spring 云配置服务器加密问题

Spring Cloud Config Server encryption issue

我们有一个 Pivotal Cloud Foundry 服务器,它配置了一个带有加密密钥的 Spring 配置服务器。在相应的属性文件中(通过 github),我们为一些简单的属性设置了 {cipher} 前缀,我们能够在应用程序中很好地获取这些值。但我们最近注意到的挑战是,当我们有需要加密的 base64 数据时,spring 加密会截断 base64 数据末尾的尾随等号。当我们的应用程序读取此数据时,它无法解析它,因为它不是有效的 base64,因为它的末尾缺少填充字符(等号)。我们尝试用反斜杠转义等号,但仍然没有成功。我们只看到两个反斜杠,所以想知道是否有解决此问题的建议。谢谢!

据我所知,这里的问题似乎与 curl 的用法有关。我可以通过 运行:

重现您遇到的问题
spring decrypt --key @${HOME}/server_rsa_no_eol.key "$(curl -H "Authorization: $(cf oauth-token)" https://config-server.example.com/encrypt -s -d 'VGVzdC0=')"

这使用 curl 命中加密端点并获取结果并立即使用 spring decrypt 对其进行解密。正如您所指出的,这个 returns VGVzdC0.

这似乎是一个 curl 问题,因为我可以对 https://httpbin.org/post 进行相同的 post 并且得到相同的结果,VGVzdC0 没有 =.

$ curl https://httpbin.org/post --data 'VGVzdC0='
{
  ...
  "form": {
    "VGVzdC0": ""
  },
  ...

如果我 url 对 = 字符进行编码,则有效。

$ curl https://httpbin.org/post --data 'VGVzdC0%3D'
{
  ...
  "form": {
    "VGVzdC0=": ""
  },
  ...

您还可以使用 --data-urlencodecurl 执行 url 编码,但有一个问题。您必须在该值前加上 =。所以这也有效

$ curl https://httpbin.org/post --data-urlencode '=VGVzdC0='
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "VGVzdC0=": ""
  },
...

来自 curl 手册页:

  --data-urlencode <data>
          (HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL-encoding.

          To be CGI-compliant, the <data> part should begin with a name followed by a separator and a content specification. The <data> part can be passed to curl using one of the following
          syntaxes:

          content
                 This will make curl URL-encode the content and pass that on. Just be careful so that the content doesn't contain any = or @ symbols, as that will then make the syntax match
                 one of the other cases below!

          =content
                 This will make curl URL-encode the content and pass that on. The preceding = symbol is not included in the data.

关键是最后一部分=content。这将使 curl url 对内容进行编码,并且不包括前缀 =

如果我重复上面的测试,我会得到预期的结果 VGVzdC0=

spring decrypt --key @${HOME}/server_rsa_no_eol.key "$(curl -H "Authorization: $(cf oauth-token)" https://config-server.example.com/encrypt -s --data-urlencode '=VGVzdC0=')"

顺便说一句,您也可以选择简单的方式安装 Spring Boot CLI + Spring Cloud Extension。然后你可以 spring encrypt --key @${HOME}/server_rsa_no_eol.key 'VGVzdC0=' 并且你会得到正确的值。这确实意味着您需要本地计算机上的密钥副本,您可能有也可能没有。

brew install springboot
spring install org.springframework.cloud:spring-cloud-cli:2.2.1.RELEASE

使用httpie:

可以避免curl引起的问题

加密:

echo -n cleartext | http https://config-server.com/encrypt

解密:

echo -n ciphertext | http https://config-server.com/decrypt