Skip to content

Commit

Permalink
added script to setup the demo
Browse files Browse the repository at this point in the history
Added a shell script that will set up the demo to `ite-4-demo-test-repo`
or a the value in environment variable `TESTREPO`.

Updated `run_demo.py` to also use the `TESTREPO` variable and call
commands to the GitHub API to the repo defined by the user.

Updated README.
  • Loading branch information
alanssitis committed Oct 21, 2022
1 parent 8b4fd45 commit 40fc1db
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 43 deletions.
48 changes: 15 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,23 @@ A simple demo that shows some of the capabilities that ITE-4 enables.

## Demo setup

Clone this repository recursively and set up a virtual environment to contain
all the dependencies for the demo.
After cloning this test repo, run the `setup_demo.sh` script to setup for the
demo. Make sure to set the environment variable `TESTREPO` to a fork of
`in-toto/ite-4-demo-test-repo` in order to create and merge PR's, or a repo
with files of the same name.

```shell
git clone --recursive [email protected]:in-toto/ite-4-demo.git && cd ite-4-demo
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```

This repo includes a monkey-patched version of in-toto. Install it on your
machine machine.

```shell
cd in-toto
pip install .

# Go back to the demo's home directory.
cd ..
```
The script will clone test projects into the home directories of the "players"
involved, Alice and Bob. In the real world, it is more likely that both already
have the project installed locally.

For this demo, make sure you have the [github cli tool](https://cli.github.com/)
installed.

Also, clone test projects into the home directories of the "players" involved,
Alice and Bob. In the real world, it is more likely that both already have the
project installed locally.

**Note:** If you are testing this demo locally, use a personal fork of the
`ite-4-demo-test-repo` since you will need access to make and merge PR's. So
replace the repo link below with one pointing to your fork.

```shell
git clone [email protected]:in-toto/ite-4-demo-test-repo.git functionary_bob/project
git clone [email protected]:in-toto/ite-4-demo-test-repo.git owner_alice/project
```

## Run the demo commands

**NOTE:** Some commands below are making calls to the test repo, make sure to
change any references to `in-toto/ite-4-demo-test-repo` to the repo you are using.

### 1. Define the software supply chain layout (Alice)

```shell
Expand Down Expand Up @@ -162,7 +140,11 @@ python3 run_demo.py --clean

### Automated run through

Use the same script to have an automated run through of the demo.
Use the same script to have an automated run through of the demo. If you are
running the demo using a repo other than `in-toto/ite-4-demo-test-repo`, make
sure you have the environment variable `TESTREPO` set to the corresponding repo.

It should be able to handle repos setup with either HTTPS and SSH.

```shell
python3 run_demo.py
Expand Down
47 changes: 37 additions & 10 deletions run_demo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import re
import sys
import shlex
import subprocess
import argparse
from shutil import copyfile, copytree, rmtree
from shutil import copyfile, rmtree

NO_PROMPT = False
TESTREPO = "in-toto/ite-4-demo-test-repo"


def prompt_key(prompt):
Expand Down Expand Up @@ -85,13 +87,12 @@ def supply_chain():
pr_link = subprocess.check_output(shlex.split(create_pr_cmd))
pr_number = pr_link.decode().replace('\n', '').split('/')[-1]

create_pr_stop_cmd = (
"in-toto-record"
" stop"
" --verbose"
" --step-name create-pr"
" --key ../bob"
f" --products github:in-toto/ite-4-demo-test-repo:pr:{pr_number}")
create_pr_stop_cmd = ("in-toto-record"
" stop"
" --verbose"
" --step-name create-pr"
" --key ../bob"
f" --products github:{TESTREPO}:pr:{pr_number}")
print(create_pr_stop_cmd)
subprocess.call(shlex.split(create_pr_stop_cmd))

Expand All @@ -103,8 +104,7 @@ def supply_chain():
" --verbose"
" --step-name merge-pr"
" --key ../alice"
f" --materials github:in-toto/ite-4-demo-test-repo:pr:{pr_number} git:commit"
)
f" --materials github:{TESTREPO}:pr:{pr_number} git:commit")
print(merge_pr_start_cmd)
subprocess.call(shlex.split(merge_pr_start_cmd))

Expand Down Expand Up @@ -188,6 +188,22 @@ def supply_chain():
print("Return value: " + str(retval))


def extract_repo(uri):
ssh_pattern = r'^[email protected]:([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+).git$'
repo = re.findall(ssh_pattern, uri)
if len(repo) > 0:
return repo[0]
https_pattern = r'^https://github.com/([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+).git$'
repo = re.findall(https_pattern, uri)
if len(repo) > 0:
return repo[0]
regular_pattern = r'^([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)$'
repo = re.findall(regular_pattern, uri)
if len(repo) > 0:
return repo[0]
sys.exit(f'failed to extract github repo from "{uri}"')


def main():
parser = argparse.ArgumentParser()
parser.add_argument("-n",
Expand All @@ -200,6 +216,11 @@ def main():
action="store_true")
args = parser.parse_args()

if repo := os.getenv("TESTREPO"):
global TESTREPO
TESTREPO = extract_repo(repo)
print(TESTREPO)

if args.clean:
files_to_delete = [
"owner_alice/root.layout",
Expand All @@ -218,8 +239,14 @@ def main():
rmtree(path)

# reset project
os.chdir("owner_alice/project")
subprocess.call(shlex.split("git checkout main"))
subprocess.call(shlex.split("git reset --hard"))
subprocess.call(shlex.split("git pull"))
os.chdir("../..")
os.chdir("functionary_bob/project")
subprocess.call(shlex.split("git checkout main"))
subprocess.call(shlex.split("git reset --hard"))
subprocess.call(shlex.split("git branch -D feature"))
subprocess.call(shlex.split("git pull"))
os.chdir("../..")
Expand Down
26 changes: 26 additions & 0 deletions setup_demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
if [[ "$TESTREPO" = "" ]]; then
[email protected]:in-toto/ite-4-demo-test-repo.git
fi

# Install demo dependencies
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

# Install monkey-patched in-toto
git submodule update --init
cd in-toto
pip install .
cd ..

# Setup test projects for demo
git clone $TESTREPO owner_alice/project
git clone $TESTREPO functionary_bob/project

# Check if gh is installed
if ! command -v gh &> /dev/null; then
echo "github-cli not installed"
echo "Checkout https://cli.github.com/manual/installation for instructions"
fi

echo "Make sure to run 'source venv/bin/activate' before starting the demo!"

0 comments on commit 40fc1db

Please sign in to comment.