Skip to content

Commit

Permalink
fixed macos test connection over the tunnel (#2)
Browse files Browse the repository at this point in the history
* added ping to test connection over the tunnel

* print configs

* print log on error

* exit on error

* added status check

* changed log format

* test config

* removed log hide

* changed to base64

* updated key

* added config

* chenged service list

* clean-up

* removed fi

* added then

* added output check

* added err handling to script

* cleaned up bitrise.yml

* debug

* fixed output

* clean up
  • Loading branch information
trapacska authored Nov 13, 2020
1 parent cbbfb01 commit 418eb86
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 83 deletions.
73 changes: 7 additions & 66 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,21 @@ app:

workflows:
test:
before_run:
- audit-this-step
steps:
- script:
inputs:
- content: |
#!/bin/bash
echo "Just an example 'secrets' print."
echo "The value of 'A_SECRET_PARAM' is: $A_SECRET_PARAM"
- change-workdir:
title: Switch working dir to test / _tmp dir
description: |-
To prevent step testing issues, like referencing relative
files with just './some-file' in the step's code, which would
work for testing the step from this directory directly
but would break if the step is included in another `bitrise.yml`.
run_if: true
inputs:
- path: ./_tmp
- is_create_path: true
- path::./:
title: Step Test
description: |-
The example input has a default value,
you can overwrite it if you want to, just like we did below,
but the step would use the default value specified in the `step.yml`
file if you would not specify another value.
run_if: true
inputs:
- example_step_input: Example Step Input's value
is_skippable: true
- script:
inputs:
- content: |
#!/bin/bash
echo "This output was generated by the Step (EXAMPLE_STEP_OUTPUT): $EXAMPLE_STEP_OUTPUT"
set -e
ls "$OPENVPN_LOG_PATH"
echo "This output was generated by the Step (\$OPENVPN_LOG_PATH): $OPENVPN_LOG_PATH"
# ----------------------------------------------------------------
# --- workflows to Share this step into a Step Library
Expand All @@ -58,43 +39,3 @@ workflows:
#!/bin/bash
set -ex
stepman audit --step-yml ./step.yml
share-this-step:
envs:
# if you want to share this step into a StepLib
- MY_STEPLIB_REPO_FORK_GIT_URL: $MY_STEPLIB_REPO_FORK_GIT_URL
- BITRISE_STEP_ID: $BITRISE_STEP_ID
- BITRISE_STEP_VERSION: $BITRISE_STEP_VERSION
- BITRISE_STEP_GIT_CLONE_URL: $BITRISE_STEP_GIT_CLONE_URL
description: |-
If this is the first time you try to share a Step you should
first call: $ bitrise share
This will print you a guide, and information about how Step sharing
works. Please read it at least once!
As noted in the Step sharing guide you'll have to fork the
StepLib you want to share this step into. Once you're done with forking
the repository you should set your own fork's git clone URL
in the `.bitrise.secrets.yml` file, or here in the `envs` section,
as the value of the `MY_STEPLIB_REPO_FORK_GIT_URL` environment.
You're now ready to share this Step, just make sure that
the `BITRISE_STEP_ID` and `BITRISE_STEP_VERSION`
environments are set to the desired values!
To share this Step into a StepLib you can just run: $ bitrise run share-this-step
Once it finishes the only thing left is to actually create a Pull Request,
the way described in the guide printed at the end of the process.
before_run:
- audit-this-step
steps:
- script:
inputs:
- content: |-
#!/bin/bash
set -ex
bitrise share start -c "${MY_STEPLIB_REPO_FORK_GIT_URL}"
bitrise share create --stepid "${BITRISE_STEP_ID}" --tag "${BITRISE_STEP_VERSION}" --git "${BITRISE_STEP_GIT_CLONE_URL}"
bitrise share finish
56 changes: 39 additions & 17 deletions step.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
#!/bin/bash
set -eu

echo "Configs:"
echo "host: $host"
echo "port: $port"
echo "proto: $proto"
echo "ca_crt: $(if [ ! -z "$ca_crt" ]; then echo "***"; fi)"
echo "client_crt: $(if [ ! -z "$client_crt" ]; then echo "***"; fi)"
echo "client_key: $(if [ ! -z "$client_key" ]; then echo "***"; fi)"
echo ""

log_path=$(mktemp)

envman add --key "OPENVPN_LOG_PATH" --value "$log_path"
echo "Log path exported (\$OPENVPN_LOG_PATH=$log_path)"
echo ""

case "$OSTYPE" in
linux*)
echo "Configuring for Ubuntu"
Expand All @@ -25,35 +40,42 @@ cert client.crt
key client.key
EOF

service openvpn start client > /dev/null 2>&1
sleep 5
echo ""
echo "Run openvpn"
service openvpn start client > $log_path 2>&1
echo "Done"
echo ""

if ifconfig | grep tun0 > /dev/null
then
echo "VPN connection succeeded"
else
echo "VPN connection failed!"
echo "Check status"
sleep 5
if ! ifconfig | grep tun0 > /dev/null ; then
echo "No open VPN tunnel found"
cat "$log_path"
exit 1
fi
echo "Done"
;;
darwin*)
echo "Configuring for Mac OS"

echo ${ca_crt} | base64 -D -o ca.crt > /dev/null 2>&1
echo ${client_crt} | base64 -D -o client.crt > /dev/null 2>&1
echo ${client_key} | base64 -D -o client.key > /dev/null 2>&1
echo ${ca_crt} | base64 -D -o ca.crt
echo ${client_crt} | base64 -D -o client.crt
echo ${client_key} | base64 -D -o client.key
echo ""

sudo openvpn --client --dev tun --proto ${proto} --remote ${host} ${port} --resolv-retry infinite --nobind --persist-key --persist-tun --comp-lzo --verb 3 --ca ca.crt --cert client.crt --key client.key > /dev/null 2>&1 &
echo "Run openvpn"
sudo openvpn --client --dev tun --proto ${proto} --remote ${host} ${port} --resolv-retry infinite --nobind --persist-key --persist-tun --comp-lzo --verb 3 --ca ca.crt --cert client.crt --key client.key > $log_path 2>&1 &
echo "Done"
echo ""

echo "Check status"
sleep 5

if ifconfig -l | grep utun0 > /dev/null
then
echo "VPN connection succeeded"
else
echo "VPN connection failed!"
if ! ps -p $! >&-; then
echo "Process exited"
cat "$log_path"
exit 1
fi
echo "Done"
;;
*)
echo "Unknown operative system: $OSTYPE, exiting"
Expand Down
4 changes: 4 additions & 0 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ inputs:
is_expand: true
is_required: true
is_sensitive: true
outputs:
- OPENVPN_LOG_PATH:
opts:
title: "Output log file path"

0 comments on commit 418eb86

Please sign in to comment.