[Inteproxy-commits] r206 - in trunk: . test

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Sep 11 21:21:04 CEST 2009


Author: bh
Date: 2009-09-11 21:21:03 +0200 (Fri, 11 Sep 2009)
New Revision: 206

Added:
   trunk/test/test_create_rewrite_rules.py
Modified:
   trunk/ChangeLog
   trunk/create-rewrite-rules.py
   trunk/test/support.py
Log:
* create-rewrite-rules.py: Substantial changes to adapt to the new
features: credentials and wild-card patterns in the config file.
Since the credentials are now in the main configuration file, the
separate credentials file is not used anymore.

* test/test_create_rewrite_rules.py: New. Test cases for the
create-rewrite-rules.py script

* test/support.py (FileTestMixin.check_file_contents): Add helper
method to check file contents


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-09-11 19:10:22 UTC (rev 205)
+++ trunk/ChangeLog	2009-09-11 19:21:03 UTC (rev 206)
@@ -1,5 +1,18 @@
 2009-09-11  Bernhard Herzog  <bh at intevation.de>
 
+	* create-rewrite-rules.py: Substantial changes to adapt to the new
+	features: credentials and wild-card patterns in the config file.
+	Since the credentials are now in the main configuration file, the
+	separate credentials file is not used anymore.
+
+	* test/test_create_rewrite_rules.py: New. Test cases for the
+	create-rewrite-rules.py script
+
+	* test/support.py (FileTestMixin.check_file_contents): Add helper
+	method to check file contents
+
+2009-09-11  Bernhard Herzog  <bh at intevation.de>
+
 	* test/test_transcoder_map.py
 	(TestTranscoderMapWithPathWildcards.test_get_transcoder_http_proxy)
 	(TestTranscoderMapWithHostWildcards.test_get_transcoder_http_proxy):

Modified: trunk/create-rewrite-rules.py
===================================================================
--- trunk/create-rewrite-rules.py	2009-09-11 19:10:22 UTC (rev 205)
+++ trunk/create-rewrite-rules.py	2009-09-11 19:21:03 UTC (rev 206)
@@ -2,97 +2,59 @@
 
 """
 Usage:
-  create-rewrite-rules.py --config-file=... --credentials-file=... [-o outfile]
+  create-rewrite-rules.py --config-file=... [-o outfile]
 
 Script to convert InteProxy's configuration to Apache RewriteRule
-definitions.  The script reads the InteProx configuration file specified
-with --config-file and the credentials to use when connecting to the
-hosts specified in the configuration files from the file specified with
---credentials-file.  For all entries in the configuration file for which
-credentials are specified a RewriteRule is written to outfile.  If no
-outfile is specified, the output is written to stdout.
+definitions.  The script reads the InteProxy configuration file
+specified with --config-file and writes a RewriteRule for all entries in
+the configuration file for which credentials are specified.  The output
+is written to outfile.  If no outfile is specified, the output is
+written to stdout.
 """
 
 import sys
 import optparse
-import urlparse
-from ConfigParser import SafeConfigParser
+import re
+from urllib import quote_plus
 
 import inteproxy.resources
 import inteproxy.config
-import inteproxy.transcoder
+from inteproxy.transcoder import pattern_to_regex
 
-credentials_desc = [inteproxy.config.Option("host"),
-                    inteproxy.config.Option("path"),
-                    inteproxy.config.Option("username"),
-                    inteproxy.config.Option("password")]
 
-class UndefinedTranscoder(inteproxy.transcoder.IdentityTranscoder):
+def read_config(config_filename):
+    return inteproxy.config.read_config(config_filename)
 
-    """Transcoder to use a default transcoder class to find credentials
-    specified for unconfigured hosts"""
 
-    pass
+def rule_for_owsproxy(rule):
+    orig_url = "/" + rule.host + rule.path
+    if rule.user is None or rule.password is None:
+        print >>sys.stderr, ("Missing credentials for url %r"
+                             % orig_url)
+        return None
+    host_regex = pattern_to_regex(rule.host, character_set="[^/]")
+    path_regex = pattern_to_regex(rule.path, character_set=".")
+    port_regex = ""
+    if rule.port is not None:
+        port_regex = re.escape(":%d" % rule.port)
+    return ("RewriteRule ^/%s%s%s$ https:/$0?user=%s&password=%s [QSA,P]\n"
+            % (host_regex, port_regex, path_regex,
+               re.escape(quote_plus(rule.user)),
+               re.escape(quote_plus(rule.password))))
 
