-
Notifications
You must be signed in to change notification settings - Fork 11
Add rule for delegating responsibility in policy #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MI2007
wants to merge
1
commit into
gittuf:main
Choose a base branch
from
MI2007:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| import os | ||
| import shlex | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
|
|
||
|
|
||
| NO_PROMPT = False | ||
| REQUIRED_BINARIES = ["git", "gittuf", "ssh-keygen"] | ||
|
|
||
|
|
||
| def check_binaries(): | ||
| for p in REQUIRED_BINARIES: | ||
| if not shutil.which(p): | ||
| raise Exception(f"required command {p} not found") | ||
|
|
||
|
|
||
| def prompt_key(prompt): | ||
| if NO_PROMPT: | ||
| print("\n" + prompt) | ||
| return | ||
| inp = False | ||
| while inp != "": | ||
| try: | ||
| inp = input(f"\n{prompt} -- press any key to continue") | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
| def display_command(cmd): | ||
| print(f"[{os.getcwd()}] $ {cmd}") | ||
|
|
||
| def run_command(cmd, expected_retcode=0): | ||
| retcode = subprocess.call(shlex.split(cmd)) | ||
| if retcode != expected_retcode: | ||
| raise Exception(f"Expected {expected_retcode} from process but it exited with {retcode}.") | ||
|
|
||
| def run_demo(): | ||
| current_dir = os.getcwd() | ||
| keys_dir = "keys" | ||
|
|
||
| tmp_dir = tempfile.TemporaryDirectory() | ||
| tmp_keys_dir = os.path.join(tmp_dir.name, keys_dir) | ||
| tmp_repo_dir = os.path.join(tmp_dir.name, "repo") | ||
|
|
||
| shutil.copytree(os.path.join(current_dir, keys_dir), tmp_keys_dir) | ||
| os.mkdir(tmp_repo_dir) | ||
| os.chdir(tmp_repo_dir) | ||
|
|
||
| for key in os.listdir(tmp_keys_dir): | ||
| os.chmod(os.path.join(tmp_keys_dir, key), 0o600) | ||
|
|
||
| root_key_path_git = os.path.join(tmp_keys_dir, "root") | ||
| alice_key_path_git = os.path.join(tmp_keys_dir, "alice") | ||
| alice_key_path_policy = os.path.join(tmp_keys_dir, "alice.pub") | ||
| bob_key_path_git = os.path.join(tmp_keys_dir, "bob.pub") | ||
|
|
||
| prompt_key("Initialize Git repository") | ||
| cmd = "git init -b main" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Set repo config to use demo identity and test key") | ||
| cmd = f"git config --local gpg.format ssh" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = f"git config --local commit.gpgsign true" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = f"git config --local user.signingkey {root_key_path_git}" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = f"git config --local user.name gittuf-demo" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = f"git config --local user.email gittuf.demo@example.com" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Set PAGER") | ||
| os.environ["PAGER"] = "cat" | ||
| display_command(f"export PAGER=cat") | ||
|
|
||
| prompt_key("Initialize gittuf root of trust") | ||
| cmd = "gittuf trust init -k ../keys/root" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Add policy key to gittuf root of trust") | ||
| cmd = ( | ||
| "gittuf trust add-policy-key" | ||
| " -k ../keys/root" | ||
| " --policy-key ../keys/targets.pub" | ||
| ) | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Initialize policy") | ||
| cmd = "gittuf policy init -k ../keys/targets" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Add trusted person (Alice) to gittuf policy file") | ||
| cmd = ( | ||
| "gittuf policy add-person" | ||
| " -k ../keys/targets" | ||
| " --person-ID 'Alice'" | ||
| f" --public-key {alice_key_path_policy}" | ||
| ) | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Add rule to protect the main branch") | ||
| cmd = ( | ||
| "gittuf policy add-rule" | ||
| " -k ../keys/targets" | ||
| " --rule-name 'protect-main'" | ||
| " --rule-pattern git:refs/heads/main" | ||
| " --authorize Alice" | ||
| ) | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| cmd = "gittuf policy stage --local-only" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| cmd = "gittuf policy apply --local-only" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Alice sets her signing key and identity on Git") | ||
| cmd = f"git config --local user.signingkey {alice_key_path_git}" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = f"git config --local user.name Alice" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = f"git config --local user.email alice@example.com" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| prompt_key("Alice makes a change to repo's main branch") | ||
| display_command("echo 'Hello, world!' > README.md") | ||
| with open("README.md", "w") as fp: | ||
| fp.write("Hello, world!\n") | ||
| cmd = "git add README.md" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = "git commit -m 'Initial commit'" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Record change to main in RSL") | ||
| cmd = "gittuf rsl record main --local-only" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = "git show refs/gittuf/reference-state-log" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Verify branch protection for this change") | ||
| cmd = "gittuf verify-ref main" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("gittuf's verification succeeded!") | ||
|
|
||
| prompt_key("Now Alice adds a policy file to delegate responsibility") | ||
| cmd = ( | ||
| "gittuf policy init" | ||
| f" -k {alice_key_path_git}" | ||
| " --policy-name protect-main" | ||
| ) | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Alice adds Bob as an authorized user in the delegated policy") | ||
| cmd = ( | ||
| "gittuf policy add-person" | ||
| f" -k {alice_key_path_git}" | ||
| " --person-ID Bob" | ||
| " --policy-name 'protect-main'" | ||
| f" --public-key {bob_key_path_git}" | ||
| ) | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Alice adds a rule to delegate her authorization to main") | ||
| cmd = ( | ||
| "gittuf policy add-rule" | ||
| f" -k {alice_key_path_git}" | ||
| " --rule-name 'protect-main-delegated'" | ||
| " --policy-name 'protect-main'" | ||
| " --rule-pattern git:refs/heads/main" | ||
| f" --authorize Bob" | ||
| ) | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
|
MI2007 marked this conversation as resolved.
|
||
| cmd = "gittuf policy stage --local-only" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| cmd = "gittuf policy apply --local-only" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Now, Bob makes a change to the main branch!") | ||
| cmd = f"git config --local user.signingkey {bob_key_path_git}" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = f"git config --local user.name Bob" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = f"git config --local user.email bob@example.com" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| display_command("echo 'This is Bob's change!' >> README.md") | ||
| with open("README.md", "a") as fp: | ||
| fp.write("Hello, Bob!\n") | ||
| cmd = "git add README.md" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = "git commit -m 'Initial commit from Bob'" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Record Bob's change to main in RSL") | ||
| cmd = "gittuf rsl record main --local-only" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
| cmd = "git show refs/gittuf/reference-state-log" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
| prompt_key("Verify branch protection for Bob's change") | ||
| cmd = "gittuf verify-ref main" | ||
| display_command(cmd) | ||
| run_command(cmd) | ||
|
|
||
|
MI2007 marked this conversation as resolved.
|
||
| prompt_key("gittuf's verification succeeded for Bob's change!") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if len(sys.argv) > 1: | ||
| if sys.argv[1] == "--no-prompt": | ||
| NO_PROMPT = True | ||
| check_binaries() | ||
| run_demo() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.