Merge "Support to retrieve an SSH key via REST"

This commit is contained in:
Shawn Pearce
2013-06-04 09:28:26 +00:00
committed by Gerrit Code Review
6 changed files with 119 additions and 19 deletions

View File

@@ -476,6 +476,39 @@ link:#ssh-key-info[SshKeyInfo] entities.
]
----
[[get-ssh-key]]
Get SSH Key
~~~~~~~~~~~
[verse]
'GET /accounts/link:#account-id[\{account-id\}]/sshkeys/link:#ssh-key-id[\{ssh-key-id\}]'
Retrieves an SSH key of a user.
.Request
----
GET /accounts/self/sshkeys/1 HTTP/1.0
----
As response an link:#ssh-key-info[SshKeyInfo] entity is returned that
describes the SSH key.
.Response
----
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Type: application/json;charset=UTF-8
)]}'
{
"seq": 1,
"ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0T...YImydZAw\u003d\u003d john.doe@example.com",
"encoded_key": "AAAAB3NzaC1yc2EAAAABIwAAAQEA0T...YImydZAw\u003d\u003d",
"algorithm": "ssh-rsa",
"comment": "john.doe@example.com",
"valid": true
}
----
[[list-account-capabilities]]
List Account Capabilities
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -843,6 +876,11 @@ user.
~~~~~~~~~~~~
The user name.
[[ssh-key-id]]
\{ssh-key-id\}
~~~~~~~~~~~~
The sequence number of the SSH key.
[[json-entities]]
JSON Entities

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.account;
import com.google.gerrit.extensions.restapi.RestResource;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.AccountSshKey;
import com.google.gerrit.server.IdentifiedUser;
import com.google.inject.TypeLiteral;
@@ -78,9 +79,15 @@ public class AccountResource implements RestResource {
}
static class SshKey extends AccountResource {
private final AccountSshKey sshKey;
public SshKey(IdentifiedUser user) {
public SshKey(IdentifiedUser user, AccountSshKey sshKey) {
super(user);
this.sshKey = sshKey;
}
public AccountSshKey getSshKey() {
return sshKey;
}
}
}

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.account;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.account.AccountResource.SshKey;
import com.google.gerrit.server.account.GetSshKeys.SshKeyInfo;
public class GetSshKey implements RestReadView<AccountResource.SshKey> {
@Override
public SshKeyInfo apply(SshKey rsrc) {
return new SshKeyInfo(rsrc.getSshKey());
}
}

View File

@@ -49,24 +49,26 @@ public class GetSshKeys implements RestReadView<AccountResource> {
List<SshKeyInfo> sshKeys = Lists.newArrayList();
for (AccountSshKey sshKey : dbProvider.get().accountSshKeys()
.byAccount(rsrc.getUser().getAccountId()).toList()) {
SshKeyInfo info = new SshKeyInfo();
info.seq = sshKey.getKey().get();
info.sshPublicKey = sshKey.getSshPublicKey();
info.encodedKey = sshKey.getEncodedKey();
info.algorithm = sshKey.getAlgorithm();
info.comment = Strings.emptyToNull(sshKey.getComment());
info.valid = sshKey.isValid();
sshKeys.add(info);
sshKeys.add(new SshKeyInfo(sshKey));
}
return sshKeys;
}
public static class SshKeyInfo {
public int seq;
public String sshPublicKey;
public String encodedKey;
public String algorithm;
public String comment;
public boolean valid;
public SshKeyInfo(AccountSshKey sshKey) {
seq = sshKey.getKey().get();
sshPublicKey = sshKey.getSshPublicKey();
encodedKey = sshKey.getEncodedKey();
algorithm = sshKey.getAlgorithm();
comment = Strings.emptyToNull(sshKey.getComment());
valid = sshKey.isValid();
}
int seq;
String sshPublicKey;
String encodedKey;
String algorithm;
String comment;
boolean valid;
}
}

View File

@@ -51,6 +51,7 @@ public class Module extends RestApiModule {
put(ACCOUNT_KIND, "password.http").to(PutHttpPassword.class);
delete(ACCOUNT_KIND, "password.http").to(PutHttpPassword.class);
child(ACCOUNT_KIND, "sshkeys").to(SshKeys.class);
get(SSH_KEY_KIND).to(GetSshKey.class);
get(ACCOUNT_KIND, "avatar").to(GetAvatar.class);
get(ACCOUNT_KIND, "avatar.change.url").to(GetAvatarChangeUrl.class);
child(ACCOUNT_KIND, "capabilities").to(Capabilities.class);

View File

@@ -19,6 +19,10 @@ import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.AccountSshKey;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -26,12 +30,17 @@ public class SshKeys implements
ChildCollection<AccountResource, AccountResource.SshKey> {
private final DynamicMap<RestView<AccountResource.SshKey>> views;
private final Provider<GetSshKeys> list;
private final Provider<CurrentUser> self;
private final Provider<ReviewDb> dbProvider;
@Inject
SshKeys(DynamicMap<RestView<AccountResource.SshKey>> views,
Provider<GetSshKeys> list) {
Provider<GetSshKeys> list, Provider<CurrentUser> self,
Provider<ReviewDb> dbProvider) {
this.views = views;
this.list = list;
this.self = self;
this.dbProvider = dbProvider;
}
@Override
@@ -40,9 +49,25 @@ public class SshKeys implements
}
@Override
public AccountResource.SshKey parse(AccountResource parent, IdString id)
throws ResourceNotFoundException {
throw new ResourceNotFoundException(id);
public AccountResource.SshKey parse(AccountResource rsrc, IdString id)
throws ResourceNotFoundException, OrmException {
if (self.get() != rsrc.getUser()
&& !self.get().getCapabilities().canAdministrateServer()) {
throw new ResourceNotFoundException();
}
try {
int seq = Integer.parseInt(id.get(), 10);
AccountSshKey sshKey =
dbProvider.get().accountSshKeys()
.get(new AccountSshKey.Id(rsrc.getUser().getAccountId(), seq));
if (sshKey == null) {
throw new ResourceNotFoundException(id);
}
return new AccountResource.SshKey(rsrc.getUser(), sshKey);
} catch (NumberFormatException e) {
throw new ResourceNotFoundException(id);
}
}
@Override