@@ -27,43 +27,43 @@ class CryptographyConfig:
2727 with_range : bool = False
2828
2929 def __post_init__ (self ):
30- # If with_range is True, purls must contain "@<version>"
31- if self .with_range :
32- if self .purl :
30+ """
31+ Validate that the configuration is valid.
32+ """
33+ if self .purl :
34+ if self .with_range :
3335 for purl in self .purl :
3436 parts = purl .split ('@' )
3537 if not (len (parts ) >= MIN_SPLIT_PARTS and parts [1 ]):
36- raise ValueError (
37- f'Invalid PURL format: "{ purl } ".'
38- f'It must include a version (e.g., pkg:type/name@version)'
38+ raise ScanossCryptographyError (
39+ f'Invalid PURL format: "{ purl } ".' f'It must include a version (e.g., pkg:type/name@version)'
3940 )
4041 if self .input_file :
4142 input_file_validation = validate_json_file (self .input_file )
4243 if not input_file_validation .is_valid :
43- raise Exception (
44+ raise ScanossCryptographyError (
4445 f'There was a problem with the purl input file. { input_file_validation .error } '
4546 )
46-
47- # Validate the input file is in PurlRequest format
4847 if (
4948 not isinstance (input_file_validation .data , dict )
5049 or 'purls' not in input_file_validation .data
5150 or not isinstance (input_file_validation .data ['purls' ], list )
52- or not all (
53- isinstance (p , dict ) and 'purl' in p
54- for p in input_file_validation .data ['purls' ]
55- )
51+ or not all (isinstance (p , dict ) and 'purl' in p for p in input_file_validation .data ['purls' ])
5652 ):
57- raise ValueError (
58- 'The supplied input file is not in the correct PurlRequest format.'
59- )
53+ raise ScanossCryptographyError ( 'The supplied input file is not in the correct PurlRequest format.' )
54+ purls = input_file_validation . data [ 'purls' ]
55+ purls_with_requirement = []
6056 if self .with_range :
61- purls = input_file_validation .data ['purls' ]
6257 if any ('requirement' not in p for p in purls ):
63- raise ValueError (
64- 'One or more PURLs are missing the "requirement" field.'
58+ raise ScanossCryptographyError (
59+ f 'One or more PURLs in " { self . input_file } " are missing the "requirement" field.'
6560 )
66- return input_file_validation .data
61+ else :
62+ for purl in purls :
63+ purls_with_requirement .append (f'{ purl ["purl" ]} @{ purl ["requirement" ]} ' )
64+ else :
65+ purls_with_requirement = purls
66+ self .purl = purls_with_requirement
6767
6868
6969def create_cryptography_config_from_args (args ) -> CryptographyConfig :
@@ -124,20 +124,14 @@ def get_algorithms(self) -> Optional[Dict]:
124124 """
125125
126126 if not self .purls_request :
127- raise ScanossCryptographyError (
128- 'No PURLs supplied. Provide --purl or --input.'
129- )
127+ raise ScanossCryptographyError ('No PURLs supplied. Provide --purl or --input.' )
130128 self .base .print_stderr (
131129 f'Getting cryptographic algorithms for { ", " .join ([p ["purl" ] for p in self .purls_request ["purls" ]])} '
132130 )
133131 if self .config .with_range :
134- response = self .client .get_crypto_algorithms_in_range_for_purl (
135- self .purls_request
136- )
132+ response = self .client .get_crypto_algorithms_in_range_for_purl (self .purls_request )
137133 else :
138- response = self .client .get_crypto_algorithms_for_purl (
139- self .purls_request
140- )
134+ response = self .client .get_crypto_algorithms_for_purl (self .purls_request )
141135 if response :
142136 self .results = response
143137
@@ -152,22 +146,16 @@ def get_encryption_hints(self) -> Optional[Dict]:
152146 """
153147
154148 if not self .purls_request :
155- raise ScanossCryptographyError (
156- 'No PURLs supplied. Provide --purl or --input.'
157- )
149+ raise ScanossCryptographyError ('No PURLs supplied. Provide --purl or --input.' )
158150 self .base .print_stderr (
159151 f'Getting encryption hints '
160152 f'{ "in range" if self .config .with_range else "" } '
161153 f'for { ", " .join ([p ["purl" ] for p in self .purls_request ["purls" ]])} '
162154 )
163155 if self .config .with_range :
164- response = self .client .get_encryption_hints_in_range_for_purl (
165- self .purls_request
166- )
156+ response = self .client .get_encryption_hints_in_range_for_purl (self .purls_request )
167157 else :
168- response = self .client .get_encryption_hints_for_purl (
169- self .purls_request
170- )
158+ response = self .client .get_encryption_hints_for_purl (self .purls_request )
171159 if response :
172160 self .results = response
173161
@@ -182,17 +170,13 @@ def get_versions_in_range(self) -> Optional[Dict]:
182170 """
183171
184172 if not self .purls_request :
185- raise ScanossCryptographyError (
186- 'No PURLs supplied. Provide --purl or --input.'
187- )
173+ raise ScanossCryptographyError ('No PURLs supplied. Provide --purl or --input.' )
188174
189175 self .base .print_stderr (
190176 f'Getting versions in range for { ", " .join ([p ["purl" ] for p in self .purls_request ["purls" ]])} '
191177 )
192178
193- response = self .client .get_versions_in_range_for_purl (
194- self .purls_request
195- )
179+ response = self .client .get_versions_in_range_for_purl (self .purls_request )
196180 if response :
197181 self .results = response
198182
@@ -211,25 +195,15 @@ def _build_purls_request(
211195 Returns:
212196 Optional[dict]: The dictionary containing the PURLs
213197 """
214- if self .config .input_file :
215- input_file_validation = validate_json_file (self .config .input_file )
216- if not input_file_validation .is_valid :
217- raise Exception (
218- f'There was a problem with the purl input file. { input_file_validation .error } '
219- )
220-
221- return input_file_validation .data
222- if self .config .purl :
223- return {
224- 'purls' : [
225- {
226- 'purl' : p ,
227- 'requirement' : self ._extract_version_from_purl (p ),
228- }
229- for p in self .config .purl
230- ]
231- }
232- return None
198+ return {
199+ 'purls' : [
200+ {
201+ 'purl' : p ,
202+ 'requirement' : self ._extract_version_from_purl (p ),
203+ }
204+ for p in self .config .purl
205+ ]
206+ }
233207
234208 def _extract_version_from_purl (self , purl : str ) -> str :
235209 """
@@ -242,22 +216,20 @@ def _extract_version_from_purl(self, purl: str) -> str:
242216 str: The extracted version
243217
244218 Raises:
245- ValueError : If the purl is not in the correct format
219+ ScanossCryptographyError : If the purl is not in the correct format
246220 """
247221 try :
248222 return purl .split ('@' )[- 1 ]
249223 except IndexError :
250- raise ValueError (f'Invalid purl format: { purl } ' )
224+ raise ScanossCryptographyError (f'Invalid purl format: { purl } ' )
251225
252226 def present (
253227 self ,
254228 output_format : Optional [str ] = None ,
255229 output_file : Optional [str ] = None ,
256230 ):
257231 """Present the results in the selected format"""
258- self .presenter .present (
259- output_format = output_format , output_file = output_file
260- )
232+ self .presenter .present (output_format = output_format , output_file = output_file )
261233
262234
263235class CryptographyPresenter (AbstractPresenter ):
0 commit comments