From 7a86ea43b027e7bb0f92af03eb08fb73dba6603d Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Wed, 10 Jul 2024 08:59:20 -0700 Subject: [PATCH 01/10] feat(storage): initial commit to create managed auth adapter --- .../createManagedAuthAdapter.ts | 8 +++ .../createManagedAuthAdapter.ts | 54 +++++++++++++++++++ packages/storage/src/storageBrowser/index.ts | 2 +- .../storageBrowser/listCallerAccessGrants.ts | 5 -- .../src/storageBrowser/managedAuthAdapter.ts | 27 ---------- 5 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthAdapter.ts create mode 100644 packages/storage/src/storageBrowser/createManagedAuthAdapter.ts delete mode 100644 packages/storage/src/storageBrowser/managedAuthAdapter.ts diff --git a/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthAdapter.ts b/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthAdapter.ts new file mode 100644 index 00000000000..b42fbff3afc --- /dev/null +++ b/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthAdapter.ts @@ -0,0 +1,8 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +describe('createManagedAuthAdapter', () => { + it.todo('should pass region to the adapter'); + it.todo('should create list locations handler'); + it.todo('should create get location credentials handler'); +}); diff --git a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts new file mode 100644 index 00000000000..1483602e596 --- /dev/null +++ b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts @@ -0,0 +1,54 @@ +/* eslint-disable unused-imports/no-unused-vars */ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import { + CredentialsProvider, + ListLocationsHandler, + LocationCredentialsHandler, +} from '../types'; + +import { createListLocationsHandler } from './createListLocationsHandler'; +import { createLocationCredentialsHandler } from './createLocationCredentialsHandler'; +import { credentialsCachingDecorator } from './credentialsCachingDecorator'; + +export interface CreateManagedAuthAdapterInput { + accountId: string; + region: string; + credentialsProvider: CredentialsProvider; +} + +export interface ManagedAuthAdaptor { + getHandlers(): { + listLocations: ListLocationsHandler; + getLocationCredentials: LocationCredentialsHandler; + }; + region: string; +} + +export const createManagedAuthAdapter = ( + // eslint-disable-next-line unused-imports/no-unused-vars + { credentialsProvider, region, accountId }: CreateManagedAuthAdapterInput, +): ManagedAuthAdaptor => { + return { + getHandlers: () => { + const credentialsProviderCaching = + credentialsCachingDecorator(credentialsProvider); + const listLocations = createListLocationsHandler({ + credentialsProvider: credentialsProviderCaching, + accountId, + region, + }); + const getLocationCredentials = createLocationCredentialsHandler({ + credentialsProvider: credentialsProviderCaching, + accountId, + region, + }); + + return { + listLocations, + getLocationCredentials, + }; + }, + region, + }; +}; diff --git a/packages/storage/src/storageBrowser/index.ts b/packages/storage/src/storageBrowser/index.ts index 7d36d419558..6df4a6a516a 100644 --- a/packages/storage/src/storageBrowser/index.ts +++ b/packages/storage/src/storageBrowser/index.ts @@ -15,6 +15,6 @@ export { export { createLocationCredentialsHandler } from './createLocationCredentialsHandler'; export { createLocationCredentialsStore } from './locationCredentialsStore'; export { - managedAuthAdapter, + createManagedAuthAdapter as managedAuthAdapter, ManagedAuthAdapterInput, } from './managedAuthAdapter'; diff --git a/packages/storage/src/storageBrowser/listCallerAccessGrants.ts b/packages/storage/src/storageBrowser/listCallerAccessGrants.ts index cd13e6a5dc5..c55b0ea75e8 100644 --- a/packages/storage/src/storageBrowser/listCallerAccessGrants.ts +++ b/packages/storage/src/storageBrowser/listCallerAccessGrants.ts @@ -7,11 +7,6 @@ export interface ListCallerAccessGrantsInput { accountId: string; credentialsProvider: CredentialsProvider; region: string; - options?: { - nextToken?: string; - // Default to 100; If > 1000, API will make multiple API calls. - pageSize?: number; - }; } export type ListCallerAccessGrantsOutput = ListLocationsOutput; diff --git a/packages/storage/src/storageBrowser/managedAuthAdapter.ts b/packages/storage/src/storageBrowser/managedAuthAdapter.ts deleted file mode 100644 index 9f3214b3158..00000000000 --- a/packages/storage/src/storageBrowser/managedAuthAdapter.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -import { - CredentialsProvider, - ListLocations, - LocationCredentialsHandler, -} from './types'; - -export interface ManagedAuthAdapterInput { - accountId: string; - region: string; - credentialsProvider: CredentialsProvider; -} - -export interface ManagedAuthAdapterOutput { - listLocations: ListLocations; - getLocationCredentials: LocationCredentialsHandler; - region: string; -} - -export const managedAuthAdapter = ( - // eslint-disable-next-line unused-imports/no-unused-vars - input: ManagedAuthAdapterInput, -): ManagedAuthAdapterOutput => { - // TODO(@AllanZhengYP) - throw new Error('Not implemented'); -}; From 46e73735ef9970024bed02ce305cf43cc7766c5a Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Wed, 10 Jul 2024 09:43:30 -0700 Subject: [PATCH 02/10] feat(storage): renaming ListLocations and GetLocationCredentials interface name --- .../createLocationCredentialsHandler.ts | 4 ++-- .../createManagedAuthAdapter.ts | 20 +++++++++---------- packages/storage/src/storageBrowser/index.ts | 12 ++--------- .../locationCredentialsStore/create.ts | 4 ++-- .../locationCredentialsStore/registry.ts | 4 ++-- .../locationCredentialsStore/store.ts | 8 ++++---- packages/storage/src/storageBrowser/types.ts | 16 +++++++++++---- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/storage/src/storageBrowser/createLocationCredentialsHandler.ts b/packages/storage/src/storageBrowser/createLocationCredentialsHandler.ts index 5da981d71bc..8ed71cdc399 100644 --- a/packages/storage/src/storageBrowser/createLocationCredentialsHandler.ts +++ b/packages/storage/src/storageBrowser/createLocationCredentialsHandler.ts @@ -1,7 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { CredentialsProvider, LocationCredentialsHandler } from './types'; +import { CredentialsProvider, GetLocationCredentials } from './types'; interface CreateLocationCredentialsHandlerInput { accountId: string; @@ -12,7 +12,7 @@ interface CreateLocationCredentialsHandlerInput { export const createLocationCredentialsHandler = ( // eslint-disable-next-line unused-imports/no-unused-vars input: CreateLocationCredentialsHandlerInput, -): LocationCredentialsHandler => { +): GetLocationCredentials => { // TODO(@AllanZhengYP) throw new Error('Not Implemented'); }; diff --git a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts index 1483602e596..452f16324c6 100644 --- a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts +++ b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts @@ -1,11 +1,10 @@ -/* eslint-disable unused-imports/no-unused-vars */ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { CredentialsProvider, - ListLocationsHandler, - LocationCredentialsHandler, -} from '../types'; + GetLocationCredentials, + ListLocations, +} from './types'; import { createListLocationsHandler } from './createListLocationsHandler'; import { createLocationCredentialsHandler } from './createLocationCredentialsHandler'; @@ -19,16 +18,17 @@ export interface CreateManagedAuthAdapterInput { export interface ManagedAuthAdaptor { getHandlers(): { - listLocations: ListLocationsHandler; - getLocationCredentials: LocationCredentialsHandler; + listLocations: ListLocations; + getLocationCredentials: GetLocationCredentials; }; region: string; } -export const createManagedAuthAdapter = ( - // eslint-disable-next-line unused-imports/no-unused-vars - { credentialsProvider, region, accountId }: CreateManagedAuthAdapterInput, -): ManagedAuthAdaptor => { +export const createManagedAuthAdapter = ({ + credentialsProvider, + region, + accountId, +}: CreateManagedAuthAdapterInput): ManagedAuthAdaptor => { return { getHandlers: () => { const credentialsProviderCaching = diff --git a/packages/storage/src/storageBrowser/index.ts b/packages/storage/src/storageBrowser/index.ts index 6df4a6a516a..a6b5435244c 100644 --- a/packages/storage/src/storageBrowser/index.ts +++ b/packages/storage/src/storageBrowser/index.ts @@ -7,14 +7,6 @@ * @aws-amplify/ui-react-storage */ -export { - listCallerAccessGrants, - ListCallerAccessGrantsInput, - ListCallerAccessGrantsOutput, -} from './listCallerAccessGrants'; -export { createLocationCredentialsHandler } from './createLocationCredentialsHandler'; export { createLocationCredentialsStore } from './locationCredentialsStore'; -export { - createManagedAuthAdapter as managedAuthAdapter, - ManagedAuthAdapterInput, -} from './managedAuthAdapter'; +export { createManagedAuthAdapter } from './createManagedAuthAdapter'; +export { GetLocationCredentials, ListLocations } from './types'; diff --git a/packages/storage/src/storageBrowser/locationCredentialsStore/create.ts b/packages/storage/src/storageBrowser/locationCredentialsStore/create.ts index dc8da21bff5..a54ae32a354 100644 --- a/packages/storage/src/storageBrowser/locationCredentialsStore/create.ts +++ b/packages/storage/src/storageBrowser/locationCredentialsStore/create.ts @@ -3,7 +3,7 @@ import { CredentialsLocation, - LocationCredentialsHandler, + GetLocationCredentials, LocationCredentialsStore, } from '../types'; import { StorageValidationErrorCode } from '../../errors/types/validation'; @@ -17,7 +17,7 @@ import { createStore, getValue, removeStore } from './registry'; import { validateCredentialsProviderLocation } from './validators'; export const createLocationCredentialsStore = (input: { - handler: LocationCredentialsHandler; + handler: GetLocationCredentials; }): LocationCredentialsStore => { const storeSymbol = createStore(input.handler); diff --git a/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts b/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts index 6c3d41780f9..54ed8e4db83 100644 --- a/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts +++ b/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts @@ -4,7 +4,7 @@ // SPDX-License-Identifier: Apache-2.0 import { AWSCredentials } from '@aws-amplify/core/internals/utils'; -import { CredentialsLocation, LocationCredentialsHandler } from '../types'; +import { CredentialsLocation, GetLocationCredentials } from '../types'; import { assertValidationError } from '../../errors/utils/assertValidationError'; import { StorageValidationErrorCode } from '../../errors/types/validation'; @@ -33,7 +33,7 @@ const storeRegistry = new WeakMap< * @internal */ export const createStore = ( - refreshHandler: LocationCredentialsHandler, + refreshHandler: GetLocationCredentials, size?: number, ) => { const storeSymbol = { value: Symbol('LocationCredentialsStore') }; diff --git a/packages/storage/src/storageBrowser/locationCredentialsStore/store.ts b/packages/storage/src/storageBrowser/locationCredentialsStore/store.ts index ac1901d669b..04e800365c7 100644 --- a/packages/storage/src/storageBrowser/locationCredentialsStore/store.ts +++ b/packages/storage/src/storageBrowser/locationCredentialsStore/store.ts @@ -6,7 +6,7 @@ import { AWSCredentials } from '@aws-amplify/core/internals/utils'; import { Permission } from '../../providers/s3/types/options'; -import { CredentialsLocation, LocationCredentialsHandler } from '../types'; +import { CredentialsLocation, GetLocationCredentials } from '../types'; import { assertValidationError } from '../../errors/utils/assertValidationError'; import { StorageValidationErrorCode } from '../../errors/types/validation'; @@ -35,7 +35,7 @@ const createCacheKey = (location: CredentialsLocation): CacheKey => */ export interface LruLocationCredentialsStore { capacity: number; - refreshHandler: LocationCredentialsHandler; + refreshHandler: GetLocationCredentials; values: Map; } @@ -43,7 +43,7 @@ export interface LruLocationCredentialsStore { * @internal */ export const initStore = ( - refreshHandler: LocationCredentialsHandler, + refreshHandler: GetLocationCredentials, size = CREDENTIALS_STORE_DEFAULT_SIZE, ): LruLocationCredentialsStore => { assertValidationError( @@ -117,7 +117,7 @@ export const fetchNewValue = async ( }; const dispatchRefresh = ( - refreshHandler: LocationCredentialsHandler, + refreshHandler: GetLocationCredentials, value: StoreValue, onRefreshFailure: () => void, ) => { diff --git a/packages/storage/src/storageBrowser/types.ts b/packages/storage/src/storageBrowser/types.ts index 2525560369f..3f184dcc313 100644 --- a/packages/storage/src/storageBrowser/types.ts +++ b/packages/storage/src/storageBrowser/types.ts @@ -68,11 +68,19 @@ export interface ListLocationsOutput { nextToken?: string; } -// Interface for listLocations() handler -export type ListLocations = () => Promise>; +/** + * @internal + */ +export interface ListLocationsInput { + pageSize?: number; + nextToken?: string; +} + +export type ListLocations = ( + input?: ListLocationsInput, +) => Promise>; -// Interface for getLocationCredentials() handler. -export type LocationCredentialsHandler = ( +export type GetLocationCredentials = ( input: CredentialsLocation, ) => Promise<{ credentials: AWSCredentials }>; From d6ba0420387247173cd879326e76286c41acfa72 Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Wed, 10 Jul 2024 09:46:38 -0700 Subject: [PATCH 03/10] feat(storage): export StorageBrowser config adapter interface --- .../storageBrowser/createManagedAuthAdapter.ts | 18 +++--------------- packages/storage/src/storageBrowser/index.ts | 6 +++++- packages/storage/src/storageBrowser/types.ts | 8 ++++++++ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts index 452f16324c6..e28643242ff 100644 --- a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts +++ b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts @@ -1,34 +1,22 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { - CredentialsProvider, - GetLocationCredentials, - ListLocations, -} from './types'; +import { CredentialsProvider, StorageBrowserConfigAdapter } from './types'; import { createListLocationsHandler } from './createListLocationsHandler'; import { createLocationCredentialsHandler } from './createLocationCredentialsHandler'; import { credentialsCachingDecorator } from './credentialsCachingDecorator'; -export interface CreateManagedAuthAdapterInput { +interface CreateManagedAuthAdapterInput { accountId: string; region: string; credentialsProvider: CredentialsProvider; } -export interface ManagedAuthAdaptor { - getHandlers(): { - listLocations: ListLocations; - getLocationCredentials: GetLocationCredentials; - }; - region: string; -} - export const createManagedAuthAdapter = ({ credentialsProvider, region, accountId, -}: CreateManagedAuthAdapterInput): ManagedAuthAdaptor => { +}: CreateManagedAuthAdapterInput): StorageBrowserConfigAdapter => { return { getHandlers: () => { const credentialsProviderCaching = diff --git a/packages/storage/src/storageBrowser/index.ts b/packages/storage/src/storageBrowser/index.ts index a6b5435244c..1c8a6b495d3 100644 --- a/packages/storage/src/storageBrowser/index.ts +++ b/packages/storage/src/storageBrowser/index.ts @@ -9,4 +9,8 @@ export { createLocationCredentialsStore } from './locationCredentialsStore'; export { createManagedAuthAdapter } from './createManagedAuthAdapter'; -export { GetLocationCredentials, ListLocations } from './types'; +export { + GetLocationCredentials, + ListLocations, + StorageBrowserConfigAdapter, +} from './types'; diff --git a/packages/storage/src/storageBrowser/types.ts b/packages/storage/src/storageBrowser/types.ts index 3f184dcc313..7cd98398de1 100644 --- a/packages/storage/src/storageBrowser/types.ts +++ b/packages/storage/src/storageBrowser/types.ts @@ -98,3 +98,11 @@ export interface LocationCredentialsStore { */ destroy(): void; } + +export interface StorageBrowserConfigAdapter { + getHandlers(): { + listLocations: ListLocations; + getLocationCredentials: GetLocationCredentials; + }; + region: string; +} From a6368bc29a26afb437558a2487d2ce13aa891ca7 Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Thu, 11 Jul 2024 10:13:56 -0700 Subject: [PATCH 04/10] fix(storage): simplify adapter interface --- .../createManagedAuthAdapter.ts | 33 ++++++++----------- packages/storage/src/storageBrowser/types.ts | 11 ++++--- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts index e28643242ff..0451b9964a6 100644 --- a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts +++ b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts @@ -4,7 +4,6 @@ import { CredentialsProvider, StorageBrowserConfigAdapter } from './types'; import { createListLocationsHandler } from './createListLocationsHandler'; import { createLocationCredentialsHandler } from './createLocationCredentialsHandler'; -import { credentialsCachingDecorator } from './credentialsCachingDecorator'; interface CreateManagedAuthAdapterInput { accountId: string; @@ -17,26 +16,20 @@ export const createManagedAuthAdapter = ({ region, accountId, }: CreateManagedAuthAdapterInput): StorageBrowserConfigAdapter => { - return { - getHandlers: () => { - const credentialsProviderCaching = - credentialsCachingDecorator(credentialsProvider); - const listLocations = createListLocationsHandler({ - credentialsProvider: credentialsProviderCaching, - accountId, - region, - }); - const getLocationCredentials = createLocationCredentialsHandler({ - credentialsProvider: credentialsProviderCaching, - accountId, - region, - }); + const listLocations = createListLocationsHandler({ + credentialsProvider, + accountId, + region, + }); + const getLocationCredentials = createLocationCredentialsHandler({ + credentialsProvider, + accountId, + region, + }); - return { - listLocations, - getLocationCredentials, - }; - }, + return { + listLocations, + getLocationCredentials, region, }; }; diff --git a/packages/storage/src/storageBrowser/types.ts b/packages/storage/src/storageBrowser/types.ts index 7cd98398de1..fc0e6d5132a 100644 --- a/packages/storage/src/storageBrowser/types.ts +++ b/packages/storage/src/storageBrowser/types.ts @@ -99,10 +99,13 @@ export interface LocationCredentialsStore { destroy(): void; } +/** + * Common interface for handlers to configure StorageBrowser behaviors + * + * @internal + */ export interface StorageBrowserConfigAdapter { - getHandlers(): { - listLocations: ListLocations; - getLocationCredentials: GetLocationCredentials; - }; + listLocations: ListLocations; + getLocationCredentials: GetLocationCredentials; region: string; } From fca6ef88cde7a919e711078b0f95bd7d1540ad7e Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Thu, 11 Jul 2024 16:53:43 -0700 Subject: [PATCH 05/10] feat(storage): address feedbacks --- .../createManagedAuthAdapter.ts | 8 --- .../createManagedAuthConfigAdapter.test.ts | 72 +++++++++++++++++++ .../createManagedAuthAdapter.ts | 35 --------- packages/storage/src/storageBrowser/index.ts | 8 +-- .../createListLocationsHandler.ts | 18 +++++ .../createLocationCredentialsHandler.ts | 2 +- .../createManagedAuthConfigAdapter.ts | 52 ++++++++++++++ .../managedAuthConfigAdapter/index.ts | 4 ++ packages/storage/src/storageBrowser/types.ts | 11 --- 9 files changed, 149 insertions(+), 61 deletions(-) delete mode 100644 packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthAdapter.ts create mode 100644 packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthConfigAdapter.test.ts delete mode 100644 packages/storage/src/storageBrowser/createManagedAuthAdapter.ts create mode 100644 packages/storage/src/storageBrowser/managedAuthConfigAdapter/createListLocationsHandler.ts rename packages/storage/src/storageBrowser/{ => managedAuthConfigAdapter}/createLocationCredentialsHandler.ts (87%) create mode 100644 packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts create mode 100644 packages/storage/src/storageBrowser/managedAuthConfigAdapter/index.ts diff --git a/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthAdapter.ts b/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthAdapter.ts deleted file mode 100644 index b42fbff3afc..00000000000 --- a/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthAdapter.ts +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -describe('createManagedAuthAdapter', () => { - it.todo('should pass region to the adapter'); - it.todo('should create list locations handler'); - it.todo('should create get location credentials handler'); -}); diff --git a/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthConfigAdapter.test.ts b/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthConfigAdapter.test.ts new file mode 100644 index 00000000000..124fc16cd7a --- /dev/null +++ b/packages/storage/__tests__/storageBrowser/managedAuthAdapter/createManagedAuthConfigAdapter.test.ts @@ -0,0 +1,72 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { createManagedAuthConfigAdapter } from '../../../src/storageBrowser/managedAuthConfigAdapter'; +import { createListLocationsHandler } from '../../../src/storageBrowser/managedAuthConfigAdapter/createListLocationsHandler'; +import { createLocationCredentialsHandler } from '../../../src/storageBrowser/managedAuthConfigAdapter/createLocationCredentialsHandler'; + +jest.mock( + '../../../src/storageBrowser/managedAuthConfigAdapter/createListLocationsHandler', +); +jest.mock( + '../../../src/storageBrowser/managedAuthConfigAdapter/createLocationCredentialsHandler', +); + +describe('createManagedAuthConfigAdapter', () => { + const region = 'us-foo-2'; + const accountId = 'XXXXXXXXXXXX'; + const credentialsProvider = jest.fn(); + + beforeEach(() => { + jest + .mocked(createListLocationsHandler) + .mockReturnValue('LIST_LOCATIONS_FN' as any); + jest + .mocked(createLocationCredentialsHandler) + .mockReturnValue('GET_LOCATION_CREDENTIALS_FN' as any); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should pass region to the adapter', () => { + expect(createManagedAuthConfigAdapter({ region } as any)).toMatchObject({ + region, + }); + }); + + it('should create list locations handler', () => { + expect( + createManagedAuthConfigAdapter({ + region, + accountId, + credentialsProvider, + }), + ).toMatchObject({ + listLocations: 'LIST_LOCATIONS_FN', + }); + expect(createListLocationsHandler).toHaveBeenCalledWith({ + region, + accountId, + credentialsProvider, + }); + }); + + it('should create get location credentials handler', () => { + expect( + createManagedAuthConfigAdapter({ + region, + accountId, + credentialsProvider, + }), + ).toMatchObject({ + getLocationCredentials: 'GET_LOCATION_CREDENTIALS_FN', + }); + expect(createLocationCredentialsHandler).toHaveBeenCalledWith({ + region, + accountId, + credentialsProvider, + }); + }); +}); diff --git a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts b/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts deleted file mode 100644 index 0451b9964a6..00000000000 --- a/packages/storage/src/storageBrowser/createManagedAuthAdapter.ts +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 -import { CredentialsProvider, StorageBrowserConfigAdapter } from './types'; - -import { createListLocationsHandler } from './createListLocationsHandler'; -import { createLocationCredentialsHandler } from './createLocationCredentialsHandler'; - -interface CreateManagedAuthAdapterInput { - accountId: string; - region: string; - credentialsProvider: CredentialsProvider; -} - -export const createManagedAuthAdapter = ({ - credentialsProvider, - region, - accountId, -}: CreateManagedAuthAdapterInput): StorageBrowserConfigAdapter => { - const listLocations = createListLocationsHandler({ - credentialsProvider, - accountId, - region, - }); - const getLocationCredentials = createLocationCredentialsHandler({ - credentialsProvider, - accountId, - region, - }); - - return { - listLocations, - getLocationCredentials, - region, - }; -}; diff --git a/packages/storage/src/storageBrowser/index.ts b/packages/storage/src/storageBrowser/index.ts index 1c8a6b495d3..455a64de505 100644 --- a/packages/storage/src/storageBrowser/index.ts +++ b/packages/storage/src/storageBrowser/index.ts @@ -8,9 +8,5 @@ */ export { createLocationCredentialsStore } from './locationCredentialsStore'; -export { createManagedAuthAdapter } from './createManagedAuthAdapter'; -export { - GetLocationCredentials, - ListLocations, - StorageBrowserConfigAdapter, -} from './types'; +export { createManagedAuthConfigAdapter } from './managedAuthConfigAdapter/createManagedAuthConfigAdapter'; +export { GetLocationCredentials, ListLocations } from './types'; diff --git a/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createListLocationsHandler.ts b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createListLocationsHandler.ts new file mode 100644 index 00000000000..c3e9c3c1a4a --- /dev/null +++ b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createListLocationsHandler.ts @@ -0,0 +1,18 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { CredentialsProvider, ListLocations } from '../types'; + +export interface CreateListLocationsHandlerInput { + accountId: string; + credentialsProvider: CredentialsProvider; + region: string; +} + +export const createListLocationsHandler = ( + // eslint-disable-next-line unused-imports/no-unused-vars + input: CreateListLocationsHandlerInput, +): ListLocations => { + // TODO(@AllanZhengYP) + throw new Error('Not Implemented'); +}; diff --git a/packages/storage/src/storageBrowser/createLocationCredentialsHandler.ts b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createLocationCredentialsHandler.ts similarity index 87% rename from packages/storage/src/storageBrowser/createLocationCredentialsHandler.ts rename to packages/storage/src/storageBrowser/managedAuthConfigAdapter/createLocationCredentialsHandler.ts index 8ed71cdc399..9cded212928 100644 --- a/packages/storage/src/storageBrowser/createLocationCredentialsHandler.ts +++ b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createLocationCredentialsHandler.ts @@ -1,7 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import { CredentialsProvider, GetLocationCredentials } from './types'; +import { CredentialsProvider, GetLocationCredentials } from '../types'; interface CreateLocationCredentialsHandlerInput { accountId: string; diff --git a/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts new file mode 100644 index 00000000000..4796a19adc8 --- /dev/null +++ b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts @@ -0,0 +1,52 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import { + CredentialsProvider, + GetLocationCredentials, + ListLocations, +} from '../types'; + +import { createListLocationsHandler } from './createListLocationsHandler'; +import { createLocationCredentialsHandler } from './createLocationCredentialsHandler'; + +interface CreateManagedAuthConfigAdapterInput { + accountId: string; + region: string; + credentialsProvider: CredentialsProvider; +} + +interface StorageBrowserAuthConfigAdapter { + listLocations: ListLocations; + getLocationCredentials: GetLocationCredentials; + region: string; +} + +/** + * Create configuration including handlers for StorageBrowser to call S3 Access Grants + * APIs to list and get credentials for different locations. + * + * @param options - Configuration options for the adapter. + * @returns - An object containing the handlers for StorageBrowser to call S3 Access Grants APIs and region + */ +export const createManagedAuthConfigAdapter = ({ + credentialsProvider, + region, + accountId, +}: CreateManagedAuthConfigAdapterInput): StorageBrowserAuthConfigAdapter => { + const listLocations = createListLocationsHandler({ + credentialsProvider, + accountId, + region, + }); + const getLocationCredentials = createLocationCredentialsHandler({ + credentialsProvider, + accountId, + region, + }); + + return { + listLocations, + getLocationCredentials, + region, + }; +}; diff --git a/packages/storage/src/storageBrowser/managedAuthConfigAdapter/index.ts b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/index.ts new file mode 100644 index 00000000000..4e04b1a2a77 --- /dev/null +++ b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/index.ts @@ -0,0 +1,4 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +export { createManagedAuthConfigAdapter } from './createManagedAuthConfigAdapter'; diff --git a/packages/storage/src/storageBrowser/types.ts b/packages/storage/src/storageBrowser/types.ts index fc0e6d5132a..3f184dcc313 100644 --- a/packages/storage/src/storageBrowser/types.ts +++ b/packages/storage/src/storageBrowser/types.ts @@ -98,14 +98,3 @@ export interface LocationCredentialsStore { */ destroy(): void; } - -/** - * Common interface for handlers to configure StorageBrowser behaviors - * - * @internal - */ -export interface StorageBrowserConfigAdapter { - listLocations: ListLocations; - getLocationCredentials: GetLocationCredentials; - region: string; -} From f1bab7eef4a9eafbfff4a316b95e83773849949e Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Thu, 11 Jul 2024 16:59:36 -0700 Subject: [PATCH 06/10] Revert "feat(storage): use WeakMap for store registry" This reverts commit db3e5ace1bd0cd7a8c2bd935f2710ad3bcf6be0d. --- .../locationCredentialsStore/registry.ts | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts b/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts index 54ed8e4db83..a06993a0640 100644 --- a/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts +++ b/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts @@ -15,19 +15,12 @@ import { initStore, } from './store'; -interface StoreRegistrySymbol { - readonly value: symbol; -} - /** * Keep all cache records for all instances of credentials store in a singleton * so we can reliably de-reference from the memory when we destroy a store * instance. */ -const storeRegistry = new WeakMap< - StoreRegistrySymbol, - LruLocationCredentialsStore ->(); +const storeRegistry = new Map(); /** * @internal @@ -36,10 +29,10 @@ export const createStore = ( refreshHandler: GetLocationCredentials, size?: number, ) => { - const storeSymbol = { value: Symbol('LocationCredentialsStore') }; - storeRegistry.set(storeSymbol, initStore(refreshHandler, size)); + const storeInstanceSymbol = Symbol('LocationCredentialsStore'); + storeRegistry.set(storeInstanceSymbol, initStore(refreshHandler, size)); - return storeSymbol; + return storeInstanceSymbol; }; const getLookUpLocations = (location: CredentialsLocation) => { @@ -52,7 +45,7 @@ const getLookUpLocations = (location: CredentialsLocation) => { return locations; }; -const getCredentialsStore = (storeSymbol: StoreRegistrySymbol) => { +const getCredentialsStore = (storeSymbol: symbol) => { assertValidationError( storeRegistry.has(storeSymbol), StorageValidationErrorCode.LocationCredentialsStoreDestroyed, @@ -65,7 +58,7 @@ const getCredentialsStore = (storeSymbol: StoreRegistrySymbol) => { * @internal */ export const getValue = async (input: { - storeSymbol: StoreRegistrySymbol; + storeSymbol: symbol; location: CredentialsLocation; forceRefresh: boolean; }): Promise<{ credentials: AWSCredentials }> => { @@ -84,6 +77,6 @@ export const getValue = async (input: { return fetchNewValue(store, location); }; -export const removeStore = (storeSymbol: StoreRegistrySymbol) => { +export const removeStore = (storeSymbol: symbol) => { storeRegistry.delete(storeSymbol); }; From 465410528e3d8137654ddd487891c74190875e4c Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Fri, 12 Jul 2024 07:56:24 -0700 Subject: [PATCH 07/10] fix(storage): remove storagebrower from interface naming --- .../createManagedAuthConfigAdapter.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts index 4796a19adc8..0f7d55161bf 100644 --- a/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts +++ b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts @@ -15,7 +15,7 @@ interface CreateManagedAuthConfigAdapterInput { credentialsProvider: CredentialsProvider; } -interface StorageBrowserAuthConfigAdapter { +interface AuthConfigAdapter { listLocations: ListLocations; getLocationCredentials: GetLocationCredentials; region: string; @@ -32,7 +32,7 @@ export const createManagedAuthConfigAdapter = ({ credentialsProvider, region, accountId, -}: CreateManagedAuthConfigAdapterInput): StorageBrowserAuthConfigAdapter => { +}: CreateManagedAuthConfigAdapterInput): AuthConfigAdapter => { const listLocations = createListLocationsHandler({ credentialsProvider, accountId, From 4de03e6b59f667208025a6871ff86317836aabbf Mon Sep 17 00:00:00 2001 From: AllanZhengYP Date: Fri, 12 Jul 2024 08:16:03 -0700 Subject: [PATCH 08/10] chore(storage): address feedbacks Co-authored-by: Jim Blanchard --- .../createManagedAuthConfigAdapter.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts index 0f7d55161bf..55d334b47c7 100644 --- a/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts +++ b/packages/storage/src/storageBrowser/managedAuthConfigAdapter/createManagedAuthConfigAdapter.ts @@ -22,11 +22,11 @@ interface AuthConfigAdapter { } /** - * Create configuration including handlers for StorageBrowser to call S3 Access Grants - * APIs to list and get credentials for different locations. + * Create configuration including handlers to call S3 Access Grant APIs to list and get + * credentials for different locations. * * @param options - Configuration options for the adapter. - * @returns - An object containing the handlers for StorageBrowser to call S3 Access Grants APIs and region + * @returns - An object containing the handlers to call S3 Access Grant APIs and region */ export const createManagedAuthConfigAdapter = ({ credentialsProvider, From 8918e335c54cd26df8ed312c5c5b8a167f0b0322 Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Fri, 12 Jul 2024 10:40:24 -0700 Subject: [PATCH 09/10] Revert "doc(storage): add disclaimer" This reverts commit eadb58f871b99e513e36a97838f49d2034d142eb. --- packages/storage/src/storageBrowser/index.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/storage/src/storageBrowser/index.ts b/packages/storage/src/storageBrowser/index.ts index 455a64de505..bde6d9bfe82 100644 --- a/packages/storage/src/storageBrowser/index.ts +++ b/packages/storage/src/storageBrowser/index.ts @@ -1,12 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -/** - * NOTE: The APIs exported from this file are ONLY intended for usage by - * Amplify UI. To use location-related features, please use - * @aws-amplify/ui-react-storage - */ - export { createLocationCredentialsStore } from './locationCredentialsStore'; export { createManagedAuthConfigAdapter } from './managedAuthConfigAdapter/createManagedAuthConfigAdapter'; export { GetLocationCredentials, ListLocations } from './types'; From 25d487e6dd52ed0dcb83069fda80670609c04c26 Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Sun, 14 Jul 2024 17:18:56 -0700 Subject: [PATCH 10/10] chore: fix merge conflicts --- .../locationCredentialsStore/registry.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts b/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts index a06993a0640..54ed8e4db83 100644 --- a/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts +++ b/packages/storage/src/storageBrowser/locationCredentialsStore/registry.ts @@ -15,12 +15,19 @@ import { initStore, } from './store'; +interface StoreRegistrySymbol { + readonly value: symbol; +} + /** * Keep all cache records for all instances of credentials store in a singleton * so we can reliably de-reference from the memory when we destroy a store * instance. */ -const storeRegistry = new Map(); +const storeRegistry = new WeakMap< + StoreRegistrySymbol, + LruLocationCredentialsStore +>(); /** * @internal @@ -29,10 +36,10 @@ export const createStore = ( refreshHandler: GetLocationCredentials, size?: number, ) => { - const storeInstanceSymbol = Symbol('LocationCredentialsStore'); - storeRegistry.set(storeInstanceSymbol, initStore(refreshHandler, size)); + const storeSymbol = { value: Symbol('LocationCredentialsStore') }; + storeRegistry.set(storeSymbol, initStore(refreshHandler, size)); - return storeInstanceSymbol; + return storeSymbol; }; const getLookUpLocations = (location: CredentialsLocation) => { @@ -45,7 +52,7 @@ const getLookUpLocations = (location: CredentialsLocation) => { return locations; }; -const getCredentialsStore = (storeSymbol: symbol) => { +const getCredentialsStore = (storeSymbol: StoreRegistrySymbol) => { assertValidationError( storeRegistry.has(storeSymbol), StorageValidationErrorCode.LocationCredentialsStoreDestroyed, @@ -58,7 +65,7 @@ const getCredentialsStore = (storeSymbol: symbol) => { * @internal */ export const getValue = async (input: { - storeSymbol: symbol; + storeSymbol: StoreRegistrySymbol; location: CredentialsLocation; forceRefresh: boolean; }): Promise<{ credentials: AWSCredentials }> => { @@ -77,6 +84,6 @@ export const getValue = async (input: { return fetchNewValue(store, location); }; -export const removeStore = (storeSymbol: symbol) => { +export const removeStore = (storeSymbol: StoreRegistrySymbol) => { storeRegistry.delete(storeSymbol); };