aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.12k stars 578 forks source link

"TypeError: headers[headerName].trim is not a function at getCanonicalHeaders" when using sdk CodeArtifact's publishPackageVersion #5713

Closed saiprasadrao closed 3 months ago

saiprasadrao commented 9 months ago

Checkboxes for prior research

Describe the bug

I am trying to use module "@aws-sdk/client-codeartifact@3.496.0" but below error when trying to use the command

Code

const client = new Codeartifact({ region: awsRegion })
const response = await client.publishPackageVersion(params)

Error

Error: TypeError: headers[headerName].trim is not a function
    at getCanonicalHeaders (/home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@smithy/signature-v4/dist-cjs/index.js:129:58)
    at _SignatureV4.signRequest (/home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@smithy/signature-v4/dist-cjs/index.js:378:30)
    at async /home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@aws-sdk/middleware-signing/dist-cjs/index.js:182:21
    at async /home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
    at async /home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:[22](https://github.com/saiprasadrao/aws-codeartifact-publish-package-version/actions/runs/7629237989/job/20782200933#step:5:23)
    at async run (/home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/src/main.js:35:22)

SDK version number

@aws-sdk/client-codeartifact@3.496.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

node version : v20.10.0

Reproduction Steps

const { Codeartifact } = require('@aws-sdk/client-codeartifact')
async function run() {
    const params = {
      assetContent: 'temp.tar.gz',
      assetName: 'temp.tar.gz', // required
      assetSHA256: 'asfasdoiuwewenasd234e23esdmdnfsdmnas', // required
      domain: 'test-domain',
      format: 'generic',
      package: 'test', // required
      packageVersion: '0.0.1', // required
      repository: 'test' // required
      domainOwner: '121212121122'
      namespace: 'test'
    }
   const client = new Codeartifact({ region: 'ap-south-1' })
   const response = await client.publishPackageVersion(params)
}

Observed Behavior

Throwing below error

Error: TypeError: headers[headerName].trim is not a function
    at getCanonicalHeaders (/home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@smithy/signature-v4/dist-cjs/index.js:129:58)
    at _SignatureV4.signRequest (/home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@smithy/signature-v4/dist-cjs/index.js:378:30)
    at async /home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@aws-sdk/middleware-signing/dist-cjs/index.js:182:21
    at async /home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
    at async /home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:[22](https://github.com/saiprasadrao/aws-codeartifact-publish-package-version/actions/runs/7629237989/job/20782200933#step:5:23)
    at async run (/home/runner/work/aws-codeartifact-publish-package-version/aws-codeartifact-publish-package-version/src/main.js:35:22)

Expected Behavior

It should able to publish artifact version to aws codeartifact

Possible Solution

No response

Additional Information/Context

No response

kuhe commented 9 months ago

domainOwner should be a string

saiprasadrao commented 9 months ago

Hi @RanVaknin - Anything am i missing... please let me know.. with domainOwner as string still getting the same issue...

RanVaknin commented 9 months ago

Hi @saiprasadrao ,

Thanks for reaching out. Unfortunately I'm not able to reproduce this behavior. Are you using an IDE to run your code? The code sample is not formatted correctly / incomplete.

import  { CodeartifactClient, PublishPackageVersionCommand } from '@aws-sdk/client-codeartifact';

async function run() {
    const params = {
      assetContent: 'temp.tar.gz',
      assetName: 'temp.tar.gz', 
      assetSHA256: 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', // replace this with the sha256 has from generated from your assetContent
      domain: 'test-domain',
      format: 'generic',
      package: 'test', 
      packageVersion: '0.0.1', 
      repository: 'test',
      domainOwner: '121212121122',
      namespace: 'test'
    }
   const client = new CodeartifactClient({ region: 'ap-south-1' })

   try {
    const response = await client.send(new PublishPackageVersionCommand(params));
    console.log(response)
   } catch (error) {
    console.log(error)
   }
}
run()

Let me know if this helps.

Thanks again, Ran~

saiprasadrao commented 9 months ago

Hi @RanVaknin,

i will test this out and let you know.. i believe its an issue with @smithy/signature-v4. Will confirm on this with code and probably the issue :)

Thanks and Regards, Sai Rao

kabo commented 9 months ago

I'm having the exact same issue, but with

const s3 = new S3ControlClient({ region: 'us-east-1' })
const response = s3.send(new GetPublicAccessBlockCommand({ AccountId: '112233445566' }))

version 3.507.0

kabo commented 9 months ago

with

new S3ControlClient({ region: 'us-east-1', logger: console })
endpoints Initial EndpointParams: {
  "UseArnRegion": false,
  "UseFIPS": false,
  "Region": "us-east-1",
  "UseDualStack": false,
  "RequiresAccountId": true,
  "AccountId": 112233445566
}
endpoints evaluateCondition: isSet($Region) = true
endpoints evaluateCondition: stringEquals($Region, snow) = false
endpoints evaluateCondition: isSet($OutpostId) = false
endpoints evaluateCondition: isSet($AccessPointName) = false
endpoints evaluateCondition: isSet($Bucket) = false
endpoints evaluateCondition: aws.partition($Region) = {
  "dnsSuffix": "amazonaws.com",
  "dualStackDnsSuffix": "api.aws",
  "implicitGlobalRegion": "us-east-1",
  "name": "aws",
  "supportsDualStack": true,
  "supportsFIPS": true,
  "description": "US East (N. Virginia)"
}
endpoints assign: partitionResult := {
  "dnsSuffix": "amazonaws.com",
  "dualStackDnsSuffix": "api.aws",
  "implicitGlobalRegion": "us-east-1",
  "name": "aws",
  "supportsDualStack": true,
  "supportsFIPS": true,
  "description": "US East (N. Virginia)"
}
endpoints evaluateCondition: isValidHostLabel($Region, true) = true
endpoints evaluateCondition: booleanEquals($UseFIPS, true) = false
endpoints evaluateCondition: isSet($RequiresAccountId) = true
endpoints evaluateCondition: booleanEquals($RequiresAccountId, true) = true
endpoints evaluateCondition: not(isSet($AccountId)) = false
endpoints evaluateCondition: isSet($AccountId) = true
endpoints evaluateCondition: not(isValidHostLabel($AccountId, false)) = false
endpoints evaluateCondition: isSet($Endpoint) = false
endpoints evaluateCondition: booleanEquals($UseFIPS, true) = false
endpoints evaluateCondition: booleanEquals($UseFIPS, true) = false
endpoints evaluateCondition: booleanEquals($UseFIPS, true) = false
endpoints evaluateCondition: booleanEquals($UseFIPS, true) = false
endpoints evaluateCondition: booleanEquals($UseFIPS, false) = true
endpoints evaluateCondition: booleanEquals($UseDualStack, true) = false
endpoints evaluateCondition: booleanEquals($UseFIPS, false) = true
endpoints evaluateCondition: booleanEquals($UseDualStack, true) = false
endpoints evaluateCondition: booleanEquals($UseFIPS, false) = true
endpoints evaluateCondition: booleanEquals($UseDualStack, false) = true
endpoints evaluateCondition: isSet($RequiresAccountId) = true
endpoints evaluateCondition: booleanEquals($RequiresAccountId, true) = true
endpoints evaluateCondition: isSet($AccountId) = true
endpoints Resolving endpoint from template: {
  "url": "https://{AccountId}.s3-control.{Region}.{partitionResult#dnsSuffix}",
  "properties": {
    "authSchemes": [
      {
        "disableDoubleEncoding": true,
        "name": "sigv4",
        "signingName": "s3",
        "signingRegion": "{Region}"
      }
    ]
  },
  "headers": {}
}
endpoints Resolved endpoint: {
  "headers": {},
  "properties": {
    "authSchemes": [
      {
        "disableDoubleEncoding": true,
        "name": "sigv4",
        "signingName": "s3",
        "signingRegion": "us-east-1"
      }
    ]
  },
  "url": "https://112233445566.s3-control.us-east-1.amazonaws.com/"
}
{
  clientName: 'S3ControlClient',
  commandName: 'GetPublicAccessBlockCommand',
  input: { AccountId: 112233445566 },
  error: TypeError: headers[headerName].trim is not a function
      at getCanonicalHeaders (/path/get-s3-public-access-block-status/node_modules/@smithy/signature-v4/dist-cjs/index.js:129:58)
      at _SignatureV4.signRequest (/path/get-s3-public-access-block-status/node_modules/@smithy/signature-v4/dist-cjs/index.js:378:30)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async _AwsSdkSigV4Signer.sign (/path/get-s3-public-access-block-status/node_modules/@aws-sdk/core/dist-cjs/index.js:112:27)
      at async /path/get-s3-public-access-block-status/node_modules/@smithy/core/dist-cjs/index.js:167:14
      at async /path/get-s3-public-access-block-status/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
      at async /path/get-s3-public-access-block-status/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22
      at async file:///path/get-s3-public-access-block-status/node_modules/p-queue/dist/index.js:187:36 {
    '$metadata': { attempts: 1, totalRetryDelay: 0 }
  },
  metadata: { attempts: 1, totalRetryDelay: 0 }
}
kabo commented 9 months ago

I figured out what was causing the issue in my case. I was taking account id as input from outside and it should be a string but turned out to be a number. Example code to reproduce the error:

import {
  GetPublicAccessBlockCommand,
  S3ControlClient,
} from '@aws-sdk/client-s3-control'

const s3 = new S3ControlClient({ region: 'us-east-1' })

s3.send(new GetPublicAccessBlockCommand({ AccountId: 112233445566 as unknown as string }))
  .then(({ PublicAccessBlockConfiguration }) => PublicAccessBlockConfiguration)
  .then(JSON.stringify)
  .then(console.log)
  .catch(console.error)

Would've been nice if the error was "AccountId should be a string" instead of "headers[headerName].trim is not a function"...

RanVaknin commented 3 months ago

Seems like this is solved. If you are a new customer running into this issue, please create a new bug report.

Closing.

Thanks, Ran~

github-actions[bot] commented 2 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.