<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from destination.base import DestinationBase
from config import CFG

from utils.dictionary import merge, head, body, head_body, keys_ending
from utils.newline import windows2unix

from app import app
from utils.output import yaml_format

from utils.format import fmt, pfmt

from cert import decompose_cert

def compose_json(crt, csr, key, note):
    return dict(properties=dict(basic=dict(
        public=crt,
        request=csr,
        private=key,
        note=note)))

class ZeusDestination(DestinationBase):
    def __init__(self, ar, cfg, verbosity):
        super(ZeusDestination, self).__init__(ar, cfg, verbosity)

    def fetch_certificates(self, certs, *dests):
        details = self._get_installed_certificates_details(certs, *dests)
        if details:
            for cert in certs:
                cert_name, common_name, crt, csr, key = decompose_cert(cert)
                destinations = details.get((common_name, crt[:40]), None)
                if destinations:
                    cert[cert_name]['destinations'] = cert[cert_name].get('destinations', {})
                    cert[cert_name]['destinations'].update(destinations)
        return certs

    def install_certificates(self, certs, *dests):
        paths, jsons = zip(*[('ssl/server_keys/' + common_name, compose_json(crt, csr, key, cert_name)) for cert_name, common_name, crt, csr, key in [decompose_cert(cert) for cert in certs]])

        calls = self.puts(paths=paths, dests=dests, jsons=jsons, verify_ssl=False)
        certs = self.fetch_certificates(certs, *dests)
        return certs

    def update_certificates(self, certs, *dests):
        raise NotImplementedError

    def remove_certificates(self, certs, *dests):
        raise NotImplementedError

    def _get_installed_summary(self, certs, *dests):
        common_names = [body(cert)['common_name'] for cert in certs]
        paths = ['ssl/server_keys']
        calls = self.gets(paths=paths, dests=dests, verify_ssl=False)
        summary = []
        for dest, call in zip(dests, calls):
            app.logger.debug(fmt('dest={dest} call=\n{call}'))
            for child in call.recv.json.children:
                if child.name in common_names:
                    summary += [(child.name, 'ssl/server_keys/' + child.name, dest)]
        return summary

    def _get_installed_certificates_details(self, certs, *dests):
        summary = self._get_installed_summary(certs, *dests)
        details = {}
        if summary:
            common_names, paths, dests = zip(*summary)
            calls = self.gets(paths=paths, dests=dests, verify_ssl=False)
            for common_name, path, dest, call in zip(common_names, paths, dests, calls):
                crt = windows2unix(call.recv.json.properties.basic.get('public', 'missing'))
                csr = windows2unix(call.recv.json.properties.basic.get('request', 'missing'))
                key = windows2unix(call.recv.json.properties.basic.get('private', 'missing'))
                note = call.recv.json.properties.basic.get('note', '')
                details[(common_name, crt[:40])] = details.get((common_name, crt[:40]), {})
                details[(common_name, crt[:40])][dest] = dict(
                    crt=crt,
                    csr=csr,
                    key=key,
                    note=note)
        return details

<commit_msg>put the try, except back in place<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from destination.base import DestinationBase
from config import CFG

from utils.dictionary import merge, head, body, head_body, keys_ending
from utils.newline import windows2unix

from app import app
from utils.output import yaml_format

from utils.format import fmt, pfmt

from cert import decompose_cert

def compose_json(crt, csr, key, note):
    return dict(properties=dict(basic=dict(
        public=crt,
        request=csr,
        private=key,
        note=note)))

class ZeusDestination(DestinationBase):
    def __init__(self, ar, cfg, verbosity):
        super(ZeusDestination, self).__init__(ar, cfg, verbosity)

    def fetch_certificates(self, certs, *dests):
        details = self._get_installed_certificates_details(certs, *dests)
        if details:
            for cert in certs:
                cert_name, common_name, crt, csr, key = decompose_cert(cert)
                destinations = details.get((common_name, crt[:40]), None)
                if destinations:
                    cert[cert_name]['destinations'] = cert[cert_name].get('destinations', {})
                    cert[cert_name]['destinations'].update(destinations)
        return certs

    def install_certificates(self, certs, *dests):
        paths, jsons = zip(*[('ssl/server_keys/' + common_name, compose_json(crt, csr, key, cert_name)) for cert_name, common_name, crt, csr, key in [decompose_cert(cert) for cert in certs]])

        calls = self.puts(paths=paths, dests=dests, jsons=jsons, verify_ssl=False)
        certs = self.fetch_certificates(certs, *dests)
        return certs

    def update_certificates(self, certs, *dests):
        raise NotImplementedError

    def remove_certificates(self, certs, *dests):
        raise NotImplementedError

    def _get_installed_summary(self, certs, *dests):
        common_names = [body(cert)['common_name'] for cert in certs]
        paths = ['ssl/server_keys']
        calls = self.gets(paths=paths, dests=dests, verify_ssl=False)
        summary = []
        for dest, call in zip(dests, calls):
            app.logger.debug(fmt('dest={dest} call=\n{call}'))
            for child in call.recv.json.children:
                if child.name in common_names:
                    summary += [(child.name, 'ssl/server_keys/' + child.name, dest)]
        return summary

    def _get_installed_certificates_details(self, certs, *dests):
        summary = self._get_installed_summary(certs, *dests)
        details = {}
        if summary:
            common_names, paths, dests = zip(*summary)
            calls = self.gets(paths=paths, dests=dests, verify_ssl=False)
            for common_name, path, dest, call in zip(common_names, paths, dests, calls):
                try:
                    crt = windows2unix(call.recv.json.properties.basic.get('public', 'missing'))
                    csr = windows2unix(call.recv.json.properties.basic.get('request', 'missing'))
                    key = windows2unix(call.recv.json.properties.basic.get('private', 'missing'))
                    note = call.recv.json.properties.basic.get('note', '')
                except Exception as ex:
                    app.logger.debug(fmt('call.recv.json=\n{0}', call.recv.json))
                details[(common_name, crt[:40])] = details.get((common_name, crt[:40]), {})
                details[(common_name, crt[:40])][dest] = dict(
                    crt=crt,
                    csr=csr,
                    key=key,
                    note=note)
        return details


{'fraction_non_alphanumeric': 0.11685905657688471, 'fraction_numerical': 0.004132820293572752, 'mean_word_length': 2.5934459805427545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1514541', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2090, 'n_words': 475}