
    Jh-              	          d dl Z d dlmZmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZ d d	lmZmZmZmZmZmZmZmZ  G d
 de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d dee      Z" G d de      Z#de$fdZ%dede"fdZ&dedee'ef   de'de"fdZ(dedede"fd Z) e
d!d"e)e#      Z*d$d%d&e"egg d'd(d)Z+y)*    N)AnyDictListOptional)systemutil)APIEndpoint)AdditionalInfo)VulnerabilityParserget_vulnerabilities)get_apt_cache_datetime)UAConfig)BoolDataValue
DataObjectDatetimeDataValueFieldFloatDataValueStringDataValue	data_dict	data_listc                   `    e Zd Z ededd       ededd      gZddddee   dee   fdZy	)
CVEsOptions	unfixableFzShow only unfixable CVES.docfixablezShow only fixable CVES.r   r   c                     || _         || _        y Nr   )selfr   r   s      E/usr/lib/python3/dist-packages/uaclient/api/u/pro/security/cves/v1.py__init__zCVEsOptions.__init__)   s     #    N)	__name__
__module____qualname__r   r   fieldsr   boolr"    r#   r!   r   r      s\    +		
 	)		
F$ %*"'	 D> $	r#   r   c                   ~    e Zd Z ededd       ededd       ededd       ed	ed
      gZdededed	efdZy)CVEAffectedPackagenameFzThe CVE namer   fix_versionz.The version that fixes the CVE for the package
fix_statusz)The status of the CVE fix for the package
fix_originz*The pocket where the fix is available fromc                 <    || _         || _        || _        || _        y r   r,   r-   r.   r/   )r    r,   r-   r.   r/   s        r!   r"   zCVEAffectedPackage.__init__N   s!     	&$$r#   Nr$   r%   r&   r   r   r'   strr"   r)   r#   r!   r+   r+   3   s    		
 	@		
 	;		
 	<	
'F4%%&)%7:%HK%r#   r+   c                   Z    e Zd Z eded       ed ee      d      gZdede	e   fdZ
y)AffectedPackagecurrent_versionz"The current version of the packager   cvesz The CVE that affects the packagec                     || _         || _        y r   r6   r7   )r    r6   r7   s      r!   r"   zAffectedPackage.__init__e   s      /	r#   N)r$   r%   r&   r   r   r   r+   r'   r3   r   r"   r)   r#   r!   r5   r5   W   sN    4	

 	()2	
F"%-12D-Er#   r5   c                   H    e Zd Z eded       eded      gZdedefdZy)
RelatedUSNr,   zThe USN namer   titlezThe USN titlec                      || _         || _        y r   r,   r<   )r    r,   r<   s      r!   r"   zRelatedUSN.__init__z   s    	
r#   Nr2   r)   r#   r!   r;   r;   l   s@    	

 		
FS  r#   r;   c                      e Zd Z eded       eded       eded       ed ee      d	d
       eded	d       eded	d      gZddddddde	de
j                  de	deee	      dee   dee	   deee      deee	      fdZy)CVEInfodescriptionzThe CVE descriptionr   published_atzThe CVE published datepriorityzThe ubuntu priority for the CVEnotesFzA list of notes for the CVE
cvss_scorezThe CVE cvss scorecvss_severityzThe CVE cvss severityN)rD   rE   rF   related_usnsrelated_packagesrG   rH   c                t    || _         || _        || _        || _        || _        || _        || _        || _        y r   rA   rB   rC   rD   rE   rF   rG   rH   )	r    rA   rB   rC   rD   rE   rF   rG   rH   s	            r!   r"   zCVEInfo.__init__   sC     '( 
$* ) 0r#   )r$   r%   r&   r   r   r   r   r   r'   r3   datetimer   r   floatr;   r"   r)   r#   r!   r@   r@      s   %	

 	(	

 	1	

 	o&-		
 	$		
 	'		
9"FT &*&*'+37041 1 ''	1
 1 S	"1 UO1  }1 tJ/01 #49-1r#   r@   c            
           e Zd Z ed ee      d       ed ee      d      gZdddee	ef   dee	ef   d	e
j                  d
ee
j                     fdZy)
CVEsResultpackages)	value_clszcA dictionary where the keys are installed package names and the values are AffectedPackage objects.r   r7   zMA dictionary where the keys are CVE names and the values are CVEInfo objects.N)apt_updated_atvulnerability_data_published_atrQ   c                <    || _         || _        || _        || _        y r   rO   r7   rR   rQ   )r    rO   r7   rR   rQ   s        r!   r"   zCVEsResult.__init__   s"     !	/N,,r#   )r$   r%   r&   r   r   r5   r@   r'   r   r3   rK   r   r"   r)   r#   r!   rN   rN      s    0u	

 	(_	
F, 7;- sO+,- 3< 	- *2):):- !!2!23-r#   rN   c                   f    e Zd ZdZdeeef   deeef   fdZdeeef   deeef   deeef   fdZy)		CVEParserr7   affected_pkgreturnc                 :    |j                  | j                  i       S r   )getvulnerability_type)r    rW   s     r!   get_package_vulnerabilitiesz%CVEParser.get_package_vulnerabilities   s      7 7<<r#   vulnerability_infovulnerabilities_datac                     |j                  d      rgg }|j                  di       j                  di       }|d   D ]6  }|j                  ||j                  |i       j                  dd      d       8 ||d<   |S )NrG   security_issuesusnsr<    r>   )rZ   append)r    r]   r^   rG   usn_inforelated_usns         r!    _post_process_vulnerability_infoz*CVEParser._post_process_vulnerability_info   s    
 !!.1L+//0A2FJJH  2.A ## +!)k2!>!B!B#R" 2>~.!!r#   N)	r$   r%   r&   r[   r   r3   r   r\   rf   r)   r#   r!   rV   rV      sf    = cN=	c3h=
" cN" #38n" 
c3h	"r#   rV   rX   c                     | j                  d      xr | j                  d      }|j                  r|ry|j                  r|syy)Nr-   r/   FT)rZ   r   r   )cveoptions
is_fixables      r!   cve_status_match_optionsrk      s;    'ACGGL,AJZ	r#   ri   c                 *    t        | t                     S r   )_cvesr   )ri   s    r!   r7   r7   	  s     (*%%r#   vulnerabilitiesrR   c                    i }t               }t        |j                  di       j                               D ]  \  }}g }t        |j                  dg       d       D ]M  }t	        ||       s|j                  t        |d   |d   |d   |d   	             |j                  |d          O |syt        |d
   |      ||<    t        |j                  di       j                         d       D 	
ci c]  \  }	}|	|v r|	t        |d   t        j                  |d         |d   |d   |d   |d   |j                  dg       D 
cg c]/  }
t        |
j                  dd      |
j                  dd            1 c}
|j                  dg              }}}	}
t        ||t        j                  |      t                     S c c}
w c c}
}}	w )NrO   r7   c                     | d   S )Nr,   r)   )rh   s    r!   <lambda>z(_parse_vulnerabilities.<locals>.<lambda>  s
    #f+ r#   )keyr,   r-   r.   r/   r1   r6   r9   rn   c                     | d   S )Nr   r)   )vs    r!   rq   z(_parse_vulnerabilities.<locals>.<lambda>A  s
    !A$ r#   rA   rB   ubuntu_priorityrD   rE   rF   rG   rb   r<   r>   rH   rJ   rT   )setsortedrZ   itemsrk   rc   r+   addr5   r@   r   parse_rfc3339_dater;   rN   r   )ri   rn   rR   rO   allowed_cvespkg_namepackage_infopkg_cvesrh   cve_namere   r7   s               r!   _parse_vulnerabilitiesr     s   
 H5L"(J+113# , VR(.E
 	.C (W5& [$'$6#&|#4#&|#4	   V-	. !0 ,-> ?"HX'R $ 126<<>
# " Hc |#) 	'M*00^1DE*+g,<(o. $'77>2#>
  	 $4%//'26 !WW%7<
 	
D 0 (,(?(?+)
 ./ !s   4AG4G 9GGcfgc                     | j                   r| j                  rd| _         d| _        t        j                         j                  }t        t               ||      }|j                  }t        | ||j                        S )z
    This endpoint shows the CVE vulnerabilites in the system.
    By default, this API will show all CVEs that affect the system.
    F)parserr   series)ri   rn   rR   )
r   r   r   get_release_infor   r   rV   vulnerabilities_infor   rR   )ri   r   r   cve_vulnerabilities_resultcve_vulnerabilitiess        r!   rm   rm   P  sv     W__!$$&--F!4{" 5II!+(B(b(b r#   v1CVEs)versionr,   fnoptions_cls35Tzs
from uaclient.api.u.pro.security.cves.v1 import cves, CVEsOptions

options = CVEsOptions()
result = cves(options)
zpro api u.pro.security.cves.v1a  
{
    "cves": {
      "CVE-2023-5678": {
        "cvss_score": 8.1,
        "cvss_severity": "high",
        "description": "description example",
        "notes": [
          "note example",
        ],
        "priority": "medium",
        "published_at": ".*"
      }
    },
    "packages": {
      "accountsservice": {
        "current_version": "0.6.40-2ubuntu11.6",
        "cves": [
          {
            "fix_origin": "esm-infra",
            "fix_status": "fixed",
            "fix_version": "0.6.40-2ubuntu11.6+esm1",
            "name": "CVE-2023-5678"
          }
        ]
      },
      "libaccountsservice0": {
        "current_version": "0.6.40-2ubuntu11.6",
        "cves": [
          {
            "fix_origin": "esm-infra",
            "fix_status": "fixed",
            "fix_version": "0.6.40-2ubuntu11.6+esm1",
            "name": "CVE-2023-5678"
          }
        ]
      }
    },
}
)introduced_inrequires_networkexample_pythonresult_classignore_result_classes
exceptionsexample_cliexample_json),rK   typingr   r   r   r   uaclientr   r   uaclient.api.apir	   uaclient.api.data_typesr
   +uaclient.api.u.pro.security.cves._common.v1r   r   uaclient.aptr   uaclient.configr   uaclient.data_typesr   r   r   r   r   r   r   r   r   r+   r5   r;   r@   rN   rV   r(   rk   r7   r3   r   rm   endpoint_docr)   r#   r!   <module>r      sA    , , ! ( 2 0 $	 	 	* 4!% !%Hj * &;1j ;1|-^ -> "#  "Fd &&&>>#s(^> &)> 	>B	 @ 		  (\3'5r#   