2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.topology.callhome;
10 import java.io.IOException;
11 import java.net.InetSocketAddress;
12 import java.net.SocketAddress;
13 import java.security.GeneralSecurityException;
14 import java.security.PublicKey;
15 import java.util.List;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
24 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.mdsal.binding.api.DataTreeModification;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.NetconfCallhomeServer;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.SshPublicKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.credentials.Credentials;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.AllowedDevices;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.Global;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.Global.MountPointNamingStrategy;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.allowed.devices.Device;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.allowed.devices.device.transport.Ssh;
35 import org.opendaylight.yangtools.concepts.Registration;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 @Component(service = CallHomeSshAuthProvider.class, immediate = true)
45 public final class CallHomeMountSshAuthProvider implements CallHomeSshAuthProvider, AutoCloseable {
46 private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountSshAuthProvider.class);
48 private final GlobalConfig globalConfig = new GlobalConfig();
49 private final DeviceConfig deviceConfig = new DeviceConfig();
50 private final DeviceOp deviceOp = new DeviceOp();
51 private final Registration configReg;
52 private final Registration deviceReg;
53 private final Registration deviceOpReg;
55 private final CallHomeMountStatusReporter statusReporter;
59 public CallHomeMountSshAuthProvider(final @Reference DataBroker broker,
60 final @Reference CallHomeMountStatusReporter statusReporter) {
61 configReg = broker.registerTreeChangeListener(
62 DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION,
63 InstanceIdentifier.create(NetconfCallhomeServer.class).child(Global.class)),
66 final var allowedDeviceWildcard =
67 InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class).child(Device.class);
69 deviceReg = broker.registerTreeChangeListener(
70 DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION, allowedDeviceWildcard),
72 deviceOpReg = broker.registerTreeChangeListener(
73 DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, allowedDeviceWildcard),
76 this.statusReporter = statusReporter;
80 public CallHomeSshAuthSettings provideAuth(final SocketAddress remoteAddress, final PublicKey serverKey) {
82 final Credentials deviceCred;
84 final var deviceSpecific = deviceConfig.get(serverKey);
85 if (deviceSpecific != null) {
86 id = deviceSpecific.getUniqueId();
87 deviceCred = deviceSpecific.getTransport() instanceof Ssh ssh ? ssh.getSshClientParams().getCredentials()
90 String syntheticId = fromRemoteAddress(remoteAddress);
91 if (globalConfig.allowedUnknownKeys()) {
94 statusReporter.reportNewSshDevice(syntheticId, serverKey, Device.DeviceStatus.DISCONNECTED);
96 Device opDevice = deviceOp.get(serverKey);
97 if (opDevice == null) {
98 statusReporter.reportNewSshDevice(syntheticId, serverKey, Device.DeviceStatus.FAILEDNOTALLOWED);
100 LOG.info("Repeating rejection of unlisted device with id of {}", opDevice.getUniqueId());
106 final var credentials = deviceCred != null ? deviceCred : globalConfig.getCredentials();
107 if (credentials == null) {
108 LOG.info("No credentials found for {}, rejecting.", id);
112 return new CallHomeSshAuthSettings.DefaultAuthSettings(id, credentials.getUsername(),
113 Set.copyOf(credentials.getPasswords()), null);
117 public void close() {
123 private String fromRemoteAddress(final SocketAddress remoteAddress) {
124 if (remoteAddress instanceof InetSocketAddress socketAddress) {
125 final var hostAddress = socketAddress.getAddress().getHostAddress();
126 return switch (globalConfig.getMountPointNamingStrategy()) {
127 case IPONLY -> hostAddress;
128 case IPPORT -> hostAddress + ":" + socketAddress.getPort();
131 return remoteAddress.toString();
134 private abstract static class AbstractDeviceListener implements DataTreeChangeListener<Device> {
137 public final void onDataTreeChanged(final List<DataTreeModification<Device>> mods) {
138 for (var dataTreeModification : mods) {
139 final var deviceMod = dataTreeModification.getRootNode();
140 final var modType = deviceMod.modificationType();
143 deleteDevice(deviceMod.dataBefore());
145 case SUBTREE_MODIFIED:
147 deleteDevice(deviceMod.dataBefore());
148 writeDevice(deviceMod.dataAfter());
151 throw new IllegalStateException("Unhandled modification type " + modType);
156 private void deleteDevice(final Device dataBefore) {
157 if (dataBefore != null) {
158 if (dataBefore.getTransport() instanceof Ssh ssh) {
159 LOG.debug("Removing device {}", dataBefore.getUniqueId());
160 removeDevice(ssh.nonnullSshClientParams().requireHostKey(), dataBefore);
162 LOG.debug("Ignoring removal of device {}, no host key present", dataBefore.getUniqueId());
167 private void writeDevice(final Device dataAfter) {
168 if (dataAfter.getTransport() instanceof Ssh ssh) {
169 LOG.debug("Adding device {}", dataAfter.getUniqueId());
170 addDevice(ssh.nonnullSshClientParams().requireHostKey(), dataAfter);
172 LOG.debug("Ignoring addition of device {}, no host key present", dataAfter.getUniqueId());
176 abstract void addDevice(SshPublicKey publicKey, Device device);
178 abstract void removeDevice(SshPublicKey publicKey, Device device);
181 private static final class DeviceConfig extends AbstractDeviceListener {
182 private final ConcurrentMap<PublicKey, Device> byPublicKey = new ConcurrentHashMap<>();
184 Device get(final PublicKey key) {
185 return byPublicKey.get(key);
189 void addDevice(final SshPublicKey publicKey, final Device device) {
190 final var key = publicKey(publicKey, device);
192 byPublicKey.put(key, device);
197 void removeDevice(final SshPublicKey publicKey, final Device device) {
198 final var key = publicKey(publicKey, device);
200 byPublicKey.remove(key);
204 private static PublicKey publicKey(final SshPublicKey hostKey, final Device device) {
206 return AuthorizedKeysDecoder.decodePublicKey(hostKey);
207 } catch (GeneralSecurityException e) {
208 LOG.error("Unable to decode SSH key for {}. Ignoring update for this device", device.getUniqueId(), e);
214 private static final class DeviceOp extends AbstractDeviceListener {
215 private final ConcurrentMap<SshPublicKey, Device> byPublicKey = new ConcurrentHashMap<>();
217 Device get(final PublicKey serverKey) {
218 final SshPublicKey skey;
220 skey = AuthorizedKeysDecoder.encodePublicKey(serverKey);
221 } catch (IOException e) {
222 LOG.error("Unable to encode server key: {}", serverKey, e);
226 return byPublicKey.get(skey);
230 void removeDevice(final SshPublicKey publicKey, final Device device) {
231 byPublicKey.remove(publicKey);
235 void addDevice(final SshPublicKey publicKey, final Device device) {
236 byPublicKey.put(publicKey, device);
240 private static final class GlobalConfig implements DataTreeChangeListener<Global> {
241 private volatile Global current = null;
244 public void onDataTreeChanged(final List<DataTreeModification<Global>> mods) {
245 if (!mods.isEmpty()) {
246 current = mods.get(mods.size() - 1).getRootNode().dataAfter();
250 boolean allowedUnknownKeys() {
251 final Global local = current;
252 // Deal with null values.
253 return local != null && Boolean.TRUE.equals(local.getAcceptAllSshKeys());
256 Credentials getCredentials() {
257 final Global local = current;
258 return local != null ? local.getCredentials() : null;
261 @NonNull MountPointNamingStrategy getMountPointNamingStrategy() {
262 final Global local = current;
263 final MountPointNamingStrategy strat = local != null ? local.getMountPointNamingStrategy() : null;
264 return strat == null ? MountPointNamingStrategy.IPPORT : strat;