Skip to content

Commit

Permalink
#1027: BS Server does not know SecurityStore and ConfigStore anymore
Browse files Browse the repository at this point in the history
This class are already needed by DefaultBootstrapSessionManager some one
could create a BootstrapSessionManager which does not use it.
  • Loading branch information
sbernard31 committed Jun 25, 2021
1 parent e6f87b2 commit 96f2a5a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,11 @@ private LeshanBootstrapServerBuilder createBootstrapBuilder(BootstrapSecuritySto
}

LeshanBootstrapServerBuilder builder = new LeshanBootstrapServerBuilder();
builder.setConfigStore(bootstrapStore);
builder.setSecurityStore(securityStore);
builder.setLocalAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
builder.setLocalSecureAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
builder.setPrivateKey(bootstrapServerPrivateKey);
builder.setPublicKey(bootstrapServerPublicKey);
builder.setSecurityStore(securityStore);
builder.setSessionManager(new DefaultBootstrapSessionManager(securityStore, bootstrapStore) {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.BootstrapHandler;
import org.eclipse.leshan.server.bootstrap.BootstrapHandlerFactory;
import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager;
import org.eclipse.leshan.server.bootstrap.LwM2mBootstrapRequestSender;
import org.eclipse.leshan.server.californium.RootResource;
import org.eclipse.leshan.server.security.BootstrapSecurityStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -52,10 +50,6 @@ public class LeshanBootstrapServer {
private final CoapEndpoint unsecuredEndpoint;
private final CoapEndpoint securedEndpoint;

// LWM2M attributes
private final BootstrapConfigStore bsStore;
private final BootstrapSecurityStore bsSecurityStore;

private LwM2mBootstrapRequestSender requestSender;

/**
Expand All @@ -65,26 +59,20 @@ public class LeshanBootstrapServer {
*
* @param unsecuredEndpoint CoAP endpoint used for <code>coap://</code> communication.
* @param securedEndpoint CoAP endpoint used for <code>coaps://</code> communication.
* @param bsStore the store containing bootstrap configuration to apply during a bootstrap session.
* @param bsSecurityStore the store containing security information needed to authenticate a client.
* @param bsSessionManager manages life cycle of a bootstrap process
* @param bsHandlerFactory responsible to create the {@link BootstrapHandler}
* @param coapConfig the CoAP {@link NetworkConfig}.
* @param encoder encode used to encode request payload.
* @param decoder decoder used to decode response payload.
*/
public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint,
BootstrapConfigStore bsStore, BootstrapSecurityStore bsSecurityStore,
BootstrapSessionManager bsSessionManager, BootstrapHandlerFactory bsHandlerFactory,
NetworkConfig coapConfig, LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {

Validate.notNull(bsStore, "bootstrap store must not be null");
Validate.notNull(bsSessionManager, "session manager must not be null");
Validate.notNull(bsHandlerFactory, "BootstrapHandler factory must not be null");
Validate.notNull(coapConfig, "coapConfig must not be null");

this.bsStore = bsStore;
this.bsSecurityStore = bsSecurityStore;
this.coapApi = new CoapAPI();

// init CoAP server
Expand Down Expand Up @@ -124,25 +112,6 @@ protected CoapResource createBootstrapResource(BootstrapHandler handler) {
return new BootstrapResource(handler);
}

/**
* Security store used for DTLS authentication on the bootstrap resource.
*
* @return the {@link BootstrapSecurityStore} containing data used to authenticate devices.
*/
public BootstrapSecurityStore getBootstrapSecurityStore() {
return bsSecurityStore;
}

/**
* Access to the bootstrap configuration store. It's used for sending configuration to the devices initiating a
* bootstrap.
*
* @return the {@link BootstrapConfigStore} containing configuration to apply to each devices.
*/
public BootstrapConfigStore getBoostrapStore() {
return bsStore;
}

/**
* Starts the server and binds it to the specified port.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigStoreTaskProvider;
import org.eclipse.leshan.server.bootstrap.BootstrapHandler;
import org.eclipse.leshan.server.bootstrap.BootstrapHandlerFactory;
import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager;
import org.eclipse.leshan.server.bootstrap.DefaultBootstrapHandler;
import org.eclipse.leshan.server.bootstrap.DefaultBootstrapSessionManager;
import org.eclipse.leshan.server.bootstrap.InMemoryBootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.LwM2mBootstrapRequestSender;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigStoreTaskProvider;
import org.eclipse.leshan.server.model.LwM2mBootstrapModelProvider;
import org.eclipse.leshan.server.model.StandardBootstrapModelProvider;
import org.eclipse.leshan.server.security.BootstrapSecurityStore;
Expand Down Expand Up @@ -397,9 +397,6 @@ public NetworkConfig createDefaultNetworkConfig() {
public LeshanBootstrapServer build() {
if (localAddress == null)
localAddress = new InetSocketAddress(LwM2m.DEFAULT_COAP_PORT);
if (configStore == null)
configStore = new InMemoryBootstrapConfigStore();

if (bootstrapHandlerFactory == null)
bootstrapHandlerFactory = new BootstrapHandlerFactory() {
@Override
Expand All @@ -408,11 +405,21 @@ public BootstrapHandler create(LwM2mBootstrapRequestSender sender,
return new DefaultBootstrapHandler(sender, sessionManager);
}
};
if (modelProvider == null)
if (configStore == null) {
configStore = new InMemoryBootstrapConfigStore();
} else if (sessionManager != null) {
LOG.warn("configStore is set but you also provide a custom SessionManager so this store will not be used");
}
if (modelProvider == null) {
modelProvider = new StandardBootstrapModelProvider();
if (sessionManager == null)
} else if (sessionManager != null) {
LOG.warn(
"modelProvider is set but you also provide a custom SessionManager so this provider will not be used");
}
if (sessionManager == null) {
sessionManager = new DefaultBootstrapSessionManager(securityStore, new SecurityChecker(),
new BootstrapConfigStoreTaskProvider(configStore), modelProvider);
}
if (coapConfig == null) {
coapConfig = createDefaultNetworkConfig();
}
Expand Down Expand Up @@ -562,8 +569,8 @@ public BootstrapHandler create(LwM2mBootstrapRequestSender sender,
"All CoAP enpoints are deactivated, at least one endpoint should be activated");
}

return createBootstrapServer(unsecuredEndpoint, securedEndpoint, configStore, securityStore, sessionManager,
bootstrapHandlerFactory, coapConfig, encoder, decoder);
return createBootstrapServer(unsecuredEndpoint, securedEndpoint, sessionManager, bootstrapHandlerFactory,
coapConfig, encoder, decoder);
}

/**
Expand All @@ -582,8 +589,6 @@ protected boolean shouldTryToCreateSecureEndpoint() {
*
* @param unsecuredEndpoint CoAP endpoint used for <code>coap://</code> communication.
* @param securedEndpoint CoAP endpoint used for <code>coaps://</code> communication.
* @param bsStore the bootstrap configuration store.
* @param bsSecurityStore the security store used to authenticate devices.
* @param bsSessionManager the manager responsible to handle bootstrap session.
* @param bsHandlerFactory the factory used to create {@link BootstrapHandler}.
* @param coapConfig the CoAP configuration.
Expand All @@ -592,10 +597,9 @@ protected boolean shouldTryToCreateSecureEndpoint() {
* @return the LWM2M Bootstrap server.
*/
protected LeshanBootstrapServer createBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint,
BootstrapConfigStore bsStore, BootstrapSecurityStore bsSecurityStore,
BootstrapSessionManager bsSessionManager, BootstrapHandlerFactory bsHandlerFactory,
NetworkConfig coapConfig, LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {
return new LeshanBootstrapServer(unsecuredEndpoint, securedEndpoint, bsStore, bsSecurityStore, bsSessionManager,
bsHandlerFactory, coapConfig, encoder, decoder);
return new LeshanBootstrapServer(unsecuredEndpoint, securedEndpoint, bsSessionManager, bsHandlerFactory,
coapConfig, encoder, decoder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public Iterator<SecurityInfo> getAllByEndpoint(String endpoint) {

assertNotNull(server.getSecuredAddress());
assertNotNull(server.getUnsecuredAddress());
assertNotNull(server.getBootstrapSecurityStore());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public DefaultBootstrapSessionManager(BootstrapSecurityStore bsSecurityStore, Bo
public DefaultBootstrapSessionManager(BootstrapSecurityStore bsSecurityStore, SecurityChecker securityChecker,
BootstrapTaskProvider tasksProvider, LwM2mBootstrapModelProvider modelProvider) {
Validate.notNull(tasksProvider);
Validate.notNull(modelProvider);
this.bsSecurityStore = bsSecurityStore;
this.securityChecker = securityChecker;
this.tasksProvider = tasksProvider;
Expand All @@ -79,14 +80,15 @@ public DefaultBootstrapSessionManager(BootstrapSecurityStore bsSecurityStore, Se
@Override
public BootstrapSession begin(BootstrapRequest request, Identity clientIdentity) {
boolean authorized;
if (bsSecurityStore != null) {
if (bsSecurityStore != null && securityChecker != null) {
Iterator<SecurityInfo> securityInfos = bsSecurityStore.getAllByEndpoint(request.getEndpointName());
authorized = securityChecker.checkSecurityInfos(request.getEndpointName(), clientIdentity, securityInfos);
} else {
authorized = true;
}
DefaultBootstrapSession session = new DefaultBootstrapSession(request, clientIdentity, authorized);
LOG.trace("Bootstrap session started : {}", session);

return session;
}

Expand Down

0 comments on commit 96f2a5a

Please sign in to comment.