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 com.google.common.collect.Iterables;
11 import com.google.common.net.InetAddresses;
12 import java.io.IOException;
13 import java.net.InetSocketAddress;
14 import java.net.SocketAddress;
15 import java.security.GeneralSecurityException;
16 import java.security.PublicKey;
17 import java.util.Collection;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.DataObjectModification;
22 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
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.protocol.AuthorizedKeysDecoder;
28 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorization;
29 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorization.Builder;
30 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorizationProvider;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.NetconfCallhomeServer;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.credentials.Credentials;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.AllowedDevices;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.Global;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev161109.netconf.callhome.server.allowed.devices.Device;
36 import org.opendaylight.yangtools.concepts.ListenerRegistration;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider, AutoCloseable {
43 private static final Logger LOG = LoggerFactory.getLogger(CallHomeAuthProviderImpl.class);
44 private static final InstanceIdentifier<Global> GLOBAL_PATH =
45 InstanceIdentifier.create(NetconfCallhomeServer.class).child(Global.class);
46 private static final DataTreeIdentifier<Global> GLOBAL =
47 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, GLOBAL_PATH);
49 private static final InstanceIdentifier<Device> ALLOWED_DEVICES_PATH =
50 InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class).child(Device.class);
51 private static final DataTreeIdentifier<Device> ALLOWED_DEVICES =
52 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, ALLOWED_DEVICES_PATH);
53 private static final DataTreeIdentifier<Device> ALLOWED_OP_DEVICES =
54 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, ALLOWED_DEVICES_PATH);
56 private final GlobalConfig globalConfig = new GlobalConfig();
57 private final DeviceConfig deviceConfig = new DeviceConfig();
58 private final DeviceOp deviceOp = new DeviceOp();
59 private final ListenerRegistration<GlobalConfig> configReg;
60 private final ListenerRegistration<DeviceConfig> deviceReg;
61 private final ListenerRegistration<DeviceOp> deviceOpReg;
63 private final CallhomeStatusReporter statusReporter;
65 CallHomeAuthProviderImpl(final DataBroker broker) {
66 configReg = broker.registerDataTreeChangeListener(GLOBAL, globalConfig);
67 deviceReg = broker.registerDataTreeChangeListener(ALLOWED_DEVICES, deviceConfig);
68 deviceOpReg = broker.registerDataTreeChangeListener(ALLOWED_OP_DEVICES, deviceOp);
69 statusReporter = new CallhomeStatusReporter(broker);
73 public CallHomeAuthorization provideAuth(final SocketAddress remoteAddress, final PublicKey serverKey) {
74 Device deviceSpecific = deviceConfig.get(serverKey);
76 Credentials deviceCred;
78 if (deviceSpecific != null) {
79 sessionName = deviceSpecific.getUniqueId();
80 deviceCred = deviceSpecific.getCredentials();
82 String syntheticId = fromRemoteAddress(remoteAddress);
83 if (globalConfig.allowedUnknownKeys()) {
84 sessionName = syntheticId;
86 statusReporter.asForceListedDevice(syntheticId, serverKey);
88 Device opDevice = deviceOp.get(serverKey);
89 if (opDevice == null) {
90 statusReporter.asUnlistedDevice(syntheticId, serverKey);
92 LOG.info("Repeating rejection of unlisted device with id of {}", opDevice.getUniqueId());
94 return CallHomeAuthorization.rejected();
98 final Credentials credentials = deviceCred != null ? deviceCred : globalConfig.getCredentials();
100 if (credentials == null) {
101 LOG.info("No credentials found for {}, rejecting.", remoteAddress);
102 return CallHomeAuthorization.rejected();
105 Builder authBuilder = CallHomeAuthorization.serverAccepted(sessionName, credentials.getUsername());
106 for (String password : credentials.getPasswords()) {
107 authBuilder.addPassword(password);
109 return authBuilder.build();
113 public void close() {
119 private static String fromRemoteAddress(final SocketAddress remoteAddress) {
120 if (remoteAddress instanceof InetSocketAddress) {
121 InetSocketAddress socketAddress = (InetSocketAddress) remoteAddress;
122 return InetAddresses.toAddrString(socketAddress.getAddress()) + ":" + socketAddress.getPort();
124 return remoteAddress.toString();
127 private abstract static class AbstractDeviceListener implements DataTreeChangeListener<Device> {
130 public final void onDataTreeChanged(final Collection<DataTreeModification<Device>> mods) {
131 for (DataTreeModification<Device> dataTreeModification : mods) {
132 final DataObjectModification<Device> deviceMod = dataTreeModification.getRootNode();
133 final ModificationType modType = deviceMod.getModificationType();
136 deleteDevice(deviceMod.getDataBefore());
138 case SUBTREE_MODIFIED:
140 deleteDevice(deviceMod.getDataBefore());
141 writeDevice(deviceMod.getDataAfter());
144 throw new IllegalStateException("Unhandled modification type " + modType);
149 private void deleteDevice(final Device dataBefore) {
150 if (dataBefore != null) {
151 final String publicKey = dataBefore.getSshHostKey();
152 if (publicKey != null) {
153 LOG.debug("Removing device {}", dataBefore.getUniqueId());
154 removeDevice(publicKey, dataBefore);
156 LOG.debug("Ignoring removal of device {}, no host key present", dataBefore.getUniqueId());
161 private void writeDevice(final Device dataAfter) {
162 final String publicKey = dataAfter.getSshHostKey();
163 if (publicKey != null) {
164 LOG.debug("Adding device {}", dataAfter.getUniqueId());
165 addDevice(publicKey, dataAfter);
167 LOG.debug("Ignoring addition of device {}, no host key present", dataAfter.getUniqueId());
171 abstract void addDevice(String publicKey, Device device);
173 abstract void removeDevice(String publicKey, Device device);
176 private static class DeviceConfig extends AbstractDeviceListener {
177 private final ConcurrentMap<PublicKey, Device> byPublicKey = new ConcurrentHashMap<>();
178 private final AuthorizedKeysDecoder keyDecoder = new AuthorizedKeysDecoder();
180 Device get(final PublicKey key) {
181 return byPublicKey.get(key);
185 void addDevice(final String publicKey, final Device device) {
186 final PublicKey key = publicKey(publicKey, device);
188 byPublicKey.put(key, device);
193 void removeDevice(final String publicKey, final Device device) {
194 final PublicKey key = publicKey(publicKey, device);
196 byPublicKey.remove(key);
200 private PublicKey publicKey(final String hostKey, final Device device) {
202 return keyDecoder.decodePublicKey(hostKey);
203 } catch (GeneralSecurityException e) {
204 LOG.error("Unable to decode SSH key for {}. Ignoring update for this device", device.getUniqueId(), e);
210 private static class DeviceOp extends AbstractDeviceListener {
211 private final ConcurrentMap<String, Device> byPublicKey = new ConcurrentHashMap<>();
213 Device get(final PublicKey serverKey) {
216 skey = AuthorizedKeysDecoder.encodePublicKey(serverKey);
217 } catch (IOException | IllegalArgumentException e) {
218 LOG.error("Unable to encode server key: {}", serverKey, e);
222 return byPublicKey.get(skey);
226 void removeDevice(final String publicKey, final Device device) {
227 byPublicKey.remove(publicKey);
231 void addDevice(final String publicKey, final Device device) {
232 byPublicKey.put(publicKey, device);
236 private static class GlobalConfig implements DataTreeChangeListener<Global> {
237 private volatile Global current = null;
240 public void onDataTreeChanged(final Collection<DataTreeModification<Global>> mods) {
241 if (!mods.isEmpty()) {
242 current = Iterables.getLast(mods).getRootNode().getDataAfter();
246 boolean allowedUnknownKeys() {
247 final Global local = current;
248 // Deal with null values.
249 return local != null && Boolean.TRUE.equals(local.isAcceptAllSshKeys());
252 Credentials getCredentials() {
253 final Global local = current;
254 return local != null ? local.getCredentials() : null;