Skip to content

Commit 531be32

Browse files
authored
convert LDAP authorization plugin configuration to YAML (#4599)
1 parent 9fd1f26 commit 531be32

File tree

15 files changed

+143
-291
lines changed

15 files changed

+143
-291
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/framework/PluginClassLoader.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
2323
*/
2424
package org.opengrok.indexer.framework;
@@ -96,7 +96,7 @@ private Class<?> loadClassFromJar(String classname) throws ClassNotFoundExceptio
9696
try (InputStream is = jar.getInputStream(entry)) {
9797
byte[] bytes = loadBytes(is);
9898
Class<?> c = defineClass(classname, bytes, 0, bytes.length);
99-
LOGGER.log(Level.FINE, "Class \"{0}\" found in file \"{1}\"",
99+
LOGGER.log(Level.FINE, "Class \"{0}\" found in file ''{1}''",
100100
new Object[]{
101101
classname,
102102
f.getAbsolutePath()
@@ -122,7 +122,7 @@ private Class<?> loadClassFromFile(String classname) throws ClassNotFoundExcepti
122122
byte[] bytes = loadBytes(in);
123123

124124
Class<?> c = defineClass(classname, bytes, 0, bytes.length);
125-
LOGGER.log(Level.FINEST, "Class \"{0}\" found in file \"{1}\"",
125+
LOGGER.log(Level.FINEST, "Class \"{0}\" found in file ''{1}''",
126126
new Object[]{
127127
classname,
128128
f.getAbsolutePath()
@@ -154,8 +154,7 @@ private boolean checkWhiteList(String name) {
154154
}
155155

156156
private void checkClassname(String name) throws SecurityException {
157-
if (name.startsWith("org.opengrok.")
158-
&& !checkWhiteList(name)) {
157+
if (name.startsWith("org.opengrok.") && !checkWhiteList(name)) {
159158
throw new SecurityException("Tried to load a blacklisted class \"" + name + "\"\n"
160159
+ "Allowed classes from opengrok package are only: "
161160
+ Arrays.toString(CLASS_WHITELIST));

opengrok-indexer/src/main/java/org/opengrok/indexer/framework/PluginFramework.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.framework;
2424

@@ -330,11 +330,12 @@ private String getClassName(JarEntry jarEntry) {
330330
File file = new File(pluginDirectory.getAbsolutePath(), filePath);
331331
try {
332332
if (!file.getCanonicalPath().startsWith(pluginDirectory.getCanonicalPath() + File.separator)) {
333-
LOGGER.log(Level.WARNING, "canonical path for jar entry {0} leads outside the origin", filePath);
333+
LOGGER.log(Level.WARNING, "canonical path for jar entry ''{0}'' leads outside the origin",
334+
filePath);
334335
return null;
335336
}
336337
} catch (IOException e) {
337-
LOGGER.log(Level.WARNING, "failed to get canonical path for {0}", file);
338+
LOGGER.log(Level.WARNING, "failed to get canonical path for ''{0}''", file);
338339
return null;
339340
}
340341

@@ -372,12 +373,12 @@ private String getClassName(JarEntry jarEntry) {
372373
*/
373374
public final void reload() {
374375
if (pluginDirectory == null || !pluginDirectory.isDirectory() || !pluginDirectory.canRead()) {
375-
LOGGER.log(Level.WARNING, "Plugin directory not found or not readable: {0}. "
376+
LOGGER.log(Level.WARNING, "Plugin directory ''{0}'' not found or not readable: "
376377
+ "All requests allowed.", pluginDirectory);
377378
return;
378379
}
379380

380-
LOGGER.log(Level.INFO, "Plugins are being reloaded from {0}", pluginDirectory.getAbsolutePath());
381+
LOGGER.log(Level.INFO, "Plugins are being reloaded from ''{0}''", pluginDirectory.getAbsolutePath());
381382

382383
// trashing out the old instance of the loader enables us
383384
// to reload the stack at runtime

opengrok-web/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1818
1919
CDDL HEADER END
2020
21-
Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2222
Portions Copyright (c) 2018, 2020, Chris Fraire <cfraire@me.com>.
2323
2424
-->
@@ -96,6 +96,12 @@ Portions Copyright (c) 2018, 2020, Chris Fraire <cfraire@me.com>.
9696
<artifactId>jersey-media-json-jackson</artifactId>
9797
<version>${jersey.version}</version>
9898
</dependency>
99+
<!-- needed for the authorization plugins' configuration -->
100+
<dependency>
101+
<groupId>com.fasterxml.jackson.dataformat</groupId>
102+
<artifactId>jackson-dataformat-yaml</artifactId>
103+
<version>${jackson.version}</version>
104+
</dependency>
99105
<dependency>
100106
<groupId>org.glassfish.jersey.ext</groupId>
101107
<artifactId>jersey-bean-validation</artifactId>

plugins/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1818
1919
CDDL HEADER END
2020
21-
Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
21+
Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
2222
Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
2323
2424
-->
@@ -75,6 +75,11 @@ Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
7575
<artifactId>jersey-media-json-jackson</artifactId>
7676
<version>${jersey.version}</version>
7777
</dependency>
78+
<dependency>
79+
<groupId>com.fasterxml.jackson.dataformat</groupId>
80+
<artifactId>jackson-dataformat-yaml</artifactId>
81+
<version>${jackson.version}</version>
82+
</dependency>
7883
<dependency>
7984
<groupId>org.glassfish.jersey.inject</groupId>
8085
<artifactId>jersey-hk2</artifactId>

plugins/src/main/java/opengrok/auth/plugin/configuration/Configuration.java

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin.configuration;
2424

25-
import java.beans.XMLDecoder;
26-
import java.beans.XMLEncoder;
27-
import java.io.BufferedInputStream;
28-
import java.io.BufferedOutputStream;
29-
import java.io.ByteArrayInputStream;
3025
import java.io.ByteArrayOutputStream;
3126
import java.io.File;
3227
import java.io.FileInputStream;
@@ -36,23 +31,44 @@
3631
import java.io.Serializable;
3732
import java.util.ArrayList;
3833
import java.util.List;
34+
35+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
36+
import com.fasterxml.jackson.annotation.JsonProperty;
37+
import com.fasterxml.jackson.databind.ObjectMapper;
38+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
39+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
3940
import opengrok.auth.plugin.ldap.LdapServer;
4041
import opengrok.auth.plugin.util.WebHooks;
4142

4243
/**
4344
* Encapsulates configuration for LDAP plugins.
4445
*/
46+
@JsonAutoDetect(
47+
fieldVisibility = JsonAutoDetect.Visibility.NONE,
48+
setterVisibility = JsonAutoDetect.Visibility.NONE,
49+
getterVisibility = JsonAutoDetect.Visibility.NONE,
50+
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
51+
creatorVisibility = JsonAutoDetect.Visibility.NONE
52+
)
4553
public class Configuration implements Serializable {
4654

4755
private static final long serialVersionUID = -1;
4856

57+
@JsonProperty
4958
private List<LdapServer> servers = new ArrayList<>();
59+
@JsonProperty
5060
private int interval;
61+
@JsonProperty
5162
private String searchBase;
63+
@JsonProperty
5264
private WebHooks webHooks;
65+
@JsonProperty
5366
private int searchTimeout;
67+
@JsonProperty
5468
private int connectTimeout;
69+
@JsonProperty
5570
private int readTimeout;
71+
@JsonProperty
5672
private int countLimit;
5773

5874
public void setServers(List<LdapServer> servers) {
@@ -119,20 +135,19 @@ public void setSearchBase(String base) {
119135
this.searchBase = base;
120136
}
121137

122-
public String getXMLRepresentationAsString() {
138+
String getObjectRepresentationAsString() throws IOException {
123139
ByteArrayOutputStream bos = new ByteArrayOutputStream();
124140
this.encodeObject(bos);
125141
return bos.toString();
126142
}
127143

128-
private void encodeObject(OutputStream out) {
129-
try (XMLEncoder e = new XMLEncoder(new BufferedOutputStream(out))) {
130-
e.writeObject(this);
131-
}
144+
void encodeObject(OutputStream out) throws IOException {
145+
var mapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
146+
mapper.writeValue(out, this);
132147
}
133148

134149
/**
135-
* Read a configuration from a file in XML format.
150+
* Read a configuration from a file.
136151
*
137152
* @param file input file
138153
* @return the new configuration object
@@ -144,32 +159,8 @@ public static Configuration read(File file) throws IOException {
144159
}
145160
}
146161

147-
/**
148-
* Read a configuration from a string in xml format.
149-
*
150-
* @param xmlconfig input string
151-
* @return the new configuration object
152-
* @throws IOException if any error occurs
153-
*/
154-
public static Configuration makeXMLStringAsConfiguration(String xmlconfig) throws IOException {
155-
final Configuration ret;
156-
final ByteArrayInputStream in = new ByteArrayInputStream(xmlconfig.getBytes());
157-
ret = decodeObject(in);
158-
return ret;
159-
}
160-
161-
private static Configuration decodeObject(InputStream in) throws IOException {
162-
final Object ret;
163-
164-
try (XMLDecoder d = new XMLDecoder(new BufferedInputStream(in), null, null,
165-
new PluginConfigurationClassLoader())) {
166-
ret = d.readObject();
167-
}
168-
169-
if (!(ret instanceof Configuration)) {
170-
throw new IOException("Not a valid configuration file");
171-
}
172-
173-
return (Configuration) ret;
162+
static Configuration decodeObject(InputStream in) throws IOException {
163+
var mapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
164+
return mapper.readValue(in, Configuration.class);
174165
}
175166
}

plugins/src/main/java/opengrok/auth/plugin/configuration/PluginConfigurationClassLoader.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)