Ejemplo n.º 1
0
    def create_certificate(self, subscription_id, management_host, hackathon):
        """Create certificate for specific subscription and hackathon

        1. check certificate dir
        2. generate pem file
        3. generate cert file
        4. add azure key to db
        5. add hackathon azure key to db
        :param subscription_id:
        :param management_host:
        :param hackathon:
        :return:
        """

        base_url = '%s/%s' % (self.CERT_BASE, subscription_id)
        pem_url = base_url + '.pem'
        cert_url = base_url + '.cer'

        # avoid duplicate pem generation
        if not os.path.isfile(pem_url):
            pem_command = 'openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout %s -out %s -batch' % \
                          (pem_url, pem_url)
            commands.getstatusoutput(pem_command)
        else:
            self.log.debug('%s exists' % pem_url)

        # avoid duplicate cert generation
        if not os.path.isfile(cert_url):
            cert_command = 'openssl x509 -inform pem -in %s -outform der -out %s' % (pem_url, cert_url)
            commands.getstatusoutput(cert_command)
        else:
            self.log.debug('%s exists' % cert_url)

        azure_key = AzureKey.objects(subscription_id=subscription_id, management_host=management_host).first()

        if azure_key is None:
            azure_key = AzureKey(
                cert_url=base_url + '.cer',
                pem_url=base_url + '.pem',
                subscription_id=subscription_id,
                management_host=management_host,
                verified=False
            )

            azure_key.save()

            hackathon.azure_keys.append(azure_key)
            hackathon.save()
        else:
            self.log.debug('azure key exists')

        if not (azure_key in hackathon.azure_keys):
            hackathon.azure_keys.append(azure_key)
        else:
            self.log.debug('hackathon azure key exists')

        # store cer file
        cer_context = Context(
            hackathon_name=hackathon.name,
            file_name=subscription_id + '.cer',
            file_type=FILE_TYPE.AZURE_CERT,
            content=file(cert_url)
        )
        self.log.debug("saving cerf file [%s] to azure" % cer_context.file_name)
        cer_context = self.storage.save(cer_context)
        azure_key.cert_url = cer_context.url

        # store pem file
        # encrypt certification file before upload to storage
        encrypted_pem_url = self.__encrypt_content(pem_url)
        pem_contex = Context(
            hackathon_name=hackathon.name,
            file_name=subscription_id + '.pem',
            file_type=FILE_TYPE.AZURE_CERT,
            content=file(encrypted_pem_url)
        )
        self.log.debug("saving pem file [%s] to azure" % pem_contex.file_name)
        pem_contex = self.storage.save(pem_contex)
        os.remove(encrypted_pem_url)
        azure_key.pem_url = pem_contex.url

        azure_key.save()

        return azure_key.dic()
Ejemplo n.º 2
0
    def create_certificate(self, subscription_id, management_host, hackathon):
        """Create certificate for specific subscription and hackathon

        1. check certificate dir
        2. generate pem file
        3. generate cert file
        4. add azure key to db
        5. add hackathon azure key to db
        :param subscription_id:
        :param management_host:
        :param hackathon:
        :return:
        """

        base_url = '%s/%s' % (self.CERT_BASE, subscription_id)
        pem_url = base_url + '.pem'
        cert_url = base_url + '.cer'

        # avoid duplicate pem generation
        if not os.path.isfile(pem_url):
            pem_command = 'openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout %s -out %s -batch' % \
                          (pem_url, pem_url)
            commands.getstatusoutput(pem_command)
        else:
            self.log.debug('%s exists' % pem_url)

        # avoid duplicate cert generation
        if not os.path.isfile(cert_url):
            cert_command = 'openssl x509 -inform pem -in %s -outform der -out %s' % (pem_url, cert_url)
            commands.getstatusoutput(cert_command)
        else:
            self.log.debug('%s exists' % cert_url)

        azure_key = AzureKey.objects(subscription_id=subscription_id, management_host=management_host).first()

        if azure_key is None:
            azure_key = AzureKey(
                cert_url=base_url + '.cer',
                pem_url=base_url + '.pem',
                subscription_id=subscription_id,
                management_host=management_host,
                verified=False
            )

            azure_key.save()

            hackathon.azure_keys.append(azure_key)
            hackathon.save()
        else:
            self.log.debug('azure key exists')

        if not (azure_key in hackathon.azure_keys):
            hackathon.azure_keys.append(azure_key)
            hackathon.save()
        else:
            self.log.debug('hackathon azure key exists')

        # store cer file
        cer_context = Context(
            hackathon_name=hackathon.name,
            file_name=subscription_id + '.cer',
            file_type=FILE_TYPE.AZURE_CERT,
            content=file(cert_url)
        )
        self.log.debug("saving cerf file [%s] to azure" % cer_context.file_name)
        cer_context = self.storage.save(cer_context)
        azure_key.cert_url = cer_context.url

        # store pem file
        # encrypt certification file before upload to storage
        encrypted_pem_url = self.__encrypt_content(pem_url)
        pem_contex = Context(
            hackathon_name=hackathon.name,
            file_name=subscription_id + '.pem',
            file_type=FILE_TYPE.AZURE_CERT,
            content=file(encrypted_pem_url)
        )
        self.log.debug("saving pem file [%s] to azure" % pem_contex.file_name)
        pem_contex = self.storage.save(pem_contex)
        os.remove(encrypted_pem_url)
        azure_key.pem_url = pem_contex.url

        azure_key.save()

        return azure_key.dic()