Skip to content

Commit

Permalink
make tls cert location persistent (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev authored Jul 24, 2023
1 parent 499d64b commit ab8a947
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ examples/fresh/deno.lock
tests/e2e/logs
tests/e2e/screenshots
bin
vscode-runme-CHANGELOG.txt
vscode-runme-CHANGELOG.txt
/tls
2 changes: 1 addition & 1 deletion src/extension/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function runCLICommand(
envs['RUNME_SERVER_ADDR'] = server.address()

if (getTLSEnabled()) {
envs['RUNME_TLS_DIR'] = getTLSDir()
envs['RUNME_TLS_DIR'] = getTLSDir(extensionBaseUri)
} else {
args.push('--insecure')
}
Expand Down
10 changes: 7 additions & 3 deletions src/extension/server/runmeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RunmeServer implements Disposable {
readonly onTransportReady = this.#onTransportReady.event

constructor(
extBasePath: Uri,
protected readonly extBasePath: Uri,
options: IServerConfig,
externalServer: boolean,
protected readonly enableRunner = false
Expand Down Expand Up @@ -125,12 +125,16 @@ class RunmeServer implements Disposable {
}
}

protected getTLSDir(): string {
return getTLSDir(this.extBasePath)
}

protected async channelCredentials(): Promise<ChannelCredentials> {
if (!getTLSEnabled()) {
return ChannelCredentials.createInsecure()
}

const { certPEM, privKeyPEM } = await RunmeServer.getTLS(getTLSDir())
const { certPEM, privKeyPEM } = await RunmeServer.getTLS(this.getTLSDir())

return ChannelCredentials.createSsl(certPEM, privKeyPEM, certPEM)
}
Expand Down Expand Up @@ -184,7 +188,7 @@ class RunmeServer implements Disposable {
}

if (getTLSEnabled()) {
args.push('--tls', getTLSDir())
args.push('--tls', this.getTLSDir())
} else {
args.push('--insecure')
}
Expand Down
10 changes: 5 additions & 5 deletions src/utils/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import os from 'node:os'

import { ExtensionContext, NotebookCell, Uri, workspace } from 'vscode'
import { z } from 'zod'
import { v4 as uuidv4 } from 'uuid'

import { getAnnotations, isWindows } from '../extension/utils'
import { SERVER_PORT } from '../constants'
Expand All @@ -17,7 +16,6 @@ const CLI_SECTION_NAME = 'runme.cli'
const APP_SECTION_NAME = 'runme.app'

export const OpenViewInEditorAction = z.enum(['split', 'toggle'])
export const DEFAULT_TLS_DIR = path.join(os.tmpdir(), 'runme', uuidv4(), 'tls')
const DEFAULT_WORKSPACE_FILE_ORDER = ['.env.local', '.env']
const DEFAULT_RUNME_APP_API_URL = 'https://api.runme.dev'
const DEFAULT_RUNME_BASE_DOMAIN = 'runme.dev'
Expand All @@ -38,7 +36,7 @@ const configurationSchema = {
binaryPath: z.string().optional(),
enableLogger: z.boolean().default(false),
enableTLS: z.boolean().default(true),
tlsDir: z.string().nonempty().default(DEFAULT_TLS_DIR),
tlsDir: z.string().optional(),
},
notebookTerminal: {
backgroundTask: z.boolean().default(true),
Expand Down Expand Up @@ -173,8 +171,10 @@ const getTLSEnabled = (): boolean => {
return getServerConfigurationValue('enableTLS', true)
}

const getTLSDir = (): string => {
return getServerConfigurationValue('tlsDir', DEFAULT_TLS_DIR)
const getTLSDir = (extensionsDir: Uri): string => {
return (
getServerConfigurationValue('tlsDir', undefined) || Uri.joinPath(extensionsDir, 'tls').fsPath
)
}

const getBinaryPath = (extensionBaseUri: Uri, platform: string): Uri => {
Expand Down
5 changes: 2 additions & 3 deletions tests/extension/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
getBinaryPath,
getServerConfigurationValue,
getTLSDir,
DEFAULT_TLS_DIR,
getNotebookTerminalFontFamily,
getNotebookTerminalFontSize,
getCodeLensEnabled,
Expand Down Expand Up @@ -124,12 +123,12 @@ suite('Configuration', () => {

test('should get default TLS dir by default', () => {
SETTINGS_MOCK.tlsDir = undefined
expect(getTLSDir()).toBe(DEFAULT_TLS_DIR)
expect(getTLSDir(Uri.file('/ext/base'))).toBe(Uri.file('/ext/base/tls').fsPath)
})

test('should get set TLS dir if set', () => {
SETTINGS_MOCK.tlsDir = '/tmp/runme/tls'
expect(getTLSDir()).toBe('/tmp/runme/tls')
expect(getTLSDir(Uri.file('/ext/base'))).toBe('/tmp/runme/tls')
})

test('getServerConfigurationValue Should default to undefined binaryPath', () => {
Expand Down

0 comments on commit ab8a947

Please sign in to comment.