Skip to content
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

Add client enroll #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions privateSBOMExchange/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ init:
test:
py.test tests

server:
bash runserver.sh

.PHONY: init test
16 changes: 15 additions & 1 deletion privateSBOMExchange/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
beartype==0.18.5
blinker==1.8.2
boolean.py==4.0
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.4.0
click==8.1.7
cryptography==43.0.3
defusedxml==0.7.1
distlib==0.3.8
filelock==3.16.1
Flask==3.0.3
idna==3.10
isodate==0.6.1
itsdangerous==2.2.0
Jinja2==3.1.4
lib4sbom==0.7.5
license-expression==30.3.1
MarkupSafe==3.0.2
numpy==2.1.1
pandas==2.2.2
platformdirs==4.3.6
ply==3.11
pycparser==2.22
pyparsing==3.1.4
python-dateutil==2.9.0.post0
pytz==2024.2
PyYAML==6.0.2
rdflib==7.0.0
requests==2.32.3
semantic-version==2.10.0
six==1.16.0
sparse-merkle-tree==0.3.0
spdx-tools==0.8.0a2
spdx-tools==0.8.3
typeguard==4.0.0
tzdata==2024.1
uritools==4.0.3
urllib3==2.2.3
virtualenv==20.26.6
Werkzeug==3.0.4
xmltodict==0.13.0
2 changes: 2 additions & 0 deletions privateSBOMExchange/runserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export PYTHONPATH=$PWD
python src/cmd/redactor.py
28 changes: 28 additions & 0 deletions privateSBOMExchange/src/cmd/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
import requests
import click
import os

@click.group()
def cli():
pass

@cli.command()
@click.argument('cert', type=str)
def enroll(cert):
"""Enrolls a target Petra server (defaults to localhost:5000)"""
if os.path.exists(cert):
with open(cert) as fp:
payload = fp.read()
else:
printf(f"Couldn't load {cert}. It doesn't exist!")

# TODO: we could be cutesy and check it's an actual PEM
request = {"cert": payload}
response = requests.post("http://localhost:5000/enroll", json=request)
print(response.json())


if __name__ == '__main__':
cli()

39 changes: 39 additions & 0 deletions privateSBOMExchange/src/cmd/redactor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from src.lib import redactor
from flask import Flask, request, make_response, jsonify
from cryptography import x509

# we keep this in memory, seems to hold some global variables that we may need
# to use.

app = Flask(__name__)

@app.route("/")
def hello_world():
return "<p>Petra: this is a JSON api, you shouldn't be looking here!</p>"

"""
enroll receives an object containing only cert, and returns a success/fail
symbol depending on how things went.


"""
@app.route("/enroll", methods=['POST'])
def enroll():
data = request.get_json()
if 'cert' not in data:
# This error handling could be done better
raise Exception("request is malformed!")
cert = x509.load_pem_x509_certificate(bytes(data['cert'].encode('ascii')))

current_role = redactor.EnrollRoles(cert)

response_data = {"status": "ok",
"current_role": current_role}

return make_response(jsonify(response_data), 200)



if __name__ == "__main__":
print("running redactor...")
app.run(debug=True)
19 changes: 10 additions & 9 deletions privateSBOMExchange/src/lib/redactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from cryptography import x509
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.serialization import PublicFormat, Encoding
from spdx_tools.spdx.model import Document
from spdx_tools.spdx3.bump_from_spdx2 import spdx_document
import os
Expand Down Expand Up @@ -85,29 +86,29 @@ def GenerateSBOM (artifact) :
#return SBOM

def GetRole(key):


logging.debug("Inside GetRoles")
role=""
return role

if key in Users:
return Users[key]
return None

def EnrollRoles (cert):
logging.debug("Inside EnrollRoles")
currentRole=""
public_key=cert.public_key()
public_key=cert.public_key().public_bytes(Encoding.PEM, PublicFormat.SubjectPublicKeyInfo)
#TODO verify the key
#import pdb; pdb.set_trace()
user = {
"organizationName": cert.subject.get_attributes_for_oid(x509.OID_ORGANIZATION_NAME)[0].value,
"commonName": cert.subject.get_attributes_for_oid(x509.OID_COMMON_NAME)[0].value,
"key":public_key
}
Users.add(user)
Users[user["key"]] = user

