blob: 077e7a4004993dbd89cbf1939b875135ea81a1dd [file] [log] [blame]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01001#!/usr/bin/env python3 -u
2
3""" lava_helper.py:
4
Xinyu Zhangc918b6e2022-10-08 17:13:17 +08005 Provide function to validate LAVA token/credentials. """
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01006
7from __future__ import print_function
8
9__copyright__ = """
10/*
Xinyu Zhangc918b6e2022-10-08 17:13:17 +080011 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010012 *
13 * SPDX-License-Identifier: BSD-3-Clause
14 *
15 */
16 """
Karl Zhang08681e62020-10-30 13:56:03 +080017
18__author__ = "tf-m@lists.trustedfirmware.org"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010019__project__ = "Trusted Firmware-M Open CI"
Xinyu Zhang06286a92021-07-22 14:00:51 +080020__version__ = "1.4.0"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010021
22import os
23import sys
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010024
25try:
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010026 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
27except ImportError:
28 dir_path = os.path.dirname(os.path.realpath(__file__))
29 sys.path.append(os.path.join(dir_path, "../"))
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010030 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
31
32
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010033def test_lava_dispatch_credentials(user_args):
34 """ Will validate if provided token/credentials are valid. It will return
35 a valid connection or exit program if not"""
36
37 # Collect the authentication tokens
38 try:
39 if user_args.token_from_env:
40 usr = os.environ['LAVA_USER']
41 secret = os.environ['LAVA_TOKEN']
Xinyu Zhang33633322021-05-20 09:08:57 +080042 elif user_args.lava_user and user_args.lava_token:
43 usr = user_args.lava_user
44 secret = user_args.lava_token
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010045
46 # Do not submit job without complete credentials
47 if not len(usr) or not len(secret):
48 raise Exception("Credentials not set")
49
50 lava = LAVA_RPC_connector(usr,
51 secret,
Xinyu Zhangc918b6e2022-10-08 17:13:17 +080052 user_args.lava_url)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010053
54 # Test the credentials againist the backend
55 if not lava.test_credentials():
56 raise Exception("Server rejected user authentication")
57 except Exception as e:
58 print("Credential validation failed with : %s" % e)
Xinyu Zhang33633322021-05-20 09:08:57 +080059 print("Did you set set --lava_user, --lava_token?")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010060 sys.exit(1)
61 return lava