Expose NetconfKeystoreService
[netconf.git] / apps / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / server / ssh / CallHomeSshAuthSettings.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.callhome.server.ssh;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.security.KeyPair;
13 import java.util.Collection;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
17
18 /**
19  * Authorization context for incoming call home sessions.
20  *
21  * @see CallHomeSshAuthProvider
22  */
23 public interface CallHomeSshAuthSettings {
24
25     /**
26      * Unique identifier of a client this auth settings belongs to.
27      *
28      * @return identifier
29      */
30     String id();
31
32     /**
33      * Applies auth settings on {@link ClientSession} instance for subsequent {@link ClientSession#auth()} invocation.
34      *
35      * @param session client session object
36      */
37     void applyTo(ClientSession session);
38
39     /**
40      * Default implementation of {@link CallHomeSshAuthSettings}. Serves SSH authentication by password(s) and/or
41      * public key(s).
42      *
43      * @param id unique client identifier
44      * @param username username
45      * @param passwords collection of passwords, optional if keyPairs defined
46      * @param keyPairs collection of {@link KeyPair} objects, optional if passwords defined
47      */
48     record DefaultAuthSettings(@NonNull String id, @NonNull  String username, @Nullable Collection<String> passwords,
49             @Nullable Collection<KeyPair> keyPairs) implements CallHomeSshAuthSettings {
50
51         public DefaultAuthSettings {
52             requireNonNull(id);
53             requireNonNull(username);
54             if ((passwords == null || passwords.isEmpty()) && (keyPairs == null || keyPairs.isEmpty())) {
55                 throw new IllegalArgumentException("Neither passwords nor keyPairs is defined");
56             }
57         }
58
59         @Override
60         public void applyTo(final ClientSession session) {
61             session.setUsername(username);
62             if (keyPairs != null) {
63                 for (KeyPair keyPair : keyPairs) {
64                     session.addPublicKeyIdentity(keyPair);
65                 }
66             }
67             if (passwords != null) {
68                 for (String password : passwords) {
69                     session.addPasswordIdentity(password);
70                 }
71             }
72         }
73     }
74 }