-
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.
Storage Browser Default Auth (#13866)
* first draft poc * upadtes * add listPaths API * update new file structure * fix types * refactor types and utils * update tests * fix test * fix bundle size test * update the listLocation handler * rename util * update Path type * fix missed type
- Loading branch information
Showing
14 changed files
with
503 additions
and
13 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
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
125 changes: 125 additions & 0 deletions
125
...ges/storage/__tests__/internals/amplifyAuthAdapter/createAmplifyAuthConfigAdapter.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,125 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Amplify, fetchAuthSession } from '@aws-amplify/core'; | ||
|
||
import { resolveLocationsForCurrentSession } from '../../../src/internals/amplifyAuthConfigAdapter/resolveLocationsForCurrentSession'; | ||
import { createAmplifyAuthConfigAdapter } from '../../../src/internals'; | ||
|
||
jest.mock('@aws-amplify/core', () => ({ | ||
ConsoleLogger: jest.fn(), | ||
Amplify: { | ||
getConfig: jest.fn(), | ||
Auth: { | ||
getConfig: jest.fn(), | ||
fetchAuthSession: jest.fn(), | ||
}, | ||
}, | ||
fetchAuthSession: jest.fn(), | ||
})); | ||
jest.mock( | ||
'../../../src/internals/amplifyAuthConfigAdapter/resolveLocationsForCurrentSession', | ||
); | ||
|
||
const credentials = { | ||
accessKeyId: 'accessKeyId', | ||
sessionToken: 'sessionToken', | ||
secretAccessKey: 'secretAccessKey', | ||
}; | ||
const identityId = 'identityId'; | ||
|
||
const mockGetConfig = jest.mocked(Amplify.getConfig); | ||
const mockFetchAuthSession = fetchAuthSession as jest.Mock; | ||
const mockResolveLocationsFromCurrentSession = | ||
resolveLocationsForCurrentSession as jest.Mock; | ||
|
||
describe('createAmplifyAuthConfigAdapter', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
mockGetConfig.mockReturnValue({ | ||
Storage: { | ||
S3: { | ||
bucket: 'bucket1', | ||
region: 'region1', | ||
buckets: { | ||
'bucket-1': { | ||
bucketName: 'bucket-1', | ||
region: 'region1', | ||
paths: {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
mockFetchAuthSession.mockResolvedValue({ | ||
credentials, | ||
identityId, | ||
tokens: { | ||
accessToken: { payload: {} }, | ||
}, | ||
}); | ||
|
||
it('should return an AuthConfigAdapter with listLocations function', async () => { | ||
const adapter = createAmplifyAuthConfigAdapter(); | ||
expect(adapter).toHaveProperty('listLocations'); | ||
const { listLocations } = adapter; | ||
await listLocations(); | ||
expect(mockFetchAuthSession).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return empty locations when buckets are not defined', async () => { | ||
mockGetConfig.mockReturnValue({ Storage: { S3: { buckets: undefined } } }); | ||
|
||
const adapter = createAmplifyAuthConfigAdapter(); | ||
const result = await adapter.listLocations(); | ||
|
||
expect(result).toEqual({ locations: [] }); | ||
}); | ||
|
||
it('should generate locations correctly when buckets are defined', async () => { | ||
const mockBuckets = { | ||
bucket1: { | ||
bucketName: 'bucket1', | ||
region: 'region1', | ||
paths: { | ||
'/path1': { | ||
entityidentity: ['read', 'write'], | ||
groupsadmin: ['read'], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
mockGetConfig.mockReturnValue({ | ||
Storage: { S3: { buckets: mockBuckets } }, | ||
}); | ||
mockResolveLocationsFromCurrentSession.mockReturnValue([ | ||
{ | ||
type: 'PREFIX', | ||
permission: ['read', 'write'], | ||
scope: { | ||
bucketName: 'bucket1', | ||
path: '/path1', | ||
}, | ||
}, | ||
]); | ||
|
||
const adapter = createAmplifyAuthConfigAdapter(); | ||
const result = await adapter.listLocations(); | ||
|
||
expect(result).toEqual({ | ||
locations: [ | ||
{ | ||
type: 'PREFIX', | ||
permission: ['read', 'write'], | ||
scope: { | ||
bucketName: 'bucket1', | ||
path: '/path1', | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
144 changes: 144 additions & 0 deletions
144
.../storage/__tests__/internals/amplifyAuthAdapter/resolveLocationsForCurrentSession.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,144 @@ | ||
import { resolveLocationsForCurrentSession } from '../../../src/internals/amplifyAuthConfigAdapter/resolveLocationsForCurrentSession'; | ||
import { BucketInfo } from '../../../src/providers/s3/types/options'; | ||
|
||
describe('resolveLocationsForCurrentSession', () => { | ||
const mockBuckets: Record<string, BucketInfo> = { | ||
bucket1: { | ||
bucketName: 'bucket1', | ||
region: 'region1', | ||
paths: { | ||
'path1/*': { | ||
guest: ['get', 'list'], | ||
authenticated: ['get', 'list', 'write'], | ||
}, | ||
'path2/*': { | ||
groupsauditor: ['get', 'list'], | ||
groupsadmin: ['get', 'list', 'write', 'delete'], | ||
}, | ||
// eslint-disable-next-line no-template-curly-in-string | ||
'profile-pictures/${cognito-identity.amazonaws.com:sub}/*': { | ||
entityidentity: ['get', 'list', 'write', 'delete'], | ||
}, | ||
}, | ||
}, | ||
bucket2: { | ||
bucketName: 'bucket2', | ||
region: 'region1', | ||
paths: { | ||
'path3/*': { | ||
guest: ['read'], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it('should generate locations correctly when tokens are true', () => { | ||
const result = resolveLocationsForCurrentSession({ | ||
buckets: mockBuckets, | ||
isAuthenticated: true, | ||
identityId: '12345', | ||
userGroup: 'admin', | ||
}); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
type: 'PREFIX', | ||
permission: ['get', 'list', 'write'], | ||
bucket: 'bucket1', | ||
prefix: 'path1/*', | ||
}, | ||
{ | ||
type: 'PREFIX', | ||
permission: ['get', 'list', 'write', 'delete'], | ||
bucket: 'bucket1', | ||
prefix: 'path2/*', | ||
}, | ||
{ | ||
type: 'PREFIX', | ||
permission: ['get', 'list', 'write', 'delete'], | ||
bucket: 'bucket1', | ||
prefix: 'profile-pictures/12345/*', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should generate locations correctly when tokens are true & bad userGroup', () => { | ||
const result = resolveLocationsForCurrentSession({ | ||
buckets: mockBuckets, | ||
isAuthenticated: true, | ||
identityId: '12345', | ||
userGroup: 'editor', | ||
}); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
type: 'PREFIX', | ||
permission: ['get', 'list', 'write'], | ||
bucket: 'bucket1', | ||
prefix: 'path1/*', | ||
}, | ||
{ | ||
type: 'PREFIX', | ||
permission: ['get', 'list', 'write', 'delete'], | ||
bucket: 'bucket1', | ||
prefix: 'profile-pictures/12345/*', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should continue to next bucket when paths are not defined', () => { | ||
const result = resolveLocationsForCurrentSession({ | ||
buckets: { | ||
bucket1: { | ||
bucketName: 'bucket1', | ||
region: 'region1', | ||
paths: undefined, | ||
}, | ||
bucket2: { | ||
bucketName: 'bucket1', | ||
region: 'region1', | ||
paths: { | ||
'path1/*': { | ||
guest: ['get', 'list'], | ||
authenticated: ['get', 'list', 'write'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
isAuthenticated: true, | ||
identityId: '12345', | ||
userGroup: 'admin', | ||
}); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
type: 'PREFIX', | ||
permission: ['get', 'list', 'write'], | ||
bucket: 'bucket1', | ||
prefix: 'path1/*', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should generate locations correctly when tokens are false', () => { | ||
const result = resolveLocationsForCurrentSession({ | ||
buckets: mockBuckets, | ||
isAuthenticated: false, | ||
}); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
type: 'PREFIX', | ||
permission: ['get', 'list'], | ||
bucket: 'bucket1', | ||
prefix: 'path1/*', | ||
}, | ||
{ | ||
type: 'PREFIX', | ||
permission: ['read'], | ||
bucket: 'bucket2', | ||
prefix: 'path3/*', | ||
}, | ||
]); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/storage/src/internals/amplifyAuthConfigAdapter/createAmplifyAuthConfigAdapter.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,16 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { ListPaths } from '../types/credentials'; | ||
|
||
import { createAmplifyListLocationsHandler } from './createAmplifyListLocationsHandler'; | ||
|
||
export interface AuthConfigAdapter { | ||
listLocations: ListPaths; | ||
} | ||
|
||
export const createAmplifyAuthConfigAdapter = (): AuthConfigAdapter => { | ||
const listLocations = createAmplifyListLocationsHandler(); | ||
|
||
return { listLocations }; | ||
}; |
Oops, something went wrong.