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 <carmen.rata@windriver.com>
This commit is contained in:
parent
71314e4af1
commit
6fec9e9ec5
@ -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
|
@ -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,
|
||||
|
@ -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 <license file> [<optional_parameter>...]")
|
||||
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()
|
||||
|
@ -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 <license file> [<optional_parameter>...]")
|
||||
|
||||
if not os.path.isfile(args[0]):
|
||||
raise exception.LicenseNotFound()
|
@ -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'
|
||||
],
|
||||
}
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user