Exemplo n.º 1
0
def get_duplicate(order_id, sub_id, file_path, domain, api_key=''):
    LOGGER.info("Getting duplicate")
    LOGGER.info("order id: %s sub id: %s" % (order_id, sub_id))
    if not api_key:
        api_key = get_temp_api_key()

    if api_key:
        order_client = CertificateOrder(HOST, customer_api_key=api_key)
        duplicate_data = order_client.download_duplicate(
            digicert_order_id=order_id, sub_id=sub_id)
        cert_file_path = os.path.join(
            file_path, '{0}.crt'.format(domain.replace(".", "_")))
        chain_file_path = os.path.join(
            file_path, '{0}.pem'.format(domain.replace(".", "_")))

        # download the certificate
        cert = duplicate_data[0]
        cert_file = open(cert_file_path, 'w')
        cert_file.write(cert)
        cert_file.close()

        # download the intermediate certificate
        chain = duplicate_data[2]
        chain_file = open(chain_file_path, 'w')
        chain_file.write(chain)
        chain_file.close()

        return {"cert": cert_file_path, "chain": chain_file_path}
Exemplo n.º 2
0
def list_duplicates(order_id, api_key=''):
    if not api_key:
        api_key = get_temp_api_key()

    if api_key:
        order_client = CertificateOrder(HOST, customer_api_key=api_key)
        return order_client.list_duplicates(order_id)
Exemplo n.º 3
0
def create_duplicate(order_id, cert_data, api_key=''):
    LOGGER.info("Creating duplicate")
    if not api_key:
        api_key = get_temp_api_key()

    if api_key:
        order_client = CertificateOrder(HOST, customer_api_key=api_key)
        return order_client.create_duplicate(order_id, **cert_data)
Exemplo n.º 4
0
def get_order_info(order_id, api_key=''):
    if not api_key:
        api_key = get_temp_api_key()

    if api_key:
        # call the V2 view order API
        orderclient = CertificateOrder(HOST, api_key)
        order_info = orderclient.view(digicert_order_id=order_id)
        if order_info:
            order_info['api_key'] = api_key
            return order_info
        else:
            raise Exception(
                "ERROR: We could not find any information regarding order #{0}."
                .format(order_id))
Exemplo n.º 5
0
def get_valid_orders(api_key=''):
    if not api_key:
        api_key = get_temp_api_key()

    if api_key:
        # call the V2 view orders API
        orders = list()
        orderclient = CertificateOrder(HOST, api_key)
        all_orders = orderclient.view_all()
        if all_orders:
            orders = list()
            for order in all_orders['orders']:
                if order['status'] == 'issued':
                    cert = order['certificate']
                    if cert:
                        orders.append(order)
            return orders
        else:
            raise Exception(
                "ERROR: We could not find any orders for your account.")
        return
Exemplo n.º 6
0
def upload_csr(order_id, csr_file, api_key=''):
    LOGGER.info("Uploading CSR file for order# {0}...".format(order_id))
    if not api_key:
        api_key = get_temp_api_key()

    if api_key:
        # call the V2 view orders API
        csr_text = None
        with open(csr_file, "r") as f:
            csr_text = f.read()

        orderclient = CertificateOrder(HOST, api_key)
        resp = orderclient.upload_csr(order_id, csr_text)
        if resp and resp['http_status']:
            # accept any 2xx status code
            import math

            result = int(math.floor(int(resp['http_status']) / 100)) * 100
            if result == 200:
                LOGGER.info("CSR uploaded successfully")
                print ""
                return True
        return False
Exemplo n.º 7
0
def download_cert(order_id, file_path=None, domain=None, api_key=''):
    msg_downloading = 'Downloading certificate files for: '
    msg_from_dc = 'from %s' % HOST
    if domain:
        LOGGER.info('%s domain "%s" %s (Order ID: %s)...' %
                    (msg_downloading, domain.lower(), msg_from_dc, order_id))
    else:
        LOGGER.info('%s order ID "%s" %s...' %
                    (msg_downloading, order_id, msg_from_dc))
    print ''

    domain = domain.lower()

    if not api_key:
        api_key = get_temp_api_key()

    if api_key:
        orderclient = CertificateOrder(HOST, api_key)
        certificates = orderclient.download(digicert_order_id=order_id)

        cert_file_path = os.path.join(file_path, 'cert.crt')
        chain_file_path = os.path.join(file_path, 'chain.pem')

        try:
            # create the download directory if it does not exist
            if file_path and not os.path.exists(file_path):
                os.mkdir(file_path.replace('*', 'star'))
                LOGGER.info('Created %s directory...' % file_path)

            if isinstance(certificates, str):
                # then we know this is a zip file containing all certs
                zip_file = ZipFile(StringIO(certificates))
                tmp_dir = tempfile.gettempdir()
                zip_file.extractall(tmp_dir)

                # get the files that were extracted
                cert_dir = os.path.join(tmp_dir, "certs")
                src_cert_file_path = os.path.join(
                    cert_dir, '{0}.crt'.format(domain.replace(".", "_")))
                src_chain_file_path = os.path.join(cert_dir, 'DigiCertCA.crt')
                cert_file_path = os.path.join(
                    file_path, os.path.basename(src_cert_file_path))
                chain_file_path = os.path.join(
                    file_path, os.path.basename(src_chain_file_path))
                src_cert_file_path = src_cert_file_path.replace('*', 'star')
                cert_file_path = cert_file_path.replace('*', 'star')
                express_utils.copy_cert(src_cert_file_path, cert_file_path)
                express_utils.copy_cert(src_chain_file_path, chain_file_path)
            else:
                certificates = certificates.get('certificates')
                if not certificates:
                    raise Exception(
                        "Failed to get certificates from order ".format(
                            order_id))

                if domain:
                    cert_file_path = os.path.join(
                        file_path, '{0}.crt'.format(domain.replace(".", "_")))
                    chain_file_path = os.path.join(
                        file_path, '{0}.pem'.format(domain.replace(".", "_")))

                # download the certificate
                cert = certificates.get('certificate')
                cert_file = open(cert_file_path, 'w')
                cert_file.write(cert)
                cert_file.close()

                # download the intermediate certificate
                chain = certificates.get('intermediate')
                chain_file = open(chain_file_path, 'w')
                chain_file.write(chain)
                chain_file.close()
        except IOError as ioe:
            raise Exception("Download failed: {0}".format(ioe))

        LOGGER.info('Created certificate file at path %s' % cert_file_path)
        LOGGER.info('Created certificate chain file at path %s' %
                    chain_file_path)
        print ''
        LOGGER.info('Certificate files downloaded successfully')
        print ''

        return {'cert': cert_file_path, 'chain': chain_file_path}
    else:
        raise Exception(
            'Username or API Key required to download certificate.')