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

Allow self signed certificates for kroki #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@
"type": "string",
"description": "The prefix to add in front of the diagram type name, eg \"kroki-\"",
"default": ""
}
},
"markdown-kroki.allowSelfSignedHost": {
"type": "boolean",
"description": "Set to true when SSL cert for Kroki server is self-signed",
"default": false}
}
},
"markdown.markdownItPlugins": true
Expand Down
21 changes: 15 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import pako from "pako";
import { workspace } from "vscode";
import * as vscode from "vscode";
import fetch from "node-fetch";
import { URL } from "url";
import https from "https";

const config = workspace.getConfiguration("markdown-kroki");

export async function activate() {
let channel = vscode.window.createOutputChannel("markdown-kroki");

let url = config.get<string>("url", "https://kroki.io");
let url = new URL(config.get<string>("url", "https://kroki.io"));
let allowSelfSignedHost = config.get<boolean>("allowSelfSignedHost", false);

let supportedDiagramTypes: string[];

try {
supportedDiagramTypes = await fetchDiagramTypes(url);
supportedDiagramTypes = await fetchDiagramTypes(url, allowSelfSignedHost);
} catch (error) {
channel.appendLine(`Failed to get supported diagram types from ${url}`);
channel.appendLine(error.message);
Expand All @@ -24,9 +27,10 @@ export async function activate() {
vscode.workspace.onDidChangeConfiguration(async (event) => {
if (event.affectsConfiguration("markdown-kroki")) {
// The setting has been changed, update it
url = config.get<string>("url", "https://kroki.io");
url = new URL(config.get<string>("url", "https://kroki.io"));
allowSelfSignedHost = config.get<boolean>("allowSelfSignedHost", false);
try {
supportedDiagramTypes = await fetchDiagramTypes(url);
supportedDiagramTypes = await fetchDiagramTypes(url, allowSelfSignedHost);
} catch (error) {
channel.appendLine(
`Failed to get supported diagram types from ${url}`,
Expand Down Expand Up @@ -75,9 +79,14 @@ export async function activate() {
}

async function fetchDiagramTypes(
url: string,
url: URL,
allowSelfSignedHost: boolean = false
): Promise<string[]> {
const response = await fetch(`${url}/v1/health`);
let sslConfiguredAgent = null;
const options = { rejectUnauthorized: allowSelfSignedHost ? true : false };

sslConfiguredAgent = new https.Agent(options);
const response = await fetch(`${url}/v1/health`, { agent: sslConfiguredAgent })
if (!response.ok) {
throw new Error(`Failed to get supported diagram types from ${url}`);
}
Expand Down