-class PasswordGetter(object):
 
-    """
-    Substitute for the inteproxy.getpassword module for predefined credentials
-    """
-
-    def __init__(self, credentials):
-        self.credentials = credentials
-
-    def get_password_with_cache(self, path):
-        result = (None, None)
-        cred = self.credentials.get(urlparse.urlsplit("http://" + path)[1:3])
-        if cred is not None:
-            result = (cred.username, cred.password)
-        return result
-
-def read_transcoder_map(config_filename):
-    """Read the transcoder definitions from the InteProxy configuration file.
-    The returned transcoder map maps unknown URLs to instances of
-    UndefinedTranscoder instances so that they can be easily found
-    later.
-    """
-    config = inteproxy.config.read_config(config_filename)
-    transcoder_map = inteproxy.transcoder.create_transcoder_map()
-    transcoder_map.add_class("undefined", UndefinedTranscoder,
-                             UndefinedTranscoder)
-    transcoder_map.set_default_class("undefined")
-    transcoder_map.add_rules(config.rules)
-    return transcoder_map
-
-
-def read_credentials_file(filename):
-    """Reads the credentials file and returns them as dict.
-    The dict maps (host, path) pairs to credentials objects.  The
-    credentials objects have username and password attributes.
-    """
-    parser = SafeConfigParser()
-    parser.read([filename])
-    credentials = dict()
-    for section in parser.sections():
-        cred = inteproxy.config.read_config_section(parser, section,
-                                                    credentials_desc)
-        credentials[(cred.host, cred.path)] = cred
-    return credentials
-
-def create_rewrite_rules(transcoder_map, credentials, outfile):
-    """Writes the RewriteRules from transcoder_map and credentials to outfile"""
-    inteproxy.transcoder.getpassword = PasswordGetter(credentials)
-    for host, path in credentials:
-        orig_url = urlparse.urlunsplit(("http", host, path, "", ""))
-        orig_url = "/" + host + path
-        transcoder = transcoder_map.get_transcoder("GET", orig_url)
-        if isinstance(transcoder, UndefinedTranscoder):
-            print >>sys.stderr, ("Credentials supplied for undefined url %r"
-                                 % orig_url)
+def create_rewrite_rules(config, outfile):
+    """Writes the RewriteRules from config to outfile"""
+    for rule in config.rules:
+        if rule.scheme != "owsproxy":
+            print >>sys.stderr, ("Unsupported scheme %r for %s/%s"
+                                 % (rule.scheme, rule.host, rule.path))
             continue
-        outfile.write("RewriteRule ^%s %s [QSA,P]\n"
-                      % (orig_url, transcoder.get_url()))
+        rewrite_rule = rule_for_owsproxy(rule)
+        if rewrite_rule:
+            outfile.write(rewrite_rule)
 
+
 def create_config_parser():
     """Creates an OptionParser instance for the script"""
     parser = optparse.OptionParser()
@@ -100,24 +62,23 @@
         config_file=inteproxy.resources.default_config_file(),
         credentials_file=inteproxy.resources.default_credentials_file())
     parser.add_option("--config-file", help=("InteProxy configuration file"))
-    parser.add_option("--credentials-file",
-                      help=("File specifying credentials for the servers"
-                            " in the InteProxy config file"))
     parser.add_option("--output-file", "-o",
                       help=("Output file (by default output is written to"
                             " stdout)"))
     return parser
 
+
 def main():
     parser = create_config_parser()
     opts, rest = parser.parse_args()
-    transcoder_map = read_transcoder_map(opts.config_file)
-    credentials = read_credentials_file(opts.credentials_file)
+    config = read_config(opts.config_file)
+
     outfile = sys.stdout
     if opts.output_file:
         outfile = open(opts.output_file, "w")
-    create_rewrite_rules(transcoder_map, credentials, outfile)
 
+    create_rewrite_rules(config, outfile)
 
