Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Open Policy Agent Installer Task #11329

Merged
merged 2 commits into from
Sep 12, 2019
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
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ Tasks/NuGetPublisherV0/* @zjrunner

Tasks/NuGetToolInstallerV0/* @zjrunner

Tasks/OpenPolicyAgentInstallerV0/* @Anumita

Tasks/PackerBuildV0/* @bishal-pdmsft

Tasks/PackerBuildV1/* @bishal-pdmsft
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"loc.friendlyName": "Open Policy Agent Installer",
"loc.helpMarkDown": "[Learn more about this task](https://go.microsoft.com/fwlink/?linkid=851275)",
"loc.description": "Install Open Policy Agent on agent machine",
"loc.instanceNameFormat": "Install opa $(opaVersion)",
"loc.input.label.opaVersion": "OPA Version Spec",
"loc.input.help.opaVersion": "Specify the version of open policy agent to install",
"loc.messages.DownloadOpaFailedFromLocation": "Failed to download opa from location %s. Error %s",
"loc.messages.OpaLatestNotKnown": "Cannot get the latest OpenPolicyAgent info from %s. Error %s. Using default OpenPolicyAgent version %s.",
"loc.messages.NotAValidSemverVersion": "Version not specified in correct format. E.g: 1.8.2, v1.8.2, 2.8.2, v2.8.2.",
"loc.messages.VerifyOpaInstallation": "Verifying opa installation..."
}
1,707 changes: 1,707 additions & 0 deletions Tasks/OpenPolicyAgentInstallerV0/ThirdPartyNotices.txt

Large diffs are not rendered by default.

Binary file added Tasks/OpenPolicyAgentInstallerV0/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Tasks/OpenPolicyAgentInstallerV0/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Tasks/OpenPolicyAgentInstallerV0/make.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
134 changes: 134 additions & 0 deletions Tasks/OpenPolicyAgentInstallerV0/package-lock.json

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

8 changes: 8 additions & 0 deletions Tasks/OpenPolicyAgentInstallerV0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"@types/node": "^6.0.101",
"@types/q": "^1.5.0",
"azure-pipelines-task-lib": "2.8.0",
"azure-pipelines-tool-lib": "0.12.0"
}
}
33 changes: 33 additions & 0 deletions Tasks/OpenPolicyAgentInstallerV0/src/opatoolinstaller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
import * as tl from 'azure-pipelines-task-lib/task';
import * as path from 'path';
import * as toolLib from 'azure-pipelines-tool-lib/tool';
import utils = require("./utils");

tl.setResourcePath(path.join(__dirname, '..', 'task.json'));

async function configureOpa() {
var version = await utils.getOpaVersion();
var opaPath = await utils.downloadOpa(version);

// prepend the tools path. instructs the agent to prepend for future tasks
if (!process.env['PATH'].startsWith(path.dirname(opaPath))) {
toolLib.prependPath(path.dirname(opaPath));
}
}

async function verifyOpa() {
console.log(tl.loc("VerifyOpaInstallation"));
var opaToolPath = tl.which("opa", true);
var opaTool = tl.tool(opaToolPath);
opaTool.arg("version");
return opaTool.exec();
}

configureOpa()
.then(() => verifyOpa())
.then(() => {
tl.setResult(tl.TaskResult.Succeeded, "");
}).catch((error) => {
tl.setResult(tl.TaskResult.Failed, error);
});
102 changes: 102 additions & 0 deletions Tasks/OpenPolicyAgentInstallerV0/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"use strict";

var fs = require('fs');
import * as path from "path";
import * as tl from "azure-pipelines-task-lib/task";
import * as os from "os";
import * as toolLib from 'azure-pipelines-tool-lib/tool';
import * as util from "util";

const opaToolName = 'opa';
const opaLatestReleaseUrl = 'https://api.github.com/repos/open-policy-agent/opa/releases/latest';
const stableOpaVersion = 'v0.13.5';

export function getTempDirectory(): string {
return tl.getVariable('agent.tempDirectory') || os.tmpdir();
}

export async function getOpaVersion(): Promise<string> {
let opaVersion = tl.getInput("opaVersion");
if(opaVersion && opaVersion != "latest") {
return sanitizeVersionString(opaVersion.trim());
}

return await getStableOpaVersion();
}

function getOpaDownloadURL(version: string): string {
switch (os.type()) {
case 'Linux':
return util.format('https://github.com/open-policy-agent/opa/releases/download/%s/opa_linux_amd64', version);

case 'Darwin':
return util.format('https://github.com/open-policy-agent/opa/releases/download/%s/opa_darwin_amd64', version);

case 'Windows_NT':
return util.format('https://github.com/open-policy-agent/opa/releases/download/%s/opa_windows_amd64.exe', version);

default:
throw Error('Unknown OS type');
}
}

export async function getStableOpaVersion(): Promise<string> {
try {
const downloadPath = await toolLib.downloadTool(opaLatestReleaseUrl);
const response = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim());
if (!response.tag_name)
{
return stableOpaVersion;
}

return response.tag_name;
} catch (error) {
tl.warning(tl.loc('OpaLatestNotKnown', opaLatestReleaseUrl, error, stableOpaVersion));
}

return stableOpaVersion;
}

export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
return '.exe';
}

return '';
}

export async function downloadOpa(version: string): Promise<string> {
let cachedToolpath = toolLib.findLocalTool(opaToolName, version);
let opaDownloadPath = '';
if (!cachedToolpath) {
try {
opaDownloadPath = await toolLib.downloadTool(getOpaDownloadURL(version));
} catch (exception) {
throw new Error(tl.loc('DownloadOpaFailedFromLocation', getOpaDownloadURL(version), exception));
}

cachedToolpath = await toolLib.cacheFile(opaDownloadPath, opaToolName + getExecutableExtension(), opaToolName, version);
}

const opaPath = path.join(cachedToolpath, opaToolName + getExecutableExtension());

if (!cachedToolpath || !fs.existsSync(opaPath)) {
const opaPathTmp = path.join(getTempDirectory(), opaToolName + getExecutableExtension());
tl.cp(opaDownloadPath, opaPathTmp, '-f');
fs.chmodSync(opaPathTmp, '777');
return opaPathTmp;
}

fs.chmodSync(opaPath, '777');
return opaPath;
}

// handle user input scenerios
export function sanitizeVersionString(inputVersion: string) : string{
var version = toolLib.cleanVersion(inputVersion);
if(!version) {
throw new Error(tl.loc("NotAValidSemverVersion"));
}

return "v"+version;
}
43 changes: 43 additions & 0 deletions Tasks/OpenPolicyAgentInstallerV0/task.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"id": "50817E39-E160-45E1-A825-1C746B7D2EB2",
"name": "OpenPolicyAgentInstaller",
"friendlyName": "Open Policy Agent Installer",
"description": "Install Open Policy Agent on agent machine",
"helpUrl": "https://docs.microsoft.com/azure/devops/pipelines/tasks",
"helpMarkDown": "[Learn more about this task](https://go.microsoft.com/fwlink/?linkid=851275)",
"category": "Utility",
"visibility": [
"Build",
"Release"
],
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 157,
"Patch": 0
},
"demands": [],
"groups": [],
"inputs": [
{
"name": "opaVersion",
"label": "OPA Version Spec",
"type": "string",
"helpMarkDown": "Specify the version of open policy agent to install",
"defaultValue": "latest"
}
],

"instanceNameFormat": "Install opa $(opaVersion)",
"execution": {
"Node": {
"target": "src//opatoolinstaller.js"
}
},
"messages": {
"DownloadOpaFailedFromLocation": "Failed to download opa from location %s. Error %s",
"OpaLatestNotKnown": "Cannot get the latest OpenPolicyAgent info from %s. Error %s. Using default OpenPolicyAgent version %s.",
"NotAValidSemverVersion": "Version not specified in correct format. E.g: 1.8.2, v1.8.2, 2.8.2, v2.8.2.",
"VerifyOpaInstallation": "Verifying opa installation..."
}
}
Loading