diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..efa6f16 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,21 @@ +version: 2 +jobs: + build: + working_directory: ~/PiAP-python-tools + docker: + - image: circleci/python:3.6.1 + environment: + CI: cicleci + POSTGRES_DB: circle_test + steps: + - checkout + - run: + command: | + make clean + - run: + command: | + make test + - run: + command: | + make clean +destination: tr1 diff --git a/.coveragerc b/.coveragerc index 7423488..9b946b3 100644 --- a/.coveragerc +++ b/.coveragerc @@ -9,11 +9,12 @@ exclude_lines = pragma: no cover except Exception + except BaseException: # Don't complain if tests don't hit defensive assertion code: raise AssertionError raise NotImplementedError raise ImportError - except unittest.SkipTest as skiperr + except unittest.SkipTest except IOError except OSError diff --git a/.stickler.yml b/.stickler.yml new file mode 100644 index 0000000..11bff83 --- /dev/null +++ b/.stickler.yml @@ -0,0 +1,10 @@ +linters: + shellcheck: + shell: bash + flake8: + max-line-length: 100 + max-complexity: 10 + ignore: 'W191,W391' + exclude: ['.git', '__pycache__', 'docs', '.tox', 'build'] +files: + ignore: ['*/*.pyc', '*.pyc', '*~', '.git', '__pycache__'] diff --git a/Makefile b/Makefile index f12ad18..7eaadb6 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ test-tox: cleanup $(QUIET)$(ECHO) "$@: Done." test-style: cleanup - $(QUIET)flake8 --ignore=W191,W391 --max-line-length=100 --verbose --count + $(QUIET)flake8 --ignore=W191,W391 --max-line-length=100 --verbose --count --config=.flake8.ini $(QUIET)$(ECHO) "$@: Done." cleanup: @@ -133,6 +133,9 @@ cleanup: $(QUIET)rm -f ./the_test_file*.json 2>/dev/null || true $(QUIET)rm -f ./the_test_file*.yml 2>/dev/null || true $(QUIET)rm -f ./the_test_file*.yaml 2>/dev/null || true + $(QUIET)rm -f ./the_test_file*.enc 2>/dev/null || true + $(QUIET)rm -f ./.weak_test_key_* || true + $(QUIET)rm -f ./test.secret || true $(QUIET)rm -f ./the_test_url_file*.txt 2>/dev/null || true $(QUIET)rm -f /tmp/.beta_PiAP_weak_key 2>/dev/null || true diff --git a/docs/conf.py b/docs/conf.py index 86fb0e2..8bdf9ff 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -41,7 +41,7 @@ master_doc = 'index' # General information about the project. -project = u'restart_service_handler' +project = u'Pocket_PiAP_Python_tools' copyright = u'2017, reactive-firewall' # The version info for the project you're documenting, acts as replacement for @@ -51,7 +51,7 @@ # The short X.Y version. version = 'v0.2' # The full version, including alpha/beta/rc tags. -release = 'v0.2.4' +release = 'v0.2.7' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/piaplib/__init__.py b/piaplib/__init__.py index acb94f2..1ac0c9a 100644 --- a/piaplib/__init__.py +++ b/piaplib/__init__.py @@ -16,7 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = """0.2.5""" +__version__ = """0.2.7""" try: import sys diff --git a/piaplib/keyring/clearify.py b/piaplib/keyring/clearify.py index 22101e0..06e21a8 100644 --- a/piaplib/keyring/clearify.py +++ b/piaplib/keyring/clearify.py @@ -52,6 +52,13 @@ except Exception: import pku.remediation as remediation +try: + from remediation import PiAPError as PiAPError +except Exception: + try: + from piaplib.pku.remediation import PiAPError as PiAPError + except Exception: + raise ImportError("Error Importing PiAPError") try: from ..pku import utils as utils @@ -242,6 +249,46 @@ def unpackFromRest(ciphertext=None, keyStore=None): raise NotImplementedError("No Implemented Backend - BUG") +@remediation.error_handling +def unpackFromFile(somefile, keyStore=None): + """Reads the raw encrypted file and decrypts it.""" + read_data = None + try: + someFilePath = utils.addExtension(somefile, str('enc')) + with utils.open_func(someFilePath, mode=u'r', encoding=u'utf-8') as enc_data_file: + read_enc_data = enc_data_file.read() + read_data = unpackFromRest(read_enc_data, keyStore) + except Exception as clearerr: + read_data = None + baton = PiAPError(clearerr, str("Failed to load or deycrypt file.")) + clearerr = None + del clearerr + raise baton + return read_data + + +@remediation.error_handling +def packToFile(somefile, data, keyStore=None): + """Writes the raw encrypted file.""" + if data is None: + return False + if somefile is None: + return False + did_write = False + try: + someFilePath = utils.literal_code(utils.addExtension(str(somefile), str("enc"))) + if someFilePath is not None: + encData = packForRest(data, keyStore) + with utils.open_func(file=someFilePath, mode=u'wb+') as enc_data_file: + utils.write_func(enc_data_file, utils.literal_str(encData).encode("utf-8")) + del(encData) + did_write = True + except Exception as clearerr: + raise remediation.PiAPError(clearerr, str("Failed to write or encrypt file.")) + did_write = False + return did_write + + WEAK_ACTIONS = {u'pack': packForRest, u'unpack': unpackFromRest} """ The Pocket bag Unit actions. pack - save/pack/pickle functions. diff --git a/piaplib/lint/clients_check_status.py b/piaplib/lint/clients_check_status.py index 1b35076..2886eb3 100755 --- a/piaplib/lint/clients_check_status.py +++ b/piaplib/lint/clients_check_status.py @@ -350,8 +350,8 @@ def get_client_list(lan_interface=None): return theResult -@remediation.error_handling -def get_client_status(client=None, use_html=False, lan_interface=None): +@remediation.error_handling # noqa C901 +def get_client_status(client=None, use_html=False, lan_interface=None): # noqa C901 """Generate the status""" theResult = None try: @@ -456,6 +456,23 @@ def get_client_ip(client=None, use_html=False, lan_interface=None): return theResult +@remediation.error_handling +def showAllClients(verbose_mode, output_html, client_interface): + """Used by main to show all. Not intended to be called directly""" + if output_html: + print( + "" + + "" + ) + client_list = get_client_list(client_interface) + if client_list is None: + client_list = [] + for client_name in client_list: + print(show_client(str(client_name), verbose_mode, output_html, client_interface)) + if output_html: + print("
ClientMACIPStatus
") + + @remediation.bug_handling def main(argv=None): """The main function.""" @@ -470,30 +487,17 @@ def main(argv=None): if args.interface is not None: client_interface = args.interface if args.show_all is True: - if output_html: - print(str( - u'' + - u'' - )) + showAllClients(verbose, output_html, client_interface) + elif args.list is True: client_list = get_client_list(client_interface) if client_list is None: client_list = [] for client_name in client_list: - print(show_client(str(client_name), verbose, output_html, client_interface)) - if output_html: - print("
ClientMACIPStatus
") + print(str(client_name)) else: - if args.list is True: - client_list = get_client_list(client_interface) - if client_list is None: - client_list = [] - for client_name in client_list: - print(str(client_name)) - else: - ip = args.ip - print(show_client(ip, verbose, output_html, client_interface)) - return 0 - return 0 + ip = args.ip + print(show_client(ip, verbose, output_html, client_interface)) + return 0 except Exception as main_err: print(str("client_check_status: REALLY BAD ERROR: ACTION will not be compleated! ABORT!")) print(str(main_err)) diff --git a/piaplib/lint/html_generator.py b/piaplib/lint/html_generator.py index bedd661..80b381e 100644 --- a/piaplib/lint/html_generator.py +++ b/piaplib/lint/html_generator.py @@ -153,16 +153,16 @@ def gen_html_ul(somelist=None, id=None, name=None): items = [gen_html_li(x) for x in somelist] theresult = None if id is not None and has_special_html_chars(id) is not True: - if name is not None and has_special_html_chars(name) is not True: - theresult = str(u'