Spring 云配置服务器加密问题



我们有一个Pivotal Cloud Foundry服务器,它配置了一个带有加密密钥的Spring配置服务器。在相应的属性文件中(通过 github(,我们为一些简单的属性添加了 {cipher} 前缀,我们能够在应用程序中很好地获取值。但我们最近注意到的挑战是,当我们有一个需要加密的base64数据时,弹簧加密会截断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解密。如您所指出的,这将返回VGVzdC0

这似乎是一个卷曲问题,因为我可以向 https://httpbin.org/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=": ""
},
...

从卷曲手册页:

--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
  • https://docs.spring.io/spring-boot/docs/2.2.7.RELEASE/reference/html/spring-boot-cli.html#cli-installation
  • https://cloud.spring.io/spring-cloud-static/spring-cloud-cli/2.2.1.RELEASE/reference/html/#_installation

你可以通过使用httpie来避免curl引起的问题:

加密:

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

解密:

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

最新更新