use of com.google.gerrit.gpg.PublicKeyStore in project gerrit by GerritCodeReview.
the class DeleteGpgKey method apply.
@Override
public Response<?> apply(GpgKey rsrc, Input input) throws RestApiException, PGPException, IOException, ConfigInvalidException {
PGPPublicKey key = rsrc.getKeyRing().getPublicKey();
String fingerprint = BaseEncoding.base16().encode(key.getFingerprint());
Optional<ExternalId> extId = externalIds.get(externalIdKeyFactory.create(SCHEME_GPGKEY, fingerprint));
if (!extId.isPresent()) {
throw new ResourceNotFoundException(fingerprint);
}
accountsUpdateProvider.get().update("Delete GPG Key via API", rsrc.getUser().getAccountId(), u -> u.deleteExternalId(extId.get()));
try (PublicKeyStore store = storeProvider.get()) {
store.remove(rsrc.getKeyRing().getPublicKey().getFingerprint());
CommitBuilder cb = new CommitBuilder();
PersonIdent committer = serverIdent.get();
cb.setAuthor(rsrc.getUser().newCommitterIdent(committer));
cb.setCommitter(committer);
cb.setMessage("Delete public key " + keyIdToString(key.getKeyID()));
RefUpdate.Result saveResult = store.save(cb);
switch(saveResult) {
case NO_CHANGE:
case FAST_FORWARD:
try {
deleteKeySenderFactory.create(rsrc.getUser(), ImmutableList.of(PublicKeyStore.keyToString(key))).send();
} catch (EmailException e) {
logger.atSevere().withCause(e).log("Cannot send GPG key deletion message to %s", rsrc.getUser().getAccount().preferredEmail());
}
break;
case LOCK_FAILURE:
case FORCED:
case IO_FAILURE:
case NEW:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
case RENAMED:
case REJECTED_MISSING_OBJECT:
case REJECTED_OTHER_REASON:
default:
throw new StorageException(String.format("Failed to delete public key: %s", saveResult));
}
}
return Response.none();
}
use of com.google.gerrit.gpg.PublicKeyStore in project gerrit by GerritCodeReview.
the class PostGpgKeys method apply.
@Override
public Response<Map<String, GpgKeyInfo>> apply(AccountResource rsrc, GpgKeysInput input) throws RestApiException, PGPException, IOException, ConfigInvalidException {
GpgKeys.checkVisible(self, rsrc);
Collection<ExternalId> existingExtIds = externalIds.byAccount(rsrc.getUser().getAccountId(), SCHEME_GPGKEY);
try (PublicKeyStore store = storeProvider.get()) {
Map<ExternalId, Fingerprint> toRemove = readKeysToRemove(input, existingExtIds);
Collection<Fingerprint> fingerprintsToRemove = toRemove.values();
List<PGPPublicKeyRing> newKeys = readKeysToAdd(input, fingerprintsToRemove);
List<ExternalId> newExtIds = new ArrayList<>(existingExtIds.size());
for (PGPPublicKeyRing keyRing : newKeys) {
PGPPublicKey key = keyRing.getPublicKey();
ExternalId.Key extIdKey = toExtIdKey(key.getFingerprint());
Account account = getAccountByExternalId(extIdKey);
if (account != null) {
if (!account.id().equals(rsrc.getUser().getAccountId())) {
throw new ResourceConflictException("GPG key already associated with another account");
}
} else {
newExtIds.add(externalIdFactory.create(extIdKey, rsrc.getUser().getAccountId()));
}
}
storeKeys(rsrc, newKeys, fingerprintsToRemove);
accountsUpdateProvider.get().update("Update GPG Keys via API", rsrc.getUser().getAccountId(), u -> u.replaceExternalIds(toRemove.keySet(), newExtIds));
return Response.ok(toJson(newKeys, fingerprintsToRemove, store, rsrc.getUser()));
}
}
Aggregations