From 6fec9e9ec55af9c62ca03558766806710d4da0b6 Mon Sep 17 00:00:00 2001 From: Carmen Rata Date: Thu, 16 Jul 2020 15:08:50 -0400 Subject: [PATCH] Verify-license plugin support Current StarlingX implementation does not support a proper license validation. Used the Stevedore plugin pattern to allow override of the verify-license implementation to add enhanced validation rules if necessary. Story: 2007403 Task: 39644 Change-Id: I6fa6626feabff06b832dd41a2778804a28956131 Signed-off-by: Carmen Rata --- .../platform_util/license/base.py | 24 +++++++++ .../platform_util/license/constants.py | 6 ++- .../platform_util/license/license.py | 52 +++++++++++++++---- .../platform_util/license/stxlicense.py | 22 ++++++++ .../platform-util/platform-util/setup.py | 5 +- 5 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 utilities/platform-util/platform-util/platform_util/license/base.py create mode 100644 utilities/platform-util/platform-util/platform_util/license/stxlicense.py diff --git a/utilities/platform-util/platform-util/platform_util/license/base.py b/utilities/platform-util/platform-util/platform_util/license/base.py new file mode 100644 index 00000000..0b9fcecd --- /dev/null +++ b/utilities/platform-util/platform-util/platform_util/license/base.py @@ -0,0 +1,24 @@ +# +# Copyright (c) 2020 Wind River Systems, Inc. +# +# SPDX-License-Identifier: Apache-2.0 +# + +import abc +import six + + +@six.add_metaclass(abc.ABCMeta) +class BaseLicense(object): + """Base class for license validation operations. + """ + + @abc.abstractmethod + def verify_license(self, *args): + """Validate license file + + :param : variable number of parameters + """ + + def __init__(self, operator): + self._operator = operator diff --git a/utilities/platform-util/platform-util/platform_util/license/constants.py b/utilities/platform-util/platform-util/platform_util/license/constants.py index 621a0f08..8624c194 100644 --- a/utilities/platform-util/platform-util/platform_util/license/constants.py +++ b/utilities/platform-util/platform-util/platform_util/license/constants.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2017 Wind River Systems, Inc. +# Copyright (c) 2017-2020 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -28,7 +28,9 @@ LICENSE_NAMES = [ AIO_SX_PRODUCT_CFG ] -# License mapping +LICENSE_PLUGIN_NAME = 'verify_license' + +# License mapping LICENSE_MAP = { LICENSE_FEATURE_STD: STD_PRODUCT_CFG, LICENSE_FEATURE_AIO: AIO_PRODUCT_CFG, diff --git a/utilities/platform-util/platform-util/platform_util/license/license.py b/utilities/platform-util/platform-util/platform_util/license/license.py index 70dd2fbd..e6b584a6 100644 --- a/utilities/platform-util/platform-util/platform_util/license/license.py +++ b/utilities/platform-util/platform-util/platform_util/license/license.py @@ -3,33 +3,59 @@ # # SPDX-License-Identifier: Apache-2.0 # -import os -from platform_util.license import exception + import sys +from stevedore import extension + +from platform_util.license import constants +from platform_util.license import exception + + +def suppress_stevedore_errors(manager, entrypoint, exception): + """stevedore.ExtensionManager + will try to import the entry point defined in the module. + License plugins use virtual modules. + So ExtensionManager will throw the "Could not load ..." + error message, which is expected. + Just suppress this error message to avoid cause confusion. + """ + pass + def verify_license(*args): - """Verify the license file""" - if not os.path.isfile(args[0]): - raise exception.LicenseNotFound() + """Verify license using plugin""" + + license_plugins = extension.ExtensionManager( + namespace='platformutil.license_plugins', + invoke_on_load=True, + invoke_args=(None,), + on_load_failure_callback=suppress_stevedore_errors + ) + license_plugins = sorted(license_plugins, key=lambda x: x.name) + + plugin_name = constants.LICENSE_PLUGIN_NAME + plugin_obj = None + for license_plugin in license_plugins: + plugin_obj = license_plugin.obj + if plugin_name != license_plugin.name[4:]: + break + if plugin_obj is not None: + plugin_obj.verify_license(*args) def main(): - # Pass the command arguments to verify_license + # Check minimum number of arguments if len(sys.argv) < 2: print("Usage: verify-license [...]") exit(-1) - arg_list = [] - for arg in sys.argv: - arg_list.append(arg) - # The arguments passed to verify_license from command line # will not include sys.argv[0] which is the script name. # Only the actual arguments: sys.argv[1] and onward will be passed, # meaning license_file followed by optional attributes. try: - verify_license(*arg_list[1:len(sys.argv)]) + verify_license(*sys.argv[1:len(sys.argv)]) except exception.InvalidLicenseType: exit(1) except exception.LicenseNotFound: @@ -40,3 +66,7 @@ def main(): exit(4) except exception.InvalidLicense: exit(5) + + +if __name__ == '__main__': + main() diff --git a/utilities/platform-util/platform-util/platform_util/license/stxlicense.py b/utilities/platform-util/platform-util/platform_util/license/stxlicense.py new file mode 100644 index 00000000..978b6896 --- /dev/null +++ b/utilities/platform-util/platform-util/platform_util/license/stxlicense.py @@ -0,0 +1,22 @@ +# Copyright (c) 2020 Wind River Systems, Inc. +# +# SPDX-License-Identifier: Apache-2.0 +# + +import os + +from platform_util.license import base +from platform_util.license import exception + + +class StxVerifyLicense(base.BaseLicense): + """Class to encapsulate license verification for starlingX """ + + def verify_license(self, *args): + """Verify the license file""" + + if len(args) < 1: + raise Exception("Usage: verify-license [...]") + + if not os.path.isfile(args[0]): + raise exception.LicenseNotFound() diff --git a/utilities/platform-util/platform-util/setup.py b/utilities/platform-util/platform-util/setup.py index a02cf139..09609c5e 100644 --- a/utilities/platform-util/platform-util/setup.py +++ b/utilities/platform-util/platform-util/setup.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2017-2018 Wind River Systems, Inc. +# Copyright (c) 2017-2020 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -16,5 +16,8 @@ setuptools.setup( 'console_scripts': [ 'verify-license = platform_util.license.license:main' ], + 'platformutil.license_plugins': [ + '001_verify_license = platform_util.license.stxlicense:StxVerifyLicense' + ], } )