首页 > 解决方案 > Google 端点忽略带有下划线键的标头

问题描述

我已将我的 k8s 部署与谷歌端点集成。我有 12 项服务,其中 11 项服务在谷歌端点上运行良好,但一项服务除外。当它将请求传输到应用程序容器时,它会忽略带有下划线的标头值。

实现我已经在我的应用程序部署中添加了这个(忽略语法,我在部署中使用了正确的格式)

- args:
  - --http_port
  - "8081"
  - --backend
  - 127.0.0.1:8080
  - --service
  - <MY_ENDPOINT>
  - --service_account_key
  - /etc/nginx/creds/<MY_CREDS>
  - --rollout_strategy=managed
  image: gcr.io/endpoints-release/endpoints-runtime:1
  imagePullPolicy: Always
  name: esp
  ports:
  - containerPort: 8081
    protocol: TCP
  resources: {}
  volumeMounts:
  - mountPath: /etc/nginx/creds
    name: service-account-creds
    readOnly: true

在下面的示例中,我发送了两个带有 TEST-CODE 和 TEST_CODE 的标头。当我通过应用程序端口发送请求时,两者都在应用程序端收到,但是当我通过 esp 端口访问它时,我只收到带有 -(连字符)而不是 _(下划线)的标题。通过 esp 访问时,服务器中正在接收 TEST-CODE 但不是 TEST_CODE。

当我使用 curl 或邮递员通过应用程序端口 8080 向我的应用程序发送请求时

curl -XPOST http://localhost:8080/<CONTEXT_PATH> -H "Content-Type: application/json" -H "USER-ID: abc" -H "TEST-CODE: xyz" -H "TEST_CODE: wxyz"

我得到这样的回应

{
    "timestamp": "2020-04-05T19:42:44.958564Z",
    "principal": null,
    "session": null,
    "request": {
        "method": "POST",
        "uri": "http://localhost:8080/<MY_URL>",
        "headers": {
            "host": [
                "localhost:8080"
            ],
            "test-code": [
                "xyz"
            ],
            "content-type": [
                "application/json"
            ],
            "x-user-id": [
                "abc"
            ],
            "test_code": [
                "wxyz"
            ],
            "user-agent": [
                "curl/7.52.1"
            ],
            "accept": [
                "*/*"
            ]
        },
        "remoteAddress": null
    },
    "response": {
        "status": 200,
        "headers": {
            "Content-Length": [
                "435"
            ],
            "Date": [
                "Sun, 05 Apr 2020 19:42:45 GMT"
            ],
            "Content-Type": [
                "application/json;charset=UTF-8"
            ]
        }
    },
    "timeTaken": 633
}

当我通过 8081(通过 esp)使用 curl 或邮递员向我的应用程序发送请求时

curl -XPOST http://localhost:8081/<CONTEXT_PATH>?key=<MY_KEY> -H "Content-Type: application/json" -H "USER-ID: abc" -H "TEST-CODE: xyz" -H "TEST_CODE: wxyz"

我得到这样的回应

{
    "timestamp": "2020-04-05T19:42:57.102184Z",
    "principal": null,
    "session": null,
    "request": {
        "method": "POST",
        "uri": "http://localhost/<MY_URL>?<MY_KEY>",
        "headers": {
            "x-real-ip": [
                "127.0.0.1"
            ],
            "x-google-real-ip": [
                "127.0.0.1"
            ],
            "x-cloud-trace-context": [
                "<MY_GOOGLE_TRACE_CONTEXT>"
            ],
            "host": [
                "localhost"
            ],
            "test-code": [
                "xyz"
            ],
            "x-endpoint-api-project-id": [
                "<MY_ENDPOINT_PROJECT_ID>"
            ],
            "content-type": [
                "application/json"
            ],
            "x-forwarded-for": [
                "127.0.0.1"
            ],
            "x-user-id": [
                "abc"
            ],
            "user-agent": [
                "curl/7.52.1"
            ],
            "accept": [
                "*/*"
            ]
        },
        "remoteAddress": null
    },
    "response": {
        "status": 400,
        "headers": {
            "Connection": [
                "close"
            ],
            "Content-Length": [
                "256"
            ],
            "Date": [
                "Sun, 05 Apr 2020 19:42:57 GMT"
            ],
            "Content-Type": [
                "application/json"
            ]
        }
    },
    "timeTaken": 21
}

再次,

请注意区别:我将测试代码标头作为 TEST-CODE 和 TEST_CODE 发送,当我通过应用程序端口访问时,两者都来了,但是当我通过 esp 容器访问时,应用程序单独接收 TEST-CODE,而不是 TEST_CODE。

我尝试了其他一些带有 -(连字符)的 HEADERS 和一些带有 _(下划线)的 HEADERS。每次通过 esp 将带有 -(连字符)的标头接收到应用程序,但不带有 _(下划线)。

我已经从弹簧执行器端点(执行器/httptrace)中获取了上述响应

有人可以帮忙吗?

标签: google-cloud-endpointsgoogle-cloud-endpoints-v2

解决方案


推荐阅读