Skip to content

Commit

Permalink
add proxy support for profiles!
Browse files Browse the repository at this point in the history
pass proxy as ssh://<auth> instead of separate --sshProxyLogin flag
  • Loading branch information
ikreymer committed Jul 31, 2024
1 parent f0a3d11 commit e893f89
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 4 deletions.
2 changes: 2 additions & 0 deletions backend/btrixcloud/crawlmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async def run_profile_browser(
crawler_image: str,
baseprofile: str = "",
profile_filename: str = "",
proxy_id: str = "",
) -> str:
"""run browser for profile creation"""

Expand All @@ -55,6 +56,7 @@ async def run_profile_browser(
"vnc_password": secrets.token_hex(16),
"expire_time": to_k8s_date(dt_now() + timedelta(seconds=30)),
"crawler_image": crawler_image,
"proxy_id": proxy_id,
}

data = self.templates.env.get_template("profile_job.yaml").render(params)
Expand Down
6 changes: 6 additions & 0 deletions backend/btrixcloud/operator/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ async def sync_profile_browsers(self, data: MCSyncData):
params["profile_filename"] = spec.get("profileFilename", "")
params["crawler_image"] = spec["crawlerImage"]

proxy_id = spec.get("proxyId")
params["proxy_id"] = proxy_id
if proxy_id:
proxy = self.crawl_config_ops.get_crawler_proxy(proxy_id)
params["ssh_proxy_auth"] = proxy.auth if proxy else ""

params["url"] = spec.get("startUrl", "about:blank")
params["vnc_password"] = spec.get("vncPassword")

Expand Down
1 change: 1 addition & 0 deletions backend/btrixcloud/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async def create_new_browser(
crawler_image=crawler_image,
baseprofile=prev_profile_id,
profile_filename=prev_profile_path,
proxy_id=profile_launch.proxyId or "",
)

if not browserid:
Expand Down
4 changes: 2 additions & 2 deletions chart/app-templates/crawler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ spec:
- "@{{ profile_filename }}"
{% endif %}
{% if proxy_id %}
- --sshProxyLogin
- "{{ ssh_proxy_auth }}"
- --proxyServer
- "ssh://{{ ssh_proxy_auth }}"
- --sshProxyPrivateKeyFile
- /tmp/ssh-proxy/private-key
- --sshProxyKnownHostsFile
Expand Down
2 changes: 2 additions & 0 deletions chart/app-templates/profile_job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ spec:
profileFilename: "{{ profile_filename }}"
vncPassword: "{{ vnc_password }}"

proxyId: "{{ proxy_id }}"

{% if expire_time %}
expireTime: "{{ expire_time }}"
{% endif %}
33 changes: 33 additions & 0 deletions chart/app-templates/profilebrowser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ spec:
emptyDir:
sizeLimit: {{ profile_browser_workdir_size }}

{% if proxy_id %}
- name: proxy-ssh-keys
secret:
secretName: proxy-ssh-keys
defaultMode: 0600
{% endif %}

{% if priorityClassName %}
priorityClassName: {{ priorityClassName }}
{% endif %}
Expand Down Expand Up @@ -73,10 +80,36 @@ spec:
- --profile
- "@{{ profile_filename }}"
{%- endif %}
{% if proxy_id %}
- --proxyServer
- "ssh://{{ ssh_proxy_auth }}"
- --sshProxyPrivateKeyFile
- /tmp/ssh-proxy/private-key
- --sshProxyKnownHostsFile
- /tmp/ssh-proxy/known-hosts
{% endif %}

volumeMounts:
- name: crawler-workdir
mountPath: /tmp/home
{% if proxy_id %}
- name: proxy-ssh-keys
mountPath: /tmp/ssh-proxy/private-key
subPath: {{ proxy_id }}-private-key
readOnly: true
- name: proxy-ssh-keys
mountPath: /tmp/ssh-proxy/known-hosts
subPath: {{ proxy_id }}-known-hosts
readOnly: true
- name: proxy-ssh-keys
mountPath: /etc/passwd
subPath: passwd
readOnly: true
- name: proxy-ssh-keys
mountPath: /etc/group
subPath: group
readOnly: true
{% endif %}

