Apache ssl.conf和regex

时间:2017-05-17 23:21:32

标签: python regex apache

我正在尝试解析并重写Apache的ssl.conf文件。我有像

这样的条目
<VirtualHost 10.0.2.165:443>
   ServerName layerv2t3.dev.ca.com
   DocumentRoot /etc/httpd/dispatcher_cache/layerv2t3

   SSLEngine on

   RewriteEngine On
   RewriteOptions Inherit
 # Redirect root to '/admin/homeRedirect' to make sure the cached version is used
 # and to satisfy the Dispatcher Filter
 RewriteRule ^/$ /admin/homeRedirect [R=302,L]
 ProxyPass /admin http://localhost:8085/admin
 ProxyPassReverse /admin http://localhost:8085/admin
 ProxyPreserveHost on
 RewriteRule system/sling/cqform/defaultlogin.html /app/login.html [R,L]
 Header edit Location ^http: https:
    <Directory />
            <IfModule disp_apache2.c>
                    SetHandler dispatcher-handler
                    ModMimeUsePathInfo On
            </IfModule>
            Options FollowSymLinks
            AllowOverride None
    </Directory>

</VirtualHost>
<VirtualHost 10.0.2.165:443>
  ServerName layerv2t3.app.qa1.w2.saasqa.ca.com
  DocumentRoot /etc/httpd/dispatcher_cache/layerv2t3

  SSLEngine on

  RewriteEngine On
  RewriteOptions Inherit
  # Redirect root to '/en_us/home.html' to make sure the cached version is used
  # and to satisfy the Dispatcher Filter
  RewriteRule ^/$ /site/en_us/home.html [R=302,L]
  RequestHeader edit Host (layerv2t3.app.qa1.w2.saasqa.ca.com) layerv2t3.dev.ca.com
  RequestHeader edit Origin (layerv2t3.app.qa1.w2.saasqa.ca.com) layerv2t3.dev.ca.com
  RequestHeader edit Referer (layerv2t3.app.qa1.w2.saasqa.ca.com) layerv2t3.dev.ca.com
  ProxyPass /admin http://localhost:8085/admin
  ProxyPassReverse /admin http://localhost:8085/admin
  ProxyPreserveHost on
  RewriteRule system/sling/cqform/defaultlogin.html /app/login.html [R,L]
  Header edit Location ^http: https:
  Header edit Location (layerv2t3.dev.ca.com) layerv2t3.app.qa1.w2.saasqa.ca.com
        <Directory />
                <IfModule disp_apache2.c>
                        SetHandler dispatcher-handler
                        ModMimeUsePathInfo On
                </IfModule>
                Options FollowSymLinks
                AllowOverride None
        </Directory>

</VirtualHost>

<VirtualHost 10.0.2.165:443>
  ServerName layerv2t5.app.qa1.w2.saasqa.ca.com
  DocumentRoot /etc/httpd/dispatcher_cache/layerv2t5

  SSLEngine on

  RewriteEngine On
  RewriteOptions Inherit
  # Redirect root to '/en_us/home.html' to make sure the cached version is used
  # and to satisfy the Dispatcher Filter
  RewriteRule ^/$ /site/en_us/home.html [R=302,L]
  RequestHeader edit Host (layerv2t5.app.qa1.w2.saasqa.ca.com) layerv2t5.dev.ca.com
  RequestHeader edit Origin (layerv2t5.app.qa1.w2.saasqa.ca.com) layerv2t5.dev.ca.com
  RequestHeader edit Referer (layerv2t5.app.qa1.w2.saasqa.ca.com) layerv2t5.dev.ca.com
  ProxyPass /admin http://localhost:8085/admin
  ProxyPassReverse /admin http://localhost:8085/admin
  ProxyPreserveHost on
  RewriteRule system/sling/cqform/defaultlogin.html /app/login.html [R,L]
  Header edit Location ^http: https:
  Header edit Location (layerv2t5.dev.ca.com) layerv2t5.app.qa1.w2.saasqa.ca.com
        <Directory />
                <IfModule disp_apache2.c>
                        SetHandler dispatcher-handler
                        ModMimeUsePathInfo On
                </IfModule>
                Options FollowSymLinks
                AllowOverride None
        </Directory>

</VirtualHost>

我希望能够匹配从&#34; VirtualHost&#34;开始的2个虚拟主机条目。结束于&#34; / VirtualHost&#34;对于ServerName layerv2t3。这包括后缀dev.ca.com以及app.qa1.w2.saasqa.ca.com,但我的正则表达式正在匹配关闭的VirtualHost,一直到下面。我想删除这些条目并重写文件内容,但这是下一步。

        app_host_entry = '\<VirtualHost \d+\.\d+\.\d+\.\d+\:443\>' \
                         '\s+ServerName\s{tenant_id}.app*com' \
                         '?\</VirtualHost\>' \
                         .format(tenant_id=self.tenant_id)

在这种情况下,tenant_id是layerv2t3。

1 个答案:

答案 0 :(得分:1)

这应该与服务器名称为layerv2t3

的All virtualhost块匹配
<VirtualHost.*\n\s+?ServerName.*?layerv2t3.*(\n.*?)+?<\/VirtualHost>

Demo URL

更新

使用multiline标志

<VirtualHost.*\s+?ServerName.*?layerv2t3.*(\s.*?)+?<\/VirtualHost>

Demo URL

匹配SSL端口:

<VirtualHost\s\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:443>\s+?ServerName.*?layerv2t3.*(\s.*?)+?<\/VirtualHost>

Demo URL