Exemple #1
0
 def _update_vhost_based_on_new_directives(
         self, vhost: obj.VirtualHost,
         directives_list: UnspacedList) -> None:
     new_server = self._get_included_directives(directives_list)
     parsed_server = self.parse_server(new_server)
     vhost.addrs = parsed_server['addrs']
     vhost.ssl = parsed_server['ssl']
     vhost.names = parsed_server['names']
     vhost.raw = new_server
Exemple #2
0
    def test_eq(self):
        from certbot_nginx._internal.obj import Addr
        from certbot_nginx._internal.obj import VirtualHost
        vhost1b = VirtualHost("filep", {Addr.fromstring("localhost blah")},
                              False, False, {'localhost'}, [], [])

        self.assertEqual(vhost1b, self.vhost1)
        self.assertEqual(str(vhost1b), str(self.vhost1))
        self.assertFalse(vhost1b == 1234)
Exemple #3
0
 def setUp(self):
     from certbot_nginx._internal.obj import VirtualHost
     from certbot_nginx._internal.obj import Addr
     raw1 = [
         ['listen', '69.50.225.155:9000'],
         [['if', '($scheme', '!=', '"https") '],
             [['return', '301', 'https://$host$request_uri']]
         ],
         ['#', ' managed by Certbot']
     ]
     self.vhost1 = VirtualHost(
         "filep",
         {Addr.fromstring("localhost")}, False, False,
         {'localhost'}, raw1, [])
     raw2 = [
         ['listen', '69.50.225.155:9000'],
         [['if', '($scheme', '!=', '"https") '],
             [['return', '301', 'https://$host$request_uri']]
         ]
     ]
     self.vhost2 = VirtualHost(
         "filep",
         {Addr.fromstring("localhost")}, False, False,
         {'localhost'}, raw2, [])
     raw3 = [
         ['listen', '69.50.225.155:9000'],
         ['rewrite', '^(.*)$', '$scheme://www.domain.com$1', 'permanent']
     ]
     self.vhost3 = VirtualHost(
         "filep",
         {Addr.fromstring("localhost")}, False, False,
         {'localhost'}, raw3, [])
     raw4 = [
         ['listen', '69.50.225.155:9000'],
         ['server_name', 'return.com']
     ]
     self.vhost4 = VirtualHost(
         "filp",
         {Addr.fromstring("localhost")}, False, False,
         {'localhost'}, raw4, [])
     raw_has_hsts = [
         ['listen', '69.50.225.155:9000'],
         ['server_name', 'return.com'],
         ['add_header', 'always', 'set', 'Strict-Transport-Security', '\"max-age=31536000\"'],
     ]
     self.vhost_has_hsts = VirtualHost(
         "filep",
         {Addr.fromstring("localhost")}, False, False,
         {'localhost'}, raw_has_hsts, [])
Exemple #4
0
 def test_contains_list(self):
     from certbot_nginx._internal.obj import VirtualHost
     from certbot_nginx._internal.obj import Addr
     from certbot_nginx._internal.configurator import _test_block_from_block
     test_block = [[
         '\n    ', 'return', ' ', '301', ' ', 'https://$host$request_uri'
     ], ['\n']]
     test_needle = _test_block_from_block(test_block)
     test_haystack = [
         ['listen', '80'], ['root', '/var/www/html'],
         ['index', 'index.html index.htm index.nginx-debian.html'],
         ['server_name', 'two.functorkitten.xyz'], ['listen', '443 ssl'],
         ['#', ' managed by Certbot'],
         [
             'ssl_certificate',
             '/etc/letsencrypt/live/two.functorkitten.xyz/fullchain.pem'
         ], ['#', ' managed by Certbot'],
         [
             'ssl_certificate_key',
             '/etc/letsencrypt/live/two.functorkitten.xyz/privkey.pem'
         ], ['#', ' managed by Certbot'],
         ['return', '301', 'https://$host$request_uri'],
         ['#', ' managed by Certbot'], []
     ]
     vhost_haystack = VirtualHost("filp",
                                  set([Addr.fromstring("localhost")]),
                                  False, False, set(['localhost']),
                                  test_haystack, [])
     test_bad_haystack = [
         ['listen', '80'], ['root', '/var/www/html'],
         ['index', 'index.html index.htm index.nginx-debian.html'],
         ['server_name', 'two.functorkitten.xyz'], ['listen', '443 ssl'],
         ['#', ' managed by Certbot'],
         [
             'ssl_certificate',
             '/etc/letsencrypt/live/two.functorkitten.xyz/fullchain.pem'
         ], ['#', ' managed by Certbot'],
         [
             'ssl_certificate_key',
             '/etc/letsencrypt/live/two.functorkitten.xyz/privkey.pem'
         ], ['#', ' managed by Certbot'],
         [['if', '($scheme', '!=', '"https")'],
          [['return', '302', 'https://$host$request_uri']]],
         ['#', ' managed by Certbot'], []
     ]
     vhost_bad_haystack = VirtualHost("filp",
                                      set([Addr.fromstring("localhost")]),
                                      False, False, set(['localhost']),
                                      test_bad_haystack, [])
     self.assertTrue(vhost_haystack.contains_list(test_needle))
     self.assertFalse(vhost_bad_haystack.contains_list(test_needle))
