11#! /usr/bin/env bash
2- set -e
2+ set -euo pipefail
3+
4+ # ---------------------------------------------------------------------------------------------------------------------
5+ # NightVision GitLab "Easy Mode" demo (NV-4418)
6+ #
7+ # Creates a GitLab project, wires the NIGHTVISION_TOKEN CI variable, creates a NightVision
8+ # app + target, records auth, and pushes the demo repo to trigger the CI/CD pipeline.
9+ #
10+ # Re-run behaviour: this script is intended to be safe to re-run. The create steps
11+ # (GitLab repo, NightVision app/target) are guarded so a second run reuses existing
12+ # resources with a logged warning instead of aborting, and the gitlab remote is
13+ # reconciled to the current "$GROUP/$REPO" on each run. The NightVision token is
14+ # the exception: each run creates a fresh one and overwrites the CI variable (the value
15+ # cannot be read back, so it is rotated, not reused); older tokens stay in NightVision, so
16+ # revoke them there if you re-run often. The script does NOT delete anything; see the
17+ # cleanup notes at the bottom to tear a demo down.
18+ # ---------------------------------------------------------------------------------------------------------------------
319
420# Check if the correct number of arguments is provided
521if [ " $# " -ne 2 ]; then
@@ -9,70 +25,108 @@ if [ "$#" -ne 2 ]; then
925fi
1026
1127# Assign positional arguments to variables
12- GROUP=$1
13- REPO=$2
28+ GROUP=" $1 "
29+ REPO=" $2 "
1430
1531echo " Creating a repository under: $GROUP /$REPO "
1632
17- # Clone the GitHub repository that we will mirror to GitLab
18- git clone https://github.com/nvsecurity/java-github-actions-demo
33+ # Clone the GitHub repository that we will mirror to GitLab.
34+ # Idempotent: reuse an existing checkout instead of failing on a second run.
35+ if [ ! -d java-github-actions-demo ]; then
36+ git clone https://github.com/nvsecurity/java-github-actions-demo
37+ fi
1938cd java-github-actions-demo
2039
2140# ---------------------------------------------------------------------------------------------------------------------
2241# Set up the GitLab repository
2342# ---------------------------------------------------------------------------------------------------------------------
24- # Create a repository
25- echo " NOTE: Select NO for 'Create a local project directory'"
26- glab repo create $REPO
27-
28- # add the nightvision token as the GitLab secret NIGHTVISION_TOKEN
29- nightvision login
30- TOKEN=$( nightvision token create)
31-
32- # Check if GitLab CLI is authenticated
43+ # Authenticate to GitLab and NightVision first, so the create steps below only run after
44+ # both logins are confirmed - an aborted login then leaves nothing half-created.
3345if ! glab auth status 2>&1 | grep -q ' Logged in to gitlab.com' ; then
3446 echo " GitLab CLI not authenticated. Running 'glab auth login'..."
3547 glab auth login
3648else
3749 echo " Logged in to gitlab already..."
3850fi
51+ nightvision login
3952
40- glab variable set NIGHTVISION_TOKEN --masked --repo $GROUP /$REPO $TOKEN < token
53+ # Create a repository in the target namespace. If it already exists, keep going.
54+ echo " NOTE: Select NO for 'Create a local project directory'"
55+ glab repo create " $GROUP /$REPO " || echo " WARNING: 'glab repo create $GROUP /$REPO ' failed (the project may already exist); continuing."
56+
57+ # Create a fresh NightVision token for the NIGHTVISION_TOKEN CI variable. tr strips any
58+ # stray whitespace so the value GitLab masks is clean; the guard catches an empty result
59+ # (an exit-0 token create with no output) before it becomes an invalid masked variable.
60+ TOKEN=" $( nightvision token create | tr -d ' [:space:]' ) "
61+ if [ -z " $TOKEN " ]; then
62+ echo " ERROR: 'nightvision token create' returned an empty token; aborting." >&2
63+ exit 1
64+ fi
65+
66+ # Set the masked CI variable to the freshly created token (rotated every run, not reused,
67+ # because a token's value cannot be read back). If it already exists (older glab errors
68+ # instead of upserting), update it. This must succeed: failing both set and update aborts
69+ # the script (set -e) rather than silently leaving CI a stale token.
70+ glab variable set NIGHTVISION_TOKEN --masked --repo " $GROUP /$REPO " " $TOKEN " \
71+ || glab variable update NIGHTVISION_TOKEN --masked --repo " $GROUP /$REPO " " $TOKEN "
4172
4273# ---------------------------------------------------------------------------------------------------------------------
4374# Note that GitLab has additional requirements vs other CI/CD providers.
4475# Instead of `localhost` you must use the `docker` hostname.
45- # First add the docker hostname reference to your `/etc/hosts` file on your laptop
76+ # First add the docker hostname reference to your `/etc/hosts` file on your laptop.
77+ #
78+ # This edits a system file with sudo. It is left in place after the demo so repeat runs work.
79+ # To revert it afterwards run:
80+ # sudo sed -i.bak '/^127\.0\.0\.1 docker$/d' /etc/hosts
4681# ---------------------------------------------------------------------------------------------------------------------
47- if ! grep -q " 127.0.0.1 docker" /etc/hosts; then \
48- echo " 127.0.0.1 docker" | sudo tee -a /etc/hosts; \
82+ if ! grep -q " 127.0.0.1 docker" /etc/hosts; then
83+ echo " NOTICE: adding '127.0.0.1 docker' to /etc/hosts (sudo). See the revert command in this script's comments."
84+ echo " 127.0.0.1 docker" | sudo tee -a /etc/hosts
4985fi
86+
5087# ---------------------------------------------------------------------------------------------------------------------
5188# NightVision commands
5289# ---------------------------------------------------------------------------------------------------------------------
53- # Create app and target
54- # Username: user
55- # Password: password
90+ # Create app and target. Guarded so a re-run reuses the existing app/target.
5691URL=" https://docker:9000"
5792APP=" javaspringvulny-api-gitlab"
58- nightvision app create $APP
59- nightvision target create $APP https://docker:9000 --type api
93+ nightvision app create " $APP " || echo " WARNING: 'nightvision app create $APP ' failed (it may already exist); continuing. "
94+ nightvision target create " $APP " " $URL " --type API || echo " WARNING: 'nightvision target create $APP ' failed (it may already exist); continuing. "
6095
6196# Start the application
6297docker compose up -d; sleep 10
63- # Record authentication - click on Form Auth
64- echo " Click on Form Auth and use these credentials: "
65- echo " \tUsername: user"
66- echo " \tPassword: password"
67- nightvision auth playwright create $APP $URL
98+ # Record authentication - click on Form Auth.
99+ # These are the demo application's default credentials (the javaspringvulny sample app),
100+ # not real secrets.
101+ echo " Click on Form Auth and use the javaspringvulny demo defaults:"
102+ echo " Username: user"
103+ echo " Password: password"
104+ nightvision auth playwright create " $APP " " $URL "
68105
69106# ---------------------------------------------------------------------------------------------------------------------
70107# sync it back with GitLab and trigger the CI/CD job.
71108# ---------------------------------------------------------------------------------------------------------------------
72- git remote add gitlab git@gitlab.com:$GROUP /$REPO .git
109+ # Point the gitlab remote at the requested project. A reused checkout may carry the
110+ # remote from a previous run with different arguments; reconciling it to the current
111+ # "$GROUP/$REPO" keeps the push from silently targeting the previous run's project.
112+ if git remote get-url gitlab > /dev/null 2>&1 ; then
113+ git remote set-url gitlab " git@gitlab.com:$GROUP /$REPO .git"
114+ else
115+ git remote add gitlab " git@gitlab.com:$GROUP /$REPO .git"
116+ fi
117+ # Add an empty commit so each run pushes a fresh commit and triggers the
118+ # pipeline. A re-run reuses the existing checkout, which has no new commits, so a
119+ # bare push would be "Everything up-to-date" and fire nothing.
120+ git commit --allow-empty -m " Trigger GitLab pipeline"
73121git push gitlab main
74122
75123# Notes:
76- # To delete the project:
77- # glab repo delete $GROUP/$REPO
124+ # To delete the project and local checkout (substitute the group and repo you ran this script with) :
125+ # glab repo delete <group>/<repo>
78126# rm -rf ./java-github-actions-demo
127+ # The NightVision app and target are not deleted; they are reused on re-run
128+ # (auth is re-recorded each run, and each run mints a fresh token that persists,
129+ # as noted in the header). Remove the app, target, and stale tokens from the
130+ # NightVision UI for a full teardown.
131+ # To revert the /etc/hosts entry:
132+ # sudo sed -i.bak '/^127\.0\.0\.1 docker$/d' /etc/hosts
0 commit comments