-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for widget injection in connect command (#238)
* auth refactor * CR fix * add missing issuer * add support for script injection in connect. refactor script injection format. added tests * formatting and removed dead code * CR Fixes * CR fixes * fix * small refactor
- Loading branch information
Showing
11 changed files
with
268 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { describe, test, expect } from '@jest/globals' | ||
import { scriptInjectionFromLabels } from './compose-tunnel-agent' | ||
|
||
describe('parse script injection labels', () => { | ||
test('should parse correctly', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.src': 'https://my-script', | ||
'preevy.inject_script.widget.defer': 'true', | ||
'preevy.inject_script.widget.async': 'false', | ||
'preevy.inject_script.widget.path_regex': 't.*t', | ||
} | ||
const scriptInjections = scriptInjectionFromLabels(labels) | ||
expect(scriptInjections).toHaveLength(1) | ||
const [script] = scriptInjections | ||
expect(script).toMatchObject({ | ||
src: 'https://my-script', | ||
defer: true, | ||
async: false, | ||
pathRegex: expect.any(RegExp), | ||
}) | ||
}) | ||
test('should revive regex correctly', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.src': 'https://my-script', | ||
'preevy.inject_script.widget.path_regex': 't.*t', | ||
} | ||
const [script] = scriptInjectionFromLabels(labels) | ||
expect('test').toMatch(script.pathRegex!) | ||
expect('best').not.toMatch(script.pathRegex!) | ||
}) | ||
|
||
test('should ignore scripts with invalid regex', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.src': 'https://my-script', | ||
'preevy.inject_script.widget.path_regex': '[', | ||
} | ||
expect(scriptInjectionFromLabels(labels)).toHaveLength(0) | ||
}) | ||
|
||
test('should drop scripts without src', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.defer': 'true', | ||
} | ||
expect(scriptInjectionFromLabels(labels)).toHaveLength(0) | ||
}) | ||
|
||
test('should support multiple scripts', () => { | ||
const labels = { | ||
'preevy.inject_script.widget.src': 'https://my-script', | ||
'preevy.inject_script.widget.defer': '1', | ||
'preevy.inject_script.widget2.src': 'https://my-script2', | ||
'preevy.inject_script.widget2.defer': 'false', | ||
'preevy.inject_script.widget3.src': 'https://my-script3', | ||
'preevy.inject_script.widget3.defer': '0', | ||
} | ||
const scripts = scriptInjectionFromLabels(labels) | ||
expect(scripts).toHaveLength(3) | ||
expect(scripts).toMatchObject([ | ||
{ | ||
src: 'https://my-script', | ||
defer: true, | ||
}, | ||
{ | ||
src: 'https://my-script2', | ||
defer: false, | ||
}, | ||
{ | ||
src: 'https://my-script3', | ||
defer: false, | ||
}, | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { set, camelCase, snakeCase } from 'lodash' | ||
|
||
export const extractSectionsFromLabels = <T>(prefix: string, labels: Record<string, string>) => { | ||
const sections:{[id:string]: T } = {} | ||
const normalizedPrefix = prefix.endsWith('.') ? prefix : `${prefix}.` | ||
Object.entries(labels) | ||
.filter(([key]) => key.startsWith(normalizedPrefix)) | ||
.map(([key, value]) => [...key.substring(normalizedPrefix.length).split('.'), value]) | ||
.forEach(([id, prop, value]) => set(sections, [id, camelCase(prop)], value)) | ||
return sections | ||
} | ||
|
||
export const parseBooleanLabelValue = (s:string) => s === 'true' || s === '1' | ||
|
||
const formatValueLabel = (x:unknown) => { | ||
if (x instanceof RegExp) { | ||
return x.source | ||
} | ||
return `${x}` | ||
} | ||
|
||
export const sectionToLabels = (prefix: string, section: Record<string, unknown>) => | ||
Object.fromEntries(Object.entries(section).map(([key, value]) => ([`${prefix}.${snakeCase(key)}`, formatValueLabel(value)]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ScriptInjection, COMPOSE_TUNNEL_AGENT_SERVICE_LABELS as PREEVY_LABELS, scriptInjectionFromLabels } from '@preevy/common' | ||
import Docker from 'dockerode' | ||
import { portFilter } from './filters' | ||
import { COMPOSE_PROJECT_LABEL, COMPOSE_SERVICE_LABEL } from './labels' | ||
|
||
export type RunningService = { | ||
project: string | ||
name: string | ||
networks: string[] | ||
ports: number[] | ||
access: 'private' | 'public' | ||
inject: ScriptInjection[] | ||
} | ||
|
||
export const containerToService = ({ | ||
container, | ||
defaultAccess, | ||
}: {container: Docker.ContainerInfo; defaultAccess: 'private' | 'public'}): RunningService => ({ | ||
project: container.Labels[COMPOSE_PROJECT_LABEL], | ||
name: container.Labels[COMPOSE_SERVICE_LABEL], | ||
access: (container.Labels[PREEVY_LABELS.ACCESS] || defaultAccess) as ('private' | 'public'), | ||
networks: Object.keys(container.NetworkSettings.Networks), | ||
// ports may have both IPv6 and IPv4 addresses, ignoring | ||
ports: [...new Set(container.Ports.filter(p => p.Type === 'tcp').filter(portFilter(container)).map(p => p.PrivatePort))], | ||
inject: scriptInjectionFromLabels(container.Labels), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { describe, test, expect } from '@jest/globals' | ||
import { ComposeModel } from './model' | ||
import { scriptInjector } from './script-injection' | ||
|
||
describe('script injection', () => { | ||
test('inject script to all services', async () => { | ||
const model:ComposeModel = { | ||
name: 'my-app', | ||
services: { | ||
frontend1: {}, | ||
frontend2: { | ||
labels: { | ||
other: 'value', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const injector = scriptInjector('test', { src: 'https://mydomain.com/myscript.ts', async: true, pathRegex: /.*/ }) | ||
const newModel = injector.inject(model) | ||
expect(newModel.services?.frontend1?.labels).toMatchObject({ 'preevy.inject_script.test.src': 'https://mydomain.com/myscript.ts', 'preevy.inject_script.test.async': 'true', 'preevy.inject_script.test.path_regex': '.*' }) | ||
expect(newModel.services?.frontend2?.labels).toMatchObject({ other: 'value', 'preevy.inject_script.test.src': 'https://mydomain.com/myscript.ts', 'preevy.inject_script.test.async': 'true', 'preevy.inject_script.test.path_regex': '.*' }) | ||
}) | ||
|
||
test('does not affect original model', async () => { | ||
const model:ComposeModel = { | ||
name: 'my-app', | ||
services: { | ||
frontend1: {}, | ||
frontend2: { | ||
labels: { | ||
other: 'value', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const injector = scriptInjector('test', { src: 'https://mydomain.com/myscript.ts' }) | ||
const newModel = injector.inject(model) | ||
expect(model.services?.frontend1?.labels).toBeUndefined() | ||
expect(model.services?.frontend2?.labels).not | ||
.toMatchObject(newModel.services?.frontend2?.labels as Record<string, unknown>) | ||
}) | ||
}) |
Oops, something went wrong.