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.callhome.mount;
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.netconf.callhome.server.ssh.CallHomeSshAuthProvider;
28 import org.opendaylight.netconf.callhome.server.ssh.CallHomeSshAuthSettings;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.NetconfCallhomeServer;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.credentials.Credentials;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.AllowedDevices;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.Global;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.Global.MountPointNamingStrategy;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.allowed.devices.Device;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.allowed.devices.device.transport.Ssh;
36 import org.opendaylight.yangtools.concepts.Registration;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 @Component(service = CallHomeSshAuthProvider.class, immediate = true)
46 public final class CallHomeMountSshAuthProvider implements CallHomeSshAuthProvider, AutoCloseable {
47 private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountSshAuthProvider.class);
49 private final GlobalConfig globalConfig = new GlobalConfig();
50 private final DeviceConfig deviceConfig = new DeviceConfig();
51 private final DeviceOp deviceOp = new DeviceOp();
52 private final Registration configReg;
53 private final Registration deviceReg;
54 private final Registration deviceOpReg;
56 private final CallHomeMountStatusReporter statusReporter;
60 public CallHomeMountSshAuthProvider(final @Reference DataBroker broker,
61 final @Reference CallHomeMountStatusReporter statusReporter) {
62 configReg = broker.registerDataTreeChangeListener(
63 DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION,
64 InstanceIdentifier.create(NetconfCallhomeServer.class).child(Global.class)),
67 final var allowedDeviceWildcard =
68 InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class).child(Device.class);
70 deviceReg = broker.registerDataTreeChangeListener(
71 DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION, allowedDeviceWildcard),
73 deviceOpReg = broker.registerDataTreeChangeListener(
74 DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, allowedDeviceWildcard),
77 this.statusReporter = statusReporter;
81 public CallHomeSshAuthSettings provideAuth(final SocketAddress remoteAddress, final PublicKey serverKey) {
83 final Credentials deviceCred;
85 final var deviceSpecific = deviceConfig.get(serverKey);
86 if (deviceSpecific != null) {
87 id = deviceSpecific.getUniqueId();
88 deviceCred = deviceSpecific.getTransport() instanceof Ssh ssh ? ssh.getSshClientParams().getCredentials()
91 String syntheticId = fromRemoteAddress(remoteAddress);
92 if (globalConfig.allowedUnknownKeys()) {
95 statusReporter.reportNewSshDevice(syntheticId, serverKey, Device.DeviceStatus.DISCONNECTED);
97 Device opDevice = deviceOp.get(serverKey);
98 if (opDevice == null) {
99 statusReporter.reportNewSshDevice(syntheticId, serverKey, Device.DeviceStatus.FAILEDNOTALLOWED);
101 LOG.info("Repeating rejection of unlisted device with id of {}", opDevice.getUniqueId());
107 final var credentials = deviceCred != null ? deviceCred : globalConfig.getCredentials();
108 if (credentials == null) {
109 LOG.info("No credentials found for {}, rejecting.", id);
113 return new CallHomeSshAuthSettings.DefaultAuthSettings(id, credentials.getUsername(),
114 Set.copyOf(credentials.getPasswords()), null);
118 public void close() {
124 private String fromRemoteAddress(final SocketAddress remoteAddress) {
125 if (remoteAddress instanceof InetSocketAddress socketAddress) {
126 final var hostAddress = socketAddress.getAddress().getHostAddress();
127 return switch (globalConfig.getMountPointNamingStrategy()) {
128 case IPONLY -> hostAddress;
129 case IPPORT -> hostAddress + ":" + socketAddress.getPort();
132 return remoteAddress.toString();
135 private abstract static class AbstractDeviceListener implements DataTreeChangeListener<Device> {
138 public final void onDataTreeChanged(final List<DataTreeModification<Device>> mods) {
139 for (var dataTreeModification : mods) {
140 final var deviceMod = dataTreeModification.getRootNode();
141 final var modType = deviceMod.modificationType();
144 deleteDevice(deviceMod.dataBefore());
146 case SUBTREE_MODIFIED:
148 deleteDevice(deviceMod.dataBefore());
149 writeDevice(deviceMod.dataAfter());
152 throw new IllegalStateException("Unhandled modification type " + modType);
157 private void deleteDevice(final Device dataBefore) {
158 if (dataBefore != null) {
159 final String publicKey = getHostPublicKey(dataBefore);
160 if (publicKey != null) {
161 LOG.debug("Removing device {}", dataBefore.getUniqueId());
162 removeDevice(publicKey, dataBefore);
164 LOG.debug("Ignoring removal of device {}, no host key present", dataBefore.getUniqueId());
169 private void writeDevice(final Device dataAfter) {
170 final String publicKey = getHostPublicKey(dataAfter);
171 if (publicKey != null) {
172 LOG.debug("Adding device {}", dataAfter.getUniqueId());
173 addDevice(publicKey, dataAfter);
175 LOG.debug("Ignoring addition of device {}, no host key present", dataAfter.getUniqueId());
179 private static String getHostPublicKey(final Device device) {
180 return device.getTransport() instanceof Ssh ssh ? ssh.nonnullSshClientParams().getHostKey() : null;
183 abstract void addDevice(String publicKey, Device device);
185 abstract void removeDevice(String publicKey, Device device);
188 private static final class DeviceConfig extends AbstractDeviceListener {
189 private final ConcurrentMap<PublicKey, Device> byPublicKey = new ConcurrentHashMap<>();
190 private final AuthorizedKeysDecoder keyDecoder = new AuthorizedKeysDecoder();
192 Device get(final PublicKey key) {
193 return byPublicKey.get(key);
197 void addDevice(final String publicKey, final Device device) {
198 final PublicKey key = publicKey(publicKey, device);
200 byPublicKey.put(key, device);
205 void removeDevice(final String publicKey, final Device device) {
206 final PublicKey key = publicKey(publicKey, device);
208 byPublicKey.remove(key);
212 private PublicKey publicKey(final String hostKey, final Device device) {
214 return keyDecoder.decodePublicKey(hostKey);
215 } catch (GeneralSecurityException e) {
216 LOG.error("Unable to decode SSH key for {}. Ignoring update for this device", device.getUniqueId(), e);
222 private static final class DeviceOp extends AbstractDeviceListener {
223 private final ConcurrentMap<String, Device> byPublicKey = new ConcurrentHashMap<>();
225 Device get(final PublicKey serverKey) {
228 skey = AuthorizedKeysDecoder.encodePublicKey(serverKey);
229 } catch (IOException | IllegalArgumentException e) {
230 LOG.error("Unable to encode server key: {}", serverKey, e);
234 return byPublicKey.get(skey);
238 void removeDevice(final String publicKey, final Device device) {
239 byPublicKey.remove(publicKey);
243 void addDevice(final String publicKey, final Device device) {
244 byPublicKey.put(publicKey, device);
248 private static final class GlobalConfig implements DataTreeChangeListener<Global> {
249 private volatile Global current = null;
252 public void onDataTreeChanged(final List<DataTreeModification<Global>> mods) {
253 if (!mods.isEmpty()) {
254 current = mods.get(mods.size() - 1).getRootNode().dataAfter();
258 boolean allowedUnknownKeys() {
259 final Global local = current;
260 // Deal with null values.
261 return local != null && Boolean.TRUE.equals(local.getAcceptAllSshKeys());
264 Credentials getCredentials() {
265 final Global local = current;
266 return local != null ? local.getCredentials() : null;
269 @NonNull MountPointNamingStrategy getMountPointNamingStrategy() {
270 final Global local = current;
271 final MountPointNamingStrategy strat = local != null ? local.getMountPointNamingStrategy() : null;
272 return strat == null ? MountPointNamingStrategy.IPPORT : strat;