Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
package org.apache.sshd;
20
 
21
import java.security.KeyPair;
22
import java.security.PublicKey;
23
import java.util.Map;
24
import java.util.concurrent.ConcurrentHashMap;
25
import java.util.concurrent.atomic.AtomicInteger;
26
 
27
import org.apache.sshd.common.KeyPairProvider;
28
import org.apache.sshd.common.util.KeyUtils;
29
import org.apache.sshd.server.Command;
30
import org.apache.sshd.server.CommandFactory;
31
import org.apache.sshd.server.PublickeyAuthenticator;
32
import org.apache.sshd.server.auth.CachingPublicKeyAuthenticator;
33
import org.apache.sshd.server.command.UnknownCommand;
34
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
35
import org.apache.sshd.server.session.ServerSession;
36
import org.apache.sshd.util.BaseTest;
37
import org.apache.sshd.util.Utils;
38
import org.junit.After;
39
import org.junit.Before;
40
import org.junit.Test;
41
 
42
import static org.junit.Assert.assertEquals;
43
import static org.junit.Assert.assertTrue;
44
 
45
/**
46
 * TODO Add javadoc
47
 *
48
 * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
49
 */
50
public class SinglePublicKeyAuthTest extends BaseTest {
51
 
52
    private SshServer sshd;
53
    private int port = 0;
54
    private KeyPair pairRsa = Utils.createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA);
55
    private KeyPair pairRsaBad = new SimpleGeneratorHostKeyProvider(null, "RSA").loadKey(KeyPairProvider.SSH_RSA);
56
    private PublickeyAuthenticator delegate;
57
 
58
    @Before
59
    public void setUp() throws Exception {
60
        port = Utils.getFreePort();
61
        sshd = SshServer.setUpDefaultServer();
62
        sshd.setPort(port);
63
        sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
64
        sshd.setCommandFactory(new CommandFactory() {
65
            public Command createCommand(String command) {
66
                return new UnknownCommand(command);
67
            }
68
        });
69
        sshd.getProperties().put(SshServer.AUTH_METHODS, "publickey");
70
        sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
71
            public boolean authenticate(String username, PublicKey key, ServerSession session) {
72
                return delegate.authenticate(username, key, session);
73
            }
74
        });
75
        sshd.start();
76
    }
77
 
78
    @After
79
    public void tearDown() throws Exception {
80
        if (sshd != null) {
81
            sshd.stop(true);
82
        }
83
    }
84
 
85
    @Test
86
    public void testPublicKeyAuthWithCache() throws Exception {
87
        final ConcurrentHashMap<String, AtomicInteger> count = new ConcurrentHashMap<String, AtomicInteger>();
88
        TestCachingPublicKeyAuthenticator auth = new TestCachingPublicKeyAuthenticator(new PublickeyAuthenticator() {
89
            public boolean authenticate(String username, PublicKey key,
90
                                        ServerSession session) {
91
                count.putIfAbsent(KeyUtils.getFingerPrint(key), new AtomicInteger());
92
                count.get(KeyUtils.getFingerPrint(key)).incrementAndGet();
93
                return key.equals(pairRsa.getPublic());
94
            }
95
        });
96
        delegate = auth;
97
        SshClient client = SshClient.setUpDefaultClient();
98
        client.start();
99
        ClientSession session = client.connect("smx", "localhost", port).await().getSession();
100
        session.addPublicKeyIdentity(pairRsaBad);
101
        session.addPublicKeyIdentity(pairRsa);
102
        assertTrue(session.auth().await().isSuccess());
103
        assertEquals(2, count.size());
104
        assertTrue(count.containsKey(KeyUtils.getFingerPrint(pairRsaBad.getPublic())));
105
        assertTrue(count.containsKey(KeyUtils.getFingerPrint(pairRsa.getPublic())));
106
        assertEquals(1, count.get(KeyUtils.getFingerPrint(pairRsaBad.getPublic())).get());
107
        assertEquals(1, count.get(KeyUtils.getFingerPrint(pairRsa.getPublic())).get());
108
        client.close(false).await();
109
        Thread.sleep(100);
110
        assertTrue(auth.getCache().isEmpty());
111
    }
112
 
113
    @Test
114
    public void testPublicKeyAuthWithoutCache() throws Exception {
115
        final ConcurrentHashMap<String, AtomicInteger> count = new ConcurrentHashMap<String, AtomicInteger>();
116
        delegate = new PublickeyAuthenticator() {
117
            public boolean authenticate(String username, PublicKey key,
118
                                        ServerSession session) {
119
                count.putIfAbsent(KeyUtils.getFingerPrint(key), new AtomicInteger());
120
                count.get(KeyUtils.getFingerPrint(key)).incrementAndGet();
121
                return key.equals(pairRsa.getPublic());
122
            }
123
        };
124
        SshClient client = SshClient.setUpDefaultClient();
125
        client.start();
126
        ClientSession session = client.connect("smx", "localhost", port).await().getSession();
127
        session.addPublicKeyIdentity(pairRsaBad);
128
        session.addPublicKeyIdentity(pairRsa);
129
        assertTrue(session.auth().await().isSuccess());
130
        assertEquals(2, count.size());
131
        assertTrue(count.containsKey(KeyUtils.getFingerPrint(pairRsaBad.getPublic())));
132
        assertTrue(count.containsKey(KeyUtils.getFingerPrint(pairRsa.getPublic())));
133
        assertEquals(1, count.get(KeyUtils.getFingerPrint(pairRsaBad.getPublic())).get());
134
        assertEquals(2, count.get(KeyUtils.getFingerPrint(pairRsa.getPublic())).get());
135
    }
136
 
137
    public static class TestCachingPublicKeyAuthenticator extends CachingPublicKeyAuthenticator {
138
        public TestCachingPublicKeyAuthenticator(PublickeyAuthenticator authenticator) {
139
            super(authenticator);
140
        }
141
        public Map<ServerSession, Map<PublicKey, Boolean>> getCache() {
142
            return cache;
143
        }
144
    }
145
 
146
}
147
 
148