#print(cert.public_key())

currentRole=GetRole(public_key)

return currentRole
return currentRole["commonName"]

def CalculateRoles (policy):
logging.debug("Inside CalculateRoles")
Expand All @@ -129,4 +130,4 @@ def CalculateRoles (policy):





27 changes: 27 additions & 0 deletions privateSBOMExchange/tests/data/certs/github.com
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN CERTIFICATE-----
MIIEozCCBEmgAwIBAgIQTij3hrZsGjuULNLEDrdCpTAKBggqhkjOPQQDAjCBjzEL
MAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE
BxMHU2FsZm9yZDEYMBYGA1UEChMPU2VjdGlnbyBMaW1pdGVkMTcwNQYDVQQDEy5T
ZWN0aWdvIEVDQyBEb21haW4gVmFsaWRhdGlvbiBTZWN1cmUgU2VydmVyIENBMB4X
DTI0MDMwNzAwMDAwMFoXDTI1MDMwNzIzNTk1OVowFTETMBEGA1UEAxMKZ2l0aHVi
LmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABARO/Ho9XdkY1qh9mAgjOUkW
mXTb05jgRulKciMVBuKB3ZHexvCdyoiCRHEMBfFXoZhWkQVMogNLo/lW215X3pGj
ggL+MIIC+jAfBgNVHSMEGDAWgBT2hQo7EYbhBH0Oqgss0u7MZHt7rjAdBgNVHQ4E
FgQUO2g/NDr1RzTK76ZOPZq9Xm56zJ8wDgYDVR0PAQH/BAQDAgeAMAwGA1UdEwEB
/wQCMAAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMEkGA1UdIARCMEAw
NAYLKwYBBAGyMQECAgcwJTAjBggrBgEFBQcCARYXaHR0cHM6Ly9zZWN0aWdvLmNv
bS9DUFMwCAYGZ4EMAQIBMIGEBggrBgEFBQcBAQR4MHYwTwYIKwYBBQUHMAKGQ2h0
dHA6Ly9jcnQuc2VjdGlnby5jb20vU2VjdGlnb0VDQ0RvbWFpblZhbGlkYXRpb25T
ZWN1cmVTZXJ2ZXJDQS5jcnQwIwYIKwYBBQUHMAGGF2h0dHA6Ly9vY3NwLnNlY3Rp
Z28uY29tMIIBgAYKKwYBBAHWeQIEAgSCAXAEggFsAWoAdwDPEVbu1S58r/OHW9lp
LpvpGnFnSrAX7KwB0lt3zsw7CAAAAY4WOvAZAAAEAwBIMEYCIQD7oNz/2oO8VGaW
WrqrsBQBzQH0hRhMLm11oeMpg1fNawIhAKWc0q7Z+mxDVYV/6ov7f/i0H/aAcHSC
Ii/QJcECraOpAHYAouMK5EXvva2bfjjtR2d3U9eCW4SU1yteGyzEuVCkR+cAAAGO
Fjrv+AAABAMARzBFAiEAyupEIVAMk0c8BVVpF0QbisfoEwy5xJQKQOe8EvMU4W8C
IGAIIuzjxBFlHpkqcsa7UZy24y/B6xZnktUw/Ne5q5hCAHcATnWjJ1yaEMM4W2zU
3z9S6x3w4I4bjWnAsfpksWKaOd8AAAGOFjrv9wAABAMASDBGAiEA+8OvQzpgRf31
uLBsCE8ktCUfvsiRT7zWSqeXliA09TUCIQDcB7Xn97aEDMBKXIbdm5KZ9GjvRyoF
9skD5/4GneoMWzAlBgNVHREEHjAcggpnaXRodWIuY29tgg53d3cuZ2l0aHViLmNv
bTAKBggqhkjOPQQDAgNIADBFAiEAru2McPr0eNwcWNuDEY0a/rGzXRfRrm+6XfZe
SzhYZewCIBq4TUEBCgapv7xvAtRKdVdi/b4m36Uyej1ggyJsiesA
-----END CERTIFICATE-----
96 changes: 96 additions & 0 deletions privateSBOMExchange/tests/data/certs/www.purdue.edu
Original file line number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