use of com.google.gerrit.reviewdb.client.AccountSshKey in project gerrit by GerritCodeReview.
the class AuthorizedKeys method parse.
public static List<Optional<AccountSshKey>> parse(Account.Id accountId, String s) {
List<Optional<AccountSshKey>> keys = new ArrayList<>();
int seq = 1;
for (String line : s.split("\\r?\\n")) {
line = line.trim();
if (line.isEmpty()) {
continue;
} else if (line.startsWith(INVALID_KEY_COMMENT_PREFIX)) {
String pub = line.substring(INVALID_KEY_COMMENT_PREFIX.length());
AccountSshKey key = new AccountSshKey(new AccountSshKey.Id(accountId, seq++), pub);
key.setInvalid();
keys.add(Optional.of(key));
} else if (line.startsWith(DELETED_KEY_COMMENT)) {
keys.add(Optional.empty());
seq++;
} else if (line.startsWith("#")) {
continue;
} else {
AccountSshKey key = new AccountSshKey(new AccountSshKey.Id(accountId, seq++), line);
keys.add(Optional.of(key));
}
}
return keys;
}
use of com.google.gerrit.reviewdb.client.AccountSshKey in project gerrit by GerritCodeReview.
the class SshKeys method parse.
public AccountResource.SshKey parse(IdentifiedUser user, IdString id) throws ResourceNotFoundException, IOException, ConfigInvalidException {
try {
int seq = Integer.parseInt(id.get(), 10);
AccountSshKey sshKey = authorizedKeys.getKey(user.getAccountId(), seq);
if (sshKey == null) {
throw new ResourceNotFoundException(id);
}
return new AccountResource.SshKey(user, sshKey);
} catch (NumberFormatException e) {
throw new ResourceNotFoundException(id);
}
}
use of com.google.gerrit.reviewdb.client.AccountSshKey in project gerrit by GerritCodeReview.
the class VersionedAuthorizedKeys method addKey.
/**
* Adds a new public SSH key.
*
* <p>If the specified public key exists already, the existing key is returned.
*
* @param pub the public SSH key to be added
* @return the new SSH key
* @throws InvalidSshKeyException
*/
private AccountSshKey addKey(String pub) throws InvalidSshKeyException {
checkLoaded();
for (Optional<AccountSshKey> key : keys) {
if (key.isPresent() && key.get().getSshPublicKey().trim().equals(pub.trim())) {
return key.get();
}
}
int seq = keys.size() + 1;
AccountSshKey.Id keyId = new AccountSshKey.Id(accountId, seq);
AccountSshKey key = sshKeyCreator.create(keyId, pub);
keys.add(Optional.of(key));
return key;
}
use of com.google.gerrit.reviewdb.client.AccountSshKey in project gerrit by GerritCodeReview.
the class InitAdminUser method createSshKey.
private AccountSshKey createSshKey(Account.Id id, String keyFile) throws IOException {
Path p = Paths.get(keyFile);
if (!Files.exists(p)) {
throw new IOException(String.format("Cannot add public SSH key: %s is not a file", keyFile));
}
String content = new String(Files.readAllBytes(p), UTF_8);
return new AccountSshKey(new AccountSshKey.Id(id, 1), content);
}
use of com.google.gerrit.reviewdb.client.AccountSshKey in project gerrit by GerritCodeReview.
the class SshKeyCreatorImpl method create.
@Override
public AccountSshKey create(AccountSshKey.Id id, String encoded) throws InvalidSshKeyException {
try {
AccountSshKey key = new AccountSshKey(id, SshUtil.toOpenSshPublicKey(encoded));
SshUtil.parse(key);
return key;
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new InvalidSshKeyException();
} catch (NoSuchProviderException e) {
log.error("Cannot parse SSH key", e);
throw new InvalidSshKeyException();
}
}
Aggregations