-
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 script injection in connect.
refactor script injection format. added tests
- Loading branch information
Showing
10 changed files
with
340 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* 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.widget2.src': 'https://my-script2', | ||
'preevy.inject_script.widget3.src': 'https://my-script3', | ||
} | ||
const scripts = scriptInjectionFromLabels(labels) | ||
expect(scripts).toHaveLength(3) | ||
expect(scripts).toMatchObject([ | ||
{ | ||
src: 'https://my-script', | ||
}, | ||
{ | ||
src: 'https://my-script2', | ||
}, | ||
{ | ||
src: 'https://my-script3', | ||
}, | ||
]) | ||
}) | ||
}) |
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,68 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { describe, test, expect } from '@jest/globals' | ||
import { scriptInjectionFromLabels } from './services' | ||
|
||
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.widget2.src': 'https://my-script2', | ||
'preevy.inject_script.widget3.src': 'https://my-script3', | ||
} | ||
const scripts = scriptInjectionFromLabels(labels) | ||
expect(scripts).toHaveLength(3) | ||
expect(scripts).toMatchObject([ | ||
{ | ||
src: 'https://my-script', | ||
}, | ||
{ | ||
src: 'https://my-script2', | ||
}, | ||
{ | ||
src: 'https://my-script3', | ||
}, | ||
]) | ||
}) | ||
}) |
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
Oops, something went wrong.