def test421Upgrade(self): """ When upgraded 4.2.0 properties to 4.2.1, ${dn} items in omero.ldap.* properties are changed to @{dn} """ p = create_path() # How config was written in 4.2.0 XML = Element("icegrid") active = SubElement(XML, "properties", id="__ACTIVE__") default = SubElement(XML, "properties", id="default") for properties in (active, default): SubElement( properties, "property", name="omero.config.version", value="4.2.0") SubElement( properties, "property", name="omero.ldap.new_user_group", value="member=${dn}") SubElement( properties, "property", name="omero.ldap.new_user_group_2", value="member=$${omero.dollar}{dn}") string = tostring(XML, 'utf-8') txt = xml.dom.minidom.parseString(string).toprettyxml(" ", "\n", None) p.write_text(txt) config = ConfigXml(filename=str(p), env_config="default") try: m = config.as_map() assert "member=@{dn}" == m["omero.ldap.new_user_group"] assert "member=@{dn}" == m["omero.ldap.new_user_group_2"] finally: config.close()
def test_4(self, input, output): p = write_config(input) config = ConfigXml(filename=str(p), env_config="default") try: m = config.as_map() s = strip_dict(m, suffix="blitz") assert s == output finally: config.close()
def testLocking(self): p = create_path() config1 = ConfigXml(filename=str(p)) try: ConfigXml(filename=str(p)) assert False, "No exception" except portalocker.LockException: pass config1.close()
def testNewVersioning(self): """ All property blocks should always have a version set. """ p = create_path() config = ConfigXml(filename=str(p)) m = config.as_map() for k, v in m.items(): assert "4.2.1" == v
def testNewVersioning(self): """ All property blocks should always have a version set. """ p = create_path() config = ConfigXml(filename=str(p)) m = config.as_map() for k, v in m.items(): assert "5.1.0" == v
def test_adjust(self, fixture): p = write_config(fixture.input) xml = template_xml() config = ConfigXml(filename=str(p), env_config="default") try: rv = adjust_settings(config, xml, **fixture.kwargs) fixture.validate(rv) finally: config.close()
def test_adjust(self, fixture, monkeypatch): monkeypatch.setattr(Strategy, "_system_memory_mb_java", lambda x: (2000, 4000)) p = write_config(fixture.input) xml = template_xml() config = ConfigXml(filename=str(p), env_config="default") try: rv = adjust_settings(config, xml, **fixture.kwargs) fixture.validate(rv) finally: config.close()
def test_adjust(self, fixture, monkeypatch): monkeypatch.setattr(Strategy, '_system_memory_mb_java', lambda x: (2000, 4000)) p = write_config(fixture.input) xml = template_xml() config = ConfigXml(filename=str(p), env_config="default") try: rv = adjust_settings(config, xml, **fixture.kwargs) fixture.validate(rv) finally: config.close()
def testAsDict(self): p = create_path() config = ConfigXml(filename=str(p), env_config="DICT") config["omero.data.dir"] = "HOME" config.close() i = initial("DICT") _ = SubElement(i[0][0], "property", name="omero.data.dir", value="HOME") _ = SubElement(i, "properties", id="DICT") _ = SubElement(_, "property", name="omero.data.dir", value="HOME") assertXml(i, XML(p.text()))
def testOldVersionDetected(self): p = create_path() config = ConfigXml(filename=str(p)) X = config.XML O = SubElement(X, "properties", {"id": "old"}) SubElement(O, "property", {"omero.ldap.keystore": "/Foo"}) config.close() try: config = ConfigXml(filename=str(p)) assert False, "Should throw" except: pass
def testSettings510Upgrade(self): """ When upgraded 5.0.x properties to 5.1.0 or later, if omero.web.ui.top_links is set, we need to prepend 'Data', 'History' and 'Help' links """ beforeUpdate = '[["Figure", "figure_index"]]' afterUpdate = '[["Data", "webindex", ' \ '{"title": "Browse Data via Projects, Tags etc"}], ' \ '["History", "history", ' \ '{"title": "History"}], ' \ '["Help", "https://help.openmicroscopy.org/", ' \ '{"target": "new", "title": ' \ '"Open OMERO user guide in a new tab"}], ' \ '["Figure", "figure_index"]]' p = create_path() XML = Element("icegrid") active = SubElement(XML, "properties", id="__ACTIVE__") default = SubElement(XML, "properties", id="default") for properties in (active, default): # Include a property to indicate version is post-4.2.0 SubElement(properties, "property", name="omero.config.version", value="4.2.1") SubElement(properties, "property", name="omero.web.ui.top_links", value=beforeUpdate) string = tostring(XML, 'utf-8') txt = xml.dom.minidom.parseString(string).toprettyxml(" ", "\n", None) p.write_text(txt) def compare_json_maps(config, afterUpdate): m = config.as_map() beforeUpdate = m["omero.web.ui.top_links"] assert json.loads(beforeUpdate) == json.loads(afterUpdate) config = ConfigXml(filename=str(p), env_config="default") try: compare_json_maps(config, afterUpdate) finally: config.close() # After config.close() calls config.save() new version should be 5.1.0 config = ConfigXml(filename=str(p), env_config="default") try: # Check version has been updated assert config.version() == "5.1.0" # And that top_links has not been modified further compare_json_maps(config, afterUpdate) finally: config.close()
def get_config(self, raise_missing=True): """ Returns a dictionary of all OMERO config properties """ configxml = os.path.join(self.dir, 'etc', 'grid', 'config.xml') try: configobj = ConfigXml(configxml, read_only=True) except Exception as e: log.warning('config.xml not found: %s', e) if raise_missing: raise return {} cfgdict = configobj.as_map() configobj.close() return cfgdict
def testCannotCreate(self): d = create_path(folder=True) d.chmod(0o555) filename = str(old_div(d, "config.xml")) with pytest.raises(IOError) as excinfo: ConfigXml(filename).close() assert excinfo.value.errno == errno.EACCES
def testSettings510Upgrade(self): """ When upgraded 5.0.x properties to 5.1.0 or later, if omero.web.ui.top_links is set, we need to prepend 'Data', 'History' and 'Help' links """ beforeUpdate = '[["Figure", "figure_index"]]' afterUpdate = '[["Data", "webindex", ' \ '{"title": "Browse Data via Projects, Tags etc"}], ' \ '["History", "history", ' \ '{"title": "History"}], ' \ '["Help", "http://help.openmicroscopy.org/", ' \ '{"target": "new", "title": ' \ '"Open OMERO user guide in a new tab"}], ' \ '["Figure", "figure_index"]]' p = create_path() XML = Element("icegrid") active = SubElement(XML, "properties", id="__ACTIVE__") default = SubElement(XML, "properties", id="default") for properties in (active, default): # Include a property to indicate version is post-4.2.0 SubElement( properties, "property", name="omero.config.version", value="4.2.1") SubElement( properties, "property", name="omero.web.ui.top_links", value=beforeUpdate) string = tostring(XML, 'utf-8') txt = xml.dom.minidom.parseString(string).toprettyxml(" ", "\n", None) p.write_text(txt) config = ConfigXml(filename=str(p), env_config="default") try: m = config.as_map() assert m["omero.web.ui.top_links"] == afterUpdate finally: config.close() # After config.close() calls config.save() new version should be 5.1.0 config = ConfigXml(filename=str(p), env_config="default") try: # Check version has been updated assert config.version() == "5.1.0" m = config.as_map() # And that top_links has not been modified further assert m["omero.web.ui.top_links"] == afterUpdate finally: config.close()
def test_12527(self, fixture, monkeypatch): monkeypatch.setattr(Strategy, '_system_memory_mb_java', lambda x: (2000, 4000)) p = write_config(fixture.input) old_templates = old_div(path(__file__).dirname(), "old_templates.xml") xml = XML(old_templates.abspath().text()) config = ConfigXml(filename=str(p), env_config="default") with pytest.raises(Exception): adjust_settings(config, xml, **fixture.kwargs)
def testCannotCreateLock(self): d = create_path(folder=True) filename = str(old_div(d, "config.xml")) lock_filename = "%s.lock" % filename with open(lock_filename, "w") as fo: fo.write("dummy\n") os.chmod(lock_filename, 0o444) with pytest.raises(IOError) as excinfo: ConfigXml(filename).close() assert excinfo.value.errno == errno.EACCES
def update_config(omerodir): """ Updates OMERO config with certificate properties if necessary """ cfg = ConfigXml(os.path.join(omerodir, "etc", "grid", "config.xml")) cfgdict = cfg.as_map() def set_if_empty(cfgkey, default): if not cfgdict.get(cfgkey): cfg[cfgkey] = default log.info("Setting %s=%s", cfgkey, default) set_if_empty( "omero.glacier2.IceSSL.DefaultDir", os.path.join(cfgdict.get("omero.data.dir", "/OMERO"), "certs"), ) set_if_empty("omero.certificates.commonname", "localhost") set_if_empty("omero.certificates.owner", "/L=OMERO/O=OMERO.server") set_if_empty("omero.certificates.key", "server.key") set_if_empty("omero.glacier2.IceSSL.CertFile", "server.p12") set_if_empty("omero.glacier2.IceSSL.CAs", "server.pem") set_if_empty("omero.glacier2.IceSSL.Password", "secret") set_if_empty("omero.glacier2.IceSSL.Ciphers", "HIGH") set_if_empty("omero.glacier2.IceSSL.ProtocolVersionMax", "TLS1_2") set_if_empty("omero.glacier2.IceSSL.Protocols", "TLS1_0,TLS1_1,TLS1_2") cfgdict = cfg.as_map() cfg.close() return cfgdict
def test_create_certificates(self, tmpdir): (tmpdir / "etc" / "grid").ensure(dir=True) omerodir = str(tmpdir) datadir = str(tmpdir / "OMERO") configxml = ConfigXml( os.path.join(omerodir, "etc", "grid", "config.xml")) configxml["omero.data.dir"] = datadir configxml.close() m = create_certificates(omerodir) assert m.startswith("certificates created: ") cfg = get_config(omerodir) assert cfg["omero.glacier2.IceSSL.DefaultDir"] == os.path.join( datadir, "certs") for filename in ("server.key", "server.p12", "server.pem"): assert os.path.isfile(os.path.join(datadir, "certs", filename)) out = subprocess.check_output([ "openssl", "pkcs12", "-in", os.path.join(datadir, "certs", "server.p12"), "-passin", "pass:secret", "-passout", "pass:secret", ]) out = out.decode().splitlines() for line in ( "subject=L = OMERO, O = OMERO.server, CN = localhost", "issuer=L = OMERO, O = OMERO.server, CN = localhost", "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----", "-----BEGIN ENCRYPTED PRIVATE KEY-----", "-----END ENCRYPTED PRIVATE KEY-----", ): assert line in out
def _get_current_as_json(config: ConfigXml, key: str) -> Optional[DictOrList]: """ Get current key value converted from JSON to a list or dict, taking into account OMERO.web's defaults """ if key in config.keys(): current = json.loads(config[key]) if not isinstance(current, (dict, list)): raise ExternalConfigException( "Property {} is not a dict or list: {}".format(key, current)) return current if key.startswith("omero.web."): return _get_omeroweb_default(key) return None
def test_config_keep_existing(self, tmpdir): (tmpdir / "etc" / "grid").ensure(dir=True) omerodir = str(tmpdir) configxml = ConfigXml( os.path.join(omerodir, "etc", "grid", "config.xml")) configxml["omero.certificates.commonname"] = "omero.example.org" configxml["omero.certificates.owner"] = "/L=universe/O=42" configxml.close() update_config(omerodir) cfg = get_config(omerodir) cfg.pop("omero.config.version") assert cfg == { "omero.glacier2.IceSSL.CAs": "server.pem", "omero.glacier2.IceSSL.CertFile": "server.p12", "omero.glacier2.IceSSL.Ciphers": "HIGH", "omero.glacier2.IceSSL.DefaultDir": "/OMERO/certs", "omero.glacier2.IceSSL.Password": "******", "omero.glacier2.IceSSL.ProtocolVersionMax": "TLS1_2", "omero.glacier2.IceSSL.Protocols": "TLS1_0,TLS1_1,TLS1_2", "omero.certificates.commonname": "omero.example.org", "omero.certificates.key": "server.key", "omero.certificates.owner": "/L=universe/O=42", }
def open_config(self, args): if args.source: cfg_xml = path(args.source) if not cfg_xml.exists(): self.ctx.die(124, "File not found: %s" % args.source) else: grid_dir = self.ctx.dir / "etc" / "grid" if grid_dir.exists(): cfg_xml = grid_dir / "config.xml" else: userdir = path(get_user_dir()) usr_xml = userdir / "omero" / "config.xml" self.ctx.err("%s not found; using %s" % (grid_dir, usr_xml)) cfg_xml = usr_xml try: return ConfigXml(str(cfg_xml)) except portalocker.LockException: self.ctx.die(112, "Could not acquire lock on %s" % cfg_xml)
def open_config(self, args): if args.source: cfg_xml = path(args.source) if not cfg_xml.exists(): self.ctx.die(124, "File not found: %s" % args.source) else: if 'OMERODIR' in os.environ: base_dir = path(os.environ.get('OMERODIR')) else: self.ctx.die(125, 'FATAL: OMERODIR env variable not set') grid_dir = base_dir / "etc" / "grid" if not grid_dir.exists(): self.ctx.err("%s not found; creating %s" % (grid_dir, grid_dir)) os.makedirs(grid_dir) cfg_xml = grid_dir / "config.xml" try: return ConfigXml(str(cfg_xml)) except portalocker.LockException: self.ctx.die(112, "Could not acquire lock on %s" % cfg_xml) except Exception as e: self.ctx.die(113, str(e))
def testReadOnlyConfigSimple(self): p = create_path() p.chmod(0444) # r--r--r-- config = ConfigXml(filename=str(p), env_config=None) # Must be None config.close() # Shouldn't save
def update_config(self, newcfg): cfg = ConfigXml(os.path.join(self.dir, 'etc', 'grid', 'config.xml')) for k, v in newcfg.items(): cfg[k] = v cfg.close()
def testBasic(self): p = create_path() config = ConfigXml(filename=str(p)) config.close() assertXml(initial(), XML(p.text()))
def testWithEnv(self): p = create_path() config = ConfigXml(filename=str(p), env_config="FOO") config.close() assertXml(initial("FOO"), XML(p.text()))
def get_config(omerodir): configxml = ConfigXml(os.path.join(omerodir, "etc", "grid", "config.xml")) try: return configxml.as_map() finally: configxml.close()
def testCannotRead(self): p = create_path() p.chmod(0) with pytest.raises(IOError) as excinfo: ConfigXml(str(p)).close() assert excinfo.value.errno == errno.EACCES
def testWithEnvThenWithoutEnv(self): """ This test shows that if you create the config using an env setting, then without, the __ACTIVE__ block should reflect "default" and not the intermediate "env" setting. """ def get_profile_name(p): """ Takes a path object to the config xml """ xml = XML(p.text()) props = xml.findall("./properties") for x in props: id = x.attrib["id"] if id == "__ACTIVE__": for y in x.getchildren(): if y.attrib["name"] == "omero.config.profile": return y.attrib["value"] p = create_path() current = os.environ.get("OMERO_CONFIG", "default") assert current != "FOO" # Just in case. config = ConfigXml(filename=str(p)) config.close() assert current == get_profile_name(p) config = ConfigXml(filename=str(p), env_config="FOO") config.close() assert "FOO" == get_profile_name(p) # Still foo config = ConfigXml(filename=str(p)) config.close() assert "FOO" == get_profile_name(p) # Re-setting with os.environ won't work try: old = os.environ.get("OMERO_CONFIG", None) os.environ["OMERO_CONFIG"] = "ABC" config = ConfigXml(filename=str(p)) config.close() assert "FOO" == get_profile_name(p) finally: if old is None: del os.environ["OMERO_CONFIG"] else: os.environ["OMERO_CONFIG"] = old # But we can reset it with env_config config = ConfigXml(filename=str(p), env_config="ABC") config.close() assert "ABC" == get_profile_name(p) # or manually. ticket:7343 config = ConfigXml(filename=str(p)) config.default("XYZ") config.close() assert "XYZ" == get_profile_name(p)
def testReadOnlyConfigPassesOnExplicitReadOnly(self): p = create_path() p.chmod(0444) # r--r--r-- ConfigXml(filename=str(p), env_config="default", read_only=True).close()
def config(self): return ConfigXml(filename=str(self.p))
def _get_config_xml(omerodir: str) -> ConfigXml: return ConfigXml(os.path.join(omerodir, "etc", "grid", "config.xml"))