Browse Source

Using custom template directives and not vars

Fabrizio Furnari 1 month ago
parent
commit
b023d2e4c6
3 changed files with 69 additions and 7 deletions
  1. 48 0
      example_tests/11-proxy-config.py
  2. 19 5
      haproxy.cfg.tpl
  3. 2 2
      httphound/proxy.py

+ 48 - 0
example_tests/11-proxy-config.py

@@ -0,0 +1,48 @@
+"""
+Test custom proxy configuration (directives)
+"""
+
+from pathlib import Path
+from httphound.main import BaseProxyTest, BackendConfig, ProxyConfig
+
+class TemplateDirectivesTest(BaseProxyTest):
+    """Test using custom proxy config directives
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Custom proxy config directives"
+
+        # backend will run on port 9999
+        self.backend_config = BackendConfig(
+            host='127.0.0.1',
+            port=9999,
+            response_status=200,
+            response_body='OK',
+        )
+
+        # configure haproxy to use production config
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy",
+            config_mode="template",
+            template_directives = {
+                "global": [
+                    "maxconn 1024",
+                ],
+                "defaults": [
+                    "timeout client 30s",
+                ],
+                "frontend_http": [
+                    "http-request deny deny_status 500 if { path_beg /test }",
+                ],
+            },
+        )
+
+        self.url = "http://127.0.0.1:4242/test"
+        self.expected_status = 500
+
+    async def run_test(self):
+        """Run the test"""
+        await self.make_request()
+        assert self.backend.request_count == 0
+
+        return True

+ 19 - 5
haproxy.cfg.tpl

@@ -1,23 +1,37 @@
 # HAProxy configuration template
 # HAProxy configuration template
-# {BACKEND_ADDRESS} will be replaced with the actual backend address
-
 global
 global
     daemon
     daemon
     log stdout local0
     log stdout local0
+    {%- if template_directives["global"] %}
+    {%- for directive in template_directives["global"] %}
+    {{ directive }}
+    {%- endfor %}
+    {%- endif %}
     
     
 defaults
 defaults
     mode http
     mode http
     timeout connect 5000ms
     timeout connect 5000ms
-    timeout client 50000ms
+    timeout client 5000ms
     timeout server 50000ms
     timeout server 50000ms
     option httplog
     option httplog
     log global
     log global
+    {%- if template_directives["defaults"] %}
+    {%- for directive in template_directives["defaults"] %}
+    {{ directive }}
+    {%- endfor %}
+    {%- endif %}
+
 
 
 listen http
 listen http
     bind {{ listen_addr }}:{{ listen_port }}
     bind {{ listen_addr }}:{{ listen_port }}
     option httplog
     option httplog
-    # TODO: add template directives
-    #http-request return status 200 content-type "text/plain" string "OK"
+    
+    {%- if template_directives["frontend_http"] %}
+    {%- for directive in template_directives["frontend_http"] %}
+    {{ directive }}
+    {%- endfor %}
+    {%- endif %}
+
     server dummy {{ backend_host }}:{{ backend_port }}
     server dummy {{ backend_host }}:{{ backend_port }}
 
 
 
 

+ 2 - 2
httphound/proxy.py

@@ -31,7 +31,7 @@ class ProxyConfig:
     template_path: Optional[str] = "haproxy.cfg.tpl"
     template_path: Optional[str] = "haproxy.cfg.tpl"
     listen_addr: str = "*"
     listen_addr: str = "*"
     listen_port: int = 4242
     listen_port: int = 4242
-    template_vars: Dict[str, Any] = field(default_factory=dict)
+    template_directives: Optional[Dict[str, List[str]]] = None
 
 
     # production mode
     # production mode
     production_config_file_path: Optional[str] = None # main config file path
     production_config_file_path: Optional[str] = None # main config file path
@@ -59,7 +59,7 @@ class ProxyManager:
             'listen_port': self.config.listen_port,
             'listen_port': self.config.listen_port,
             'backend_host': backend_config.host,
             'backend_host': backend_config.host,
             'backend_port': backend_config.port,
             'backend_port': backend_config.port,
-            **self.config.template_vars
+            'template_directives': self.config.template_directives,
         }
         }
 
 
         try:
         try: