AccountApi: Add SSH key methods
Add methods to list the SSH keys, and add an SSH key. Change-Id: Ie3e058ea8ecab5dff11678570f81cc7742a26c3f
This commit is contained in:
@@ -160,12 +160,12 @@ public class AccountCreator {
|
||||
return new AccountExternalId.Key(AccountExternalId.SCHEME_MAILTO, email);
|
||||
}
|
||||
|
||||
private static KeyPair genSshKey() throws JSchException {
|
||||
public static KeyPair genSshKey() throws JSchException {
|
||||
JSch jsch = new JSch();
|
||||
return KeyPair.genKeyPair(jsch, KeyPair.RSA);
|
||||
}
|
||||
|
||||
private static String publicKey(KeyPair sshKey, String comment)
|
||||
public static String publicKey(KeyPair sshKey, String comment)
|
||||
throws UnsupportedEncodingException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
sshKey.writePublicKey(out, comment);
|
||||
|
||||
@@ -30,11 +30,13 @@ import com.google.common.collect.FluentIterable;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import com.google.gerrit.acceptance.AbstractDaemonTest;
|
||||
import com.google.gerrit.acceptance.AccountCreator;
|
||||
import com.google.gerrit.acceptance.PushOneCommit;
|
||||
import com.google.gerrit.acceptance.TestAccount;
|
||||
import com.google.gerrit.extensions.api.accounts.EmailInput;
|
||||
import com.google.gerrit.extensions.common.AccountInfo;
|
||||
import com.google.gerrit.extensions.common.GpgKeyInfo;
|
||||
import com.google.gerrit.extensions.common.SshKeyInfo;
|
||||
import com.google.gerrit.extensions.restapi.BadRequestException;
|
||||
import com.google.gerrit.extensions.restapi.ResourceConflictException;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
@@ -333,6 +335,28 @@ public class AccountIT extends AbstractDaemonTest {
|
||||
ImmutableList.of(key2.getKeyIdString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sshKeys() throws Exception {
|
||||
// The test account should initially have exactly one ssh key
|
||||
List<SshKeyInfo> info = gApi.accounts().self().listSshKeys();
|
||||
assertThat(info).hasSize(1);
|
||||
SshKeyInfo key = info.get(0);
|
||||
String inital = AccountCreator.publicKey(admin.sshKey, admin.email);
|
||||
assertThat(key.sshPublicKey).isEqualTo(inital);
|
||||
|
||||
// Add a new key
|
||||
String newKey = AccountCreator.publicKey(
|
||||
AccountCreator.genSshKey(), admin.email);
|
||||
gApi.accounts().self().addSshKey(newKey);
|
||||
info = gApi.accounts().self().listSshKeys();
|
||||
assertThat(info).hasSize(2);
|
||||
|
||||
// Add an existing key again
|
||||
gApi.accounts().self().addSshKey(inital);
|
||||
info = gApi.accounts().self().listSshKeys();
|
||||
assertThat(info).hasSize(3);
|
||||
}
|
||||
|
||||
private PGPPublicKey getOnlyKeyFromStore(TestKey key) throws Exception {
|
||||
try (PublicKeyStore store = publicKeyStoreProvider.get()) {
|
||||
Iterable<PGPPublicKeyRing> keys = store.get(key.getKeyId());
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.google.gerrit.extensions.client.EditPreferencesInfo;
|
||||
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
|
||||
import com.google.gerrit.extensions.common.AccountInfo;
|
||||
import com.google.gerrit.extensions.common.GpgKeyInfo;
|
||||
import com.google.gerrit.extensions.common.SshKeyInfo;
|
||||
import com.google.gerrit.extensions.restapi.NotImplementedException;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
|
||||
@@ -46,6 +47,9 @@ public interface AccountApi {
|
||||
void unstarChange(String id) throws RestApiException;
|
||||
void addEmail(EmailInput input) throws RestApiException;
|
||||
|
||||
List<SshKeyInfo> listSshKeys() throws RestApiException;
|
||||
SshKeyInfo addSshKey(String key) throws RestApiException;
|
||||
|
||||
Map<String, GpgKeyInfo> listGpgKeys() throws RestApiException;
|
||||
Map<String, GpgKeyInfo> putGpgKeys(List<String> add, List<String> remove)
|
||||
throws RestApiException;
|
||||
@@ -114,6 +118,16 @@ public interface AccountApi {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SshKeyInfo> listSshKeys() throws RestApiException {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SshKeyInfo addSshKey(String key) throws RestApiException {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, GpgKeyInfo> putGpgKeys(List<String> add,
|
||||
List<String> remove) throws RestApiException {
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
package com.google.gerrit.server.api.accounts;
|
||||
|
||||
import com.google.gerrit.common.RawInputUtil;
|
||||
import com.google.gerrit.common.errors.EmailException;
|
||||
import com.google.gerrit.extensions.api.accounts.AccountApi;
|
||||
import com.google.gerrit.extensions.api.accounts.EmailInput;
|
||||
@@ -23,17 +24,20 @@ import com.google.gerrit.extensions.client.EditPreferencesInfo;
|
||||
import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
|
||||
import com.google.gerrit.extensions.common.AccountInfo;
|
||||
import com.google.gerrit.extensions.common.GpgKeyInfo;
|
||||
import com.google.gerrit.extensions.common.SshKeyInfo;
|
||||
import com.google.gerrit.extensions.restapi.IdString;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
import com.google.gerrit.extensions.restapi.TopLevelResource;
|
||||
import com.google.gerrit.server.GpgException;
|
||||
import com.google.gerrit.server.account.AccountLoader;
|
||||
import com.google.gerrit.server.account.AccountResource;
|
||||
import com.google.gerrit.server.account.AddSshKey;
|
||||
import com.google.gerrit.server.account.CreateEmail;
|
||||
import com.google.gerrit.server.account.GetAvatar;
|
||||
import com.google.gerrit.server.account.GetDiffPreferences;
|
||||
import com.google.gerrit.server.account.GetEditPreferences;
|
||||
import com.google.gerrit.server.account.GetPreferences;
|
||||
import com.google.gerrit.server.account.GetSshKeys;
|
||||
import com.google.gerrit.server.account.SetDiffPreferences;
|
||||
import com.google.gerrit.server.account.SetEditPreferences;
|
||||
import com.google.gerrit.server.account.SetPreferences;
|
||||
@@ -70,6 +74,8 @@ public class AccountApiImpl implements AccountApi {
|
||||
private final StarredChanges.Delete starredChangesDelete;
|
||||
private final CreateEmail.Factory createEmailFactory;
|
||||
private final GpgApiAdapter gpgApiAdapter;
|
||||
private final GetSshKeys getSshKeys;
|
||||
private final AddSshKey addSshKey;
|
||||
|
||||
@Inject
|
||||
AccountApiImpl(AccountLoader.Factory ailf,
|
||||
@@ -85,6 +91,8 @@ public class AccountApiImpl implements AccountApi {
|
||||
StarredChanges.Delete starredChangesDelete,
|
||||
CreateEmail.Factory createEmailFactory,
|
||||
GpgApiAdapter gpgApiAdapter,
|
||||
GetSshKeys getSshKeys,
|
||||
AddSshKey addSshKey,
|
||||
@Assisted AccountResource account) {
|
||||
this.account = account;
|
||||
this.accountLoaderFactory = ailf;
|
||||
@@ -99,6 +107,8 @@ public class AccountApiImpl implements AccountApi {
|
||||
this.starredChangesCreate = starredChangesCreate;
|
||||
this.starredChangesDelete = starredChangesDelete;
|
||||
this.createEmailFactory = createEmailFactory;
|
||||
this.getSshKeys = getSshKeys;
|
||||
this.addSshKey = addSshKey;
|
||||
this.gpgApiAdapter = gpgApiAdapter;
|
||||
}
|
||||
|
||||
@@ -213,6 +223,26 @@ public class AccountApiImpl implements AccountApi {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SshKeyInfo> listSshKeys() throws RestApiException {
|
||||
try {
|
||||
return getSshKeys.apply(account);
|
||||
} catch (OrmException e) {
|
||||
throw new RestApiException("Cannot list SSH keys", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SshKeyInfo addSshKey(String key) throws RestApiException {
|
||||
AddSshKey.Input in = new AddSshKey.Input();
|
||||
in.raw = RawInputUtil.create(key);
|
||||
try {
|
||||
return addSshKey.apply(account, in).value();
|
||||
} catch (OrmException | IOException e) {
|
||||
throw new RestApiException("Cannot add SSH key", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, GpgKeyInfo> listGpgKeys() throws RestApiException {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user