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.SshPublicKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.credentials.Credentials;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.AllowedDevices;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.Global;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.Global.MountPointNamingStrategy;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.allowed.devices.Device;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev240129.netconf.callhome.server.allowed.devices.device.transport.Ssh;
37 import org.opendaylight.yangtools.concepts.Registration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @Component(service = CallHomeSshAuthProvider.class, immediate = true)
47 public final class CallHomeMountSshAuthProvider implements CallHomeSshAuthProvider, AutoCloseable {
48 private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountSshAuthProvider.class);
50 private final GlobalConfig globalConfig = new GlobalConfig();
51 private final DeviceConfig deviceConfig = new DeviceConfig();
52 private final DeviceOp deviceOp = new DeviceOp();
53 private final Registration configReg;
54 private final Registration deviceReg;
55 private final Registration deviceOpReg;
57 private final CallHomeMountStatusReporter statusReporter;
61 public CallHomeMountSshAuthProvider(final @Reference DataBroker broker,
62 final @Reference CallHomeMountStatusReporter statusReporter) {
63 configReg = broker.registerTreeChangeListener(
64 DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION,
65 InstanceIdentifier.create(NetconfCallhomeServer.class).child(Global.class)),
68 final var allowedDeviceWildcard =
69 InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class).child(Device.class);
71 deviceReg = broker.registerTreeChangeListener(
72 DataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION, allowedDeviceWildcard),
74 deviceOpReg = broker.registerTreeChangeListener(
75 DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL, allowedDeviceWildcard),
78 this.statusReporter = statusReporter;
82 public CallHomeSshAuthSettings provideAuth(final SocketAddress remoteAddress, final PublicKey serverKey) {
84 final Credentials deviceCred;
86 final var deviceSpecific = deviceConfig.get(serverKey);
87 if (deviceSpecific != null) {
88 id = deviceSpecific.getUniqueId();
89 deviceCred = deviceSpecific.getTransport() instanceof Ssh ssh ? ssh.getSshClientParams().getCredentials()
92 String syntheticId = fromRemoteAddress(remoteAddress);
93 if (globalConfig.allowedUnknownKeys()) {
96 statusReporter.reportNewSshDevice(syntheticId, serverKey, Device.DeviceStatus.DISCONNECTED);
98 Device opDevice = deviceOp.get(serverKey);
99 if (opDevice == null) {
100 statusReporter.reportNewSshDevice(syntheticId, serverKey, Device.DeviceStatus.FAILEDNOTALLOWED);
102 LOG.info("Repeating rejection of unlisted device with id of {}", opDevice.getUniqueId());
108 final var credentials = deviceCred != null ? deviceCred : globalConfig.getCredentials();
109 if (credentials == null) {
110 LOG.info("No credentials found for {}, rejecting.", id);
114 return new CallHomeSshAuthSettings.DefaultAuthSettings(id, credentials.getUsername(),
115 Set.copyOf(credentials.getPasswords()), null);
119 public void close() {
125 private String fromRemoteAddress(final SocketAddress remoteAddress) {
126 if (remoteAddress instanceof InetSocketAddress socketAddress) {
127 final var hostAddress = socketAddress.getAddress().getHostAddress();
128 return switch (globalConfig.getMountPointNamingStrategy()) {
129 case IPONLY -> hostAddress;
130 case IPPORT -> hostAddress + ":" + socketAddress.getPort();
133 return remoteAddress.toString();
136 private abstract static class AbstractDeviceListener implements DataTreeChangeListener<Device> {
139 public final void onDataTreeChanged(final List<DataTreeModification<Device>> mods) {
140 for (var dataTreeModification : mods) {
141 final var deviceMod = dataTreeModification.getRootNode();
142 final var modType = deviceMod.modificationType();
145 deleteDevice(deviceMod.dataBefore());
147 case SUBTREE_MODIFIED:
149 deleteDevice(deviceMod.dataBefore());
150 writeDevice(deviceMod.dataAfter());
153 throw new IllegalStateException("Unhandled modification type " + modType);
158 private void deleteDevice(final Device dataBefore) {
159 if (dataBefore != null) {
160 if (dataBefore.getTransport() instanceof Ssh ssh) {
161 LOG.debug("Removing device {}", dataBefore.getUniqueId());
162 removeDevice(ssh.nonnullSshClientParams().requireHostKey(), dataBefore);
164 LOG.debug("Ignoring removal of device {}, no host key present", dataBefore.getUniqueId());
169 private void writeDevice(final Device dataAfter) {
170 if (dataAfter.getTransport() instanceof Ssh ssh) {
171 LOG.debug("Adding device {}", dataAfter.getUniqueId());
172 addDevice(ssh.nonnullSshClientParams().requireHostKey(), dataAfter);
174 LOG.debug("Ignoring addition of device {}, no host key present", dataAfter.getUniqueId());
178 abstract void addDevice(SshPublicKey publicKey, Device device);
180 abstract void removeDevice(SshPublicKey publicKey, Device device);
183 private static final class DeviceConfig extends AbstractDeviceListener {
184 private final ConcurrentMap<PublicKey, Device> byPublicKey = new ConcurrentHashMap<>();
186 Device get(final PublicKey key) {
187 return byPublicKey.get(key);
191 void addDevice(final SshPublicKey publicKey, final Device device) {
192 final var key = publicKey(publicKey, device);
194 byPublicKey.put(key, device);
199 void removeDevice(final SshPublicKey publicKey, final Device device) {
200 final var key = publicKey(publicKey, device);
202 byPublicKey.remove(key);
206 private static PublicKey publicKey(final SshPublicKey hostKey, final Device device) {
208 return AuthorizedKeysDecoder.decodePublicKey(hostKey);
209 } catch (GeneralSecurityException e) {
210 LOG.error("Unable to decode SSH key for {}. Ignoring update for this device", device.getUniqueId(), e);
216 private static final class DeviceOp extends AbstractDeviceListener {
217 private final ConcurrentMap<SshPublicKey, Device> byPublicKey = new ConcurrentHashMap<>();
219 Device get(final PublicKey serverKey) {
220 final SshPublicKey skey;
222 skey = AuthorizedKeysDecoder.encodePublicKey(serverKey);
223 } catch (IOException e) {
224 LOG.error("Unable to encode server key: {}", serverKey, e);
228 return byPublicKey.get(skey);
232 void removeDevice(final SshPublicKey publicKey, final Device device) {
233 byPublicKey.remove(publicKey);
237 void addDevice(final SshPublicKey publicKey, final Device device) {
238 byPublicKey.put(publicKey, device);
242 private static final class GlobalConfig implements DataTreeChangeListener<Global> {
243 private volatile Global current = null;
246 public void onDataTreeChanged(final List<DataTreeModification<Global>> mods) {
247 if (!mods.isEmpty()) {
248 current = mods.get(mods.size() - 1).getRootNode().dataAfter();
252 boolean allowedUnknownKeys() {
253 final Global local = current;
254 // Deal with null values.
255 return local != null && Boolean.TRUE.equals(local.getAcceptAllSshKeys());
258 Credentials getCredentials() {
259 final Global local = current;
260 return local != null ? local.getCredentials() : null;
263 @NonNull MountPointNamingStrategy getMountPointNamingStrategy() {
264 final Global local = current;
265 final MountPointNamingStrategy strat = local != null ? local.getMountPointNamingStrategy() : null;
266 return strat == null ? MountPointNamingStrategy.IPPORT : strat;