Skip to content

Commit

Permalink
Issue #223: Fixed authmode was not set correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
hypfvieh committed Jul 3, 2023
1 parent 0f57979 commit f28ac7e
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,17 @@ protected ObjectTree getObjectTree() {
return objectTree;
}

/**
* Returns the transport's configuration.<br>
* Please note: changing any value will not change the transport settings!<br>
* This is read-only.
*
* @return transport config
*/
public TransportConfig getTransportConfig() {
return transport.getTransportConfig();
}

/**
* Set the endianness to use for all connections.
* Defaults to the system architectures endianness.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,13 @@ public void setFileDescriptorSupport(boolean _fileDescriptorSupport) {
fileDescriptorSupport = _fileDescriptorSupport;
}

@Override
public String toString() {
return getClass().getSimpleName() + " [mode=" + mode + ", authMode=" + authMode
+ ", guid=" + guid + ", saslUid=" + saslUid
+ ", strictCookiePermissions=" + strictCookiePermissions
+ ", fileDescriptorSupport="
+ fileDescriptorSupport + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.freedesktop.dbus.connections.transports.TransportBuilder.SaslAuthMode;

import java.util.OptionalLong;
import java.util.function.Supplier;

/**
* Configuration used to setup a sasl authentication.
Expand All @@ -13,12 +12,12 @@
*/
public final class SaslConfigBuilder<R> {

private final SaslConfig saslConfig;
private SaslConfig saslConfig;

private final Supplier<TransportConfigBuilder<?, R>> transportBuilder;
private final TransportConfigBuilder<?, R> transportBuilder;

SaslConfigBuilder(SaslConfig _saslConfig, Supplier<TransportConfigBuilder<?, R>> _transportBuilder) {
saslConfig = _saslConfig;
SaslConfigBuilder(TransportConfigBuilder<?, R> _transportBuilder) {
saslConfig = new SaslConfig();
transportBuilder = _transportBuilder;
}

Expand All @@ -32,7 +31,7 @@ public final class SaslConfigBuilder<R> {
* @return previous builder, maybe null
*/
public TransportConfigBuilder<?, R> back() {
return transportBuilder.get();
return transportBuilder;
}

/**
Expand Down Expand Up @@ -81,4 +80,18 @@ public SaslConfigBuilder<R> withStrictCookiePermissions(boolean _strictCookiePer
public SaslConfig build() {
return saslConfig;
}

/**
* Replace the current {@link SaslConfig} with the given instance.<br>
* Will do nothing if <code>null</code> is given.
*
* @param _cfg sasl config
* @return this
*/
SaslConfigBuilder<R> withConfig(SaslConfig _cfg) {
if (_cfg != null) {
saslConfig = _cfg;
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
import org.freedesktop.dbus.utils.Util;

import java.nio.file.attribute.PosixFilePermission;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;

/**
Expand All @@ -21,7 +16,7 @@
*/
public final class TransportConfig {

private final SaslConfig saslConfig;
private SaslConfig saslConfig;

private BusAddress busAddress;

Expand All @@ -48,7 +43,6 @@ public final class TransportConfig {

public TransportConfig(BusAddress _address) {
busAddress = _address;
saslConfig = new SaslConfig();
}

public TransportConfig() {
Expand Down Expand Up @@ -136,9 +130,16 @@ public void setAdditionalConfig(Map<String, Object> _additionalConfig) {
}

public SaslConfig getSaslConfig() {
if (saslConfig == null) {
saslConfig = new SaslConfig();
}
return saslConfig;
}

void setSaslConfig(SaslConfig _saslCfg) {
saslConfig = _saslCfg;
}

/**
* Toggles the busaddress to be a listening (server) or non listening (client) connection.
* @param _listening true to be a server connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import java.util.function.Supplier;

public class TransportConfigBuilder<X extends TransportConfigBuilder<?, R>, R> {
private final Supplier<R> connectionBuilder;
private final Supplier<R> connectionBuilder;

private TransportConfig config = new TransportConfig();

private final SaslConfigBuilder<R> saslConfigBuilder;

public TransportConfigBuilder(Supplier<R> _sup) {
connectionBuilder = _sup;
saslConfigBuilder = new SaslConfigBuilder<>(config.getSaslConfig(), () -> this);
saslConfigBuilder = new SaslConfigBuilder<>(this);
}

/**
Expand All @@ -41,8 +41,8 @@ X self() {
* @return this
*/
public X withConfig(TransportConfig _config) {
Objects.requireNonNull(_config, "TransportConfig required");
config = _config;
config = Objects.requireNonNull(_config, "TransportConfig required");
saslConfigBuilder.withConfig(_config.getSaslConfig());
return self();
}

Expand Down Expand Up @@ -266,9 +266,12 @@ public R back() {

/**
* Returns the transport config.
*
* @return config
*/
public TransportConfig build() {
SaslConfig saslCfg = saslConfigBuilder.build();
config.setSaslConfig(saslCfg);
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ protected Logger getLogger() {
}

/**
* Returns the current configuration used for SASL authentication.
* Returns the current configuration used for SASL authentication.<br>
*
* @return SaslConfig, never null
*/
Expand Down Expand Up @@ -313,6 +313,10 @@ protected void setSaslAuthMode(int _mode) {
getSaslConfig().setAuthMode(_mode);
}

public TransportConfig getTransportConfig() {
return config;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
package org.freedesktop.dbus.connections.transports;

import org.freedesktop.dbus.connections.AbstractConnection;
import org.freedesktop.dbus.connections.BusAddress;
import org.freedesktop.dbus.connections.SASL;
import org.freedesktop.dbus.connections.*;
import org.freedesktop.dbus.connections.config.TransportConfig;
import org.freedesktop.dbus.connections.config.TransportConfigBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.TransportConfigurationException;
import org.freedesktop.dbus.exceptions.TransportRegistrationException;
import org.freedesktop.dbus.exceptions.*;
import org.freedesktop.dbus.spi.transport.ITransportProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -299,6 +289,8 @@ public TransportConfigBuilder<TransportConfigBuilder<?, TransportBuilder>, Trans
public AbstractTransport build() throws DBusException, IOException {
BusAddress myBusAddress = getAddress();
TransportConfig config = transportConfigBuilder.build();
int configuredSaslAuthMode = config.getSaslConfig().getAuthMode();

if (myBusAddress == null) {
throw new DBusException("Transport requires a BusAddress, use withBusAddress() to configure before building");
}
Expand All @@ -315,9 +307,11 @@ public AbstractTransport build() throws DBusException, IOException {
transport = provider.createTransport(myBusAddress, config);
Objects.requireNonNull(transport, "Transport required"); // in case the factory returns null, we cannot continue

if (config.getSaslConfig().getAuthMode() > 0) {
transport.getSaslConfig().setAuthMode(config.getSaslConfig().getAuthMode());
// another authentication algorithm was configured manually
if (configuredSaslAuthMode > 0 && config.getSaslConfig().getAuthMode() != configuredSaslAuthMode) {
transport.getSaslConfig().setAuthMode(configuredSaslAuthMode);
}

} catch (TransportConfigurationException _ex) {
LOGGER.error("Could not initialize transport", _ex);
}
Expand Down
29 changes: 4 additions & 25 deletions dbus-java-core/src/main/java/org/freedesktop/dbus/utils/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.connections.transports.TransportBuilder;
import org.freedesktop.dbus.connections.transports.TransportBuilder.SaslAuthMode;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.test.AbstractBaseTest;
import org.junit.jupiter.api.Test;
Expand All @@ -17,6 +18,39 @@
*/
public class EmbeddedDBusDaemonTest extends AbstractBaseTest {

@Test
public void testAnonymousAuthentication() throws DBusException {
String protocolType = TransportBuilder.getRegisteredBusTypes().get(0);
String newAddress = TransportBuilder.createDynamicSession(protocolType, false);

BusAddress busAddress = BusAddress.of(newAddress);
BusAddress listenBusAddress = BusAddress.of(newAddress + ",listen=true");

logger.debug("Starting embedded bus on address {})", listenBusAddress);
try (EmbeddedDBusDaemon daemon = new EmbeddedDBusDaemon(listenBusAddress)) {
daemon.setSaslAuthMode(SaslAuthMode.AUTH_ANONYMOUS);
daemon.startInBackground();
logger.debug("Started embedded bus on address {}", listenBusAddress);

waitForDaemon(daemon);

// connect to started daemon process
logger.info("Connecting to embedded DBus {}", busAddress);

try (DBusConnection conn = DBusConnectionBuilder.forAddress(busAddress)
.transportConfig().configureSasl().withAuthMode(SaslAuthMode.AUTH_EXTERNAL).back().back()
.build()) {
logger.debug("Connected to embedded DBus {}", busAddress);
} catch (Exception _ex) {
fail("Connection to EmbeddedDbusDaemon failed", _ex);
logger.error("Error connecting to EmbeddedDbusDaemon", _ex);
}
} catch (IOException _ex1) {
fail("Failed to start EmbeddedDbusDaemon", _ex1);
logger.error("Error starting EmbeddedDbusDaemon", _ex1);
}
}

@Test
public void testStartAndConnectEmbeddedDBusDaemon() throws DBusException {
String protocolType = TransportBuilder.getRegisteredBusTypes().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.freedesktop.dbus.bin.EmbeddedDBusDaemon;
import org.freedesktop.dbus.utils.Util;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down

0 comments on commit f28ac7e

Please sign in to comment.