在Kubernetes/CoreOS上,仅在Go中出现无效的标头字段值。

huangapple go评论99阅读模式
英文:

Invalid header field value in Go ONLY on kubernetes/CoreOS

问题

我有一个使用aws-sdk-go与dynamodb通信的Go程序。依赖项已经打包。Go版本为1.7.1,aws-sdk-go版本为1.6.24。该程序在以下所有环境中按预期工作:

  • 从shell运行的开发机(Arch Linux)
  • 在我的开发机上运行的Docker容器(Docker 1.13.1)
  • 从shell运行的Ec2实例(Ubuntu 16.04)

当我在Kubernetes上运行Docker容器(与我在开发机上测试的容器相同)时,我遇到了以下错误:
<pre>

2017/03/02 22:30:13 DEBUG ERROR: Request dynamodb/GetItem:
---[ REQUEST DUMP ERROR ]-----------------------------
net/http: invalid header field value "AWS4-HMAC-SHA256 Credential=hidden\n/20170302/us-east-1/dynamodb/aws4_request, SignedHeaders=accept-encoding;content-length;content-type;host;x-amz-date;x-amz-target, Signature=483f56dd0b17d8945d3c2f2044b7f97e531190602f132a4d5f828264b3a2cff2" for key Authorization

2017/03/02 22:30:13 DEBUG: Response dynamodb/GetItem Details:
---[ RESPONSE ]--------------------------------------
HTTP/0.0 000 status code 0
Content-Length: 0

</pre>

根据:
https://golang.org/src/net/http/transport.go
https://godoc.org/golang.org/x/net/lex/httplex#ValidHeaderFieldValue

看起来问题出在头部值的验证上,但我不明白为什么除了我的k8s集群之外,它在其他地方都能正常工作。该集群由运行最新CoreOS稳定ami(CoreOS stable 1235.8.0)的Ec2实例组成。

在我的开发机上,可以正常工作的Docker镜像是基于scratch的。为了排除故障,我创建了一个基于最新的Ubuntu的镜像,并编写了一个简单的go程序,只是从dynamodb中获取一个项目。当在我的k8s集群上运行此镜像并从交互式shell中运行程序时,我得到相同的错误。我已确认我可以从该环境中ping通dynamodb的端点。

我很难排查这个问题:我是否遗漏了一些愚蠢的东西?有人可以指点我正确的方向或者对发生的事情有什么想法吗?

英文:

I have a Go program that uses aws-sdk-go to talk to dynamodb. Dependencies are vendored. Go version 1.7.1. aws-sdk-go version 1.6.24. The program works as expected in all the following environments:

  • dev box from shell (Arch Linux)
  • docker container running on my dev box (Docker 1.13.1)
  • Ec2 instance from shell (Ubuntu 16.04)

When I run the docker container on kubernetes (same one I tested on my dev box), I get the following error:
<pre>

2017/03/02 22:30:13 DEBUG ERROR: Request dynamodb/GetItem:
---[ REQUEST DUMP ERROR ]-----------------------------
net/http: invalid header field value "AWS4-HMAC-SHA256 Credential=hidden\n/20170302/us-east-1/dynamodb/aws4_request, SignedHeaders=accept-encoding;content-length;content-type;host;x-amz-date;x-amz-target, Signature=483f56dd0b17d8945d3c2f2044b7f97e531190602f132a4d5f828264b3a2cff2" for key Authorization

2017/03/02 22:30:13 DEBUG: Response dynamodb/GetItem Details:
---[ RESPONSE ]--------------------------------------
HTTP/0.0 000 status code 0
Content-Length: 0

</pre>

Based on:
https://golang.org/src/net/http/transport.go
https://godoc.org/golang.org/x/net/lex/httplex#ValidHeaderFieldValue

It looks like the problem is with the header value validation, yet I am at a loss to understand why it works everywhere except on my k8s cluster. The cluster is composed of Ec2 instances running the latest CoreOS stable ami (CoreOS stable 1235.8.0)

The docker image that works on my dev machine is scratch based. To troubleshoot I created an image based on Ubuntu latest with a separate go program that just does a simple get item from dynamodb. When this image is run on my k8s cluster and the program run from an interactive shell, I get the same errors. I have confirmed I can ping the dynamodb endpoints from this env.

I am having a hard time troubleshooting this issue: am I missing something stupid here? Can someone point me in the right direction or have an idea of what is going on?

答案1

得分: 11

记住在执行此操作时要加上"-n":
echo -n key | base64

英文:

remember the "-n" when you do this:
echo -n key | base64

答案2

得分: 5

hidden后面的\n肯定是无效的。不确定它实际上是否存在,或者在你清理发布时是否被错误地插入。

考虑以下代码:

package main

import (
	"fmt"
	"golang.org/x/net/lex/httplex"
)

func main() {
	fmt.Println("Is valid (without new line)", httplex.ValidHeaderFieldValue("AWS4-HMAC-SHA256 Credential=hidden/20170302/us-east-1/dynamodb/aws4_request, SignedHeaders=accept-encoding;content-length;content-type;host;x-amz-date;x-amz-target, Signature=483f56dd0b17d8945d3c2f2044b7f97e531190602f132a4d5f828264b3a2cff2"))
	fmt.Println("Is valid (with new line)", httplex.ValidHeaderFieldValue("AWS4-HMAC-SHA256 Credential=hidden\n/20170302/us-east-1/dynamodb/aws4_request, SignedHeaders=accept-encoding;content-length;content-type;host;x-amz-date;x-amz-target, Signature=483f56dd0b17d8945d3c2f2044b7f97e531190602f132a4d5f828264b3a2cff2"))
}

一个猜测是从实际隐藏值被提取的地方(配置文件等)错误地包含了\n,并且在这种情况下它被错误地包含在你的标头中。

英文:

The \n after hidden is certainly invalid. Not sure if it is actually there or somehow got inserted when you were cleansing for posting.

Consider:

package main

import (
	&quot;fmt&quot;
	&quot;golang.org/x/net/lex/httplex&quot;
)

func main() {
	fmt.Println(&quot;Is valid (without new line)&quot;, httplex.ValidHeaderFieldValue(&quot;AWS4-HMAC-SHA256 Credential=hidden/20170302/us-east-1/dynamodb/aws4_request, SignedHeaders=accept-encoding;content-length;content-type;host;x-amz-date;x-amz-target, Signature=483f56dd0b17d8945d3c2f2044b7f97e531190602f132a4d5f828264b3a2cff2&quot;))
	fmt.Println(&quot;Is valid (with new line)&quot;, httplex.ValidHeaderFieldValue(&quot;AWS4-HMAC-SHA256 Credential=hidden\n/20170302/us-east-1/dynamodb/aws4_request, SignedHeaders=accept-encoding;content-length;content-type;host;x-amz-date;x-amz-target, Signature=483f56dd0b17d8945d3c2f2044b7f97e531190602f132a4d5f828264b3a2cff2&quot;))
}

One guess would be wherever the real hidden value is getting pulled from (config file etc) mistakenly has the \n in there and it's happily getting pulled into your header, but only in this case.

huangapple
  • 本文由 发表于 2017年3月3日 06:58:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/42567606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定