mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-04 05:39:11 +01:00
work on getting signatures running
This commit is contained in:
@@ -422,16 +422,8 @@ public final class GitUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param revWalk
|
||||
* @param ref
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
* Returns the commit for the given ref.
|
||||
* If the given ref is for a tag, the commit that this tag belongs to is returned instead.
|
||||
*/
|
||||
public static RevCommit getCommit(org.eclipse.jgit.lib.Repository repository,
|
||||
RevWalk revWalk, Ref ref)
|
||||
@@ -710,22 +702,20 @@ public final class GitUtil
|
||||
return name;
|
||||
}
|
||||
|
||||
private static final byte[] GPG_HEADER = {'P', 'G', 'P'};
|
||||
private static final String GPG_HEADER = "-----BEGIN PGP SIGNATURE-----";
|
||||
|
||||
public static Optional<Signature> getTagSignature(RevObject revObject, GPG gpg) {
|
||||
public static Optional<Signature> getTagSignature(RevObject revObject, GPG gpg, RevWalk revWalk) throws IOException {
|
||||
if (revObject instanceof RevTag) {
|
||||
RevTag tag = (RevTag) revObject;
|
||||
byte[] raw = tag.getFullMessage().getBytes();
|
||||
|
||||
int start = RawParseUtils.headerStart(GPG_HEADER, raw, 0);
|
||||
if (start < 0) {
|
||||
final byte[] bytes = revWalk.getObjectReader().open(revObject.getId()).getBytes();
|
||||
final String message = new String(bytes);
|
||||
final int signatureStartIndex = message.indexOf(GPG_HEADER);
|
||||
if (signatureStartIndex < 0) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
int end = RawParseUtils.headerEnd(raw, start);
|
||||
byte[] signature = Arrays.copyOfRange(raw, start, end);
|
||||
final String signature = message.substring(signatureStartIndex);
|
||||
|
||||
String publicKeyId = gpg.findPublicKeyId(signature);
|
||||
String publicKeyId = gpg.findPublicKeyId(signature.getBytes());
|
||||
if (Strings.isNullOrEmpty(publicKeyId)) {
|
||||
// key not found
|
||||
return Optional.of(new Signature(publicKeyId, "gpg", SignatureStatus.NOT_FOUND, null, Collections.emptySet()));
|
||||
@@ -739,19 +729,7 @@ public final class GitUtil
|
||||
|
||||
PublicKey publicKey = publicKeyById.get();
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
byte[] headerPrefix = Arrays.copyOfRange(raw, 0, start - GPG_HEADER.length - 1);
|
||||
baos.write(headerPrefix);
|
||||
|
||||
byte[] headerSuffix = Arrays.copyOfRange(raw, end + 1, raw.length);
|
||||
baos.write(headerSuffix);
|
||||
} catch (IOException ex) {
|
||||
// this will never happen, because we are writing into memory
|
||||
throw new IllegalStateException("failed to write into memory", ex);
|
||||
}
|
||||
|
||||
boolean verified = publicKey.verify(baos.toByteArray(), signature);
|
||||
boolean verified = publicKey.verify(message.substring(0, signatureStartIndex - 1).getBytes(), signature.getBytes());
|
||||
return Optional.of(new Signature(
|
||||
publicKeyId,
|
||||
"gpg",
|
||||
|
||||
@@ -111,7 +111,7 @@ public class GitTagCommand extends AbstractGitCommand implements TagCommand {
|
||||
|
||||
try (RevWalk walk = new RevWalk(git.getRepository())) {
|
||||
revObject = walk.parseTag(ref.getObjectId());
|
||||
final Optional<Signature> tagSignature = GitUtil.getTagSignature(revObject, gpg);
|
||||
final Optional<Signature> tagSignature = GitUtil.getTagSignature(revObject, gpg, walk);
|
||||
tagSignature.ifPresent(tag::addSignature);
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
List<Ref> tagList = git.tagList().call();
|
||||
|
||||
tags = Lists.transform(tagList,
|
||||
new TransformFuntion(git.getRepository(), revWalk, gpg, git));
|
||||
new TransformFunction(git.getRepository(), revWalk, gpg, git));
|
||||
} catch (GitAPIException ex) {
|
||||
throw new InternalRepositoryException(repository, "could not read tags from repository", ex);
|
||||
} finally {
|
||||
@@ -100,13 +100,13 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
* @author Enter your name here...
|
||||
* @version Enter version here..., 12/07/06
|
||||
*/
|
||||
private static class TransformFuntion implements Function<Ref, Tag> {
|
||||
private static class TransformFunction implements Function<Ref, Tag> {
|
||||
|
||||
/**
|
||||
* the logger for TransformFuntion
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(TransformFuntion.class);
|
||||
LoggerFactory.getLogger(TransformFunction.class);
|
||||
|
||||
//~--- constructors -------------------------------------------------------
|
||||
|
||||
@@ -116,10 +116,10 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
* @param repository
|
||||
* @param revWalk
|
||||
*/
|
||||
public TransformFuntion(org.eclipse.jgit.lib.Repository repository,
|
||||
RevWalk revWalk,
|
||||
GPG gpg,
|
||||
Git git) {
|
||||
public TransformFunction(org.eclipse.jgit.lib.Repository repository,
|
||||
RevWalk revWalk,
|
||||
GPG gpg,
|
||||
Git git) {
|
||||
this.repository = repository;
|
||||
this.revWalk = revWalk;
|
||||
this.gpg = gpg;
|
||||
@@ -149,12 +149,12 @@ public class GitTagsCommand extends AbstractGitCommand implements TagsCommand {
|
||||
try {
|
||||
RevTag revTag = GitUtil.getTag(repository, revWalk, ref);
|
||||
|
||||
final Optional<Signature> tagSignature = GitUtil.getTagSignature(revTag, gpg);
|
||||
final Optional<Signature> tagSignature = GitUtil.getTagSignature(revTag, gpg, revWalk);
|
||||
if (tagSignature.isPresent()) {
|
||||
tag.addSignature(tagSignature.get());
|
||||
}
|
||||
} catch (IncorrectObjectTypeException e) {
|
||||
// Ignore, this must be a lightweight tag
|
||||
// Ignore because it is a lightweight tag which cannot have signatures
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user