Skip to content

Commit

Permalink
Enable Policy Debugging Feature (#118)
Browse files Browse the repository at this point in the history
* debug refactor

* temp

* connect to cloud

* temp

* fix bugs

* connect to cloud update

* temp change

* Use bearer token

* Refactor code

* temp change

* comment code

* UpdateOne

* Change dogfood endpoint

* Fix more issues

* temp changes

* Neat code

* Fix lint error
  • Loading branch information
RupengLiu authored Sep 10, 2020
1 parent 552cd50 commit d6d6304
Show file tree
Hide file tree
Showing 22 changed files with 2,229 additions and 249 deletions.
18 changes: 16 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@
"NODE_DEBUG": "",
"ENABLE_LONG_RUNNING_TESTS": ""
}
}
]
},
{
"type": "node",
"request": "launch",
"name": "Server",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/resources/debug/debugAdapter.js",
"args": [ "--server=4711" ]
},
],
"compounds": [
{
"name": "Extension + Server",
"configurations": [ "Launch Extension (no build)", "Server" ]
}
]
}
532 changes: 308 additions & 224 deletions package-lock.json

Large diffs are not rendered by default.

97 changes: 78 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"onCommand:azureApiManagement.selectSubscriptions",
"onView:azureApiManagementExplorer",
"onLanguage:policy",
"onLanguage:apim-policy",
"onCommand:azureApiManagement.createService",
"onCommand:azureApiManagement.deleteService",
"onCommand:azureApiManagement.copySubscriptionKey",
Expand Down Expand Up @@ -67,10 +68,50 @@
"onCommand:azureApiManagement.importWebAppToApi",
"onCommand:azureApiManagement.copyDockerRunCommand",
"onCommand:azureApiManagement.generateKubernetesDeployment",
"onCommand:azureApiManagement.generateNewGatewayToken"
"onCommand:azureApiManagement.generateNewGatewayToken",
"onCommand:azureApiManagement.debugPolicy"
],
"main": "main",
"contributes": {
"breakpoints": [
{
"language": "apim-policy"
}
],
"languages": [
{
"id": "apim-policy",
"mimetypes": [
"application/vnd.ms-azure-apim.policy.raw+xml"
]
}
],
"debuggers": [
{
"type": "apim-policy",
"label": "Attach to APIM",
"runtime": "node",
"configurationAttributes": {
"launch": {
"properties": {
"stopOnEntry": {
"type": "boolean",
"description": "Automatically stop after launch.",
"default": true
}
}
}
},
"initialConfigurations": [
{
"type": "apim-policy",
"request": "launch",
"name": "Attach to APIM",
"stopOnEntry": true
}
]
}
],
"jsonValidation": [
{
"fileMatch": "/*-api-arm.json",
Expand Down Expand Up @@ -274,6 +315,11 @@
"title": "%azureApiManagement.importWebAppToApi%",
"category": "Azure API Management"
},
{
"command": "azureApiManagement.debugPolicy",
"title": "%azureApiManagement.debugPolicy%",
"category": "Azure API Management"
},
{
"command": "azureApiManagement.copyDockerRunCommand",
"title": "%azureApiManagement.copyDockerRunCommand%",
Expand Down Expand Up @@ -443,6 +489,11 @@
"when": "view == azureApiManagementExplorer && viewItem == azureApiManagementApiOperation",
"group": "1@1"
},
{
"command": "azureApiManagement.debugPolicy",
"when": "view == azureApiManagementExplorer && viewItem == azureApiManagementApiOperation",
"group": "2@1"
},
{
"command": "azureApiManagement.createNamedValue",
"when": "view == azureApiManagementExplorer && viewItem == azureApiManagementNamedValues",
Expand Down Expand Up @@ -542,38 +593,46 @@
"devDependencies": {
"@types/fs-extra": "^4.0.3",
"@types/gulp": "^4.0.6",
"@types/node": "^10.0.0",
"@types/request": "^2.47.0",
"@types/request-promise": "^4.1.43",
"@types/swagger-parser": "^4.0.3",
"@types/mocha": "^5.2.6",
"tslint": "^5.7.0",
"typescript": "^3.3.1",
"@types/node": "^8.10.59",
"@types/request": "^2.48.4",
"@types/request-promise": "^4.1.46",
"@types/request-promise-native": "^1.0.17",
"@types/swagger-parser": "^4.0.3",
"@types/ws": "^6.0.4",
"gulp": "^4.0.0",
"mocha": "^5.2.0",
"mocha-junit-reporter": "^1.23.3",
"mocha-multi-reporters": "^1.1.7",
"ts-node": "^7.0.1",
"tslint": "^5.20.1",
"tslint-microsoft-contrib": "5.0.1",
"vscode-extension-telemetry": "^0.0.18",
"gulp": "^4.0.0",
"vsce": "^1.59.0",
"vscode": "^1.1.33",
"typescript": "^3.8.3",
"vsce": "^1.74.0",
"vscode": "^1.1.36",
"vscode-azureextensiondev": "0.1.8",
"vscode-debugadapter-testsupport": "1.40.2",
"vscode-extension-telemetry": "^0.0.18",
"webpack": "4.29.6",
"webpack-cli": "^3.3.0",
"mocha": "^5.2.0",
"mocha-junit-reporter": "^1.18.0",
"mocha-multi-reporters": "^1.1.7"
"webpack-cli": "^3.3.11"
},
"dependencies": {
"@types/xml": "^1.0.3",
"@types/xml": "^1.0.4",
"await-notify": "1.0.1",
"azure-arm-apimanagement": "^6.0.0",
"azure-arm-resource": "^3.0.0-preview",
"azure-arm-website": "5.7.0",
"fs-extra": "^4.0.2",
"opn": "^5.3.0",
"request": "^2.83.0",
"request-promise": "^4.2.2",
"request": "^2.88.2",
"request-promise": "^4.2.5",
"request-promise-native": "^1.0.8",
"swagger-parser": "^6.0.5",
"vscode-azureextensionui": "^0.23.3",
"vscode-nls": "^4.1.0"
"vscode-debugadapter": "^1.39.1",
"vscode-debugprotocol": "^1.39.0",
"vscode-nls": "^4.1.2",
"ws": "^7.2.3"
},
"extensionDependencies": [
"ms-vscode.azure-account",
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
"azureApiManagement.importWebAppToApi": "Import Azure App Service",
"azureApiManagement.copyDockerRunCommand": "Copy Docker Run Command",
"azureApiManagement.generateKubernetesDeployment": "Generate Kubernetes Deployment File",
"azureApiManagement.generateNewGatewayToken": "Generate New Gateway Token"
"azureApiManagement.generateNewGatewayToken": "Generate New Gateway Token",
"azureApiManagement.debugPolicy": "Start Policy Debugging"
}
5 changes: 5 additions & 0 deletions src/azure/apim/ApimService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export class ApimService {
return res.value;
}

public async getSubscriptionMasterkey(): Promise<string> {
const masterKeyUrl = `${this.baseUrl}/subscriptions/master?api-version=${this.apiVersion}`;
return await requestUtil(masterKeyUrl, this.credentials, 'GET');
}

private genSiteUrl(endPointUrl: string, subscriptionId: string, resourceGroup: string, serviceName: string): string {
return `${endPointUrl}/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.ApiManagement/service/${serviceName}`;
}
Expand Down
12 changes: 12 additions & 0 deletions src/azure/apim/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ export interface IGatewayTokenList {
primary: string;
secondary: string;
}

export interface IMasterSubscription {
id : string;
name : string;
properties: ISubscriptionProperty;
}

export interface ISubscriptionProperty {
displayName: string;
primaryKey: string;
secondaryKey: string;
}
68 changes: 68 additions & 0 deletions src/commands/debugPolicies/debugPolicy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { ApiOperationTreeItem } from '../../explorer/ApiOperationTreeItem';
import { IOperationTreeRoot } from '../../explorer/IOperationTreeRoot';
import { ext } from "../../extensionVariables";
import { nameUtil } from '../../utils/nameUtil';

export async function debugPolicy(node?: ApiOperationTreeItem): Promise<void> {
if (!node) {
node = <ApiOperationTreeItem>await ext.tree.showTreeItemPicker(ApiOperationTreeItem.contextValue);
}

const operationData = await node.getOperationDebugInfo();
const debugConfig: vscode.DebugConfiguration = {
type: "apim-policy",
request: "launch",
name: "Attach to APIM",
stopOnEntry: true,
// tslint:disable-next-line: no-non-null-assertion
gatewayAddress: getDebugGatewayAddressUrl(node!.root.serviceName),
managementAddress: getManagementUrl(node.root),
subscriptionId: node.root.subscriptionId,
operationData: operationData,
fileName: `${nameUtil(node.root)}.http`
};

// const debugConfig3: vscode.DebugConfiguration = {
// type: "apim-policy",
// request: "launch",
// name: "Attach to APIM",
// stopOnEntry: true,
// gatewayAddress: 'wss://proxy.apim.net/debug-0123456789abcdef',
// managementAddress: 'https://management.apim.net/subscriptions/x/resourceGroups/x/providers/microsoft.apimanagement/service/x',
// managementAuth: '',
// operationData: getLocalDebugOperationData2(),
// fileName: `${nameUtil(node.root)}.http`
// };

if (!vscode.debug.activeDebugSession) {
await vscode.debug.startDebugging(undefined, debugConfig);
// await vscode.debug.startDebugging(undefined, debugConfig3);
vscode.debug.onDidTerminateDebugSession(_ => {
const editors = vscode.window.visibleTextEditors;
editors.forEach(editor => {
editor.hide();
});
}, vscode.debug.activeDebugSession);
}
}

function getManagementUrl(root: IOperationTreeRoot): string {
return `${root.environment.resourceManagerEndpointUrl}/subscriptions/${root.subscriptionId}/resourceGroups/${root.resourceGroupName}/providers/microsoft.apimanagement/service/${root.serviceName}`;
}

function getDebugGatewayAddressUrl(serviceName: string): string {
return `wss://${serviceName}.azure-api.net/debug-0123456789abcdef`;
}

// function getLocalDebugOperationData2(): string {
// return `
// GET https://proxy.apim.net/echo/resource
// Ocp-Apim-Subscription-Key:
// Ocp-Apim-Debug:`;
// }
2 changes: 1 addition & 1 deletion src/commands/importWebApp/importWebApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function createXmlPolicyForSwaggerOperations(backendId: string): string {
export async function setAppBackendEntity(node: ApisTreeItem | ApiTreeItem, backendId: string, appName: string, appPath: string, appResourceGroup: string, webAppName: string, BackendCredentials?: BackendCredentialsContract): Promise<void> {
const nbackend: BackendContract = {
description: `${appName}`,
resourceId: getWebAppResourceId(node.root.environment.managementEndpointUrl, node.root.subscriptionId, appResourceGroup, webAppName),
resourceId: getWebAppResourceId(node.root.environment.resourceManagerEndpointUrl, node.root.subscriptionId, appResourceGroup, webAppName),
url: appPath,
id: backendId,
name: backendId,
Expand Down
17 changes: 16 additions & 1 deletion src/commands/testOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@ export async function testOperation(node?: ApiOperationTreeItem): Promise<void>
if (!node) {
node = <ApiOperationTreeItem>await ext.tree.showTreeItemPicker(ApiOperationTreeItem.contextValue);
}
// tslint:disable-next-line: no-non-null-assertion
await createOperationTestFile(node!, OperationRunMode.test);
}

export async function createOperationTestFile(node: ApiOperationTreeItem, mode: OperationRunMode): Promise<vscode.TextEditor> {
// using https://github.com/Huachao/vscode-restclient
const fileName = `${nameUtil(node.root)}.http`;
const localFilePath: string = await createTemporaryFile(fileName);
const data: string = await node.getOperationTestInfo();
let data: string;
if (mode === OperationRunMode.debug) {
data = await node.getOperationDebugInfo();
} else {
data = await node.getOperationTestInfo();
}
const document: vscode.TextDocument = await vscode.workspace.openTextDocument(localFilePath);
const textEditor: vscode.TextEditor = await vscode.window.showTextDocument(document);
await writeToEditor(textEditor, data);
await textEditor.document.save();
return textEditor;
}

export enum OperationRunMode {
debug = "debug",
test = "test"
}
Loading

0 comments on commit d6d6304

Please sign in to comment.