Skip to content

Commit

Permalink
Merge pull request #9 from chris-allan/escaping
Browse files Browse the repository at this point in the history
Configuration Escaping
  • Loading branch information
chris-allan authored Jan 15, 2024
2 parents ef03e29 + 70c945e commit c70c823
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
build:
strategy:
matrix:
java: [8, 11]
java: [11, 17]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Requirements
============

* OMERO 5.6.x+
* Java 8+
* Java 11+

Workflow
========
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ version = '0.3.1-SNAPSHOT'

mainClassName = 'com.glencoesoftware.ldaptool.Main'

sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava {
options.release = 11
}

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
46 changes: 31 additions & 15 deletions src/main/java/com/glencoesoftware/ldaptool/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import ome.logic.LdapImpl;
import ome.system.OmeroContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -67,7 +70,7 @@ public class Main
required = true,
scope = ScopeType.INHERIT
)
File config;
Path config;

// Non-CLI fields
OmeroContext context;
Expand Down Expand Up @@ -97,32 +100,45 @@ private int executionStrategy(ParseResult parseResult) {
if (doInit.get()
&& !parseResult.isUsageHelpRequested()
&& !parseResult.isVersionHelpRequested()) {
init();
try {
init();
} catch (RuntimeException re) {
throw re;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return new CommandLine.RunLast().execute(parseResult);
}

public void init() {
public void init() throws IOException {
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger)
LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.toLevel(logLevel));

log.info("Loading LDAP configuration from: {}",
config.getAbsolutePath());
try (FileInputStream v = new FileInputStream(config)) {
Properties properties = System.getProperties();
properties.load(v);
log.info("Properties: {}", properties);
System.setProperties(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
log.info("Loading LDAP configuration from: {}", config);
String asString = Files.readString(config);
// Configuration will come out of `omero config get` or similar without
// backslash escaping. If configuration is sourced this way it is
// unlikely to be escaped correctly, a Java properties requirement,
// before being passed in so we will perform the escaping ourselves.
asString = asString.replace("\\", "\\\\");
ByteArrayInputStream v = new ByteArrayInputStream(
asString.getBytes(StandardCharsets.UTF_8));
Properties properties = System.getProperties();
properties.load(v);
log.info("Properties: {}", properties);
System.setProperties(properties);

context = new OmeroContext(new String[]{
"classpath:ome/config.xml",
"classpath:ome/services/datalayer.xml",
"classpath*:beanRefContext.xml"});
ldapImpl = (LdapImpl) context.getBean("internal-ome.api.ILdap");
if (!ldapImpl.getSetting()) {
throw new RuntimeException(
"LDAP is not enabled, is `omero.ldap.config` set?");
}
ldapTemplate = (LdapTemplate) context.getBean("ldapTemplate");
}
}

0 comments on commit c70c823

Please sign in to comment.