use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.
the class AddSshKey method apply.
public Response<SshKeyInfo> apply(IdentifiedUser user, SshKeyInput input) throws BadRequestException, IOException, ConfigInvalidException {
if (input == null) {
input = new SshKeyInput();
}
if (input.raw == null) {
throw new BadRequestException("SSH public key missing");
}
final RawInput rawKey = input.raw;
String sshPublicKey = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return rawKey.getInputStream();
}
}.asCharSource(UTF_8).read();
try {
AccountSshKey sshKey = authorizedKeys.addKey(user.getAccountId(), sshPublicKey);
try {
addKeyFactory.create(user, sshKey).send();
} catch (EmailException e) {
logger.atSevere().withCause(e).log("Cannot send SSH key added message to %s", user.getAccount().preferredEmail());
}
user.getUserName().ifPresent(sshKeyCache::evict);
return Response.created(GetSshKeys.newSshKeyInfo(sshKey));
} catch (InvalidSshKeyException e) {
throw new BadRequestException(e.getMessage());
}
}
use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.
the class SetAccountCommand method deleteSshKey.
private void deleteSshKey(SshKeyInfo i) throws AuthException, RepositoryNotFoundException, IOException, ConfigInvalidException, PermissionBackendException {
AccountSshKey sshKey = AccountSshKey.create(user.getAccountId(), i.seq, i.sshPublicKey);
deleteSshKey.apply(new AccountResource.SshKey(user.asIdentifiedUser(), sshKey), null);
}
use of com.google.gerrit.server.account.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, e);
}
}
use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.
the class SshKeyCreatorImpl method create.
@Override
public AccountSshKey create(Account.Id accountId, int seq, String encoded) throws InvalidSshKeyException {
try {
AccountSshKey key = AccountSshKey.create(accountId, seq, SshUtil.toOpenSshPublicKey(encoded));
SshUtil.parse(key);
return key;
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new InvalidSshKeyException(e);
} catch (NoSuchProviderException e) {
logger.atSevere().withCause(e).log("Cannot parse SSH key");
throw new InvalidSshKeyException(e);
}
}
use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.
the class InitAdminUser method postRun.
@Override
public void postRun() throws Exception {
if (!accounts.hasAnyAccount()) {
welcome();
}
AuthType authType = flags.cfg.getEnum(AuthType.values(), "auth", null, "type", null);
if (authType != AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT) {
return;
}
if (!accounts.hasAnyAccount()) {
ui.header("Gerrit Administrator");
if (ui.yesno(true, "Create administrator user")) {
Account.Id id = Account.id(sequencesOnInit.nextAccountId());
String username = ui.readString("admin", "username");
String name = ui.readString("Administrator", "name");
String httpPassword = ui.readString("secret", "HTTP password");
AccountSshKey sshKey = readSshKey(id);
String email = readEmail(sshKey);
List<ExternalId> extIds = new ArrayList<>(2);
extIds.add(externalIdFactory.createUsername(username, id, httpPassword));
if (email != null) {
extIds.add(externalIdFactory.createEmail(id, email));
}
externalIds.insert("Add external IDs for initial admin user", extIds);
Account persistedAccount = accounts.insert(Account.builder(id, TimeUtil.now()).setFullName(name).setPreferredEmail(email));
// Only two groups should exist at this point in time and hence iterating over all of them
// is cheap.
Optional<GroupReference> adminGroupReference = groupsOnInit.getAllGroupReferences().filter(group -> group.getName().equals("Administrators")).findAny();
if (!adminGroupReference.isPresent()) {
throw new NoSuchGroupException("Administrators");
}
GroupReference adminGroup = adminGroupReference.get();
groupsOnInit.addGroupMember(adminGroup.getUUID(), persistedAccount);
if (sshKey != null) {
VersionedAuthorizedKeysOnInit authorizedKeys = authorizedKeysFactory.create(id).load();
authorizedKeys.addKey(sshKey.sshPublicKey());
authorizedKeys.save("Add SSH key for initial admin user\n");
}
AccountState as = AccountState.forAccount(persistedAccount, extIds);
for (AccountIndex accountIndex : accountIndexCollection.getWriteIndexes()) {
accountIndex.replace(as);
}
InternalGroup adminInternalGroup = groupsOnInit.getExistingGroup(adminGroup);
for (GroupIndex groupIndex : groupIndexCollection.getWriteIndexes()) {
groupIndex.replace(adminInternalGroup);
}
}
}
}
Aggregations