Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/lib/azure/azurecredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import { Config } from "../../config";
import { logger } from "../../logger";
import { AzureAccessOpts } from "../../types";
import { build as buildError } from "../../lib/errorBuilder";
import { errorStatusCode } from "../errorStatusCode";

const verifyConfigDefined = (
servicePrincipalId?: string,
Expand Down Expand Up @@ -85,13 +87,21 @@ export const getManagementCredentials = async (
return undefined;
}

// verifyConfigDefined has confirmed that these values are defined.
return msRestNodeAuth.loginWithServicePrincipalSecret(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
servicePrincipalId!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
servicePrincipalPassword!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
tenantId!
);
try {
// verifyConfigDefined has confirmed that these values are defined.
return await msRestNodeAuth.loginWithServicePrincipalSecret(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
servicePrincipalId!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
servicePrincipalPassword!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
tenantId!
);
} catch (err) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"azure-client-auth-sp-err",
err
);
}
};
87 changes: 86 additions & 1 deletion src/lib/azure/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ jest.mock("@azure/arm-storage");
jest.mock("azure-storage");
jest.mock("../../config");

import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import uuid from "uuid/v4";
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
import { Config } from "../../config";
import { getStorageAccount, validateStorageAccount } from "./storage";
import * as config from "../../config";
import {
getStorageAccount,
getStorageManagementClient,
validateStorageAccount,
} from "./storage";
import * as storage from "./storage";
import * as azureStorage from "azure-storage";
import { getErrorMessage } from "../../lib/errorBuilder";
Expand All @@ -14,6 +20,17 @@ const resourceGroupName = uuid();
const storageAccountName = uuid();
const location = uuid();

jest.mock("@azure/arm-storage", () => {
class MockClient {
constructor() {
return {};
}
}
return {
StorageManagementClient: MockClient,
};
});

(Config as jest.Mock).mockReturnValue({
introspection: {
azure: {
Expand Down Expand Up @@ -373,3 +390,71 @@ describe("test validateStorageAccount function", () => {
expect(res).toBe(true);
});
});

describe("test getStorageManagementClient function", () => {
it("negative test: missing credential", async () => {
jest.spyOn(config, "Config").mockReturnValueOnce({});
await expect(getStorageManagementClient({})).rejects.toThrow(
getErrorMessage("storage-client-err-missing-creds")
);
});
it("negative test: incorrect credential", async () => {
jest.spyOn(config, "Config").mockReturnValueOnce({});
await expect(
getStorageManagementClient({
servicePrincipalId: "servicePrincipalId",
servicePrincipalPassword: "servicePrincipalPassword",
tenantId: "tenantId",
})
).rejects.toThrow(getErrorMessage("azure-client-auth-sp-err"));
});
it("negative test: authentication to management client failed", async () => {
jest.spyOn(config, "Config").mockReturnValueOnce({});
jest
.spyOn(msRestNodeAuth, "loginWithServicePrincipalSecret")
.mockResolvedValueOnce(null as never);
await expect(
getStorageManagementClient({
servicePrincipalId: "servicePrincipalId",
servicePrincipalPassword: "servicePrincipalPassword",
tenantId: "tenantId",
})
).rejects.toThrow(getErrorMessage("storage-client-err-missing-creds"));
});
it("negative test: missing storage cred.", async () => {
jest.spyOn(config, "Config").mockReturnValueOnce({});
jest.spyOn(config, "Config").mockReturnValueOnce({});
jest
.spyOn(msRestNodeAuth, "loginWithServicePrincipalSecret")
.mockResolvedValueOnce({} as never);
await expect(
getStorageManagementClient({
servicePrincipalId: "servicePrincipalId",
servicePrincipalPassword: "servicePrincipalPassword",
tenantId: "tenantId",
})
).rejects.toThrow(getErrorMessage("storage-client-err-missing-sub-id"));
});
it("positive test: missing storage cred.", async () => {
jest.spyOn(config, "Config").mockReturnValueOnce({});
jest.spyOn(config, "Config").mockReturnValueOnce({
introspection: {
azure: {
subscription_id: "something",
},
},
});
jest
.spyOn(msRestNodeAuth, "loginWithServicePrincipalSecret")
.mockResolvedValueOnce({} as never);
await getStorageManagementClient({
servicePrincipalId: "servicePrincipalId",
servicePrincipalPassword: "servicePrincipalPassword",
tenantId: "tenantId",
});
});
it("positive test: client should be cached.", async () => {
const client = await getStorageManagementClient(); // cached copy will be returned
expect(client).toBeDefined();
});
});
1 change: 1 addition & 0 deletions src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@
"service-endpoint-err-create-missing-name": "Could not create service endpoint because name was missing.",
"service-endpoint-err-create": "Could not create service endpoint.",

"azure-client-auth-sp-err": "Could not authenticate with service principal credential.",
"azure-client-get-web-api-err-missing-access-token": "Could not get azure web API because personal access token was missing. Provide it.",
"azure-client-get-web-api-err-missing-org": "Could not get azure web API because organization name was missing. Provide it.",
"azure-client-get-rest-client-err": "Could not get REST client. Check the Azure credential",
Expand Down