示例#1
0
def update_apache_config(site):
    vhost = site['VHost']
    server_name = site['ServerName']
    Logger.info('Updating Apache configuration for virtual host at {}{}'.format(
        vhost, ', {}'.format(server_name) if server_name else ''))
    default_cert_path = apache_util.get_apache_ssl_cert_path(vhost, server_name)
    default_key_path = apache_util.get_apache_ssl_key_path(vhost, server_name)
    current_cert_path = apache_util.get_apache_vhost_directive(
        vhost, server_name, 'SSLCertificateFile')
    current_key_path = apache_util.get_apache_vhost_directive(
        vhost, server_name, 'SSLCertificateKeyFile')

    if current_cert_path != default_cert_path:
        apache_util.set_apache_vhost_directive(
            vhost,
            server_name,
            'SSLCertificateFile',
            default_cert_path)

    if current_key_path != default_key_path:
        apache_util.set_apache_vhost_directive(
            vhost,
            server_name,
            'SSLCertificateKeyFile',
            default_key_path)
    def test_set_apache_vhost_directive(self):
        temp_dir = None
        try:
            temp_dir = tempfile.mkdtemp()
            for file_path in glob.glob('./keytalk-test-*.conf'):
                shutil.copy(file_path, temp_dir)
            conf_files = glob.glob(temp_dir + '/keytalk-test-*.conf')
            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3000',
                    None,
                    'SSLCertificateFile',
                    config_files=conf_files),
                '/etc/ssl/certs/keytalk-test-3000-ssl.pem')

            apache_util.set_apache_vhost_directive(
                '*:3000',
                None,
                'SSLCertificateFile',
                '/some/path',
                config_files=conf_files)

            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3000',
                    None,
                    'SSLCertificateFile',
                    config_files=conf_files),
                '/some/path')

            # No occurrence, single occurence, multiple occurrence
            self.assertRaises(
                Exception,
                apache_util.get_apache_vhost_directive,
                '*:3003',
                None,
                'ToBeCreatedDirective',
                config_files=conf_files)
            apache_util.set_apache_vhost_directive(
                '*:3000',
                None,
                'ToBeCreatedDirective',
                'some_value',
                config_files=conf_files)
            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3000',
                    None,
                    'ToBeCreatedDirective',
                    config_files=conf_files),
                'some_value')

            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3000',
                    None,
                    'TestSingleDirective',
                    config_files=conf_files),
                'a')
            apache_util.set_apache_vhost_directive(
                '*:3000', None, 'TestSingleDirective', 'aaa', config_files=conf_files)
            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3000',
                    None,
                    'TestSingleDirective',
                    config_files=conf_files),
                'aaa')

            self.assertRaises(
                Exception,
                apache_util.set_apache_vhost_directive,
                '*:3003',
                None,
                'TestDoubleDirective',
                'some_value',
                config_files=conf_files)

            # Name-based vhosts
            self.assertRaises(
                Exception,
                apache_util.set_apache_vhost_directive,
                '*:3003',
                None,
                'SSLCertificateFile',
                '/some/path',
                config_files=glob.glob('./keytalk-test-*.conf'))

            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3003',
                    'a.example.com',
                    'SSLCertificateFile',
                    config_files=conf_files),
                '/etc/ssl/certs/keytalk-test-3003-a.example.com-ssl.pem')
            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3003',
                    'b.example.com',
                    'SSLCertificateFile',
                    config_files=conf_files),
                '/etc/ssl/certs/keytalk-test-3003-b.example.com-ssl.pem')

            apache_util.set_apache_vhost_directive(
                '*:3003',
                'a.example.com',
                'SSLCertificateFile',
                '/some/path/a',
                config_files=conf_files)
            apache_util.set_apache_vhost_directive(
                '*:3003',
                'b.example.com',
                'SSLCertificateFile',
                '/some/path/b',
                config_files=conf_files)

            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3003',
                    'a.example.com',
                    'SSLCertificateFile',
                    config_files=conf_files),
                '/some/path/a')
            self.assertEquals(
                apache_util.get_apache_vhost_directive(
                    '*:3003',
                    'b.example.com',
                    'SSLCertificateFile',
                    config_files=conf_files),
                '/some/path/b')
        finally:
            if temp_dir is not None:
                shutil.rmtree(temp_dir)
    def test_get_apache_vhost_directive(self):
        conf_files = glob.glob('./keytalk-test-*.conf')
        self.assertEquals(
            apache_util.get_apache_vhost_directive(
                '*:3000',
                None,
                'SSLCertificateFile',
                config_files=conf_files),
            '/etc/ssl/certs/keytalk-test-3000-ssl.pem')
        self.assertEquals(
            apache_util.get_apache_vhost_directive(
                '*:3001',
                None,
                'SSLCertificateFile',
                config_files=conf_files),
            '/etc/ssl/certs/keytalk-test-3001-ssl.pem')
        self.assertEquals(
            apache_util.get_apache_vhost_directive(
                '*:3002',
                None,
                'SSLCertificateFile',
                config_files=conf_files),
            '/etc/ssl/certs/keytalk-test-3002-ssl.pem')

        # No occurrence, single occurence, multiple occurrence
        self.assertEquals(
            apache_util.get_apache_vhost_directive(
                '*:3000',
                None,
                'TestSingleDirective',
                config_files=conf_files),
            'a')
        self.assertRaises(
            Exception,
            apache_util.get_apache_vhost_directive,
            '*:3003',
            None,
            'TestDoubleDirective',
            config_files=conf_files)
        self.assertRaises(
            Exception,
            apache_util.get_apache_vhost_directive,
            '*:3003',
            None,
            'NonexistingDirective',
            config_files=conf_files)
        self.assertRaises(
            Exception,
            apache_util.get_apache_vhost_directive,
            '*:9999',
            None,
            'TestSingleDirective',
            config_files=conf_files)

        # Name-based vhosts
        self.assertRaises(
            Exception,
            apache_util.get_apache_vhost_directive,
            '*:3003',
            None,
            'SSLCertificateFile',
            config_files=conf_files)
        self.assertRaises(
            Exception,
            apache_util.get_apache_vhost_directive,
            '*:3000',
            'a.example.com',  # Not name-based
            'SSLCertificateFile',
            config_files=conf_files)
        self.assertEquals(
            apache_util.get_apache_vhost_directive(
                '*:3003',
                'a.example.com',
                'SSLCertificateFile',
                config_files=conf_files),
            '/etc/ssl/certs/keytalk-test-3003-a.example.com-ssl.pem')
        self.assertEquals(
            apache_util.get_apache_vhost_directive(
                '*:3003',
                'b.example.com',
                'SSLCertificateFile',
                config_files=conf_files),
            '/etc/ssl/certs/keytalk-test-3003-b.example.com-ssl.pem')
        self.assertRaises(
            Exception,
            apache_util.get_apache_vhost_directive,
            '*:3003',
            'doesnotexist.example.com',
            'SSLCertificateFile',
            config_files=conf_files)