@@ -8,6 +8,11 @@ import {
88 credentialsAccessMockFns ,
99} from '@sim/testing/mocks/credentials-access.mock'
1010import { environmentUtilsMockFns } from '@sim/testing/mocks/environment-utils.mock'
11+ import { featureFlagsMock , featureFlagsMockFns } from '@sim/testing/mocks/feature-flags.mock'
12+ import {
13+ internalAwsCredentialsMock ,
14+ internalAwsCredentialsMockFns ,
15+ } from '@sim/testing/mocks/internal-aws-credentials.mock'
1116import {
1217 knowledgeAccessScopeMock ,
1318 knowledgeAccessScopeMockFns ,
@@ -71,6 +76,8 @@ vi.mock('@/lib/credentials/application/organization-credentials', () => ({
7176} ) )
7277
7378vi . mock ( '@/lib/oauth/credential-service' , ( ) => authOAuthUtilsMock )
79+ vi . mock ( '@/lib/internal/aws/credentials' , ( ) => internalAwsCredentialsMock )
80+ vi . mock ( '@/lib/core/config/feature-flags' , ( ) => featureFlagsMock )
7481vi . mock ( '@/lib/api-key/crypto' , ( ) => ( { decryptApiKey : hoisted . decryptApiKey } ) )
7582
7683vi . mock ( '@/lib/permission-groups/resolve.server' , ( ) => permissionGroupsResolveMock )
@@ -130,6 +137,7 @@ import { confluenceConnectorMeta } from '@/connectors/confluence/meta'
130137import { gmailConnectorMeta } from '@/connectors/gmail/meta'
131138import { googleCalendarConnectorMeta } from '@/connectors/google-calendar/meta'
132139import { googleDriveConnectorMeta } from '@/connectors/google-drive/meta'
140+ import { s3ConnectorMeta } from '@/connectors/s3/meta'
133141
134142const mocks = {
135143 ...hoisted ,
@@ -341,6 +349,43 @@ describe('knowledge connector application use cases', () => {
341349 expect ( auditMockFns . mockRecordAudit ) . not . toHaveBeenCalled ( )
342350 } )
343351
352+ it ( 'refuses another service’s credential for an S3 connector before resolving it' , async ( ) => {
353+ featureFlagsMockFns . mockIsFeatureEnabled . mockResolvedValueOnce ( true )
354+ mocks . getCredentialActorContext . mockResolvedValueOnce ( {
355+ credential : {
356+ id : 'credential-1' ,
357+ workspaceId : 'workspace-b' ,
358+ providerId : 'google-service-account' ,
359+ } ,
360+ member : { role : 'admin' } ,
361+ hasWorkspaceAccess : true ,
362+ canWriteWorkspace : true ,
363+ isAdmin : true ,
364+ } )
365+ mocks . createConnector . mockImplementationOnce (
366+ async ( input : { resolveAccessToken : ( credentialId : string ) => Promise < unknown > } ) => {
367+ await input . resolveAccessToken ( 'credential-1' )
368+ throw new Error ( 'An ineligible credential reached connector persistence' )
369+ }
370+ )
371+ await expect (
372+ createKnowledgeConnector . execute ( {
373+ principal : patPrincipal ,
374+ input : {
375+ knowledgeBaseId : 'knowledge-b' ,
376+ connectorType : 's3' ,
377+ credentialId : 'credential-1' ,
378+ sourceConfig : { region : 'us-east-1' , bucket : 'docs' } ,
379+ syncIntervalMinutes : 1440 ,
380+ } ,
381+ } )
382+ ) . rejects . toMatchObject ( {
383+ code : 'validation' ,
384+ message : expect . stringContaining ( 'belongs to another service' ) ,
385+ } )
386+ expect ( internalAwsCredentialsMockFns . mockResolveAuthorizedAwsCredential ) . not . toHaveBeenCalled ( )
387+ } )
388+
344389 it ( 'rejects a personal Confluence OAuth credential before central token use' , async ( ) => {
345390 await expect (
346391 resolveConnectorCredentialAccessToken ( {
@@ -1022,6 +1067,29 @@ describe('organization connector credential authorization', () => {
10221067 }
10231068 )
10241069
1070+ it . each ( [
1071+ [ 'refused by AWS' , { name : 'AccessDenied' } , 400 ] ,
1072+ [ 'throttled by AWS' , { name : 'ThrottlingException' , $metadata : { httpStatusCode : 400 } } , 500 ] ,
1073+ ] ) (
1074+ 'projects a saved AWS credential %s with the matching setup status' ,
1075+ async ( _case , failure , status ) => {
1076+ mocks . authorizeOrganizationCredentialUse . mockResolvedValueOnce ( {
1077+ credential : { ...credential , providerId : 'aws' } ,
1078+ userId : principal . userId ,
1079+ } )
1080+ internalAwsCredentialsMockFns . mockResolveAuthorizedAwsCredential . mockRejectedValueOnce (
1081+ Object . assign ( new Error ( 'AWS said no' ) , failure )
1082+ )
1083+ const error = await resolveConnectorCredentialAccessToken ( {
1084+ ...input ,
1085+ auth : s3ConnectorMeta . auth ,
1086+ accessMode : 'workspace' ,
1087+ sourceConfig : { region : 'us-east-1' , bucket : 'docs' } ,
1088+ } ) . catch ( ( error : unknown ) => error )
1089+ expect ( internalOrchestrationErrorPolicy . project ( error ) ?. status ?? 500 ) . toBe ( status )
1090+ }
1091+ )
1092+
10251093 it ( 'does not mint a token after the credential creator leaves the organization' , async ( ) => {
10261094 mocks . resolveTokenIdentity . mockResolvedValueOnce ( null )
10271095 await expect ( resolveConnectorCredentialAccessToken ( input ) ) . resolves . toBeNull ( )
0 commit comments