From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 67BBB42D5F for ; Mon, 26 Jun 2023 21:00:22 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 594D040223; Mon, 26 Jun 2023 21:00:22 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 619714013F for ; Mon, 26 Jun 2023 21:00:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687806020; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rjuVp3hvU54R+yys2knIjd3eoI8OMGViGubyG446B9Y=; b=cTQWbkRTBwSybDESy3wSprojRWoJfSgoM//YqYcZH3K4e7TzJxr7q1vNDPlzlQhr61Mv+y WKX/7/+RtrexS+p/gL+mJmOoiQR2R+Zq2IhpuwpUn0jn8/bkk/Vs5zgCTAEpbqvL22QKJD VzL483Jmw3p+jIsSTcl9CzZHxpzhV5U= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-503-Cc0mViY1OUm9lhtQIuv7ww-1; Mon, 26 Jun 2023 15:00:17 -0400 X-MC-Unique: Cc0mViY1OUm9lhtQIuv7ww-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2308B1C05143; Mon, 26 Jun 2023 19:00:15 +0000 (UTC) Received: from RHTPC1VM0NT (unknown [10.22.32.232]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E98D014682FA; Mon, 26 Jun 2023 19:00:14 +0000 (UTC) From: Aaron Conole To: Jeremy Spewock Cc: ci@dpdk.org Subject: Re: [PATCH v1] tools: add jwt renewal function to acvp_tool References: <20230621195342.5035-2-jspewock@iol.unh.edu> Date: Mon, 26 Jun 2023 15:00:14 -0400 In-Reply-To: (Jeremy Spewock's message of "Fri, 23 Jun 2023 17:23:15 -0400") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: ci@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK CI discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ci-bounces@dpdk.org Jeremy Spewock writes: > On Fri, Jun 23, 2023 at 10:15=E2=80=AFAM Aaron Conole wrote: > > jspewock@iol.unh.edu writes: > > > From: Jeremy Spewock > > > > Adds a method that follows the process for renewing your jwt according > > to NIST API documentation. This way, if there are load issues and it > > takes too long to get vectors back with multi-algorithm testing, you c= an > > still get restuls and the script will not error. > > > > Also, added a maximum number of renewals so that the script cannot run > > infinitely. > > > > Signed-off-by: Jeremy Spewock > > --- > > Just some nits - I haven't run this through black / flake8. > > > tools/acvp/acvp_tool.py | 73 +++++++++++++++++++++++++++++++++++-----= - > > 1 file changed, 63 insertions(+), 10 deletions(-) > > > > diff --git a/tools/acvp/acvp_tool.py b/tools/acvp/acvp_tool.py > > index 40d2f2f..8d50a58 100755 > > --- a/tools/acvp/acvp_tool.py > > +++ b/tools/acvp/acvp_tool.py > > @@ -1,7 +1,7 @@ > > #!/usr/bin/env python3 > > =20 > > # SPDX-License-Identifier: BSD-3-Clause > > -# Copyright 2022 The University of New Hampshire > > +# Copyright 2023 The University of New Hampshire > > =20 > > import hashlib > > import sys > > @@ -36,6 +36,8 @@ class ACVPProxy: > > self.totp_path: str =3D totp_path > > self.login_data: Optional[Dict[str, Any]] =3D None > > self.session_data: Optional[Dict[str, Any]] =3D None > > + self.retries: int =3D 0 > > + self.max_retries: int =3D 2 > > =20 > > with open(config_path, 'r') as f: > > self.config: Any =3D json.load(f) > > @@ -70,7 +72,13 @@ class ACVPProxy: > > cert=3Dself.cert, > > headers=3D{'Authorization': f'Bearer {token}'} > > ) > > - if not response.ok: > > + if response.status_code =3D=3D 401: > > + if self.__renew_jwt(): > > + token =3D self.session_data['jwt'] > > + continue > > + logging.error("Failed to renew expired jwt") > > + return None > > + elif not response.ok: > > logging.error(f'Failed to fetch vector set {url}') > > logging.error(json.dumps(response.json(), indent=3D4)= ) > > return None > > @@ -85,6 +93,35 @@ class ACVPProxy: > > logging.info(f'Downloaded vector set {url}') > > return vector_set_json > > =20 > > + def __renew_jwt(self) -> bool: > > + """Renews the jwt in session_data. > > + > > + JWTs provided by the NIST API last 30 minutes which can cause= this > > + script to fail even with good data. This method renews the jw= t using > > + the login endpoint. > > + > > + @return: True if successfully renewed token > > + """ > > + if self.retries >=3D self.max_retries: > > + logging.error("Maximum number of jwt renewals has been re= ached.") > > + return False > > + response =3D requests.post( > > + url=3Df'{self.config["url"]}/acvp/v1/login', > > + json=3D[ > > + {'acvVersion': '1.0'}, > > + { > > + 'password': self.__get_totp(), > > + 'accessToken': self.session_data["jwt"] > > + } > > + ], > > + cert=3Dself.cert, > > + ) > > + if response.ok: > > + self.retries +=3D 1 > > + self.session_data["jwt"] =3D response.json()[1].pop("acce= ssToken") > > + return True > > + return False > > + > > def login(self) -> bool: > > """Log into the API server. > > =20 > > @@ -141,7 +178,6 @@ class ACVPProxy: > > cert=3Dself.cert, > > headers=3D{'Authorization': f'Bearer {self.login_data["jw= t"]}'} > > ) > > - > > Why this line changed? > > I guess this line just ended up getting removed when I was making edits. = I didn't do it for any deliberate > reason other than spacing when I was making edits. > =20 > =20 > > if not response.ok: > > logging.error('Unable to register.') > > logging.error(json.dumps(response.json(), indent=3D4)) > > @@ -178,6 +214,12 @@ class ACVPProxy: > > 'Authorization': f'Bearer {self.session_data[= "jwt"]}' > > } > > ) > > + if result.status_code =3D=3D 401: > > + if self.__renew_jwt(): > > + write_data[0]["jwt"] =3D self.session_data["j= wt"] > > + continue > > + logging.error("Failed to renew jwt") > > + return None > > version, result_json =3D result.json() > > if 'retry' in result_json: > > duration =3D result_json['retry'] > > @@ -207,8 +249,19 @@ class ACVPProxy: > > cert=3Dself.cert, > > headers=3D{'Authorization': f'Bearer {self.session_da= ta["jwt"]}'} > > ) > > - > > - if not response.ok: > > You also dropped a line spacing here as well. > > I think this is the same thing as above, when I was testing and writing t= hings in a couple of places this line > also disappeared just because I felt it wasn't needed, but there wasn't a= ny particular reason. > =20 > =20 > > + if response.status_code =3D=3D 401: > > + if self.__renew_jwt(): > > + response =3D requests.post( > > + f'{self.config["url"]}{session_url}/vectorSet= s/' > > + f'{vector_set["vsId"]}/results', > > + json=3D[version, vector_set], > > + cert=3Dself.cert, > > + headers=3D{'Authorization': f'Bearer {self.se= ssion_data["jwt"]}'} > > + ) > > + else: > > + logging.error("Failed to renew jwt") > > + return False > > + elif not response.ok: > > has_error =3D True > > logging.error(f'Could not upload vector set response = for ' > > f'vector set ID {vector_set["vsId"]}.') > > @@ -239,13 +292,13 @@ def main(request_path: Optional[str], > > config_path=3Dconfig_path, > > ) > > =20 > > - logging.info('Attempting to log in...') > > - if not proxy.login(): > > - logging.error('Could not log in.') > > - sys.exit(1) > > - logging.info('Successfully logged in.') > > =20 > > if request_path: > > + logging.info('Attempting to log in...') > > + if not proxy.login(): > > + logging.error('Could not log in.') > > + sys.exit(1) > > + logging.info('Successfully logged in.') > > logging.info('Creating a new test session and downloading vec= tors...') > > test_session =3D proxy.register() > > if not test_session: > > Would it be preferred that I add these lines back so that they closer ali= gn with the original script? Please do, yes. Try to keep whitespace changes to a minimum. It gets to be difficult sometimes to review. > Thanks, > Jeremy