1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 00:33:44 +00:00

Move sm code to new encrypt service interface (#14545)

This commit is contained in:
Bernd Schoolmann
2025-05-09 00:36:07 +02:00
committed by GitHub
parent 00a37d9d0a
commit 9b85123acf
9 changed files with 38 additions and 35 deletions

View File

@@ -93,7 +93,7 @@ export class ProjectService {
): Promise<ProjectRequest> { ): Promise<ProjectRequest> {
const orgKey = await this.getOrganizationKey(organizationId); const orgKey = await this.getOrganizationKey(organizationId);
const request = new ProjectRequest(); const request = new ProjectRequest();
request.name = await this.encryptService.encrypt(projectView.name, orgKey); request.name = await this.encryptService.encryptString(projectView.name, orgKey);
return request; return request;
} }
@@ -108,7 +108,7 @@ export class ProjectService {
projectView.revisionDate = projectResponse.revisionDate; projectView.revisionDate = projectResponse.revisionDate;
projectView.read = projectResponse.read; projectView.read = projectResponse.read;
projectView.write = projectResponse.write; projectView.write = projectResponse.write;
projectView.name = await this.encryptService.decryptToUtf8( projectView.name = await this.encryptService.decryptString(
new EncString(projectResponse.name), new EncString(projectResponse.name),
orgKey, orgKey,
); );
@@ -127,7 +127,7 @@ export class ProjectService {
projectListView.organizationId = s.organizationId; projectListView.organizationId = s.organizationId;
projectListView.read = s.read; projectListView.read = s.read;
projectListView.write = s.write; projectListView.write = s.write;
projectListView.name = await this.encryptService.decryptToUtf8( projectListView.name = await this.encryptService.decryptString(
new EncString(s.name), new EncString(s.name),
orgKey, orgKey,
); );

View File

@@ -24,10 +24,10 @@ describe("SecretService", () => {
sut = new SecretService(keyService, apiService, encryptService, accessPolicyService); sut = new SecretService(keyService, apiService, encryptService, accessPolicyService);
encryptService.encrypt.mockResolvedValue({ encryptService.encryptString.mockResolvedValue({
encryptedString: "mockEncryptedString", encryptedString: "mockEncryptedString",
} as EncString); } as EncString);
encryptService.decryptToUtf8.mockResolvedValue(mockUnencryptedData); encryptService.decryptString.mockResolvedValue(mockUnencryptedData);
}); });
it("instantiates", () => { it("instantiates", () => {

View File

@@ -166,9 +166,9 @@ export class SecretService {
const orgKey = await this.getOrganizationKey(organizationId); const orgKey = await this.getOrganizationKey(organizationId);
const request = new SecretRequest(); const request = new SecretRequest();
const [key, value, note] = await Promise.all([ const [key, value, note] = await Promise.all([
this.encryptService.encrypt(secretView.name, orgKey), this.encryptService.encryptString(secretView.name, orgKey),
this.encryptService.encrypt(secretView.value, orgKey), this.encryptService.encryptString(secretView.value, orgKey),
this.encryptService.encrypt(secretView.note, orgKey), this.encryptService.encryptString(secretView.note, orgKey),
]); ]);
request.key = key.encryptedString; request.key = key.encryptedString;
request.value = value.encryptedString; request.value = value.encryptedString;
@@ -193,9 +193,9 @@ export class SecretService {
secretView.revisionDate = secretResponse.revisionDate; secretView.revisionDate = secretResponse.revisionDate;
const [name, value, note] = await Promise.all([ const [name, value, note] = await Promise.all([
this.encryptService.decryptToUtf8(new EncString(secretResponse.name), orgKey), this.encryptService.decryptString(new EncString(secretResponse.name), orgKey),
this.encryptService.decryptToUtf8(new EncString(secretResponse.value), orgKey), this.encryptService.decryptString(new EncString(secretResponse.value), orgKey),
this.encryptService.decryptToUtf8(new EncString(secretResponse.note), orgKey), this.encryptService.decryptString(new EncString(secretResponse.note), orgKey),
]); ]);
secretView.name = name; secretView.name = name;
secretView.value = value; secretView.value = value;
@@ -230,7 +230,7 @@ export class SecretService {
const secretListView = new SecretListView(); const secretListView = new SecretListView();
secretListView.id = s.id; secretListView.id = s.id;
secretListView.organizationId = s.organizationId; secretListView.organizationId = s.organizationId;
secretListView.name = await this.encryptService.decryptToUtf8( secretListView.name = await this.encryptService.decryptString(
new EncString(s.name), new EncString(s.name),
orgKey, orgKey,
); );
@@ -259,7 +259,7 @@ export class SecretService {
const projectsMappedToSecretView = new SecretProjectView(); const projectsMappedToSecretView = new SecretProjectView();
projectsMappedToSecretView.id = s.id; projectsMappedToSecretView.id = s.id;
projectsMappedToSecretView.name = s.name projectsMappedToSecretView.name = s.name
? await this.encryptService.decryptToUtf8(new EncString(s.name), orgKey) ? await this.encryptService.decryptString(new EncString(s.name), orgKey)
: null; : null;
return projectsMappedToSecretView; return projectsMappedToSecretView;
}), }),

View File

@@ -102,12 +102,12 @@ export class AccessService {
const organizationKey = await this.getOrganizationKey(organizationId); const organizationKey = await this.getOrganizationKey(organizationId);
const accessTokenRequest = new AccessTokenRequest(); const accessTokenRequest = new AccessTokenRequest();
const [name, encryptedPayload, key] = await Promise.all([ const [name, encryptedPayload, key] = await Promise.all([
await this.encryptService.encrypt(accessTokenView.name, organizationKey), await this.encryptService.encryptString(accessTokenView.name, organizationKey),
await this.encryptService.encrypt( await this.encryptService.encryptString(
JSON.stringify({ encryptionKey: organizationKey.keyB64 }), JSON.stringify({ encryptionKey: organizationKey.keyB64 }),
encryptionKey, encryptionKey,
), ),
await this.encryptService.encrypt(encryptionKey.keyB64, organizationKey), await this.encryptService.encryptString(encryptionKey.keyB64, organizationKey),
]); ]);
accessTokenRequest.name = name; accessTokenRequest.name = name;
@@ -130,7 +130,7 @@ export class AccessService {
accessTokenResponses.map(async (s) => { accessTokenResponses.map(async (s) => {
const view = new AccessTokenView(); const view = new AccessTokenView();
view.id = s.id; view.id = s.id;
view.name = await this.encryptService.decryptToUtf8(new EncString(s.name), orgKey); view.name = await this.encryptService.decryptString(new EncString(s.name), orgKey);
view.scopes = s.scopes; view.scopes = s.scopes;
view.expireAt = s.expireAt ? new Date(s.expireAt) : null; view.expireAt = s.expireAt ? new Date(s.expireAt) : null;
view.creationDate = new Date(s.creationDate); view.creationDate = new Date(s.creationDate);

View File

@@ -130,7 +130,10 @@ export class ServiceAccountService {
serviceAccountView: ServiceAccountView, serviceAccountView: ServiceAccountView,
) { ) {
const request = new ServiceAccountRequest(); const request = new ServiceAccountRequest();
request.name = await this.encryptService.encrypt(serviceAccountView.name, organizationKey); request.name = await this.encryptService.encryptString(
serviceAccountView.name,
organizationKey,
);
return request; return request;
} }
@@ -144,7 +147,7 @@ export class ServiceAccountService {
serviceAccountView.creationDate = serviceAccountResponse.creationDate; serviceAccountView.creationDate = serviceAccountResponse.creationDate;
serviceAccountView.revisionDate = serviceAccountResponse.revisionDate; serviceAccountView.revisionDate = serviceAccountResponse.revisionDate;
serviceAccountView.name = serviceAccountResponse.name serviceAccountView.name = serviceAccountResponse.name
? await this.encryptService.decryptToUtf8( ? await this.encryptService.decryptString(
new EncString(serviceAccountResponse.name), new EncString(serviceAccountResponse.name),
organizationKey, organizationKey,
) )
@@ -163,7 +166,7 @@ export class ServiceAccountService {
view.revisionDate = response.revisionDate; view.revisionDate = response.revisionDate;
view.accessToSecrets = response.accessToSecrets; view.accessToSecrets = response.accessToSecrets;
view.name = response.name view.name = response.name
? await this.encryptService.decryptToUtf8(new EncString(response.name), organizationKey) ? await this.encryptService.decryptString(new EncString(response.name), organizationKey)
: null; : null;
return view; return view;
} }

View File

@@ -28,8 +28,8 @@ describe("SecretsManagerPortingApiService", () => {
sut = new SecretsManagerPortingApiService(apiService, encryptService, keyService); sut = new SecretsManagerPortingApiService(apiService, encryptService, keyService);
encryptService.encrypt.mockResolvedValue(mockEncryptedString); encryptService.encryptString.mockResolvedValue(mockEncryptedString);
encryptService.decryptToUtf8.mockResolvedValue(mockUnencryptedString); encryptService.decryptString.mockResolvedValue(mockUnencryptedString);
const mockRandomBytes = new Uint8Array(64) as CsprngArray; const mockRandomBytes = new Uint8Array(64) as CsprngArray;
const mockOrgKey = new SymmetricCryptoKey(mockRandomBytes) as OrgKey; const mockOrgKey = new SymmetricCryptoKey(mockRandomBytes) as OrgKey;

View File

@@ -86,7 +86,7 @@ export class SecretsManagerPortingApiService {
importData.projects.map(async (p: any) => { importData.projects.map(async (p: any) => {
const project = new SecretsManagerImportedProjectRequest(); const project = new SecretsManagerImportedProjectRequest();
project.id = p.id; project.id = p.id;
project.name = await this.encryptService.encrypt(p.name, orgKey); project.name = await this.encryptService.encryptString(p.name, orgKey);
return project; return project;
}), }),
); );
@@ -96,9 +96,9 @@ export class SecretsManagerPortingApiService {
const secret = new SecretsManagerImportedSecretRequest(); const secret = new SecretsManagerImportedSecretRequest();
[secret.key, secret.value, secret.note] = await Promise.all([ [secret.key, secret.value, secret.note] = await Promise.all([
this.encryptService.encrypt(s.key, orgKey), this.encryptService.encryptString(s.key, orgKey),
this.encryptService.encrypt(s.value, orgKey), this.encryptService.encryptString(s.value, orgKey),
this.encryptService.encrypt(s.note, orgKey), this.encryptService.encryptString(s.note, orgKey),
]); ]);
secret.id = s.id; secret.id = s.id;
@@ -129,7 +129,7 @@ export class SecretsManagerPortingApiService {
exportData.projects.map(async (p) => { exportData.projects.map(async (p) => {
const project = new SecretsManagerExportProject(); const project = new SecretsManagerExportProject();
project.id = p.id; project.id = p.id;
project.name = await this.encryptService.decryptToUtf8(new EncString(p.name), orgKey); project.name = await this.encryptService.decryptString(new EncString(p.name), orgKey);
return project; return project;
}), }),
); );
@@ -139,9 +139,9 @@ export class SecretsManagerPortingApiService {
const secret = new SecretsManagerExportSecret(); const secret = new SecretsManagerExportSecret();
[secret.key, secret.value, secret.note] = await Promise.all([ [secret.key, secret.value, secret.note] = await Promise.all([
this.encryptService.decryptToUtf8(new EncString(s.key), orgKey), this.encryptService.decryptString(new EncString(s.key), orgKey),
this.encryptService.decryptToUtf8(new EncString(s.value), orgKey), this.encryptService.decryptString(new EncString(s.value), orgKey),
this.encryptService.decryptToUtf8(new EncString(s.note), orgKey), this.encryptService.decryptString(new EncString(s.note), orgKey),
]); ]);
secret.id = s.id; secret.id = s.id;

View File

@@ -138,7 +138,7 @@ describe("AccessPolicyService", () => {
const mockRandomBytes = new Uint8Array(64) as CsprngArray; const mockRandomBytes = new Uint8Array(64) as CsprngArray;
const mockOrgKey = new SymmetricCryptoKey(mockRandomBytes) as OrgKey; const mockOrgKey = new SymmetricCryptoKey(mockRandomBytes) as OrgKey;
keyService.getOrgKey.mockResolvedValue(mockOrgKey); keyService.getOrgKey.mockResolvedValue(mockOrgKey);
encryptService.decryptToUtf8.mockImplementation((c) => Promise.resolve(c.encryptedString)); encryptService.decryptString.mockImplementation((c) => Promise.resolve(c.encryptedString));
const organizationId = Utils.newGuid(); const organizationId = Utils.newGuid();
const serviceAccountId = Utils.newGuid(); const serviceAccountId = Utils.newGuid();
@@ -175,7 +175,7 @@ describe("AccessPolicyService", () => {
const mockRandomBytes = new Uint8Array(64) as CsprngArray; const mockRandomBytes = new Uint8Array(64) as CsprngArray;
const mockOrgKey = new SymmetricCryptoKey(mockRandomBytes) as OrgKey; const mockOrgKey = new SymmetricCryptoKey(mockRandomBytes) as OrgKey;
keyService.getOrgKey.mockResolvedValue(mockOrgKey); keyService.getOrgKey.mockResolvedValue(mockOrgKey);
encryptService.decryptToUtf8.mockImplementation((c) => Promise.resolve(c.encryptedString)); encryptService.decryptString.mockImplementation((c) => Promise.resolve(c.encryptedString));
const organizationId = Utils.newGuid(); const organizationId = Utils.newGuid();
const projectId = Utils.newGuid(); const projectId = Utils.newGuid();

View File

@@ -350,7 +350,7 @@ export class AccessPolicyService {
...this.createBaseAccessPolicyView(response), ...this.createBaseAccessPolicyView(response),
grantedProjectId: response.grantedProjectId, grantedProjectId: response.grantedProjectId,
grantedProjectName: response.grantedProjectName grantedProjectName: response.grantedProjectName
? await this.encryptService.decryptToUtf8( ? await this.encryptService.decryptString(
new EncString(response.grantedProjectName), new EncString(response.grantedProjectName),
organizationKey, organizationKey,
) )
@@ -394,7 +394,7 @@ export class AccessPolicyService {
...this.createBaseAccessPolicyView(response), ...this.createBaseAccessPolicyView(response),
serviceAccountId: response.serviceAccountId, serviceAccountId: response.serviceAccountId,
serviceAccountName: response.serviceAccountName serviceAccountName: response.serviceAccountName
? await this.encryptService.decryptToUtf8( ? await this.encryptService.decryptString(
new EncString(response.serviceAccountName), new EncString(response.serviceAccountName),
orgKey, orgKey,
) )
@@ -420,7 +420,7 @@ export class AccessPolicyService {
if (r.type === "serviceAccount" || r.type === "project") { if (r.type === "serviceAccount" || r.type === "project") {
view.name = r.name view.name = r.name
? await this.encryptService.decryptToUtf8(new EncString(r.name), orgKey) ? await this.encryptService.decryptString(new EncString(r.name), orgKey)
: null; : null;
} else { } else {
view.name = r.name; view.name = r.name;