From c7b3239660802094d7c774b59b5e8d7da16819eb Mon Sep 17 00:00:00 2001 From: Ben Pearce Date: Fri, 21 Apr 2023 08:58:39 +1000 Subject: [PATCH] support ignoring ssl errors for self signed instances (#308) consolidate client creation --- .../BuildInformationV6/buildInformation.ts | 13 +++-------- .../CreateOctopusReleaseV6/release.ts | 23 +++---------------- source/tasks/Deploy/DeployV6/deploy.ts | 13 +++-------- .../DeployTenant/TenantedDeployV6/deploy.ts | 13 +++-------- source/tasks/Push/PushV6/index.ts | 14 +++-------- .../RunRunbook/RunRunbookV6/runbookRun.ts | 13 +++-------- source/tasks/Utils/client.ts | 18 +++++++++++++++ 7 files changed, 36 insertions(+), 71 deletions(-) create mode 100644 source/tasks/Utils/client.ts diff --git a/source/tasks/BuildInformation/BuildInformationV6/buildInformation.ts b/source/tasks/BuildInformation/BuildInformationV6/buildInformation.ts index c1b82f9a..640c2dcd 100644 --- a/source/tasks/BuildInformation/BuildInformationV6/buildInformation.ts +++ b/source/tasks/BuildInformation/BuildInformationV6/buildInformation.ts @@ -1,24 +1,17 @@ import { OctoServerConnectionDetails } from "../../Utils/connection"; import { createCommandFromInputs } from "./inputCommandBuilder"; -import { BuildInformationRepository, Client, ClientConfiguration, Logger } from "@octopusdeploy/api-client"; +import { BuildInformationRepository, Logger } from "@octopusdeploy/api-client"; import { TaskWrapper } from "tasks/Utils/taskInput"; import { getOverwriteMode } from "./overwriteMode"; import { IVstsHelper } from "./vsts"; -import { getUserAgentApp } from "../../Utils/pluginInformation"; +import { getClient } from "../../Utils/client"; export class BuildInformation { constructor(readonly connection: OctoServerConnectionDetails, readonly logger: Logger, readonly task: TaskWrapper, readonly vsts: IVstsHelper) {} public async run() { const command = await createCommandFromInputs(this.logger, this.task, this.vsts); - - const config: ClientConfiguration = { - userAgentApp: getUserAgentApp("build-information", "push", 6), - instanceURL: this.connection.url, - apiKey: this.connection.apiKey, - logging: this.logger, - }; - const client = await Client.create(config); + const client = await getClient(this.connection, this.logger, "build-information", "push", 6) const overwriteMode = await getOverwriteMode(this.logger, this.task); this.logger.debug?.(`Build Information:\n${JSON.stringify(command, null, 2)}`); diff --git a/source/tasks/CreateOctopusRelease/CreateOctopusReleaseV6/release.ts b/source/tasks/CreateOctopusRelease/CreateOctopusReleaseV6/release.ts index 25460b4d..94b6d932 100644 --- a/source/tasks/CreateOctopusRelease/CreateOctopusReleaseV6/release.ts +++ b/source/tasks/CreateOctopusRelease/CreateOctopusReleaseV6/release.ts @@ -1,23 +1,14 @@ -import { - Client, - ClientConfiguration, - CreateReleaseCommandV1, - Logger, - Project, - ProjectRepository, - Space, - SpaceRepository -} from "@octopusdeploy/api-client"; +import { Client, CreateReleaseCommandV1, Logger, Project, ProjectRepository, Space, SpaceRepository } from "@octopusdeploy/api-client"; import { OctoServerConnectionDetails } from "../../Utils/connection"; import { createReleaseFromInputs } from "./createRelease"; import { createCommandFromInputs } from "./inputCommandBuilder"; import os from "os"; import { TaskWrapper } from "tasks/Utils/taskInput"; -import { getUserAgentApp } from "../../Utils/pluginInformation"; import path from "path"; import { getVstsEnvironmentVariables } from "../../../tasksLegacy/Utils/environment"; import { v4 as uuidv4 } from "uuid"; import * as tasks from "azure-pipelines-task-lib"; +import { getClient } from "../../Utils/client"; export class Release { constructor(readonly connection: OctoServerConnectionDetails, readonly task: TaskWrapper, readonly logger: Logger) {} @@ -25,15 +16,7 @@ export class Release { public async run() { try { const command = createCommandFromInputs(this.logger, this.task); - - const config: ClientConfiguration = { - userAgentApp: getUserAgentApp("release", "create", 6), - instanceURL: this.connection.url, - apiKey: this.connection.apiKey, - logging: this.logger, - }; - const client = await Client.create(config); - + const client = await getClient(this.connection, this.logger, "release", "create", 6); const version = await createReleaseFromInputs(client, command, this.task, this.logger); await this.tryCreateSummary(client, command, version); diff --git a/source/tasks/Deploy/DeployV6/deploy.ts b/source/tasks/Deploy/DeployV6/deploy.ts index 527104fb..cb58bf5e 100644 --- a/source/tasks/Deploy/DeployV6/deploy.ts +++ b/source/tasks/Deploy/DeployV6/deploy.ts @@ -1,10 +1,10 @@ -import { Client, ClientConfiguration, Logger } from "@octopusdeploy/api-client"; +import { Logger } from "@octopusdeploy/api-client"; import { OctoServerConnectionDetails } from "../../Utils/connection"; import { createDeploymentFromInputs } from "./createDeployment"; import { createCommandFromInputs } from "./inputCommandBuilder"; import os from "os"; import { TaskWrapper } from "tasks/Utils/taskInput"; -import { getUserAgentApp } from "../../Utils/pluginInformation"; +import { getClient } from "../../Utils/client"; export class Deploy { constructor(readonly connection: OctoServerConnectionDetails, readonly task: TaskWrapper, readonly logger: Logger) {} @@ -12,14 +12,7 @@ export class Deploy { public async run() { try { const command = createCommandFromInputs(this.logger, this.task); - - const config: ClientConfiguration = { - userAgentApp: getUserAgentApp("release", "deploy", 6), - instanceURL: this.connection.url, - apiKey: this.connection.apiKey, - logging: this.logger, - }; - const client = await Client.create(config); + const client = await getClient(this.connection, this.logger, "release", "deploy", 6); createDeploymentFromInputs(client, command, this.task, this.logger); diff --git a/source/tasks/DeployTenant/TenantedDeployV6/deploy.ts b/source/tasks/DeployTenant/TenantedDeployV6/deploy.ts index b2152cf9..65b0a884 100644 --- a/source/tasks/DeployTenant/TenantedDeployV6/deploy.ts +++ b/source/tasks/DeployTenant/TenantedDeployV6/deploy.ts @@ -1,10 +1,10 @@ -import { Client, ClientConfiguration, Logger } from "@octopusdeploy/api-client"; +import { Logger } from "@octopusdeploy/api-client"; import { OctoServerConnectionDetails } from "../../Utils/connection"; import { createDeploymentFromInputs } from "./createDeployment"; import { createCommandFromInputs } from "./inputCommandBuilder"; import os from "os"; import { TaskWrapper } from "tasks/Utils/taskInput"; -import { getUserAgentApp } from "../../Utils/pluginInformation"; +import { getClient } from "../../Utils/client"; export class Deploy { constructor(readonly connection: OctoServerConnectionDetails, readonly task: TaskWrapper, readonly logger: Logger) {} @@ -12,14 +12,7 @@ export class Deploy { public async run() { try { const command = createCommandFromInputs(this.logger, this.task); - - const config: ClientConfiguration = { - userAgentApp: getUserAgentApp("release", "deploy-tenanted", 6), - instanceURL: this.connection.url, - apiKey: this.connection.apiKey, - logging: this.logger, - }; - const client = await Client.create(config); + const client = await getClient(this.connection, this.logger, "release", "deploy-tenanted", 6); createDeploymentFromInputs(client, command, this.task, this.logger); diff --git a/source/tasks/Push/PushV6/index.ts b/source/tasks/Push/PushV6/index.ts index 9c6d46e0..6aac5bec 100644 --- a/source/tasks/Push/PushV6/index.ts +++ b/source/tasks/Push/PushV6/index.ts @@ -1,10 +1,10 @@ -import { Client, ClientConfiguration, Logger } from "@octopusdeploy/api-client"; +import { Logger } from "@octopusdeploy/api-client"; import * as tasks from "azure-pipelines-task-lib/task"; import { getDefaultOctopusConnectionDetailsOrThrow } from "../../Utils/connection"; import { getLineSeparatedItems, getOverwriteModeFromReplaceInput, getRequiredInput } from "../../Utils/inputs"; import { Push } from "./push"; import os from "os"; -import { getUserAgentApp } from "../../Utils/pluginInformation"; +import { getClient } from "../../Utils/client"; async function run() { try { @@ -29,15 +29,7 @@ async function run() { }, }; - const config: ClientConfiguration = { - userAgentApp: getUserAgentApp("package", "push", 6), - instanceURL: connection.url, - apiKey: connection.apiKey, - logging: logger, - }; - - const client: Client = await Client.create(config); - + const client = await getClient(connection, logger, "package", "push", 6); await new Push(client).run(spaceName, packages, overwriteMode); } catch (error: unknown) { if (error instanceof Error) { diff --git a/source/tasks/RunRunbook/RunRunbookV6/runbookRun.ts b/source/tasks/RunRunbook/RunRunbookV6/runbookRun.ts index 731e026d..16dbd379 100644 --- a/source/tasks/RunRunbook/RunRunbookV6/runbookRun.ts +++ b/source/tasks/RunRunbook/RunRunbookV6/runbookRun.ts @@ -1,10 +1,10 @@ -import { Client, ClientConfiguration, Logger } from "@octopusdeploy/api-client"; +import { Logger } from "@octopusdeploy/api-client"; import { OctoServerConnectionDetails } from "../../Utils/connection"; import { createRunbookRunFromInputs } from "./runRunbook"; import { createCommandFromInputs } from "./inputCommandBuilder"; import os from "os"; import { TaskWrapper } from "tasks/Utils/taskInput"; -import { getUserAgentApp } from "../../Utils/pluginInformation"; +import { getClient } from "../../Utils/client"; export class RunbookRun { constructor(readonly connection: OctoServerConnectionDetails, readonly task: TaskWrapper, readonly logger: Logger) {} @@ -12,14 +12,7 @@ export class RunbookRun { public async run() { try { const command = createCommandFromInputs(this.logger, this.task); - - const config: ClientConfiguration = { - userAgentApp: getUserAgentApp("runbook", "run", 6), - instanceURL: this.connection.url, - apiKey: this.connection.apiKey, - logging: this.logger, - }; - const client = await Client.create(config); + const client = await getClient(this.connection, this.logger, "runbook", "run", 6); createRunbookRunFromInputs(client, command, this.task, this.logger); diff --git a/source/tasks/Utils/client.ts b/source/tasks/Utils/client.ts new file mode 100644 index 00000000..04ab015e --- /dev/null +++ b/source/tasks/Utils/client.ts @@ -0,0 +1,18 @@ +import { Client, ClientConfiguration, Logger } from "@octopusdeploy/api-client"; +import { getUserAgentApp } from "./pluginInformation"; +import https from "https"; +import { OctoServerConnectionDetails } from "./connection"; + +export async function getClient(connection: OctoServerConnectionDetails, logger: Logger, stepNoun: string, stepVerb: string, stepVersion: number) { + const config: ClientConfiguration = { + userAgentApp: getUserAgentApp(stepNoun, stepVerb, stepVersion), + instanceURL: connection.url, + apiKey: connection.apiKey, + logging: logger, + }; + if (connection.ignoreSslErrors) { + config.httpsAgent = new https.Agent({ rejectUnauthorized: false }); + } + + return await Client.create(config); +}