google / kctf

kCTF is a Kubernetes-based infrastructure for CTF competitions. For documentation, see
https://google.github.io/kctf/
Apache License 2.0
665 stars 73 forks source link

Update activate #392

Closed trvon closed 1 year ago

trvon commented 1 year ago

Moved "export KCTF_CTF_DIR" into macOS check to use grealpath instead of realpath which doesn't seem supported by default.

google-cla[bot] commented 1 year ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

sroettger commented 1 year ago

Hi, thanks for the pull request! On my mac I have the opposite problem, realpath is available but grealpath is not..

Could you make it check which one exists and then put that in an environment variable maybe?

trvon commented 1 year ago

Real path does exist on my system. I should correct my previous statement to be that how realpath is used, does not seem to be have the intended behavior. In my environment, when I source the kctf/activate after a clean download I see:

realpath: illegal option -- -
usage: realpath [-q] [path ...]
kctf/activate:source:50: no such file or directory: /kctf/bin/kctf-log
gstat: cannot stat '/kctf/bin/kctf-cluster': No such file or directory
_kctf_check_umask:6: command not found: _kctf_log_err
_kctf_error_cleanup:unset:12: no such hash table element: _kctf_log
_kctf_error_cleanup:unset:13: no such hash table element: _kctf_log_err

I can update my commit to include the follow so it will work in both environments.

  if ! grealpath &> /dev/null
  then
    export KCTF_CTF_DIR="$(grealpath --no-symlinks "$(dirname "${BASH_SOURCE-$0}")/..")"
  else
    export KCTF_CTF_DIR="$(realpath --no-symlinks "$(dirname "${BASH_SOURCE-$0}")/..")"
  fi
sroettger commented 1 year ago

ah, that makes sense. I think the better solution would be to skip the --no-symlinks in that case. I think it's there to support a symlinked kctf directory inside the challenge dir.

I.e. maybe something like this should work?

export KCTF_CTF_DIR="$(realpath "$(dirname "$(dirname "${BASH_SOURCE-$0}")")")"

In a quick test, that seems to handle all the cases of relative and absolute paths with a symlinked kctf dir

... Thinking more about it, it needs some handling of source ./activate. So maybe more like:

script_dir="$(dirname "${BASH_SOURCE-$0}")"
if [[ "$script_dir" == "." ]]; then
  script_dir="../."
fi
export KCTF_CTF_DIR="$(realpath "$(dirname "${script_dir}")")"
unset script_dir
trvon commented 1 year ago

That solution worked as well in my initial testing. I'll update my branch to reflect the suggestion.

sroettger commented 1 year ago

That solution worked as well in my initial testing. I'll update my branch to reflect the suggestion.

Sounds good. Please let me know once you updated it.

trvon commented 1 year ago

Just updated the code.