shelfio / jest-dynamodb

Jest preset for DynamoDB local server
MIT License
181 stars 38 forks source link

UnrecognizedClientException #212

Open skilbjo opened 1 year ago

skilbjo commented 1 year ago

Have been using @shelf/jest-dynamodb for 2 yrs and just started getting this error in github actions (not locally)

    UnrecognizedClientException: The Access Key ID or security token is invalid.

      at Request.extractError (node_modules/aws-sdk/lib/protocol/json.js:52:27)
      at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:106:20)
      at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:78:10)
      at Request.emit (node_modules/aws-sdk/lib/request.js:686:14)
      at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
      at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
      at node_modules/aws-sdk/lib/state_machine.js:26:10
      at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
      at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:688:12)
      at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:116:18)
      at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:78:10)
      at Request.emit (node_modules/aws-sdk/lib/request.js:686:14)
      at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
      at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
      at node_modules/aws-sdk/lib/state_machine.js:26:10
      at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
      at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:688:12)
      at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:116:18)
      at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:96:12)
      at IncomingMessage.onEnd (node_modules/aws-sdk/lib/event_listeners.js:335:13)

package.json

...
    "@shelf/jest-dynamodb": "3.4.1",
    "aws-sdk": "2.1046.0",
...

test/file.test.ts

  const localClient = new AWS.DynamoDB({
    credentials: {
      accessKeyId: 'fake-access-key',
      secretAccessKey: 'fake-secret-access-key',
    },
    endpoint,
    region: 'local',
    sslEnabled: false,
  });
...

src/file.ts

...
  dynamo
    .putItem(<AWS.DynamoDB.PutItemInput>{
      ...(!overwrite && { ConditionExpression: 'attribute_not_exists(PK)' }), // if overwrite is true, the ConditionExpression is omitted
      Item: item,
      TableName: tableName,
    })
    .promise();
...

github actions:

jobs:
  publish-artifact:
    permissions:
      issues: write
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v3
        with:
          token: ${{ secrets.ACTIONS_TOKEN }}
          persist-credentials: false
      - uses: actions/setup-node@v3
        with:
          node-version: 16 

...
skilbjo commented 1 year ago

this just started happening as of 29 June; without any code changes.

I go back to a working build, click "re-run jobs" and it fails w the above error. So I think it's something to do with github actions.

Anyways opening a thread here to see if anyone has come up w a workaround

0aprl1 commented 1 year ago

My team just suffered the same issue. One of my teammates realized that when initializing dynamodb-local, we were downloading the latest version from AWS repo. We now have pinned to the latest working version, dynamodb_local_2023-06-09.tar.gz, and the issue is solved.

skilbjo commented 1 year ago

@0aprl1

could you expand? how did you get it to work? can you share the code sample of your workaround?

slmarcos commented 1 year ago

@skilbjo I just setup this config on my config file

  installerConfig: {
    downloadUrl: 'https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/dynamodb_local_2023-06-09.tar.gz'
  }
skilbjo commented 1 year ago

great! thanks for that. and just to clarify, here's where it goes:

jest-dynamodb-config.js

module.exports = {
  // https://github.com/shelfio/jest-dynamodb/issues/212
  installerConfig: {
    downloadUrl:
      'https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/dynamodb_local_2023-06-09.tar.gz',
  },

i will leave the issue open in case the maintainer wants to address, but ^^^ is a valid workaround atm

rommelcapa commented 1 year ago

Same issue here. What worked for us was "installing" an older version of dynamodb-local before running the unit test.

module.exports = {
  installerConfig: {
    installPath: '.dynamodb',
  },
}
#!/bin/sh
curl -O https://d1ni2b6xgvw0s0.cloudfront.net/dynamodb_local_2023-06-09.tar.gz <-- this is where the link goes from AWS in my part of the world. Chose the same file from @skilbjo above
mkdir -p .dynamodb && mv dynamodb_local_2023-06-09.tar.gz .dynamodb/
tar -xvf .dynamodb/dynamodb_local_2023-06-09.tar.gz -C .dynamodb/
npm run test

You can add checks not to run the above commands everytime you run the test.

venkatesh-kl commented 6 months ago

For anyone who is facing this issue, this is the fix which resolved the issues for me. Add below logic to your jest-dynamodb-config.js: jest-dynamodb-config.js before the fix:

module.exports = {
  // standard table configuration, ports etc
};

Solution:

module.exports = {
  // defaulting to a release of dynamodb as latest version needs some changes to the environment variables
  // References: https://github.com/shelfio/jest-dynamodb/issues/212
  // https://repost.aws/articles/ARc4hEkF9CRgOrw8kSMe6CwQ/troubleshooting-the-access-key-id-or-security-token-is-invalid-error-after-upgrading-dynamodb-local-to-version-2-0-or-greater
 installerConfig: {
    downloadUrl: 'https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/dynamodb_local_2023-06-09.tar.gz',
  },
    // standard table configuration, ports etc
};