-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement getLocationCredentials handler & integrate with adapt…
…er (#13600)
- Loading branch information
Showing
10 changed files
with
284 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/storage/__tests__/storageBrowser/apis/getDataAccess.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getDataAccess } from '../../../src/storageBrowser/apis/getDataAccess'; | ||
import { getDataAccess as getDataAccessClient } from '../../../src/providers/s3/utils/client/s3control'; | ||
import { GetDataAccessInput } from '../../../src/storageBrowser/apis/types'; | ||
|
||
jest.mock('../../../src/providers/s3/utils/client/s3control'); | ||
|
||
const MOCK_ACCOUNT_ID = 'accountId'; | ||
const MOCK_REGION = 'us-east-2'; | ||
const MOCK_ACCESS_ID = 'accessId'; | ||
const MOCK_SECRET_ACCESS_KEY = 'secretAccessKey'; | ||
const MOCK_SESSION_TOKEN = 'sessionToken'; | ||
const MOCK_EXPIRATION = '2013-09-17T18:07:53.000Z'; | ||
const MOCK_EXPIRATION_DATE = new Date(MOCK_EXPIRATION); | ||
const MOCK_SCOPE = 's3://mybucket/files/*'; | ||
const MOCK_CREDENTIALS = { | ||
credentials: { | ||
accessKeyId: MOCK_ACCESS_ID, | ||
secretAccessKey: MOCK_SECRET_ACCESS_KEY, | ||
sessionToken: MOCK_SESSION_TOKEN, | ||
expiration: MOCK_EXPIRATION_DATE, | ||
}, | ||
}; | ||
const MOCK_ACCESS_CREDENTIALS = { | ||
AccessKeyId: MOCK_ACCESS_ID, | ||
SecretAccessKey: MOCK_SECRET_ACCESS_KEY, | ||
SessionToken: MOCK_SESSION_TOKEN, | ||
Expiration: MOCK_EXPIRATION_DATE, | ||
}; | ||
const MOCK_CREDENTIAL_PROVIDER = async () => MOCK_CREDENTIALS; | ||
|
||
const sharedGetDataAccessParams: GetDataAccessInput = { | ||
accountId: MOCK_ACCOUNT_ID, | ||
credentialsProvider: MOCK_CREDENTIAL_PROVIDER, | ||
durationSeconds: 900, | ||
permission: 'READWRITE', | ||
region: MOCK_REGION, | ||
scope: MOCK_SCOPE, | ||
}; | ||
|
||
describe('getDataAccess', () => { | ||
const getDataAccessClientMock = getDataAccessClient as jest.Mock; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
getDataAccessClientMock.mockResolvedValue({ | ||
Credentials: MOCK_ACCESS_CREDENTIALS, | ||
MatchedGrantTarget: MOCK_SCOPE, | ||
}); | ||
}); | ||
|
||
it('should invoke the getDataAccess client correctly', async () => { | ||
const result = await getDataAccess(sharedGetDataAccessParams); | ||
|
||
expect(getDataAccessClientMock).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
credentials: MOCK_CREDENTIALS.credentials, | ||
region: MOCK_REGION, | ||
userAgentValue: expect.stringContaining('storage/8'), | ||
}), | ||
expect.objectContaining({ | ||
AccountId: MOCK_ACCOUNT_ID, | ||
Target: MOCK_SCOPE, | ||
Permission: 'READWRITE', | ||
TargetType: undefined, | ||
DurationSeconds: 900, | ||
}), | ||
); | ||
|
||
expect(result.credentials).toEqual(MOCK_CREDENTIALS.credentials); | ||
expect(result.scope).toEqual(MOCK_SCOPE); | ||
}); | ||
|
||
it('should throw an error if the service does not return credentials', async () => { | ||
expect.assertions(1); | ||
|
||
getDataAccessClientMock.mockResolvedValue({ | ||
Credentials: undefined, | ||
MatchedGrantTarget: MOCK_SCOPE, | ||
}); | ||
|
||
expect(getDataAccess(sharedGetDataAccessParams)).rejects.toThrow( | ||
'Service did not return credentials.', | ||
); | ||
}); | ||
|
||
it('should set the correct target type when accessing an object', async () => { | ||
const MOCK_OBJECT_SCOPE = 's3://mybucket/files/file.md'; | ||
|
||
getDataAccessClientMock.mockResolvedValue({ | ||
Credentials: MOCK_ACCESS_CREDENTIALS, | ||
MatchedGrantTarget: MOCK_OBJECT_SCOPE, | ||
}); | ||
|
||
const result = await getDataAccess({ | ||
...sharedGetDataAccessParams, | ||
scope: MOCK_OBJECT_SCOPE, | ||
}); | ||
|
||
expect(getDataAccessClientMock).toHaveBeenCalledWith( | ||
expect.any(Object), | ||
expect.objectContaining({ | ||
AccountId: MOCK_ACCOUNT_ID, | ||
Target: MOCK_OBJECT_SCOPE, | ||
Permission: 'READWRITE', | ||
TargetType: 'Object', | ||
DurationSeconds: 900, | ||
}), | ||
); | ||
|
||
expect(result.scope).toEqual(MOCK_OBJECT_SCOPE); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export const DEFAULT_CRED_TTL = 15 * 60; // 15 minutes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
AmplifyErrorCode, | ||
StorageAction, | ||
} from '@aws-amplify/core/internals/utils'; | ||
|
||
import { getStorageUserAgentValue } from '../../providers/s3/utils/userAgent'; | ||
import { getDataAccess as getDataAccessClient } from '../../providers/s3/utils/client/s3control'; | ||
import { StorageError } from '../../errors/StorageError'; | ||
import { logger } from '../../utils'; | ||
|
||
import { GetDataAccessInput, GetDataAccessOutput } from './types'; | ||
import { DEFAULT_CRED_TTL } from './constants'; | ||
|
||
export const getDataAccess = async ( | ||
input: GetDataAccessInput, | ||
): Promise<GetDataAccessOutput> => { | ||
const targetType = input.scope.endsWith('*') ? undefined : 'Object'; | ||
const { credentials } = await input.credentialsProvider(); | ||
|
||
const result = await getDataAccessClient( | ||
{ | ||
credentials, | ||
region: input.region, | ||
userAgentValue: getStorageUserAgentValue(StorageAction.GetDataAccess), | ||
}, | ||
{ | ||
AccountId: input.accountId, | ||
Target: input.scope, | ||
Permission: input.permission, | ||
TargetType: targetType, | ||
DurationSeconds: DEFAULT_CRED_TTL, | ||
}, | ||
); | ||
|
||
const grantCredentials = result.Credentials; | ||
|
||
// Ensure that S3 returned credentials (this shouldn't happen) | ||
if (!grantCredentials) { | ||
throw new StorageError({ | ||
name: AmplifyErrorCode.Unknown, | ||
message: 'Service did not return credentials.', | ||
}); | ||
} else { | ||
logger.debug(`Retrieved credentials for: ${result.MatchedGrantTarget}`); | ||
} | ||
|
||
const { | ||
AccessKeyId: accessKeyId, | ||
SecretAccessKey: secretAccessKey, | ||
SessionToken: sessionToken, | ||
Expiration: expiration, | ||
} = grantCredentials; | ||
|
||
return { | ||
credentials: { | ||
accessKeyId: accessKeyId!, | ||
secretAccessKey: secretAccessKey!, | ||
sessionToken, | ||
expiration, | ||
}, | ||
scope: result.MatchedGrantTarget, | ||
}; | ||
}; |
13 changes: 4 additions & 9 deletions
13
.../storageBrowser/listCallerAccessGrants.ts → ...ageBrowser/apis/listCallerAccessGrants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
AccessGrant, | ||
CredentialsProvider, | ||
ListLocationsOutput, | ||
LocationCredentials, | ||
Permission, | ||
PrefixType, | ||
Privilege, | ||
} from '../types'; | ||
|
||
export interface ListCallerAccessGrantsInput { | ||
accountId: string; | ||
credentialsProvider: CredentialsProvider; | ||
region: string; | ||
} | ||
|
||
export type ListCallerAccessGrantsOutput = ListLocationsOutput<AccessGrant>; | ||
|
||
export interface GetDataAccessInput { | ||
accountId: string; | ||
credentialsProvider: CredentialsProvider; | ||
durationSeconds?: number; | ||
permission: Permission; | ||
prefixType?: PrefixType; | ||
privilege?: Privilege; | ||
region: string; | ||
scope: string; | ||
} | ||
|
||
export type GetDataAccessOutput = LocationCredentials; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.