Search in sources :

Example 1 with AccountSshKey

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;
}
Also used : Optional(java.util.Optional) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) ArrayList(java.util.ArrayList)

Example 2 with AccountSshKey

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);
    }
}
Also used : AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 3 with AccountSshKey

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;
}
Also used : Id(com.google.gerrit.reviewdb.client.AccountSshKey.Id) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) Id(com.google.gerrit.reviewdb.client.AccountSshKey.Id)

Example 4 with AccountSshKey

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);
}
Also used : Path(java.nio.file.Path) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) IOException(java.io.IOException)

Example 5 with AccountSshKey

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();
    }
}
Also used : InvalidSshKeyException(com.google.gerrit.common.errors.InvalidSshKeyException) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

AccountSshKey (com.google.gerrit.reviewdb.client.AccountSshKey)15 Account (com.google.gerrit.reviewdb.client.Account)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 InvalidSshKeyException (com.google.gerrit.common.errors.InvalidSshKeyException)3 VersionedAuthorizedKeys (com.google.gerrit.server.account.VersionedAuthorizedKeys)3 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)3 Collection (java.util.Collection)3 Strings (com.google.common.base.Strings)2 Ordering (com.google.common.collect.Ordering)2 Id (com.google.gerrit.reviewdb.client.AccountSshKey.Id)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 SimpleSshKeyCreator (com.google.gerrit.server.account.VersionedAuthorizedKeys.SimpleSshKeyCreator)2 AllUsersName (com.google.gerrit.server.config.AllUsersName)2 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)2 OrmException (com.google.gwtorm.server.OrmException)2 Inject (com.google.inject.Inject)2 Provider (com.google.inject.Provider)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2