envFrom:
- secretRef:
Expand Down
2 changes: 1 addition & 1 deletion chart/templates/proxies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ stringData:
{{ .id }}-private-key: |
{{ .private_key | indent 4 }}
{{ .id }}-known-hosts: |
{{ .host_public_key | nindent 4 }}
{{ .host_public_key | indent 4 }}
{{- end }}

# slightly hacky: override /etc/passwd and /etc/group in crawler to be able to ssh to proxies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
import queryString from "query-string";

import type { Dialog } from "@/components/ui/dialog";
import { type SelectCrawlerChangeEvent } from "@/components/ui/select-crawler";
import type { SelectCrawlerChangeEvent } from "@/components/ui/select-crawler";
import type { SelectCrawlerProxyChangeEvent } from "@/components/ui/select-crawler-proxy";
import type { AuthState } from "@/utils/AuthService";
import LiteElement, { html } from "@/utils/LiteElement";

Expand All @@ -32,6 +33,9 @@ export class NewBrowserProfileDialog extends LiteElement {
@state()
private crawlerChannel = "default";

@state()
private proxyId: string | null = null;

@query("btrix-dialog")
private readonly dialog?: Dialog;

Expand Down Expand Up @@ -88,6 +92,15 @@ export class NewBrowserProfileDialog extends LiteElement {
(this.crawlerChannel = e.detail.value!)}
></btrix-select-crawler>
</div>
<div class="mt-4">
<btrix-select-crawler-proxy
orgId=${this.orgId}
.proxyId="${this.proxyId || ""}"
.authState=${this.authState}
@on-change=${(e: SelectCrawlerProxyChangeEvent) =>
(this.proxyId = e.detail.value!)}
></btrix-select-crawler-proxy>
</div>
<input class="invisible size-0" type="submit" />
</form>
<div slot="footer" class="flex justify-between">
Expand Down Expand Up @@ -135,6 +148,7 @@ export class NewBrowserProfileDialog extends LiteElement {
const data = await this.createBrowser({
url: url,
crawlerChannel: this.crawlerChannel,
proxyId: this.proxyId,
});

this.notify({
Expand All @@ -150,6 +164,7 @@ export class NewBrowserProfileDialog extends LiteElement {
url,
name: msg("My Profile"),
crawlerChannel: this.crawlerChannel,
proxyId: this.proxyId,
})}`,
);
} catch (e) {
Expand All @@ -165,13 +180,16 @@ export class NewBrowserProfileDialog extends LiteElement {
private async createBrowser({
url,
crawlerChannel,
proxyId,
}: {
url: string;
crawlerChannel: string;
proxyId: string | null;
}) {
const params = {
url,
crawlerChannel,
proxyId,
};

return this.apiFetch<{ browserid: string }>(
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/pages/org/browser-profiles-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ export class BrowserProfilesNew extends TailwindElement {
crawlerChannel?: string;
profileId?: string | null;
navigateUrl?: string;
proxyId: string | null;
} = {
name: "",
url: "",
proxyId: null,
};

private readonly api = new APIController(this);
Expand Down Expand Up @@ -288,9 +290,11 @@ export class BrowserProfilesNew extends TailwindElement {
}

const crawlerChannel = this.browserParams.crawlerChannel || "default";
const proxyId = this.browserParams.proxyId;
const data = await this.createBrowser({
url,
crawlerChannel,
proxyId,
});

this.nav.to(
Expand All @@ -300,6 +304,7 @@ export class BrowserProfilesNew extends TailwindElement {
url,
name: this.browserParams.name || msg("My Profile"),
crawlerChannel,
proxyId,
})}`,
);
}
Expand All @@ -314,6 +319,7 @@ export class BrowserProfilesNew extends TailwindElement {
name: formData.get("name"),
description: formData.get("description"),
crawlerChannel: this.browserParams.crawlerChannel,
proxyId: this.browserParams.proxyId,
};

try {
Expand Down Expand Up @@ -362,13 +368,16 @@ export class BrowserProfilesNew extends TailwindElement {
private async createBrowser({
url,
crawlerChannel,
proxyId,
}: {
url: string;
crawlerChannel: string;
proxyId: string | null;
}) {
const params = {
url,
crawlerChannel,
proxyId,
};

return this.api.fetch<{ browserid: string }>(
Expand Down

0 comments on commit e893f89

Please sign in to comment.