refactor: change KasmVNC config from JSON map to YAML string with improved config merging
This commit is contained in:
parent
bb634a2b5b
commit
e753134bff
@ -55,9 +55,9 @@ variable "subdomain" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
variable "kasm_config" {
|
variable "kasm_config" {
|
||||||
type = map(any)
|
type = string
|
||||||
default = {}
|
default = ""
|
||||||
description = "Additional KasmVNC configuration options. Can be used to set DLP policies and other advanced settings. See https://kasmweb.com/docs/develop/how_to/kasmvnc_dlp_policies.html for details."
|
description = "Additional KasmVNC configuration in YAML format. Can be used to set DLP policies and other advanced settings. See https://kasmweb.com/docs/develop/how_to/kasmvnc_dlp_policies.html for details."
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "coder_script" "kasm_vnc" {
|
resource "coder_script" "kasm_vnc" {
|
||||||
@ -71,7 +71,7 @@ resource "coder_script" "kasm_vnc" {
|
|||||||
KASM_VERSION = var.kasm_version
|
KASM_VERSION = var.kasm_version
|
||||||
SUBDOMAIN = tostring(var.subdomain)
|
SUBDOMAIN = tostring(var.subdomain)
|
||||||
PATH_VNC_HTML = var.subdomain ? "" : file("${path.module}/path_vnc.html")
|
PATH_VNC_HTML = var.subdomain ? "" : file("${path.module}/path_vnc.html")
|
||||||
KASM_CONFIG = jsonencode(var.kasm_config)
|
KASM_CONFIG = var.kasm_config
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -193,21 +193,35 @@ else
|
|||||||
SUDO=""
|
SUDO=""
|
||||||
|
|
||||||
echo "WARNING: Sudo access not available, using user config dir!"
|
echo "WARNING: Sudo access not available, using user config dir!"
|
||||||
|
|
||||||
|
# Always ensure the directory exists
|
||||||
|
mkdir -p "$HOME/.vnc"
|
||||||
|
|
||||||
|
# We'll handle existing configs differently - we'll merge instead of skipping
|
||||||
if [[ -f "$kasm_config_file" ]]; then
|
if [[ -f "$kasm_config_file" ]]; then
|
||||||
echo "WARNING: Custom user KasmVNC config exists, not overwriting!"
|
echo "INFO: Custom user KasmVNC config exists, will merge with new settings."
|
||||||
echo "WARNING: Ensure that you manually configure the appropriate settings."
|
# Create a backup of the existing config
|
||||||
kasm_config_file="/dev/stderr"
|
cp "$kasm_config_file" "${kasm_config_file}.bak"
|
||||||
else
|
|
||||||
echo "WARNING: This may prevent custom user KasmVNC settings from applying!"
|
|
||||||
mkdir -p "$HOME/.vnc"
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Writing KasmVNC config to $kasm_config_file"
|
echo "Writing KasmVNC config to $kasm_config_file"
|
||||||
|
|
||||||
# Create base config
|
# Create a temporary file for our config
|
||||||
$SUDO tee "$kasm_config_file" > /dev/null << EOF
|
TEMP_CONFIG_FILE=$(mktemp)
|
||||||
|
|
||||||
|
# Check if existing config file exists and preserve its content
|
||||||
|
if [[ -f "$kasm_config_file" ]]; then
|
||||||
|
echo "Preserving existing KasmVNC configuration settings."
|
||||||
|
cp "$kasm_config_file" "$TEMP_CONFIG_FILE"
|
||||||
|
|
||||||
|
# Update only the network section
|
||||||
|
if grep -q "^network:" "$TEMP_CONFIG_FILE"; then
|
||||||
|
# Network section exists, update only the websocket_port
|
||||||
|
sed -i "s/\([ \t]*websocket_port:\).*/\1 ${PORT}/" "$TEMP_CONFIG_FILE"
|
||||||
|
else
|
||||||
|
# Network section doesn't exist, add it
|
||||||
|
cat >> "$TEMP_CONFIG_FILE" << EOF
|
||||||
network:
|
network:
|
||||||
protocol: http
|
protocol: http
|
||||||
interface: 127.0.0.1
|
interface: 127.0.0.1
|
||||||
@ -219,33 +233,46 @@ network:
|
|||||||
udp:
|
udp:
|
||||||
public_ip: 127.0.0.1
|
public_ip: 127.0.0.1
|
||||||
EOF
|
EOF
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# Start with base network configuration for new config
|
||||||
|
cat > "$TEMP_CONFIG_FILE" << EOF
|
||||||
|
network:
|
||||||
|
protocol: http
|
||||||
|
interface: 127.0.0.1
|
||||||
|
websocket_port: ${PORT}
|
||||||
|
ssl:
|
||||||
|
require_ssl: false
|
||||||
|
pem_certificate:
|
||||||
|
pem_key:
|
||||||
|
udp:
|
||||||
|
public_ip: 127.0.0.1
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
# Add additional KasmVNC configuration if provided
|
# Add additional KasmVNC configuration if provided
|
||||||
if [[ -n "${KASM_CONFIG}" && "${KASM_CONFIG}" != "{}" ]]; then
|
if [[ -n "${KASM_CONFIG}" ]]; then
|
||||||
# Check if jq is available
|
echo "Adding custom KasmVNC configuration."
|
||||||
if ! command -v jq &> /dev/null; then
|
|
||||||
echo "WARNING: jq is not installed. Cannot parse additional KasmVNC configuration."
|
# Add a comment to mark the start of custom config
|
||||||
echo "WARNING: Install jq or provide configuration in the correct format."
|
echo "" >> "$TEMP_CONFIG_FILE"
|
||||||
else
|
echo "# ---- START CUSTOM KASMVNC CONFIG ----" >> "$TEMP_CONFIG_FILE"
|
||||||
# Create a temporary file for the additional config
|
echo "" >> "$TEMP_CONFIG_FILE"
|
||||||
TEMP_CONFIG_FILE=$(mktemp)
|
|
||||||
|
# Directly append the YAML configuration
|
||||||
# Parse the JSON and convert to YAML format
|
echo "${KASM_CONFIG}" >> "$TEMP_CONFIG_FILE"
|
||||||
echo '${KASM_CONFIG}' | jq -r 'to_entries | .[] |
|
|
||||||
if .value | type == "object" then
|
# Add a comment to mark the end of custom config
|
||||||
.key + ":\n" + (.value | to_entries | map(" " + .key + ": " + (.value | tostring)) | join("\n"))
|
echo "" >> "$TEMP_CONFIG_FILE"
|
||||||
else
|
echo "# ---- END CUSTOM KASMVNC CONFIG ----" >> "$TEMP_CONFIG_FILE"
|
||||||
.key + ": " + (.value | tostring)
|
|
||||||
end' > "$TEMP_CONFIG_FILE"
|
|
||||||
|
|
||||||
# Append the additional config to the main config file
|
|
||||||
$SUDO tee -a "$kasm_config_file" > /dev/null < "$TEMP_CONFIG_FILE"
|
|
||||||
|
|
||||||
# Clean up
|
|
||||||
rm "$TEMP_CONFIG_FILE"
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Apply the configuration
|
||||||
|
$SUDO cp "$TEMP_CONFIG_FILE" "$kasm_config_file"
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
rm "$TEMP_CONFIG_FILE"
|
||||||
|
|
||||||
# This password is not used since we start the server without auth.
|
# This password is not used since we start the server without auth.
|
||||||
# The server is protected via the Coder session token / tunnel
|
# The server is protected via the Coder session token / tunnel
|
||||||
# and does not listen publicly
|
# and does not listen publicly
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user