-
Notifications
You must be signed in to change notification settings - Fork 23
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
Provide support script for adding new test subject #817
Open
mbuechse
wants to merge
3
commits into
main
Choose a base branch
from
issue/816
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
htmlcov/ | ||
.coverage | ||
.secret |
This file contains 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,88 @@ | ||
#!/usr/bin/env python3 | ||
# vim: set ts=4 sw=4 et: | ||
# | ||
# add_subject.py | ||
# | ||
# (c) Matthias Büchse <[email protected]> | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import base64 | ||
import getpass | ||
import os | ||
import os.path | ||
import re | ||
import shutil | ||
import subprocess | ||
import sys | ||
import tempfile | ||
|
||
try: | ||
from passlib.context import CryptContext | ||
import argon2 | ||
except ImportError: | ||
print('Missing passlib and/or argon2. Please do:\npip install passlib argon2_cffi', file=sys.stderr) | ||
sys.exit(1) | ||
|
||
# see ../compliance-monitor/monitor.py | ||
CRYPTCTX = CryptContext(schemes=('argon2', 'bcrypt'), deprecated='auto') | ||
SSH_KEYGEN = shutil.which('ssh-keygen') | ||
SUBJECT_RE = re.compile(r"[a-zA-Z0-9_\-]+") | ||
|
||
|
||
def main(argv, cwd): | ||
if len(argv) != 1: | ||
raise RuntimeError("Need to supply precisely one argument: name of subject") | ||
subject = argv[0] | ||
print(f"Attempt to add subject {subject!r}") | ||
keyfile_path = os.path.join(cwd, '.secret', 'keyfile') | ||
tokenfile_path = os.path.join(cwd, '.secret', 'tokenfile') | ||
if os.path.exists(keyfile_path): | ||
raise RuntimeError(f"Keyfile {keyfile_path} already present. Please proceed manually") | ||
if os.path.exists(tokenfile_path): | ||
raise RuntimeError(f"Tokenfile {tokenfile_path} already present. Please proceed manually") | ||
if not(SUBJECT_RE.fullmatch(subject)): | ||
raise RuntimeError(f"Subject name {subject!r} using disallowed characters") | ||
sanitized_subject = subject.replace('-', '_') | ||
print("Creating API key...") | ||
while True: | ||
password = getpass.getpass("Enter passphrase: ") | ||
if password == getpass.getpass("Repeat passphrase: "): | ||
break | ||
print("No match. Try again...") | ||
token = base64.b64encode(f"{subject}:{password}".encode('utf-8')) | ||
hash_ = CRYPTCTX.hash(password) | ||
with open(tokenfile_path, "wb") as fileobj: | ||
fileobj.write(token) | ||
print("Creating key file using `ssh-keygen`...") | ||
subprocess.check_call([SSH_KEYGEN, '-t', 'ed25519', '-C', sanitized_subject, '-f', keyfile_path, '-N', '', '-q']) | ||
with open(keyfile_path + '.pub', "r") as fileobj: | ||
pubkey_components = fileobj.readline().split() | ||
print(f''' | ||
The following SECRET files have been created: | ||
|
||
- {keyfile_path} | ||
- {tokenfile_path} | ||
|
||
They are required for submitting test reports. You MUST keep them secure and safe. | ||
|
||
Insert the following snippet into compliance-monitor/bootstrap.yaml: | ||
|
||
- subject: {subject} | ||
api_keys: | ||
- "{hash_}" | ||
keys: | ||
- public_key: "{pubkey_components[1]}" | ||
public_key_type: "{pubkey_components[0]}" | ||
public_key_name: "primary" | ||
|
||
Make sure to submit a pull request with the changed file. Otherwise, the reports cannot be submitted. | ||
''', end='') | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
sys.exit(main(sys.argv[1:], cwd=os.path.dirname(sys.argv[0]) or os.getcwd()) or 0) | ||
except RuntimeError as e: | ||
print(str(e), file=sys.stderr) | ||
sys.exit(1) | ||
except KeyboardInterrupt: | ||
print("Interrupted", file=sys.stderr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
Oops, something went wrong.
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.
Check failure
Code scanning / CodeQL
Clear-text storage of sensitive information High
Copilot Autofix AI 8 days ago
To fix the problem, we need to ensure that the password is encrypted before being stored in the token file. We can use the
cryptography
library to encrypt the password before encoding it in base64 and writing it to the file. This will ensure that the password is not stored in clear text.cryptography
library.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Erm what? And the
key
? Where am I storing the key???There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without spending an hour to understand what's required here, I can not comment on this, sorry.
If we can avoid storing sensitive data in plaintext, we should of course. I have no idea whether or not that is possible without major effort. If we need to store it, we should adjust the permissions / umask to 0600 (rw-------).