<commit_before># coding=utf-8
# Copyright 2016 F5 Networks Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import shutil
import tempfile

from oslo_log import log as logging
from requests import HTTPError

LOG = logging.getLogger(__name__)


class SSLProfileHelper(object):

    @staticmethod
    def create_client_ssl_profile(bigip, name, cert, key, sni_default=False):
        uploader = bigip.shared.file_transfer.uploads
        cert_registrar = bigip.tm.sys.crypto.certs
        key_registrar = bigip.tm.sys.crypto.keys
        ssl_client_profile = bigip.tm.ltm.profile.client_ssls.client_ssl

        # No need to create if it exists
        if ssl_client_profile.exists(name=name, partition='Common'):
            return

        certfilename = name + '.crt'
        keyfilename = name + '.key'
        tls_dir = tempfile.mkdtemp()

        # write certificate to temp file
        certpath = os.path.join(tls_dir, certfilename)
        f = open(certpath, "w")
        f.write(cert)
        f.close()

        # write key to temp file
        keypath = os.path.join(tls_dir, keyfilename)
        f = open(keypath, "w")
        f.write(key)
        f.close()

        # upload files to BIG-IP
        try:
            uploader.upload_file(str(certpath))
            uploader.upload_file(str(keypath))

            # create BIG-IP cert objects
            cert_registrar.install_cert(certfilename)
            key_registrar.install_key(certfilename, keyfilename)

            # create ssl-client profile
            chain = [{'name': name,
                      'cert': '/Common/' + certfilename,
                      'key': '/Common/' + keyfilename}]
            ssl_client_profile.create(name=name,
                                      certKeyChain=chain,
                                      sniDefault=sni_default)
        except HTTPError as err:
            LOG.error("Error uploading cert/key %s"
                      "Repsponse status code: %s. Response "
                      "message: %s." % (certpath,
                                        err.response.status_code,
                                        err.message))
            raise err
        finally:
            shutil.rmtree(tls_dir)<commit_msg>Replace tmp file cert/key upload with memory buffer upload.<commit_after># coding=utf-8
# Copyright 2016 F5 Networks Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from oslo_log import log as logging

LOG = logging.getLogger(__name__)


class SSLProfileError(Exception):
    pass


class SSLProfileHelper(object):

    @staticmethod
    def create_client_ssl_profile(bigip, name, cert, key, sni_default=False):
        uploader = bigip.shared.file_transfer.uploads
        cert_registrar = bigip.tm.sys.crypto.certs
        key_registrar = bigip.tm.sys.crypto.keys
        ssl_client_profile = bigip.tm.ltm.profile.client_ssls.client_ssl

        # No need to create if it exists
        if ssl_client_profile.exists(name=name, partition='Common'):
            return

        certfilename = name + '.crt'
        keyfilename = name + '.key'

        try:
            # In-memory upload -- data not written to local file system but
            # is saved as a file on the BIG-IP.
            uploader.upload_bytes(cert, certfilename)
            uploader.upload_bytes(key, keyfilename)

            # import certificate
            param_set = {}
            param_set['name'] = certfilename
            param_set['from-local-file'] = \
                '/var/config/rest/downloads/' + certfilename
            cert_registrar.exec_cmd('install', **param_set)

            # import key
            param_set['name'] = keyfilename
            param_set['from-local-file'] = \
                '/var/config/rest/downloads/' + keyfilename
            key_registrar.exec_cmd('install', **param_set)

            # create ssl-client profile from cert/key pair
            chain = [{'name': name,
                      'cert': '/Common/' + certfilename,
                      'key': '/Common/' + keyfilename}]
            ssl_client_profile.create(name=name,
                                      certKeyChain=chain,
                                      sniDefault=sni_default)
        except Exception as err:
            LOG.error("Error creating SSL profile: %s" % err.message)
            raise SSLProfileError(err.message)
{'fraction_non_alphanumeric': 0.0868107302533532, 'fraction_numerical': 0.0037257824143070045, 'mean_word_length': 2.1106604866743917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4819870', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1424, 'n_words': 425}