Skip to content

convert LDAP authorization plugin configuration to YAML #4599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]

CDDL HEADER END

Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.

-->
Expand Down Expand Up @@ -75,6 +75,11 @@ Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@
*/

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

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -36,23 +31,44 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import opengrok.auth.plugin.ldap.LdapServer;
import opengrok.auth.plugin.util.WebHooks;

/**
* Encapsulates configuration for LDAP plugins.
*/
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
creatorVisibility = JsonAutoDetect.Visibility.NONE
)
public class Configuration implements Serializable {

private static final long serialVersionUID = -1;

@JsonProperty
private List<LdapServer> servers = new ArrayList<>();
@JsonProperty
private int interval;
@JsonProperty
private String searchBase;
@JsonProperty
private WebHooks webHooks;
@JsonProperty
private int searchTimeout;
@JsonProperty
private int connectTimeout;
@JsonProperty
private int readTimeout;
@JsonProperty
private int countLimit;

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

public String getXMLRepresentationAsString() {
String getObjectRepresentationAsString() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
this.encodeObject(bos);
return bos.toString();
}

private void encodeObject(OutputStream out) {
try (XMLEncoder e = new XMLEncoder(new BufferedOutputStream(out))) {
e.writeObject(this);
}
void encodeObject(OutputStream out) throws IOException {
var mapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
mapper.writeValue(out, this);
}

/**
* Read a configuration from a file in XML format.
* Read a configuration from a file.
*
* @param file input file
* @return the new configuration object
Expand All @@ -144,32 +159,8 @@ public static Configuration read(File file) throws IOException {
}
}

/**
* Read a configuration from a string in xml format.
*
* @param xmlconfig input string
* @return the new configuration object
* @throws IOException if any error occurs
*/
public static Configuration makeXMLStringAsConfiguration(String xmlconfig) throws IOException {
final Configuration ret;
final ByteArrayInputStream in = new ByteArrayInputStream(xmlconfig.getBytes());
ret = decodeObject(in);
return ret;
}

private static Configuration decodeObject(InputStream in) throws IOException {
final Object ret;

try (XMLDecoder d = new XMLDecoder(new BufferedInputStream(in), null, null,
new PluginConfigurationClassLoader())) {
ret = d.readObject();
}

if (!(ret instanceof Configuration)) {
throw new IOException("Not a valid configuration file");
}

return (Configuration) ret;
static Configuration decodeObject(InputStream in) throws IOException {
var mapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
return mapper.readValue(in, Configuration.class);
}
}

This file was deleted.

33 changes: 27 additions & 6 deletions plugins/src/main/java/opengrok/auth/plugin/ldap/LdapServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
*/

/*
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
*/
package opengrok.auth.plugin.ldap;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
Expand All @@ -44,6 +49,13 @@
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
creatorVisibility = JsonAutoDetect.Visibility.NONE
)
public class LdapServer implements Serializable {

private static final long serialVersionUID = -1;
Expand All @@ -55,17 +67,22 @@ public class LdapServer implements Serializable {
private static final String LDAP_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

// default connectTimeout value in milliseconds
private static final int LDAP_CONNECT_TIMEOUT = 5000;
private static final int DEFAULT_LDAP_CONNECT_TIMEOUT = 5000;
// default readTimeout value in milliseconds
private static final int LDAP_READ_TIMEOUT = 3000;
private static final int DEFAULT_LDAP_READ_TIMEOUT = 3000;

@JsonProperty
private String url;
@JsonProperty
private String username;
@JsonProperty
private String password;
@JsonProperty
private int connectTimeout;
@JsonProperty
private int readTimeout;
private int interval = 10 * 1000;

private int interval = 10 * 1000;
private final Map<String, String> env;
private transient LdapContext ctx;
private long errorTimestamp = 0;
Expand Down Expand Up @@ -166,6 +183,7 @@ public int getPort() throws URISyntaxException {
}
}

@JsonIgnore
private boolean isReachable(InetAddress addr, int port, int timeOutMillis) {
try (Socket soc = new Socket()) {
soc.connect(new InetSocketAddress(addr, port), timeOutMillis);
Expand All @@ -190,6 +208,7 @@ public InetAddress[] getAddresses(String hostname) throws UnknownHostException {
* Go through all IP addresses and find out if they are reachable.
* @return true if all IP addresses are reachable, false otherwise
*/
@JsonIgnore
public boolean isReachable() {
try {
InetAddress[] addresses = getAddresses(urlToHostname(getUrl()));
Expand Down Expand Up @@ -224,6 +243,7 @@ public boolean isReachable() {
*
* @return true if it is working
*/
@JsonIgnore
public synchronized boolean isWorking() {
if (ctx == null) {
if (!isReachable()) {
Expand All @@ -240,6 +260,7 @@ public synchronized boolean isWorking() {
*
* @return the new connection or null
*/
@Nullable
private synchronized LdapContext connect() {
LOGGER.log(Level.INFO, "Connecting to LDAP server {0} ", this);

Expand Down Expand Up @@ -354,8 +375,8 @@ private static Map<String, String> prepareEnv() {
var e = new HashMap<String, String>();

e.put(Context.INITIAL_CONTEXT_FACTORY, LDAP_CONTEXT_FACTORY);
e.put(LDAP_CONNECT_TIMEOUT_PARAMETER, Integer.toString(LDAP_CONNECT_TIMEOUT));
e.put(LDAP_READ_TIMEOUT_PARAMETER, Integer.toString(LDAP_READ_TIMEOUT));
e.put(LDAP_CONNECT_TIMEOUT_PARAMETER, Integer.toString(DEFAULT_LDAP_CONNECT_TIMEOUT));
e.put(LDAP_READ_TIMEOUT_PARAMETER, Integer.toString(DEFAULT_LDAP_READ_TIMEOUT));

return e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
*/
package opengrok.auth.plugin;

Expand Down Expand Up @@ -207,7 +207,7 @@ void testAttrLookup() throws LdapException {
private Map<String, Object> getParamsMap() {
Map<String, Object> params = new TreeMap<>();
params.put(AbstractLdapPlugin.CONFIGURATION_PARAM,
Objects.requireNonNull(getClass().getResource("config.xml")).getFile());
Objects.requireNonNull(getClass().getResource("config.yml")).getFile());

return params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
*/
package opengrok.auth.plugin;

Expand Down Expand Up @@ -121,7 +121,7 @@ void testLoadTransformsNegative() {
private Map<String, Object> getParamsMap() {
Map<String, Object> params = new TreeMap<>();
params.put(AbstractLdapPlugin.CONFIGURATION_PARAM,
Objects.requireNonNull(getClass().getResource("config.xml")).getFile());
Objects.requireNonNull(getClass().getResource("config.yml")).getFile());

return params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
*/
package opengrok.auth.plugin;

Expand Down Expand Up @@ -73,7 +73,7 @@ void setUp() {
private Map<String, Object> getParamsMap() {
Map<String, Object> params = new TreeMap<>();
params.put(AbstractLdapPlugin.CONFIGURATION_PARAM,
getClass().getResource("config.xml").getFile());
getClass().getResource("config.yml").getFile());

return params;
}
Expand Down
Loading
Loading