aws/aws-sdk-js-v3

Cannot read property 'byteLength' of undefined

Opened this issue · 7 comments

Describe the bug

  • Authenticated primary account using @aws-sdk/credential-provider-sso
  • Leveraged STSClient and AssumeRoleCommand to assume role with SSO Creds to another AWS Account
  • Leveraged STS Credentials to Open a Connection to S3Client and ListBucketsCommand
  • Bug is apparent utilising the above method and repeatable

Your environment

SDK version number

"@aws-sdk/client-s3": "^3.17.0",
"@aws-sdk/client-sts": "^3.17.0",
"@aws-sdk/credential-provider-sso": "^3.17.0",

Is the issue in the browser/Node.js/ReactNative?

Node.JS

Details of the browser/Node.js/ReactNative version

v14.16.1

Steps to reproduce

const { fromSSO } = require("@aws-sdk/credential-provider-sso");
const { S3Client, ListBucketsCommand } = require("@aws-sdk/client-s3");
const { STSClient, AssumeRoleCommand } = require("@aws-sdk/client-sts");

const awsProfile = "default";
const awsRegion = "ap-southeast-2";
const awsAssumedRole = {
  RoleArn: "arn:aws:iam::x:role/x",
  RoleSessionName: "currentSession",
};

// Use AWS CLI SSO to create default authentication
const connectionConfig = {
  credentials: fromSSO({ profile: awsProfile }),
  region: awsRegion,
};

// Using SSO Credentials assume a role to a customers environment
const stsClient = new STSClient(connectionConfig);
const stsCommand = new AssumeRoleCommand(awsAssumedRole);

// Query STS to Assume role and generate credentials
stsClient
  .send(stsCommand)
  .then((stsData) => {
    // This method where change the object's keys to lowercase works
    // let currentCredentials = {
    //   credentials: {
    //     accessKeyId: stsData.Credentials.AccessKeyId,
    //     secretAccessKey: stsData.Credentials.SecretAccessKey,
    //     sessionToken: stsData.Credentials.SessionToken,
    //   },
    //   region: awsRegion,
    // };

    // This method always fails with error:
    // TypeError: Cannot read property 'byteLength' of undefined
    let currentCredentials = {
      credentials: stsData.Credentials,
      region: awsRegion,
    };

    // Create connection to S3
    let s3Command = new ListBucketsCommand({});
    let s3Client = new S3Client(currentCredentials);

    // Query Buckets using S3 Config
    s3Client
      .send(s3Command)
      .then((s3Data) => {
        console.log(s3Data.Buckets);
      })
      .catch((err) => {
        console.error(err);
      });
  })
  .catch((err) => {
    console.error(err);
  });

Observed behavior

Fails to login and provides error described in title see extended error below

Expected behavior

Login as intended without have to recreate the object with different key spelling

Additional context

TypeError: Cannot read property 'byteLength' of undefined
    at Object.fromArrayBuffer (C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\util-buffer-from\dist\cjs\index.js:6:60)
    at castSourceData (C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\hash-node\dist\cjs\index.js:29:31)
    at Hash.update (C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\hash-node\dist\cjs\index.js:12:26)
    at hmac (C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\signature-v4\dist\cjs\credentialDerivation.js:60:10)
    at Object.getSigningKey (C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\signature-v4\dist\cjs\credentialDerivation.js:32:29)
    at SignatureV4.getSigningKey (C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\signature-v4\dist\cjs\SignatureV4.js:139:39)
    at SignatureV4.signRequest (C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\signature-v4\dist\cjs\SignatureV4.js:98:73)
    at async C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\middleware-signing\dist\cjs\middleware.js:14:22
    at async StandardRetryStrategy.retry (C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\middleware-retry\dist\cjs\defaultStrategy.js:56:46)
    at async C:\Users\benja\code\wip-js\patching-automation\node_modules\@aws-sdk\middleware-logger\dist\cjs\loggerMiddleware.js:6:22 {
  '$metadata': { attempts: 1, totalRetryDelay: 0 }
}

