[PM-42213] refactor: Use SDK admin attachment client for admin attachment creation (#23683)

* [PM-42213] refactor: Use SDK admin attachment client for admin attachment creation

sdk-internal moves the admin flag off CreateAttachmentRequest and onto
AttachmentAdminClient::create_attachment, and drops the is_admin argument
from CiphersClient.update_collection. Route admin attachment creation through
attachments().admin() and drop the redundant update_collection argument.

* bump sdk version
This commit is contained in:
Jordan Aasen
2026-10-21 21:20:02 -07:00
committed by GitHub
parent 9a03963fa0
commit 589d8b50ef
7 changed files with 36 additions and 17 deletions

View File

@@ -240,11 +240,13 @@ export abstract class CipherSdkService {
* @param cipherId The cipher to attach the new file to
* @param request Encrypted attachment metadata (key, fileName, fileSize, lastKnownRevisionDate)
* @param userId The user ID to use for SDK client
* @param asAdmin Whether to use the admin endpoint, which does not update local state
*/
abstract createAttachment(
cipherId: CipherId,
request: CreateAttachmentRequest,
userId: UserId,
asAdmin?: boolean,
): Promise<CreatedAttachment>;
/**

View File

@@ -75,6 +75,7 @@ describe("DefaultCipherSdkService", () => {
admin: jest.fn().mockReturnValue(mockAdminSdk),
};
mockAttachmentsAdminSdk = {
create_attachment: jest.fn(),
delete_attachment: jest.fn().mockResolvedValue(undefined),
get_attachment_download_url: jest.fn(),
};
@@ -958,7 +959,6 @@ describe("DefaultCipherSdkService", () => {
fileName: "2.encryptedFileName" as unknown as EncString,
fileSize: 65,
lastKnownRevisionDate: "2024-05-31T11:20:58.456Z",
asAdmin: false,
};
const created = {
attachmentId: "newatt9999999999999999999999999",
@@ -974,6 +974,18 @@ describe("DefaultCipherSdkService", () => {
expect(sdkService.userClient$).toHaveBeenCalledWith(userId);
expect(mockVaultSdk.attachments).toHaveBeenCalled();
expect(mockAttachmentsSdk.create_attachment).toHaveBeenCalledWith(testCipherId, request);
expect(mockAttachmentsSdk.admin).not.toHaveBeenCalled();
expect(result).toBe(created);
});
it("delegates to SDK attachments.admin().create_attachment when asAdmin is true", async () => {
mockAttachmentsAdminSdk.create_attachment.mockResolvedValue(created);
const result = await cipherSdkService.createAttachment(testCipherId, request, userId, true);
expect(mockAttachmentsSdk.admin).toHaveBeenCalled();
expect(mockAttachmentsAdminSdk.create_attachment).toHaveBeenCalledWith(testCipherId, request);
expect(mockAttachmentsSdk.create_attachment).not.toHaveBeenCalled();
expect(result).toBe(created);
});
@@ -1245,7 +1257,7 @@ describe("DefaultCipherSdkService", () => {
expect(sdkService.userClient$).toHaveBeenCalledWith(userId);
expect(mockVaultSdk.ciphers).toHaveBeenCalled();
expect(mockCiphersSdk.update_collection).toHaveBeenCalledWith(cipherId, collectionIds, false);
expect(mockCiphersSdk.update_collection).toHaveBeenCalledWith(cipherId, collectionIds);
expect(mockCiphersSdk.admin).not.toHaveBeenCalled();
expect(result).toBeInstanceOf(CipherView);
});

View File

@@ -397,12 +397,17 @@ export class DefaultCipherSdkService implements CipherSdkService {
cipherId: CipherId,
request: CreateAttachmentRequest,
userId: UserId,
asAdmin = false,
): Promise<CreatedAttachment> {
return await firstValueFrom(
this.sdkService.userClient$(userId).pipe(
switchMap(async (sdk) => {
using ref = sdk.take();
return await ref.value.vault().attachments().create_attachment(asUuid(cipherId), request);
const attachments = ref.value.vault().attachments();
return asAdmin
? await attachments.admin().create_attachment(asUuid(cipherId), request)
: await attachments.create_attachment(asUuid(cipherId), request);
}),
catchError((error: unknown) => {
this.logService.error(`Failed to create attachment: ${error}`);
@@ -648,7 +653,6 @@ export class DefaultCipherSdkService implements CipherSdkService {
const result = await sdkCiphersClient.update_collection(
asUuid(cipherId),
collectionIds.map((id) => asUuid(id)),
false,
);
return CipherView.fromSdkCipherView(result);
} catch (e) {

View File

@@ -202,10 +202,10 @@ describe("Cipher Service", () => {
expect(cipherSdkService.createAttachment).toHaveBeenCalledWith(
testCipher.id,
expect.objectContaining({
asAdmin: false,
lastKnownRevisionDate: testCipher.revisionDate.toISOString(),
}),
userId,
false,
);
expect(cipherFileUploadService.uploadPrepared).toHaveBeenCalledWith(
testCipher.id,
@@ -252,8 +252,9 @@ describe("Cipher Service", () => {
expect(cipherSdkService.createAttachment).toHaveBeenCalledWith(
testCipher.id,
expect.objectContaining({ asAdmin: true }),
expect.not.objectContaining({ asAdmin: expect.anything() }),
userId,
true,
);
expect(cipherFileUploadService.uploadPrepared).toHaveBeenCalledWith(
testCipher.id,

View File

@@ -1080,9 +1080,9 @@ export class CipherService implements CipherServiceAbstraction {
fileName: encFileName.encryptedString,
fileSize: encData.buffer.byteLength,
lastKnownRevisionDate: cipher.revisionDate.toISOString(),
asAdmin: admin,
},
userId,
admin,
);
await this.cipherFileUploadService.uploadPrepared(

16
package-lock.json generated
View File

@@ -23,9 +23,9 @@
"@angular/forms": "21.2.17",
"@angular/platform-browser": "21.2.17",
"@angular/router": "21.2.17",
"@bitwarden/commercial-sdk-internal": "0.2.0-main.1135",
"@bitwarden/commercial-sdk-internal": "0.2.0-main.1141",
"@bitwarden/desktop-napi": "file:apps/desktop/desktop_native/napi",
"@bitwarden/sdk-internal": "0.2.0-main.1135",
"@bitwarden/sdk-internal": "0.2.0-main.1141",
"@bufbuild/protobuf": "2.13.0",
"@electron/fuses": "2.1.1",
"@emotion/css": "11.13.5",
@@ -5068,9 +5068,9 @@
"link": true
},
"node_modules/@bitwarden/commercial-sdk-internal": {
"version": "0.2.0-main.1135",
"resolved": "https://registry.npmjs.org/@bitwarden/commercial-sdk-internal/-/commercial-sdk-internal-0.2.0-main.1135.tgz",
"integrity": "sha512-Z31vF2wRf4TOxCEOxa+/TEeCQmX/bWbTXf1wHnnDD2D9gyVVr2BJAUlkIqIt1FtMey+OMIL/38YOMiW/9EFlPg==",
"version": "0.2.0-main.1141",
"resolved": "https://registry.npmjs.org/@bitwarden/commercial-sdk-internal/-/commercial-sdk-internal-0.2.0-main.1141.tgz",
"integrity": "sha512-wTQZOPvAYR9uaSE5lRRinKUCUyznjyXhrWFvFId+KBb2fWgsHcytdOInCDX2ZQolA9Br6Y/yKUy8gzffpaJn5g==",
"license": "BITWARDEN SOFTWARE DEVELOPMENT KIT LICENSE AGREEMENT",
"dependencies": {
"type-fest": "^5.0.0"
@@ -5204,9 +5204,9 @@
"link": true
},
"node_modules/@bitwarden/sdk-internal": {
"version": "0.2.0-main.1135",
"resolved": "https://registry.npmjs.org/@bitwarden/sdk-internal/-/sdk-internal-0.2.0-main.1135.tgz",
"integrity": "sha512-yIE0lskxiW6VV8QgOTUHY/z+VvAHt/qW99I2Ac4YzaOI3xAWOGRQdeQkh5gt1WpDOajtTF9zHti4ehBEHfVW1Q==",
"version": "0.2.0-main.1141",
"resolved": "https://registry.npmjs.org/@bitwarden/sdk-internal/-/sdk-internal-0.2.0-main.1141.tgz",
"integrity": "sha512-SjJEuswl3UFVBdotOe+nZgly19y0n3bcgDl2t0ZI2LsAA6ilTnihrARgzoCpIZ3ojOJzdYvZ2lsR7peWDPWxOg==",
"license": "GPL-3.0",
"dependencies": {
"type-fest": "^5.0.0"

View File

@@ -185,9 +185,9 @@
"@angular/forms": "21.2.17",
"@angular/platform-browser": "21.2.17",
"@angular/router": "21.2.17",
"@bitwarden/commercial-sdk-internal": "0.2.0-main.1135",
"@bitwarden/commercial-sdk-internal": "0.2.0-main.1141",
"@bitwarden/desktop-napi": "file:apps/desktop/desktop_native/napi",
"@bitwarden/sdk-internal": "0.2.0-main.1135",
"@bitwarden/sdk-internal": "0.2.0-main.1141",
"@bufbuild/protobuf": "2.13.0",
"@electron/fuses": "2.1.1",
"@emotion/css": "11.13.5",