Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance.testsuite.account; 16 : 17 : import static com.google.common.base.Preconditions.checkState; 18 : import static java.nio.charset.StandardCharsets.US_ASCII; 19 : 20 : import com.google.gerrit.acceptance.SshEnabled; 21 : import com.google.gerrit.acceptance.testsuite.request.SshSessionFactory; 22 : import com.google.gerrit.common.Nullable; 23 : import com.google.gerrit.entities.Account; 24 : import com.google.gerrit.server.account.VersionedAuthorizedKeys; 25 : import com.google.gerrit.server.ssh.SshKeyCache; 26 : import com.google.inject.Inject; 27 : import com.google.inject.Singleton; 28 : import java.io.ByteArrayOutputStream; 29 : import java.io.IOException; 30 : import java.security.GeneralSecurityException; 31 : import java.security.KeyPair; 32 : import java.util.HashMap; 33 : import java.util.Map; 34 : import org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter; 35 : 36 : @Singleton 37 : public class TestSshKeys { 38 : private final Map<String, KeyPair> sshKeyPairs; 39 : 40 : private final VersionedAuthorizedKeys.Accessor authorizedKeys; 41 : private final SshKeyCache sshKeyCache; 42 : private final boolean sshEnabled; 43 : 44 : @Inject 45 : TestSshKeys( 46 : VersionedAuthorizedKeys.Accessor authorizedKeys, 47 : SshKeyCache sshKeyCache, 48 138 : @SshEnabled boolean sshEnabled) { 49 138 : this.authorizedKeys = authorizedKeys; 50 138 : this.sshKeyCache = sshKeyCache; 51 138 : this.sshEnabled = sshEnabled; 52 138 : this.sshKeyPairs = new HashMap<>(); 53 138 : } 54 : 55 : // TODO(ekempin): Remove this method when com.google.gerrit.acceptance.TestAccount is gone 56 : public KeyPair getKeyPair(com.google.gerrit.acceptance.TestAccount account) throws Exception { 57 2 : checkState(sshEnabled, "Requested SSH key pair, but SSH is disabled"); 58 2 : checkState( 59 2 : account.username() != null, 60 : "Requested SSH key pair for account %s, but username is not set", 61 2 : account.id()); 62 : 63 2 : String username = account.username(); 64 2 : KeyPair keyPair = sshKeyPairs.get(username); 65 2 : if (keyPair == null) { 66 0 : keyPair = createKeyPair(account.id(), username, account.email()); 67 0 : sshKeyPairs.put(username, keyPair); 68 : } 69 2 : return keyPair; 70 : } 71 : 72 : public KeyPair getKeyPair(TestAccount account) throws Exception { 73 13 : checkState(sshEnabled, "Requested SSH key pair, but SSH is disabled"); 74 13 : checkState( 75 13 : account.username().isPresent(), 76 : "Requested SSH key pair for account %s, but username is not set", 77 13 : account.accountId()); 78 : 79 13 : String username = account.username().get(); 80 13 : KeyPair keyPair = sshKeyPairs.get(username); 81 13 : if (keyPair == null) { 82 13 : keyPair = createKeyPair(account.accountId(), username, account.preferredEmail().orElse(null)); 83 13 : sshKeyPairs.put(username, keyPair); 84 : } 85 13 : return keyPair; 86 : } 87 : 88 : private KeyPair createKeyPair(Account.Id accountId, String username, @Nullable String email) 89 : throws Exception { 90 13 : KeyPair keyPair = SshSessionFactory.genSshKey(); 91 13 : authorizedKeys.addKey(accountId, publicKey(keyPair, email)); 92 13 : sshKeyCache.evict(username); 93 13 : return keyPair; 94 : } 95 : 96 : public static String publicKey(KeyPair sshKey, @Nullable String comment) 97 : throws IOException, GeneralSecurityException { 98 13 : return preparePublicKey(sshKey, comment).toString(US_ASCII.name()).trim(); 99 : } 100 : 101 : public static byte[] publicKeyBlob(KeyPair sshKey) throws IOException, GeneralSecurityException { 102 0 : return preparePublicKey(sshKey, null).toByteArray(); 103 : } 104 : 105 : private static ByteArrayOutputStream preparePublicKey(KeyPair sshKey, String comment) 106 : throws IOException, GeneralSecurityException { 107 13 : OpenSSHKeyPairResourceWriter keyPairWriter = new OpenSSHKeyPairResourceWriter(); 108 13 : ByteArrayOutputStream out = new ByteArrayOutputStream(); 109 13 : keyPairWriter.writePublicKey(sshKey, comment, out); 110 13 : return out; 111 : } 112 : }