Skip to content

Commit

Permalink
Ensure webhookName meets requirements (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejizba authored Sep 13, 2019
1 parent d7edd31 commit 6340f1d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
22 changes: 12 additions & 10 deletions src/commands/registries/azure/DockerWebhookCreateStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DockerHubRepositoryTreeItem } from '../../../tree/registries/dockerHub/
import { RemoteTagTreeItem } from '../../../tree/registries/RemoteTagTreeItem';
import { nonNullProp } from "../../../utils/nonNull";
import { openExternal } from '../../../utils/openExternal';
import { randomUtils } from '../../../utils/randomUtils';

export class DockerWebhookCreateStep extends AzureWizardExecuteStep<IAppServiceWizardContext> {
public priority: number = 141; // execute after DockerSiteCreate
Expand Down Expand Up @@ -67,19 +68,20 @@ export class DockerWebhookCreateStep extends AzureWizardExecuteStep<IAppServiceW
}

private async createWebhookForApp(node: RemoteTagTreeItem, site: Site, appUri: string): Promise<Webhook | undefined> {
// fields derived from the app service wizard
let siteName: string = site.name;
let baseName = `webapp${siteName}`;
let webhookName: string = baseName;
const maxLength: number = 50;
const numRandomChars: number = 6;

let webhookName: string = site.name;
// remove disallowed characters
webhookName = webhookName.replace(/[^a-zA-Z0-9]/g, '');
// trim to max length
webhookName = webhookName.substr(0, maxLength - numRandomChars);
// add random chars for uniqueness and to ensure min length is met
webhookName += randomUtils.getRandomHexString(numRandomChars);

// variables derived from the container registry
const registryTreeItem: AzureRegistryTreeItem = (<AzureRepositoryTreeItem>node.parent).parent;
const crmClient = createAzureClient(registryTreeItem.parent.root, ContainerRegistryManagementClient);
const existingWebhooks = await crmClient.webhooks.list(registryTreeItem.resourceGroup, registryTreeItem.registryName);
let dedupeCount: number = 0;
while (existingWebhooks.find((hook) => hook.name === webhookName)) {
webhookName = `${baseName}_${dedupeCount}`;
dedupeCount++;
}
let webhookCreateParameters: WebhookCreateParameters = {
location: registryTreeItem.registryLocation,
serviceUri: appUri,
Expand Down
13 changes: 4 additions & 9 deletions src/debugging/coreclr/CommandLineDotNetClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import * as crypto from 'crypto';
import * as semver from 'semver';
import { parseError } from 'vscode-azureextensionui';
import { randomUtils } from '../../utils/randomUtils';
import { ProcessProvider } from "./ChildProcessProvider";
import { FileSystemProvider } from "./fsProvider";
import { OSProvider } from "./LocalOSProvider";
Expand Down Expand Up @@ -100,7 +100,7 @@ export class CommandLineDotNetClient implements DotNetClient {
const dotNetVer = await this.getVersion();
if (semver.gte(dotNetVer, '3.0.0')) {
// The dotnet 3.0 CLI has `dotnet user-secrets init`, let's use that if possible
const userSecretsInitCommand = `dotnet user-secrets init --project "${projectFile}" --id ${this.getRandomHexString(32)}`;
const userSecretsInitCommand = `dotnet user-secrets init --project "${projectFile}" --id ${randomUtils.getRandomHexString(32)}`;
await this.processProvider.exec(userSecretsInitCommand, {});
} else {
// Otherwise try to manually edit the project file by adding a property group immediately after the <Project> tag
Expand All @@ -112,7 +112,7 @@ export class CommandLineDotNetClient implements DotNetClient {
// If found, add the new property group immediately after
const propertyGroup = `
<PropertyGroup>
<UserSecretsId>${this.getRandomHexString(32)}</UserSecretsId>
<UserSecretsId>${randomUtils.getRandomHexString(32)}</UserSecretsId>
</PropertyGroup>`;
const newContents = contents.replace(matches[0], matches[0] + propertyGroup);
await this.fsProvider.writeFile(projectFile, newContents);
Expand All @@ -121,7 +121,7 @@ export class CommandLineDotNetClient implements DotNetClient {
}

private async exportCertificateAndSetPassword(projectFile: string, certificateExportPath: string): Promise<void> {
const password = this.getRandomHexString(32);
const password = randomUtils.getRandomHexString(32);

// Export the certificate
const exportCommand = `dotnet dev-certs https -ep "${certificateExportPath}" -p "${password}"`;
Expand All @@ -131,11 +131,6 @@ export class CommandLineDotNetClient implements DotNetClient {
const userSecretsPasswordCommand = `dotnet user-secrets --project "${projectFile}" set Kestrel:Certificates:Development:Password "${password}"`;
await this.processProvider.exec(userSecretsPasswordCommand, {});
}

private getRandomHexString(length: number): string {
const buffer: Buffer = crypto.randomBytes(Math.ceil(length / 2));
return buffer.toString('hex').slice(0, length);
}
}

export default CommandLineDotNetClient;
13 changes: 13 additions & 0 deletions src/utils/randomUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as crypto from "crypto";

export namespace randomUtils {
export function getRandomHexString(length: number = 10): string {
const buffer: Buffer = crypto.randomBytes(Math.ceil(length / 2));
return buffer.toString('hex').slice(0, length);
}
}

0 comments on commit 6340f1d

Please sign in to comment.