Skip to content

Commit

Permalink
Fix flake errors
Browse files Browse the repository at this point in the history
Signed-off-by: Anja Strunk <[email protected]>
  • Loading branch information
anjastrunk committed Aug 14, 2024
1 parent 4ce48ea commit d620598
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 27 deletions.
4 changes: 2 additions & 2 deletions generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from generator.common import credentials, crypto
from generator.common.config import Config
from generator.discovery.csp_generator import CspGenerator
from generator.discovery.gxdch_services import ComplianceService, NotaryService
from generator.discovery.gxdch_services import ComplianceService
from generator.discovery.openstack.openstack_discovery import \
OpenstackDiscovery

Expand Down Expand Up @@ -132,7 +132,7 @@ def create_vmso_vcs(conf: Config, cloud: str, csp_vcs: List[dict], timeout: int
@return: A list of Gaia-X Credentials describing given OpenStack cloud.
"""
csp = conf.get_value([const.CONFIG_CSP])
iaas = conf.get_value([const.CONFIG_IAAS]) # not yet used, as Gaia-X "abuses" id attribute of Verifiable Credentials
# iaas = conf.get_value([const.CONFIG_IAAS]) not yet used, as Gaia-X "abuses" id attribute of Verifiable Credentials
cred_settings = conf.get_value([const.CONFIG_CRED])

# init services
Expand Down
1 change: 0 additions & 1 deletion generator/common/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,3 @@
JWS_CONTEXT = "https://w3id.org/security/suites/jws-2020/v1"
REG_CONTEXT = "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#"
LRN_CONTEXT = "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/participant"

4 changes: 2 additions & 2 deletions generator/common/credentials.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
from typing import List

from generator.common import const, crypto
from generator.common import const


def convert_to_vp(creds: List[dict]) -> str:
vp = dict()
vp['@context'] = const.VP_CONTEXT
vp['type'] = "VerifiablePresentation"
vp['verifiableCredential'] = creds
return json.dumps(vp)
return json.dumps(vp)
10 changes: 5 additions & 5 deletions generator/common/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pyld import jsonld

"""
Methods to handle cryptography. Most source code is taken from Gaia-X Example
Methods to handle cryptography. Most source code is taken from Gaia-X Example
in https://gitlab.com/gaia-x/lab/workshops/gaia-x-101/-/tree/master
"""

Expand Down Expand Up @@ -49,9 +49,10 @@ def sign_cred(cred: dict, key: JWK, verification_method: str) -> dict:

# create JSON Web Signature for credential's hash
jwstoken = JWS(payload)
jwstoken.add_signature(key, None,
json_encode({"alg": "PS256", "b64": False, "crit": ["b64"]}),
json_encode({"kid": key.thumbprint()}))
jwstoken.add_signature(key,
None,
json_encode({"alg": "PS256", "b64": False, "crit": ["b64"]}),
json_encode({"kid": key.thumbprint()}))
sig = jwstoken.serialize(compact=True)

# add proof to credential as JsonWebSiganture2020, see https://w3c-ccg.github.io/lds-jws2020/
Expand All @@ -77,4 +78,3 @@ def load_jwk_from_file(path: str) -> JWK:
with open(path, "rb") as key_file:
bytes = key_file.read()
return JWK.from_pem(bytes, password=None)

6 changes: 3 additions & 3 deletions generator/discovery/csp_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _sign_legal_person(self, lrn_cred_id: str):
lp_vc['issuer'] = self.csp['did']
lp_vc['issuanceDate'] = str(datetime.now(tz=timezone.utc).isoformat())
lp_vc['credentialSubject'] = {
"id": self.cred_base_url + "/legal_person_cs.json", # I think "self.csp['did']" is correct, but Gaia-X expects link,
"id": self.cred_base_url + "/legal_person_cs.json", # I think "self.csp['did']" is correct, but Gaia-X expects link,
"type": "gx:LegalParticipant",
"gx:legalName": self.csp['legal-name'],
"gx:legalRegistrationNumber": {
Expand All @@ -127,5 +127,5 @@ def _sign_legal_person(self, lrn_cred_id: str):
}
}
return crypto.sign_cred(cred=lp_vc,
key=crypto.load_jwk_from_file(self.cred_settings[const.CONFIG_CRED_KEY]),
verification_method=self.cred_settings[const.CONFIG_CRED_VER_METH])
key=crypto.load_jwk_from_file(self.cred_settings[const.CONFIG_CRED_KEY]),
verification_method=self.cred_settings[const.CONFIG_CRED_VER_METH])
2 changes: 1 addition & 1 deletion generator/discovery/gxdch_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def request_reg_number_vc(self, csp: dict, cred_id: str, cred_subject_id: str) -
body = dict()
body['@context'] = const.LRN_CONTEXT
body['type'] = "gx:legalRegistrationNumber"
body['id'] = cred_subject_id #csp['did'] TODO: I think DID is correct here, but Gaia-X requires credential id, instead of credential subject id
body['id'] = cred_subject_id # csp['did'] TODO: I think DID is correct here, but Gaia-X requires credential id, instead of credential subject id
body['gx:vatID'] = csp[const.CONFIG_CSP_REG_NUMBER][const.CONFIG_CSP_VAT_ID]

resp = requests.post(self.api + "/registrationNumberVC?vcid=" + str(cred_id), json=body)
Expand Down
8 changes: 3 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

import yaml
from click.testing import CliRunner
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec, rsa
from cryptography.hazmat.primitives.serialization import Encoding
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from jwcrypto.jwt import JWK

from generator import cli
from generator.common import config, const, crypto
from generator.common import config, const
from generator.common.gx_schema import (DataAccountExport, TermsAndConditions,
VirtualMachineServiceOffering)
from tests.common import MockConnection, get_absolute_path
Expand Down Expand Up @@ -88,7 +87,6 @@ def test_init_connection(self, os_connect):
con = cli.init_openstack_connection("myCloud")
self.assertIsNotNone(con)


@patch("generator.discovery.csp_generator.CspGenerator.generate")
def test_csp(self, gen_csp):
gen_csp.return_value = {"vc": "bar"}
Expand Down
14 changes: 6 additions & 8 deletions tests/test_gxdch_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ def test_request_registration_number_vc(self, post_mock):

# run test
not_serv = NotaryService("https://exampple.com/gxdch/notary")
resp = not_serv.request_reg_number_vc(csp={"did": "did:web:example.com", "registration_numbers": {"vat-id": "DE123456789"}},
cred_subject_id=cred_subject_id, cred_id=cred_id)
resp = not_serv.request_reg_number_vc(
csp={"did": "did:web:example.com", "registration_numbers": {"vat-id": "DE123456789"}},
cred_subject_id=cred_subject_id, cred_id=cred_id)

# check results
self.assertEqual(
Expand All @@ -50,8 +51,9 @@ def test_request_registration_number_vc_exception(self, post_mock):

# run test
not_serv = NotaryService("https://exampple.com/gxdch/notary")
resp = not_serv.request_reg_number_vc(csp={"did": "did:web:example.com", "registration_numbers": {"vat-id": "DE123456789"}},
cred_subject_id="foo", cred_id="bar")
resp = not_serv.request_reg_number_vc(
csp={"did": "did:web:example.com", "registration_numbers": {"vat-id": "DE123456789"}},
cred_subject_id="foo", cred_id="bar")

# check results
self.assertIsNone(resp)
Expand Down Expand Up @@ -97,7 +99,6 @@ def test_get_gx_tandc(self, get_mock):
get_mock.called_with("https://example/gxdch/registry" + "/api/termsAndConditions")
self.assertEqual({'version': "22.10", "text": "foo"}, tandc)


@patch("requests.get")
def test_get_gx_tandc_exception(self, get_mock):
get_mock.return_value.ok = False
Expand All @@ -111,6 +112,3 @@ def test_get_gx_tandc_exception(self, get_mock):

if __name__ == "__main__":
unittest.main()



0 comments on commit d620598

Please sign in to comment.