Skip to content

Commit

Permalink
fix password auto-login issue
Browse files Browse the repository at this point in the history
  • Loading branch information
DininduSenanayake committed Aug 7, 2024
1 parent 69396b8 commit 68aa6ee
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 55 deletions.
2 changes: 1 addition & 1 deletion manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ category: Interactive Apps
subcategory: Servers
role: batch_connect
description: |
This app will launch a code-server
This app will launch a code-server
2 changes: 1 addition & 1 deletion submit.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ script:
native:
container:
name: "codeserver"
image: "ghcr.io/nesi/training-environment-codeserver-app:v0.1.23"
image: "ghcr.io/nesi/training-environment-codeserver-app:v0.2.0"
command: ["/bin/bash","-l","<%= staged_root %>/job_script_content.sh"]
restart_policy: 'OnFailure'
env:
Expand Down
15 changes: 6 additions & 9 deletions template/before.sh.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ exec &> >(tee -a "pod.log")
# Export the module function if it exists
[[ $(type -t module) == "function" ]] && export -f module

# Find available port to run server on
export port=$(find_port ${host})

# Export compute node the script is running on
export host="${host}"
source /bin/find_host_port
source /bin/save_passwd_as_secret
export host="$HOST_CFG"
export port="$PORT_CFG"

echo "host is: ${host}"
echo "port is: ${port}"

# Generate SHA1 encrypted password (requires OpenSSL installed)
#commenting this out to check whether this is the cause of the $PASSWORD variable not getting invoked correctly
export password="$(create_passwd 16)"

password=$PASSWORD
export password PASSWORD]
38 changes: 19 additions & 19 deletions template/script.sh.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#!/usr/bin/bash -l
<%
#!/usr/bin/env bash

# Set our working directory.
working_dir = Pathname.new(context.working_dir)
export WORKING_DIR="<%= session.staged_root %>"


# Ensure that code-server always starts up in either a user defined directory or the home directory.
if ! working_dir.exist?
working_dir = Pathname.new(ENV['HOME'])
elsif working_dir.file?
working_dir = working_dir.parent
end
%>
if [ ! -e "${WORKING_DIR}" ]; then
${WORKING_DIR}="$HOME"
elif [ -f "${WORKING_DIR}" ]; then
${WORKING_DIR}="$(dirname "${WORKING_DIR}")"
fi

CODE_SERVER_DATAROOT="$HOME/.local/share/code-server"
mkdir -p "$CODE_SERVER_DATAROOT/extensions"

# Expose the password to the server.
export PASSWORD="$password"

# Print compute node.
echo "$(date): Running on compute node ${compute_node}:$port"
echo "$(date): Running on compute node ${host}:$port"


CPP_FILE="<%= working_dir.to_s %>/.vscode/c_cpp_properties.json"
#Probably not needed but leaving as it is for the moment as per upstream record.
CPP_FILE="${WORKING_DIR}/.vscode/c_cpp_properties.json"

if [[ -f "$CPP_FILE" ]]; then
CPP_DIR="${TMPDIR:=/tmp/$USER}/cpp-vscode"
Expand All @@ -44,11 +43,12 @@ if [[ -f "$CPP_FILE" ]]; then
echo "$(date): Started code-server"
echo ""

/usr/bin/code-server \
# avoind using ${host} for --bind-addr

code-server \
--auth="password" \
--bind-addr="0.0.0.0:${port}" \
--bind-addr="0.0.0.0:8443" \
--disable-telemetry \
--ignore-last-opened \
--user-data-dir="$CODE_SERVER_DATAROOT" \
--log debug \
"<%= working_dir.to_s %>"
--user-data-dir="${CODE_SERVER_DATAROOT}" \
--extensions-dir="$CODE_SERVER_DATAROOT/extensions" \
--log debug
25 changes: 0 additions & 25 deletions view.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
<%
require 'digest'

# Generate form id, based on host and port
form_id = Digest::SHA1.hexdigest("--" + host.to_s + "--" + port.to_s + "--")

# Generate SHA256 digest of Code Server Password
cookieValue = Digest::SHA256.hexdigest(password)
%>

<script type="text/javascript">
(function () {
let date = new Date()
date.setTime(date.getTime() + (7*24*60*60*1000))
let expires = "expires=" + date.toUTCString()
let cookiePath = "path=/rnode/" + "<%= host.to_s %>" + "/" + "<%= port.to_s %>/";
/**
We have to use "key" as the cookie name since upstream in cdr/code-server
the authentication cookie is hard coded "key"
*/
let cookie = `key=<%= cookieValue %>;${expires};${cookiePath};secure`;
document.cookie = cookie;
})();
</script>

<form id="<%= form_id %>" action="/rnode/<%= host %>/<%= port %>/login?to=" method="post" target="_blank">
<input type="hidden" name="password" value="<%= password %>">
<button class="btn btn-primary" type="submit">
Expand Down

0 comments on commit 68aa6ee

Please sign in to comment.