Skip to content

Commit

Permalink
[JavaToolInstaller] New method to get account keys. (#18405)
Browse files Browse the repository at this point in the history
* [JavaToolInstaller] New method to get account keys.

-- Added new input to the task to set resource group name
-- Added new logic to fetch storage account when resource group name is provided

* [JavaToolInstaller] New method to get account keys.

-- bump azure-arm-rest-v2

* [JavaToolInstaller] New method to get account keys.

-- bump task version

---------

Co-authored-by: Kirill Ivlev <102740624+kirill-ivlev@users.noreply.github.com>
  • Loading branch information
DmitriiBobreshev and kirill-ivlev authored Jun 12, 2023
1 parent 7acfb8e commit 3df8f18
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import { AzureRMEndpoint } from 'azure-pipelines-tasks-azure-arm-rest-v2/azure-a
export class AzureStorageArtifactDownloader {
public connectedService: string;
public azureStorageAccountName: string;
public azureResourceGroupName?: string;
public containerName: string;
public commonVirtualPath: string;


constructor(connectedService: string, azureStorageAccountName: string, containerName: string, commonVirtualPath: string) {
constructor(connectedService: string, azureStorageAccountName: string, containerName: string, commonVirtualPath: string, azureResourceGroupName?: string) {
this.connectedService = connectedService;
this.azureStorageAccountName = azureStorageAccountName;
this.azureResourceGroupName = azureResourceGroupName;
this.containerName = containerName;
this.commonVirtualPath = commonVirtualPath;
}
Expand All @@ -41,9 +43,19 @@ export class AzureStorageArtifactDownloader {
? !!process.env.AZP_TASK_FF_JAVATOOLINSTALLER_USE_OLD_SA_QUERY
: false;

const storageAccount = isUseOldStorageAccountQuery
? await this._legacyGetStorageAccount(storageArmClient)
: await this._getStorageAccount(storageArmClient);
let storageAccount = null;
if (this.azureResourceGroupName) {
tl.debug("Group name is provided. Using fast query to get storage account details.");
storageAccount = await this._getStorageAccountWithResourceGroup(storageArmClient, this.azureResourceGroupName, this.azureStorageAccountName);
}

if (!storageAccount) {
tl.debug("Group name is not provided or fast query failed. Using legacy query to get storage account details.");
storageAccount = isUseOldStorageAccountQuery
? await this._legacyGetStorageAccount(storageArmClient)
: await this._getStorageAccount(storageArmClient);

}

const storageAccountResourceGroupName = armStorage.StorageAccounts.getResourceGroupNameFromUri(storageAccount.id);

Expand Down Expand Up @@ -77,6 +89,22 @@ export class AzureStorageArtifactDownloader {
return storageAccount;
}

private async _getStorageAccountWithResourceGroup(storageArmClient: armStorage.StorageManagementClient, resourceGroupName: string, storageAccountName: string): Promise<Model.StorageAccount | undefined> {
let storageAccount = undefined;

try {
storageAccount = await storageArmClient.storageAccounts.getStorageAccountProperties(resourceGroupName, storageAccountName);
} catch (e) {
tl.warning("Failed to get storage account details using fast query.");
}

if (storageAccount) {
tl.debug("Found storage account details using fast query.");
}

return storageAccount;
}

private async _getARMCredentials(): Promise<msRestAzure.ApplicationTokenCredentials> {
const endpoint: AzureEndpoint = await new AzureRMEndpoint(this.connectedService).getEndpoint();
return endpoint.applicationTokenCredentials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"loc.input.help.azureCommonVirtualFile": "Path to the JDK inside the Azure storage container.",
"loc.input.label.jdkDestinationDirectory": "Destination directory",
"loc.input.help.jdkDestinationDirectory": "On Linux and Windows, this is used as the destination directory for JDK installation. On macOS, this directory is used as a temporary folder for extracting of .dmg's since macOS doesn't support installing of JDK to specific directory.",
"loc.input.label.azureResourceGroupName": "Resource Group name",
"loc.input.help.azureResourceGroupName": "Resource Group name of the storage account.",
"loc.input.label.cleanDestinationDirectory": "Clean destination directory",
"loc.input.help.cleanDestinationDirectory": "Select this option to clean the destination directory before JDK is extracted into it.",
"loc.input.label.createExtractDirectory": "Create directory for extracting",
Expand Down
9 changes: 7 additions & 2 deletions Tasks/JavaToolInstallerV0/javatoolinstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ async function getJava(versionSpec: string, jdkArchitectureOption: string): Prom
// download from azure and save to temporary directory
console.log(taskLib.loc('RetrievingJdkFromAzure'));
const fileNameAndPath: string = taskLib.getInput('azureCommonVirtualFile', true);
const azureDownloader = new AzureStorageArtifactDownloader(taskLib.getInput('azureResourceManagerEndpoint', true),
taskLib.getInput('azureStorageAccountName', true), taskLib.getInput('azureContainerName', true), "");
const azureDownloader = new AzureStorageArtifactDownloader(
taskLib.getInput('azureResourceManagerEndpoint', true),
taskLib.getInput('azureStorageAccountName', true),
taskLib.getInput('azureContainerName', true),
"",
taskLib.getInput('azureResourceGroupName', false),
);
await azureDownloader.downloadArtifacts(extractLocation, '*' + fileNameAndPath);
await taskutils.sleepFor(250); //Wait for the file to be released before extracting it.
let jdkArchiveName = path.basename(fileNameAndPath);
Expand Down
12 changes: 6 additions & 6 deletions Tasks/JavaToolInstallerV0/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tasks/JavaToolInstallerV0/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@types/q": "^1.0.7",
"azp-tasks-az-blobstorage-provider": "^2.219.1",
"azure-pipelines-task-lib": "^4.0.0-preview",
"azure-pipelines-tasks-azure-arm-rest-v2": "3.223.3",
"azure-pipelines-tasks-azure-arm-rest-v2": "^3.223.6",
"azure-pipelines-tasks-utility-common": "^3.0.3",
"azure-pipelines-tool-lib": "^2.0.0-preview"
},
Expand Down
10 changes: 9 additions & 1 deletion Tasks/JavaToolInstallerV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 223,
"Minor": 224,
"Patch": 0
},
"satisfies": [
Expand Down Expand Up @@ -117,6 +117,14 @@
"visibleRule": "jdkSourceOption != PreInstalled",
"helpMarkDown": "On Linux and Windows, this is used as the destination directory for JDK installation. On macOS, this directory is used as a temporary folder for extracting of .dmg's since macOS doesn't support installing of JDK to specific directory."
},
{
"name": "azureResourceGroupName",
"type": "string",
"label": "Resource Group name",
"required": false,
"visibleRule": "jdkSourceOption == AzureStorage",
"helpMarkDown": "Resource Group name of the storage account."
},
{
"name": "cleanDestinationDirectory",
"type": "boolean",
Expand Down
10 changes: 9 additions & 1 deletion Tasks/JavaToolInstallerV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 223,
"Minor": 224,
"Patch": 0
},
"satisfies": [
Expand Down Expand Up @@ -117,6 +117,14 @@
"visibleRule": "jdkSourceOption != PreInstalled",
"helpMarkDown": "ms-resource:loc.input.help.jdkDestinationDirectory"
},
{
"name": "azureResourceGroupName",
"type": "string",
"label": "ms-resource:loc.input.label.azureResourceGroupName",
"required": false,
"visibleRule": "jdkSourceOption == AzureStorage",
"helpMarkDown": "ms-resource:loc.input.help.azureResourceGroupName"
},
{
"name": "cleanDestinationDirectory",
"type": "boolean",
Expand Down

0 comments on commit 3df8f18

Please sign in to comment.