Exemple #5
0
class VirtualHostTest(unittest.TestCase):
    """Test the VirtualHost class."""
    def setUp(self):
        from certbot_nginx._internal.obj import VirtualHost
        from certbot_nginx._internal.obj import Addr
        raw1 = [['listen', '69.50.225.155:9000'],
                [['if', '($scheme', '!=', '"https") '],
                 [['return', '301', 'https://$host$request_uri']]],
                ['#', ' managed by Certbot']]
        self.vhost1 = VirtualHost("filep", {Addr.fromstring("localhost")},
                                  False, False, {'localhost'}, raw1, [])
        raw2 = [['listen', '69.50.225.155:9000'],
                [['if', '($scheme', '!=', '"https") '],
                 [['return', '301', 'https://$host$request_uri']]]]
        self.vhost2 = VirtualHost("filep", {Addr.fromstring("localhost")},
                                  False, False, {'localhost'}, raw2, [])
        raw3 = [['listen', '69.50.225.155:9000'],
                [
                    'rewrite', '^(.*)$', '$scheme://www.domain.com$1',
                    'permanent'
                ]]
        self.vhost3 = VirtualHost("filep", {Addr.fromstring("localhost")},
                                  False, False, {'localhost'}, raw3, [])
        raw4 = [['listen', '69.50.225.155:9000'],
                ['server_name', 'return.com']]
        self.vhost4 = VirtualHost("filp", {Addr.fromstring("localhost")},
                                  False, False, {'localhost'}, raw4, [])
        raw_has_hsts = [
            ['listen', '69.50.225.155:9000'],
            ['server_name', 'return.com'],
            [
                'add_header', 'always', 'set', 'Strict-Transport-Security',
                '\"max-age=31536000\"'
            ],
        ]
        self.vhost_has_hsts = VirtualHost("filep",
                                          {Addr.fromstring("localhost")},
                                          False, False, {'localhost'},
                                          raw_has_hsts, [])

    def test_eq(self):
        from certbot_nginx._internal.obj import Addr
        from certbot_nginx._internal.obj import VirtualHost
        vhost1b = VirtualHost("filep", {Addr.fromstring("localhost blah")},
                              False, False, {'localhost'}, [], [])

        self.assertEqual(vhost1b, self.vhost1)
        self.assertEqual(str(vhost1b), str(self.vhost1))
        self.assertFalse(vhost1b == 1234)

    def test_str(self):
        stringified = '\n'.join([
            'file: filep', 'addrs: localhost', "names: ['localhost']",
            'ssl: False', 'enabled: False'
        ])
        self.assertEqual(stringified, str(self.vhost1))

    def test_has_header(self):
        self.assertTrue(
            self.vhost_has_hsts.has_header('Strict-Transport-Security'))
        self.assertFalse(self.vhost_has_hsts.has_header('Bogus-Header'))
        self.assertFalse(self.vhost1.has_header('Strict-Transport-Security'))
        self.assertFalse(self.vhost1.has_header('Bogus-Header'))

    def test_contains_list(self):
        from certbot_nginx._internal.obj import VirtualHost
        from certbot_nginx._internal.obj import Addr
        from certbot_nginx._internal.configurator import _test_block_from_block
        test_block = [[
            '\n    ', 'return', ' ', '301', ' ', 'https://$host$request_uri'
        ], ['\n']]
        test_needle = _test_block_from_block(test_block)
        test_haystack = [
            ['listen', '80'], ['root', '/var/www/html'],
            ['index', 'index.html index.htm index.nginx-debian.html'],
            ['server_name', 'two.functorkitten.xyz'], ['listen', '443 ssl'],
            ['#', ' managed by Certbot'],
            [
                'ssl_certificate',
                '/etc/letsencrypt/live/two.functorkitten.xyz/fullchain.pem'
            ], ['#', ' managed by Certbot'],
            [
                'ssl_certificate_key',
                '/etc/letsencrypt/live/two.functorkitten.xyz/privkey.pem'
            ], ['#', ' managed by Certbot'],
            ['return', '301', 'https://$host$request_uri'],
            ['#', ' managed by Certbot'], []
        ]
        vhost_haystack = VirtualHost("filp", {Addr.fromstring("localhost")},
                                     False, False, {'localhost'},
                                     test_haystack, [])
        test_bad_haystack = [
            ['listen', '80'], ['root', '/var/www/html'],
            ['index', 'index.html index.htm index.nginx-debian.html'],
            ['server_name', 'two.functorkitten.xyz'], ['listen', '443 ssl'],
            ['#', ' managed by Certbot'],
            [
                'ssl_certificate',
                '/etc/letsencrypt/live/two.functorkitten.xyz/fullchain.pem'
            ], ['#', ' managed by Certbot'],
            [
                'ssl_certificate_key',
                '/etc/letsencrypt/live/two.functorkitten.xyz/privkey.pem'
            ], ['#', ' managed by Certbot'],
            [['if', '($scheme', '!=', '"https")'],
             [['return', '302', 'https://$host$request_uri']]],
            ['#', ' managed by Certbot'], []
        ]
        vhost_bad_haystack = VirtualHost("filp",
                                         {Addr.fromstring("localhost")}, False,
                                         False, {'localhost'},
                                         test_bad_haystack, [])
        self.assertTrue(vhost_haystack.contains_list(test_needle))
        self.assertFalse(vhost_bad_haystack.contains_list(test_needle))