Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: instance ssh form improvement (#5915)

Liu Ji2f94a53c08ec
diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vue
index 700be8ade0..ce4fef2660 100644
--- a/frontend/src/components/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm.vue
@@ -473,76 +473,10 @@
{{ $t("data-source.ssh-connection") }}
</label>
</div>
- <div class="mt-2 sm:col-span-1 sm:col-start-1">
- <label for="sshHost" class="textlabel block">
- {{ $t("data-source.ssh.host") }}
- </label>
- <input
- id="sshHost"
- name="sshHost"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="''"
- :value="currentDataSource.options.sshHost"
- @input="handleCurrentDataSourceSshHostInput"
- />
- </div>
- <div class="mt-2 sm:col-span-1 sm:col-start-1">
- <label for="sshPort" class="textlabel block">
- {{ $t("data-source.ssh.port") }}
- </label>
- <input
- id="sshPort"
- name="sshPort"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="''"
- :value="currentDataSource.options.sshPort"
- @input="handleCurrentDataSourceSshPortInput"
- />
- </div>
- <div class="mt-2 sm:col-span-1 sm:col-start-1">
- <label for="sshUser" class="textlabel block">
- {{ $t("data-source.ssh.user") }}
- </label>
- <input
- id="sshUser"
- name="sshUser"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="''"
- :value="currentDataSource.options.sshUser"
- @input="handleCurrentDataSourceSshUserInput"
- />
- </div>
- <div class="mt-2 sm:col-span-1 sm:col-start-1">
- <label for="sshPassword" class="textlabel block">
- {{ $t("data-source.ssh.password") }}
- </label>
- <input
- id="sshPassword"
- name="sshPassword"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="''"
- :value="currentDataSource.options.sshPassword"
- @input="handleCurrentDataSourceSshPasswordInput"
- />
- </div>
- <div class="mt-2 sm:col-span-1 sm:col-start-1">
- <label for="sshPrivateKey" class="textlabel block">
- {{ $t("data-source.ssh.ssh-key") }}
- </label>
- <input
- id="sshPrivateKey"
- name="sshPrivateKey"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="''"
- :value="currentDataSource.options.sshPrivateKey"
- @input="handleCurrentDataSourceSshPrivateKeyInput"
- />
- </div>
+ <SshConnectionForm
+ :value="currentDataSource.options"
+ @change="handleCurrentDataSourceSshChange"
+ />
</div>
</div>
@@ -663,6 +597,7 @@ import {
InstanceCreate,
unknown,
ValidatedMessage,
+ DataSourceOptions,
} from "../types";
import isEmpty from "lodash-es/isEmpty";
import { useI18n } from "vue-i18n";
@@ -684,6 +619,7 @@ import {
SpannerHostInput,
SpannerCredentialInput,
SslCertificateForm,
+ SshConnectionForm,
OracleSIDAndServiceNameInput,
} from "./InstanceForm";
import { useInstanceV1Store } from "@/store/modules/v1/instance";
@@ -1171,29 +1107,16 @@ const handleCurrentDataSourceSslChange = (
currentDataSource.value.updateSsl = true;
};
-const handleCurrentDataSourceSshHostInput = (event: Event) => {
- const str = (event.target as HTMLInputElement).value.trim();
- currentDataSource.value.options.sshHost = str;
-};
-
-const handleCurrentDataSourceSshPortInput = (event: Event) => {
- const str = (event.target as HTMLInputElement).value.trim();
- currentDataSource.value.options.sshPort = str;
-};
-
-const handleCurrentDataSourceSshUserInput = (event: Event) => {
- const str = (event.target as HTMLInputElement).value.trim();
- currentDataSource.value.options.sshUser = str;
-};
-
-const handleCurrentDataSourceSshPasswordInput = (event: Event) => {
- const str = (event.target as HTMLInputElement).value;
- currentDataSource.value.options.sshPassword = str;
-};
-
-const handleCurrentDataSourceSshPrivateKeyInput = (event: Event) => {
- const str = (event.target as HTMLInputElement).value.trim();
- currentDataSource.value.options.sshPrivateKey = str;
+const handleCurrentDataSourceSshChange = (
+ value: Partial<
+ Pick<
+ DataSourceOptions,
+ "sshHost" | "sshPort" | "sshUser" | "sshPassword" | "sshPrivateKey"
+ >
+ >
+) => {
+ Object.assign(currentDataSource.value.options, value);
+ currentDataSource.value.updateSsh = true;
};
const handleCreateRODataSource = () => {
diff --git a/frontend/src/components/InstanceForm/SshConnectionForm.vue b/frontend/src/components/InstanceForm/SshConnectionForm.vue
new file mode 100644
index 0000000000..93551e7348
--- /dev/null
+++ b/frontend/src/components/InstanceForm/SshConnectionForm.vue
@@ -0,0 +1,199 @@
+<template>
+ <div class="radio-set-row mt-2">
+ <label v-for="type in SshTypes" :key="type" class="radio">
+ <input v-model="state.type" type="radio" class="btn" :value="type" />
+ <span class="label">
+ {{ getSshTypeLabel(type) }}
+ </span>
+ </label>
+ </div>
+
+ <template v-if="state.type === 'TUNNEL' || state.type === 'TUNNEL+PK'">
+ <div
+ class="sm:col-span-1 sm:col-start-1 mt-4 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-4"
+ >
+ <div class="sm:col-span-3 sm:col-start-1">
+ <label for="sshHost" class="textlabel block">
+ {{ $t("data-source.ssh.host") }}
+ </label>
+ <input
+ id="sshHost"
+ v-model="state.value.sshHost"
+ name="sshHost"
+ type="text"
+ class="textfield mt-1 w-full"
+ :placeholder="''"
+ />
+ </div>
+
+ <div class="sm:col-span-1">
+ <label for="sshPort" class="textlabel block">
+ {{ $t("data-source.ssh.port") }}
+ </label>
+ <input
+ id="sshPort"
+ v-model="state.value.sshPort"
+ name="sshPort"
+ type="text"
+ class="textfield mt-1 w-full"
+ :placeholder="''"
+ />
+ </div>
+ </div>
+
+ <div
+ class="mt-2 grid grid-cols-1 gap-y-2 gap-x-4 border-none sm:grid-cols-3"
+ >
+ <div class="mt-2 sm:col-span-1 sm:col-start-1">
+ <label for="sshUser" class="textlabel block">
+ {{ $t("data-source.ssh.user") }}
+ </label>
+ <input
+ id="sshUser"
+ v-model="state.value.sshUser"
+ name="sshUser"
+ type="text"
+ class="textfield mt-1 w-full"
+ :placeholder="''"
+ />
+ </div>
+ <div class="mt-2 sm:col-span-1 sm:col-start-1">
+ <label for="sshPassword" class="textlabel block">
+ {{ $t("data-source.ssh.password") }}
+ </label>
+ <input
+ id="sshPassword"
+ v-model="state.value.sshPassword"
+ name="sshPassword"
+ type="text"
+ class="textfield mt-1 w-full"
+ :placeholder="$t('instance.password-write-only')"
+ />
+ </div>
+ </div>
+ <div
+ v-if="state.type === 'TUNNEL+PK'"
+ class="mt-4 sm:col-span-3 sm:col-start-1"
+ >
+ <div class="mt-2 sm:col-span-1 sm:col-start-1">
+ <label for="sshPrivateKey" class="textlabel block">
+ {{ $t("data-source.ssh.ssh-key") }}
+ </label>
+ <DroppableTextarea
+ v-model:value="state.value.sshPrivateKey"
+ :rounded="true"
+ class="mt-2 block w-full resize-none whitespace-pre-wrap h-24"
+ />
+ </div>
+ </div>
+ </template>
+</template>
+
+<script lang="ts" setup>
+import { PropType, reactive, watch } from "vue";
+import { useI18n } from "vue-i18n";
+import { cloneDeep } from "lodash-es";
+
+import DroppableTextarea from "../misc/DroppableTextarea.vue";
+
+const SshTypes = ["NONE", "TUNNEL", "TUNNEL+PK"] as const;
+
+type SshType = "NONE" | "TUNNEL" | "TUNNEL+PK";
+
+type WithSshOptions = {
+ sshHost?: string;
+ sshPort?: string;
+ sshUser?: string;
+ sshPassword?: string;
+ sshPrivateKey?: string;
+};
+
+type LocalState = {
+ type: SshType;
+ value: WithSshOptions;
+ tab: "TUNNEL" | "TUNNEL+PK";
+};
+
+const props = defineProps({
+ value: {
+ type: Object as PropType<WithSshOptions>,
+ required: true,
+ },
+});
+
+const emit = defineEmits<{
+ (e: "change", value: WithSshOptions): void;
+}>();
+
+const { t } = useI18n();
+
+const state = reactive<LocalState>({
+ type: guessSshType(props.value),
+ value: {
+ sshHost: props.value.sshHost,
+ sshPort: props.value.sshPort,
+ sshUser: props.value.sshUser,
+ sshPassword: props.value.sshPassword,
+ sshPrivateKey: props.value.sshPrivateKey,
+ },
+ tab: "TUNNEL",
+});
+
+// Sync the latest version to local state when props.value changed.
+watch(
+ () => props.value,
+ (newValue) => {
+ state.type = guessSshType(newValue);
+ state.value = {
+ sshHost: props.value.sshHost,
+ sshPort: props.value.sshPort,
+ sshUser: props.value.sshUser,
+ sshPassword: props.value.sshPassword,
+ sshPrivateKey: props.value.sshPrivateKey,
+ };
+ }
+);
+
+// Emit the latest lo the parent when local value has been edited.
+watch(
+ () => state.value,
+ (localValue) => {
+ emit("change", cloneDeep(localValue));
+ },
+ { deep: true }
+);
+
+watch(
+ () => state.type,
+ (type) => {
+ if (type === "NONE") {
+ state.value.sshHost = "";
+ state.value.sshPort = "";
+ state.value.sshUser = "";
+ state.value.sshPassword = "";
+ state.value.sshPrivateKey = "";
+ } else if (type === "TUNNEL") {
+ state.value.sshPrivateKey = "";
+ state.tab = "TUNNEL";
+ }
+ }
+);
+
+function getSshTypeLabel(type: SshType): string {
+ if (type === "TUNNEL") {
+ return t("data-source.ssh-type.tunnel");
+ }
+ if (type === "TUNNEL+PK") {
+ return t("data-source.ssh-type.tunnel-and-private-key");
+ }
+ return t("data-source.ssh-type.none");
+}
+
+function guessSshType(value: WithSshOptions): SshType {
+ if (value.sshPort) {
+ if (value.sshPrivateKey) return "TUNNEL+PK";
+ return "TUNNEL";
+ }
+ return "NONE";
+}
+</script>
diff --git a/frontend/src/components/InstanceForm/index.ts b/frontend/src/components/InstanceForm/index.ts
index 1bc7fd6358..2b8b3d4499 100644
--- a/frontend/src/components/InstanceForm/index.ts
+++ b/frontend/src/components/InstanceForm/index.ts
@@ -1,10 +1,12 @@
import SslCertificateForm from "./SslCertificateForm.vue";
+import SshConnectionForm from "./SshConnectionForm.vue";
import SpannerHostInput from "./SpannerHostInput.vue";
import SpannerCredentialInput from "./SpannerCredentialInput.vue";
import OracleSIDAndServiceNameInput from "./OracleSIDAndServiceNameInput.vue";
export {
SslCertificateForm,
+ SshConnectionForm,
SpannerHostInput,
SpannerCredentialInput,
OracleSIDAndServiceNameInput,
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index be16ee00b9..91267ef4d9 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1582,12 +1582,19 @@
"client-key": "Client Key",
"client-cert": "Client Certificate"
},
+ "ssh-type": {
+ "tunnel": "@:{'data-source.ssh.tunnel'}",
+ "tunnel-and-private-key": "@:{'data-source.ssh.tunnel'} + @:{'data-source.ssh.private-key'}",
+ "none": "None"
+ },
"ssh": {
"host": "Server",
"port": "Port",
"user": "User",
"password": "Password",
- "ssh-key": "SSH Key"
+ "ssh-key": "SSH Key",
+ "tunnel": "Tunnel",
+ "private-key": "Private Key"
},
"ssl-connection": "SSL Connection",
"ssh-connection": "SSH Connection",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 1bdc0797ed..e61311b4a0 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1579,12 +1579,19 @@
"client-key": "客户端密钥",
"client-cert": "客户端证书"
},
+ "ssh-type": {
+ "tunnel": "@:{'data-source.ssh.tunnel'}",
+ "tunnel-and-private-key": "@:{'data-source.ssh.tunnel'} + @:{'data-source.ssh.private-key'}",
+ "none": "不使用"
+ },
"ssh": {
"host": "服务器",
"port": "端口",
"user": "用户名",
"password": "密码",
- "ssh-key": "SSH 密钥"
+ "ssh-key": "SSH 密钥",
+ "tunnel": "隧道",
+ "private-key": "私钥"
},
"ssl-connection": "SSL 连接",
"ssh-connection": "SSH 连接",