in apm-agent-attach-cli/src/main/java/co/elastic/apm/attach/PgpSignatureVerifierLoader.java [59:142]
static PgpSignatureVerifierLoader getInstance(final String sourceLib, final Path targetLib, String verifierClassName) throws Exception {
// done lazily so that we create the logger only after proper initialization
final Logger logger = LogManager.getLogger(PgpSignatureVerifierLoader.class);
Path libPath;
// first try to see if the lib is available as a resource available to the current class loader
URL resourceUrl = PgpSignatureVerifierLoader.class.getResource(sourceLib);
if (resourceUrl != null) {
URI libUri = resourceUrl.toURI();
if (libUri.getScheme().equals("jar")) {
FileSystem fileSystem;
try {
fileSystem = FileSystems.getFileSystem(libUri);
} catch (FileSystemNotFoundException e) {
fileSystem = FileSystems.newFileSystem(libUri, Collections.<String, Object>emptyMap());
}
libPath = fileSystem.getPath(sourceLib);
} else {
// this allows unit testing
libPath = Paths.get(libUri);
}
} else {
// try to locate as an external lib
libPath = Paths.get(sourceLib);
}
logger.debug("Traversing [{}] for PGP signature verifier implementation and related dependencies", libPath);
if (!Files.exists(libPath)) {
throw new IllegalArgumentException(String.format("%s dir cannot be found", sourceLib));
}
final List<URL> urlList = new ArrayList<>();
Files.walkFileTree(libPath, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String fileName = file.getFileName().toString();
if (fileName.endsWith(".jar")) {
boolean copyJar = true;
Path jarInTargetLib = Paths.get(targetLib.toString(), fileName);
if (Files.exists(jarInTargetLib)) {
logger.trace("{} already exists at {}", fileName, targetLib);
if (Files.isReadable(jarInTargetLib)) {
copyJar = false;
} else {
// quick and dirty - this is a temporary solution until we download agent from the Fleet package registry
logger.info("{} is not readable for the current user, removing and copying new one", jarInTargetLib);
Files.delete(jarInTargetLib);
}
}
if (copyJar) {
logger.debug("Copying {} to {}", fileName, targetLib);
Files.copy(file, jarInTargetLib);
}
urlList.add(jarInTargetLib.toUri().toURL());
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
throw exc;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return FileVisitResult.CONTINUE;
}
});
if (urlList.isEmpty()) {
throw new IllegalArgumentException(String.format("%s dir is empty", sourceLib));
} else {
logger.debug("{} directory contents: {}", sourceLib, urlList);
}
return new PgpSignatureVerifierLoader(urlList.toArray(new URL[0]), verifierClassName);
}