I'm facing the same issue with "@aws-sdk/client-elastic-transcoder": "^3.17.0".

const client: ElasticTranscoderClient =
  new ElasticTranscoderClient<ElasticTranscoderClientConfig>({
    credentials: {
      AccessKeyId: AWS_ACCESS_KEY_ID,
      SecretAccessKey: AWS_SECRET_ACCESS_KEY,
    },
    region: AWS_STORAGE_MEDIA_INPUT_BUCKET_REGION,
  });

const command: CreateJobCommand =
  new CreateJobCommand<CreateJobCommandInput>({
    PipelineId: AWS_ELASTIC_TRANSCODER_PIPELINE_ID,
    Inputs: [
      {
        Key: INPUT_KEY,
      },
    ],
    Outputs: [
      {
        Key: INPUT_KEY + "/hls_1000k_",
        PresetId: "1351620000001-200030",
        SegmentDuration: "1",
        ThumbnailPattern: "{resolution}_{count}",
      },
    ],
  });

try {
  const data = await client.send(command);
} catch (error) {
 console.error(error)
} finally {
  // finally.
}

which returns

ERROR [TypeError: undefined is not an object (evaluating 'data.byteLength')]

Any idea on what it might be?

I'll go ahead and create a repo to reproduce this.

Here is a minimal repo.

Hi @ernestofgonzalez, thanks for opening this issue, can you check if you get the same error with the latest version of the SDK?

I used the code:

import { ElasticTranscoderClient, CreateJobCommand } from "@aws-sdk/client-elastic-transcoder";


(async ()=> {
    const client: ElasticTranscoderClient =
    new ElasticTranscoderClient({
      region: "us-west-2",
    });
  
  const command: CreateJobCommand =
  new CreateJobCommand({
      PipelineId: "id-123",
      Inputs: [
        {
          Key: 	
          "video-to-pipe.mp4",
        },
      ],
      Outputs: [
        {
          Key: "key" + '/hls_1000k_',
          PresetId: '1351620000001-200030',
          SegmentDuration: '1',
          ThumbnailPattern: '{resolution}_{count}',
        },
      ],
    });
  
  try {
    const data = await client.send(command);

    console.log(data)
  } catch (error) {
   console.log(error)
  } finally {
    // finally.
  }
})();

And I get httpStatusCode: 201
@aws-sdk/client-elastic-transcoder@3.22.0

Hi @ernestofgonzalez, thanks for opening this issue, can you check if you get the same error with the latest version of the SDK?

I used the code:

import { ElasticTranscoderClient, CreateJobCommand } from "@aws-sdk/client-elastic-transcoder";


(async ()=> {
    const client: ElasticTranscoderClient =
    new ElasticTranscoderClient({
      region: "us-west-2",
    });
  
  const command: CreateJobCommand =
  new CreateJobCommand({
      PipelineId: "id-123",
      Inputs: [
        {
          Key: 	
          "video-to-pipe.mp4",
        },
      ],
      Outputs: [
        {
          Key: "key" + '/hls_1000k_',
          PresetId: '1351620000001-200030',
          SegmentDuration: '1',
          ThumbnailPattern: '{resolution}_{count}',
        },
      ],
    });
  
  try {
    const data = await client.send(command);

    console.log(data)
  } catch (error) {
   console.log(error)
  } finally {
    // finally.
  }
})();

And I get httpStatusCode: 201
@aws-sdk/client-elastic-transcoder@3.22.0

Hi @ajredniwja. Thanks for answering.

Eventually I fixed it. I think it occurs when you don't have the required permissions to do the action you're requesting.
In my case, the user whose credentials I was using did not have all the Elastic Transcoder and S3 permissions necessary. As soon as I fixed this, all worked correctly.

I can't today, but I'll test it with @aws-sdk/client-elastic-transcoder@3.22.0 and a user without the necessary permissions and see if I get the same error. I'll get back to you.

This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.

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.