def test_urlEncoding(self): """Test URL encoding of username and password.""" auth = DbAuth(authList=[ dict(url="postgresql://host.example.com/my_database", username="******", password="******")]) self.assertEqual( auth.getUrl("postgresql://host.example.com:5432/my_database"), "postgresql://foo%3A%C3%A9tude:bar%2C.%40%25%26%2F" "%2A%5B%[email protected]:5432/my_database")
def test_load(self): """Test loading from a YAML file and matching in order.""" filePath = os.path.join(TESTDIR, "good_test.yaml") os.chmod(filePath, 0o600) auth = DbAuth(filePath) self.assertEqual( auth.getAuth("postgresql", "user", "host.example.com", "5432", "my_database"), ("user", "test1")) self.assertEqual( auth.getAuth("postgresql", "user", "host.example.com", "3360", "my_database"), ("user", "test2")) self.assertEqual( auth.getAuth("postgresql", "", "host.example.com", "5432", "my_database"), ("user", "test3")) self.assertEqual( auth.getAuth("postgresql", None, "host.example.com", "", "my_database"), ("user", "test4")) self.assertEqual( auth.getAuth("postgresql", "", "host2.example.com", None, "my_other"), ("user", "test5")) self.assertEqual( auth.getAuth("postgresql", None, "host3.example.com", 3360, "some_database"), ("user", "test6")) self.assertEqual( auth.getAuth("postgresql", "user", "host4.other.com", None, "other_database"), ("user", "test8"))
def test_search(self): """Test ordered searching.""" auth = DbAuth(authList=[ dict(url="postgresql://[email protected]:3360/database3", password="******"), dict(url="postgresql://host2.example.com:3360/database3", username="******", password="******"), dict(url="postgresql://host2.example.com:5432/database3", username="******", password="******"), dict(url="postgresql://host3.example.com/", username="******", password="******"), dict(url="oracle://oracle.example.com/other_database", username="******", password="******"), dict(url="postgresql://*.example.com/database3", username="******", password="******"), dict(url="postgresql://*.example.com", username="******", password="******")]) self.assertEqual( auth.getAuth("postgresql", "user1", "host2.example.com", "3360", "database3"), ("user1", "first")) self.assertEqual( auth.getAuth("postgresql", "user2", "host2.example.com", "3360", "database3"), ("user2", "second")) self.assertEqual( auth.getAuth("postgresql", "", "host2.example.com", "3360", "database3"), ("second_u", "second")) self.assertEqual( auth.getAuth("postgresql", None, "host2.example.com", "3360", "database3"), ("second_u", "second")) self.assertEqual( auth.getAuth("postgresql", None, "host3.example.com", "3360", "database3"), ("third_u", "third")) self.assertEqual( auth.getAuth("postgresql", None, "host3.example.com", "3360", "database4"), ("third_u", "third")) self.assertEqual( auth.getAuth("postgresql", None, "host4.example.com", "3360", "database3"), ("fourth_u", "fourth")) self.assertEqual( auth.getAuth("postgresql", None, "host4.example.com", "3360", "database4"), ("fifth_u", "fifth"))
def test_patterns(self): """Test various patterns against a fixed connection URL.""" for matchPattern in [ "postgresql://*", "postgresql://*.example.com", "postgresql://*.example.com/my_*", "postgresql://host.example.com/my_database", "postgresql://host.example.com:5432/my_database", "postgresql://[email protected]/my_database" ]: auth = DbAuth(authList=[ dict(url=matchPattern, username="******", password="******") ]) user, pwd = auth.getAuth("postgresql", "user", "host.example.com", "5432", "my_database") self.assertEqual(user, "user") self.assertEqual(pwd, "bar")
def test_connStrings(self): """Test various connection URLs against a fixed pattern.""" auth = DbAuth(authList=[ dict(url="postgresql://host.example.com/my_database", username="******", password="******") ]) for connComponents in [ ("postgresql", "user", "host.example.com", 5432, "my_database"), ("postgresql", "user", "host.example.com", None, "my_database") ]: user, pwd = auth.getAuth(*connComponents) self.assertEqual(user, "user") self.assertEqual(pwd, "bar") for connComponents in [ ("postgresql", None, "host.example.com", None, "my_database"), ("postgresql", None, "host.example.com", "5432", "my_database") ]: user, pwd = auth.getAuth(*connComponents) self.assertEqual(user, "foo") self.assertEqual(pwd, "bar")
def test_ipv6(self): """Test IPv6 addresses as host names.""" auth = DbAuth(authList=[ dict(url="postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db", password="******"), dict(url="postgresql://[fe80::1ff:fe23:4567:890a]:5432/db", password="******"), dict(url="postgresql://user3@[fe80::1ff:fe23:4567:890a]/db", password="******"), dict(url="postgresql://[fe80::1ff:fe23:4567:890a]/db", password="******")]) self.assertEqual( auth.getAuth("postgresql", "user", "fe80::1ff:fe23:4567:890a", "5432", "db"), ("user", "pwd")) self.assertEqual( auth.getAuth("postgresql", "user2", "fe80::1ff:fe23:4567:890a", "5432", "db"), ("user2", "pwd2")) self.assertEqual( auth.getAuth("postgresql", "user3", "fe80::1ff:fe23:4567:890a", "3360", "db"), ("user3", "pwd3")) self.assertEqual( auth.getAuth("postgresql", "user4", "fe80::1ff:fe23:4567:890a", "3360", "db"), ("user4", "pwd4"))
def test_getUrl(self): """Repeat relevant tests using getUrl interface.""" for matchPattern in [ "postgresql://*", "postgresql://*.example.com", "postgresql://*.example.com/my_*", "postgresql://host.example.com/my_database", "postgresql://host.example.com:5432/my_database", "postgresql://[email protected]/my_database" ]: auth = DbAuth(authList=[ dict(url=matchPattern, username="******", password="******") ]) self.assertEqual( auth.getUrl( "postgresql://[email protected]:5432/my_database"), "postgresql://*****:*****@host.example.com:5432/my_database") auth = DbAuth(authList=[ dict(url="postgresql://host.example.com/my_database", username="******", password="******") ]) self.assertEqual( auth.getUrl("postgresql://[email protected]:5432/my_database"), "postgresql://*****:*****@host.example.com:5432/my_database"), self.assertEqual( auth.getUrl("postgresql://[email protected]/my_database"), "postgresql://*****:*****@host.example.com/my_database"), self.assertEqual( auth.getUrl("postgresql://host.example.com/my_database"), "postgresql://*****:*****@host.example.com/my_database"), self.assertEqual( auth.getUrl("postgresql://host.example.com:5432/my_database"), "postgresql://*****:*****@host.example.com:5432/my_database"), filePath = os.path.join(TESTDIR, "good_test.yaml") os.chmod(filePath, 0o600) auth = DbAuth(filePath) self.assertEqual( auth.getUrl("postgresql://[email protected]:5432/my_database"), "postgresql://*****:*****@host.example.com:5432/my_database") self.assertEqual( auth.getUrl("postgresql://[email protected]:3360/my_database"), "postgresql://*****:*****@host.example.com:3360/my_database") self.assertEqual( auth.getUrl("postgresql://host.example.com:5432/my_database"), "postgresql://*****:*****@host.example.com:5432/my_database") self.assertEqual( auth.getUrl("postgresql://host.example.com/my_database"), "postgresql://*****:*****@host.example.com/my_database") self.assertEqual( auth.getUrl("postgresql://host2.example.com/my_other"), "postgresql://*****:*****@host2.example.com/my_other") self.assertEqual( auth.getUrl("postgresql://host3.example.com:3360/some_database"), "postgresql://*****:*****@host3.example.com:3360/some_database") self.assertEqual( auth.getUrl("postgresql://[email protected]/other_database"), "postgresql://*****:*****@host4.other.com/other_database") auth = DbAuth(authList=[ dict(url="postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db", password="******"), dict(url="postgresql://[fe80::1ff:fe23:4567:890a]:5432/db", password="******"), dict(url="postgresql://user3@[fe80::1ff:fe23:4567:890a]/db", password="******"), dict(url="postgresql://[fe80::1ff:fe23:4567:890a]/db", password="******") ]) self.assertEqual( auth.getUrl( "postgresql://user@[fe80::1ff:fe23:4567:890a]:5432/db"), "postgresql://*****:*****@[fe80::1ff:fe23:4567:890a]:5432/db") self.assertEqual( auth.getUrl( "postgresql://user2@[fe80::1ff:fe23:4567:890a]:5432/db"), "postgresql://*****:*****@[fe80::1ff:fe23:4567:890a]:5432/db") self.assertEqual( auth.getUrl( "postgresql://user3@[fe80::1ff:fe23:4567:890a]:3360/db"), "postgresql://*****:*****@[fe80::1ff:fe23:4567:890a]:3360/db") self.assertEqual( auth.getUrl( "postgresql://user4@[fe80::1ff:fe23:4567:890a]:3360/db"), "postgresql://*****:*****@[fe80::1ff:fe23:4567:890a]:3360/db") auth = DbAuth(authList=[ dict(url="postgresql://[email protected]:3360/database3", password="******"), dict(url="postgresql://host2.example.com:3360/database3", username="******", password="******"), dict(url="postgresql://host2.example.com:5432/database3", username="******", password="******"), dict(url="postgresql://host3.example.com/", username="******", password="******"), dict(url="oracle://oracle.example.com/other_database", username="******", password="******"), dict(url="postgresql://*.example.com/database3", username="******", password="******"), dict(url="postgresql://*.example.com", username="******", password="******") ]) self.assertEqual( auth.getUrl("postgresql://[email protected]:3360/database3"), "postgresql://*****:*****@host2.example.com:3360/database3") self.assertEqual( auth.getUrl("postgresql://[email protected]:3360/database3"), "postgresql://*****:*****@host2.example.com:3360/database3") self.assertEqual( auth.getUrl("postgresql://host2.example.com:3360/database3"), "postgresql://*****:*****@host2.example.com:3360/database3") self.assertEqual( auth.getUrl("postgresql://host3.example.com:3360/database3"), "postgresql://*****:*****@host3.example.com:3360/database3") self.assertEqual( auth.getUrl("postgresql://host3.example.com:3360/database4"), "postgresql://*****:*****@host3.example.com:3360/database4") self.assertEqual( auth.getUrl("postgresql://host4.example.com:3360/database3"), "postgresql://*****:*****@host4.example.com:3360/database3") self.assertEqual( auth.getUrl("postgresql://host4.example.com:3360/database4"), "postgresql://*****:*****@host4.example.com:3360/database4") auth = DbAuth(authList=[]) with self.assertRaisesRegex(DbAuthError, r"^Missing drivername parameter$"): auth.getUrl("/") with self.assertRaisesRegex(DbAuthError, r"^Missing host parameter$"): auth.getUrl("postgresql:///") with self.assertRaisesRegex(DbAuthError, r"^Missing database parameter$"): auth.getUrl("postgresql://example.com") with self.assertRaisesRegex(DbAuthError, r"^Missing database parameter$"): auth.getUrl("postgresql://example.com/") with self.assertRaisesRegex( DbAuthError, r"^No matching DbAuth configuration for: " r"\(postgresql, None, example\.com, None, foo\)$"): auth.getUrl("postgresql://example.com/foo")
def test_errors(self): """Test for error exceptions.""" with self.assertRaisesRegex( DbAuthError, r"^No default path provided to DbAuth configuration file$"): auth = DbAuth() with self.assertRaisesRegex( DbAuthError, r"^No DbAuth configuration file: .*this_does_not_exist$"): auth = DbAuth(os.path.join(TESTDIR, "this_does_not_exist")) with self.assertRaisesRegex( DbAuthError, r"^No DbAuth configuration file: .*this_does_not_exist$"): auth = DbAuth(os.path.join(TESTDIR, "this_does_not_exist"), envVar="LSST_NONEXISTENT") with self.assertRaisesRegex( DbAuthError, r"^DbAuth configuration file .*bad_test1.yaml has incorrect " r"permissions: \d*644$"): filePath = os.path.join(TESTDIR, "bad_test1.yaml") os.chmod(filePath, 0o644) auth = DbAuth(filePath) with self.assertRaisesRegex( DbAuthError, r"^Unable to load DbAuth configuration file: " r".*bad_test2.yaml"): filePath = os.path.join(TESTDIR, "bad_test2.yaml") os.chmod(filePath, 0o600) os.environ["LSST_DB_AUTH_TEST"] = filePath auth = DbAuth(envVar="LSST_DB_AUTH_TEST") auth = DbAuth(authList=[]) with self.assertRaisesRegex(DbAuthError, r"^Missing drivername parameter$"): auth.getAuth(None, None, None, None, None) with self.assertRaisesRegex(DbAuthError, r"^Missing drivername parameter$"): auth.getAuth("", None, None, None, None) with self.assertRaisesRegex(DbAuthError, r"^Missing host parameter$"): auth.getAuth("postgresql", None, None, None, None) with self.assertRaisesRegex(DbAuthError, r"^Missing host parameter$"): auth.getAuth("postgresql", None, "", None, None) with self.assertRaisesRegex(DbAuthError, r"^Missing database parameter$"): auth.getAuth("postgresql", None, "example.com", None, None) with self.assertRaisesRegex(DbAuthError, r"^Missing database parameter$"): auth.getAuth("postgresql", None, "example.com", None, "") with self.assertRaisesRegex( DbAuthError, r"^No matching DbAuth configuration for: " r"\(postgresql, None, example\.com, None, foo\)$"): auth.getAuth("postgresql", None, "example.com", None, "foo") auth = DbAuth(authList=[dict(password="******")]) with self.assertRaisesRegex(DbAuthError, r"^Missing URL in DbAuth configuration$"): auth.getAuth("postgresql", None, "example.com", None, "foo") auth = DbAuth(authList=[dict(url="testing", password="******")]) with self.assertRaisesRegex( DbAuthError, r"^Missing database driver in URL: testing$"): auth.getAuth("postgresql", None, "example.com", None, "foo") auth = DbAuth( authList=[dict(url="postgresql:///foo", password="******")]) with self.assertRaisesRegex( DbAuthError, r"^Missing host in URL: postgresql:///foo$"): auth.getAuth("postgresql", None, "example.com", None, "foo")