+
 if __name__  == "__main__":
     main()

Modified: trunk/test/support.py
===================================================================
--- trunk/test/support.py	2009-09-11 19:10:22 UTC (rev 205)
+++ trunk/test/support.py	2009-09-11 19:21:03 UTC (rev 206)
@@ -76,7 +76,11 @@
             os.chmod(filename, permissions)
         return filename
 
+    def check_file_contents(self, filename, contents):
+        """check the contents of a file"""
+        self.assertEquals(open(filename).read(), contents)
 
+
 class AttributeTestMixin:
 
     def check_attributes(self, obj, **attrs):

Added: trunk/test/test_create_rewrite_rules.py
===================================================================
--- trunk/test/test_create_rewrite_rules.py	2009-09-11 19:10:22 UTC (rev 205)
+++ trunk/test/test_create_rewrite_rules.py	2009-09-11 19:21:03 UTC (rev 206)
@@ -0,0 +1,82 @@
+# Copyright (C) 2009 by Intevation GmbH
+# Authors:
+# Bernhard Herzog <bh at intevation.de>
+#
+# This program is free software under the GPL (>=v2)
+# Read the file COPYING coming with the software for details.
+
+"""Tests for inteproxy.httpserver and serversupport
+
+The tests of inteproxy.httpserver are done indirectly via the server
+implemented in serversupport so that the tests server more or less as
+tests for both modules.
+"""
+
+import sys
+import os
+import httplib
+import unittest
+import subprocess
+
+import support
+
+
+class TestCreateRewriteRules(unittest.TestCase, support.FileTestMixin):
+
+    def setUp(self):
+        self.create_rewrite_rules_program = \
+                         os.path.join(os.path.dirname(__file__), os.pardir,
+                                      "create-rewrite-rules.py")
+
+    def run_test(self, config_contents, expected_rules):
+        config_filename = self.create_temp_file("config", config_contents)
+        rules_filename = self.create_temp_file("rules", "")
+        error_filename = self.create_temp_file("errors", "")
+        error_file = open(error_filename, "w")
+        try:
+            command = [self.create_rewrite_rules_program,
+                       "--config-file=" + config_filename,
+                       "-o", rules_filename]
+            process = subprocess.Popen(command, stderr=error_file)
+            ret = process.wait()
+            if ret != 0:
+                raise RuntimeError("%r failed with exit code %d"
+                                   % (command, ret))
+
+        finally:
+            error_file.close()
+
+        self.check_file_contents(rules_filename, expected_rules)
+
+
+    def test_create_rewrite_rules_no_wild_cards(self):
+        self.run_test("""[inteproxy-rules]
+urls=owsproxy://bob:secret@demo.intevation.de/wms
+""",
+                      """\
+RewriteRule ^/demo\\.intevation\\.de\\/wms$ https:/$0?user=bob&password=secret [QSA,P]
+""")
+
+    def test_create_rewrite_rules_wild_cards(self):
+        self.run_test("""[inteproxy-rules]
+urls=owsproxy://bob:secret@*.intevation.de/wms/*
+""",
+                      """\
+RewriteRule ^/[^/]*\\.intevation\\.de\\/wms\\/.*$ https:/$0?user=bob&password=secret [QSA,P]
+""")
+
+    def test_create_rewrite_rules_wild_cards_special_characters(self):
+        self.run_test("""[inteproxy-rules]
+urls=owsproxy://bob:$ec%%20ret!@*.intevation.de/wms/*
+""",
+                      """\
+RewriteRule ^/[^/]*\\.intevation\\.de\\/wms\\/.*$ https:/$0?user=bob&password=\\%24ec\\+ret\\%21 [QSA,P]
+""")
+
+    def test_create_rewrite_rules_port(self):
+        self.run_test("""[inteproxy-rules]
+urls=owsproxy://bob:secret@*.intevation.de:8000/wms/*
+""",
+                      """\
+RewriteRule ^/[^/]*\\.intevation\\.de\\:8000\\/wms\\/.*$ https:/$0?user=bob&password=secret [QSA,P]
+""")


Property changes on: trunk/test/test_create_rewrite_rules.py
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native



More information about the Inteproxy-commits mailing list