Skip to content

Commit

Permalink
fix(cli): failure if account cache is malformed (aws#10887)
Browse files Browse the repository at this point in the history
Because of concurrent running of integration tests, the account
cache (which is supposed to be a JSON file) can be read in a state
where it's empty or incompletely written, which fails the JSON parse.

If that happens, ignore the error and pretend the cache is empty.

Fixes sporadic concurrency issues in the integration tests.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr authored Oct 16, 2020
1 parent ddff369 commit 9b2438a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/aws-cdk/lib/api/aws-auth/account-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export class AccountAccessKeyCache {
// File doesn't exist or is not readable. This is a cache,
// pretend we successfully loaded an empty map.
if (e.code === 'ENOENT' || e.code === 'EACCES') { return {}; }
// File is not JSON, could be corrupted because of concurrent writes.
// Again, an empty cache is fine.
if (e instanceof SyntaxError) { return {}; }
throw e;
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/aws-cdk/test/account-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,14 @@ test(`cache is nuked if it exceeds ${AccountAccessKeyCache.MAX_ENTRIES} entries`
await nukeCache(cacheDir);
}
});

test('cache pretends to be empty if cache file does not contain JSON', async() => {
const { cacheDir, cacheFile, cache } = await makeCache();
try {
await fs.writeFile(cacheFile, '');

await expect(cache.get('abc')).resolves.toEqual(undefined);
} finally {
await nukeCache(cacheDir);
}
});

0 comments on commit 9b2438a

Please sign in to comment.