use of org.commonjava.atlas.maven.ident.ref.ProjectRef in project indy by Commonjava.
the class KojiMavenMetadataProvider method getMetadata.
@Override
@Measure
public Metadata getMetadata(StoreKey targetKey, String path) throws IndyWorkflowException {
Logger logger = LoggerFactory.getLogger(getClass());
if (group != targetKey.getType()) {
logger.debug("Not a group. Cannot supplement with metadata from Koji builds");
return null;
}
if (!kojiConfig.isEnabled()) {
logger.debug("Koji add-on is disabled.");
return null;
}
try {
ArtifactStore target = storeDataManager.getArtifactStore(targetKey);
if (!kojiConfig.isEnabledFor(target)) {
logger.debug("Koji integration is not enabled for group: {}", targetKey);
return null;
}
} catch (IndyDataException e) {
logger.error("Failed to get metadata for path {} in store {}: {}", path, targetKey, e.getMessage());
}
File mdFile = new File(path);
File artifactDir = mdFile.getParentFile();
File groupDir = artifactDir == null ? null : artifactDir.getParentFile();
if (artifactDir == null || groupDir == null) {
logger.debug("Invalid groupId / artifactId directory structure: '{}' / '{}'", groupDir, artifactDir);
return null;
}
String groupId = groupDir.getPath().replace(File.separatorChar, '.');
String artifactId = artifactDir.getName();
ProjectRef ref = null;
try {
ref = new SimpleProjectRef(groupId, artifactId);
} catch (InvalidRefException e) {
logger.warn("Not a valid Maven GA: {}:{}. Skipping Koji metadata retrieval.", groupId, artifactId);
}
if (ref == null) {
logger.debug("Could not render a valid Maven GA for path: '{}'", path);
return null;
}
ProjectRef ga = ref;
AtomicReference<IndyWorkflowException> wfError = new AtomicReference<>();
return versionMetadataLocks.lockAnd(ga, kojiConfig.getLockTimeoutSeconds(), k -> {
Metadata metadata = versionMetadata.get(ga);
if (metadata == null) {
try {
metadata = executeKojiMetadataLookup(ga, path);
} catch (IndyWorkflowException e) {
wfError.set(e);
metadata = null;
} catch (KojiClientException e) {
// FIXME: Should this bubble up like IndyWorkflowException does in the case of overloaded threadpool?
Throwable cause = e.getCause();
logger.error(String.format("Failed to retrieve version metadata for: %s from Koji. Reason: %s", ga, e.getMessage()), e);
if (cause instanceof RuntimeException) {
logger.error("Previous exception's nested cause was a RuntimeException variant:", cause);
}
metadata = null;
}
if (metadata != null) {
Metadata md = metadata;
// FIXME: Need a way to listen for cache expiration and re-request this?
versionMetadata.execute((cache) -> cache.put(ga, md, kojiConfig.getMetadataTimeoutSeconds(), TimeUnit.SECONDS));
} else {
logger.debug("Returning null metadata result for unknown reason (path: '{}')", path);
}
}
return metadata;
}, (k, lock) -> {
logger.error("Failed to acquire Koji GA version metadata lock on: '{}' in {} seconds.", ga, kojiConfig.getLockTimeoutSeconds());
return false;
});
}
use of org.commonjava.atlas.maven.ident.ref.ProjectRef in project indy by Commonjava.
the class KojiBuildAuthority method isAuthorized.
@Measure
public boolean isAuthorized(String path, EventMetadata eventMetadata, ProjectRef ref, KojiBuildInfo build, KojiSessionInfo session, Map<Integer, KojiBuildArchiveCollection> seenBuildArchives) throws KojiClientException {
ArtifactStore authoritativeStore = getAuthoritativeStore();
if (authoritativeStore != null) {
KojiBuildArchiveCollection archiveCollection = seenBuildArchives.get(build.getId());
if (archiveCollection == null) {
List<KojiArchiveInfo> archiveList = kojiContentProvider.listArchivesForBuild(build.getId(), session);
archiveCollection = new KojiBuildArchiveCollection(build, archiveList);
seenBuildArchives.put(build.getId(), archiveCollection);
}
if (archiveCollection == null) {
throw new KojiClientException("Failed to retrieve archives for build: %s", build);
}
// @formatter:off
Predicate<KojiArchiveInfo> archiveInfoFilter = (archive) -> EXCLUDED_FILE_ENDINGS.stream().allMatch(ending -> !archive.getFilename().endsWith(ending));
List<KojiArchiveInfo> sortedArchives = archiveCollection.getArchives().stream().filter(archiveInfoFilter).sorted((a1, a2) -> {
TypePriority t1 = TypePriority.get(a1.getExtension());
TypePriority t2 = TypePriority.get(a2.getExtension());
return Integer.valueOf(t1.ordinal()).compareTo(t2.ordinal());
}).collect(Collectors.toList());
for (KojiArchiveInfo archive : sortedArchives) {
try {
if (isMavenArtifact(archive)) {
// skip non-Maven artifacts
continue;
}
if (containsPlaceholders(archive)) {
return false;
}
String artifactPath = ArtifactPathUtils.formatArtifactPath(archive.asArtifact(), typeMapper);
String md5 = checksumArtifact(authoritativeStore, artifactPath, eventMetadata);
if (isNotBlank(md5)) {
// FIXME: not sure if all koji archives are using md5 as checksum type for maven build
String kojiMd5 = archive.getChecksum();
Logger logger = LoggerFactory.getLogger(getClass());
logger.info("Checking checksum for {} (path: {}) in auth store {}, auth store checksum:{}, koji build check sum:{}", ref, path, authoritativeStore, md5, kojiMd5);
if (!md5.equals(kojiMd5)) {
// if checksum is not the same, it means the artifact in koji is DIFFERENT from the one in the authoritative store. Reject this.
return false;
}
}
} catch (Exception e) {
Logger logger = LoggerFactory.getLogger(getClass());
logger.error("SHOULD NEVER HAPPEN: Failed to transform artifact to path: " + e.getMessage(), e);
}
}
}
return true;
}
use of org.commonjava.atlas.maven.ident.ref.ProjectRef in project indy by Commonjava.
the class KojiMavenMetadataProvider method handleExpired.
private void handleExpired(ProjectRef key) {
Logger logger = LoggerFactory.getLogger(getClass());
if (!kojiConfig.isEnabled()) {
logger.debug("Koji add-on is disabled.");
return;
}
logger.info("Koji metadata expired for GA: {}", key);
try {
Set<Group> affected = storeDataManager.query().getAll(s -> group == s.getType() && kojiConfig.isEnabledFor(s)).stream().map(s -> (Group) s).collect(Collectors.toSet());
if (storeDataManager instanceof AbstractStoreDataManager) {
affected = ((AbstractStoreDataManager) storeDataManager).filterAffectedGroups(affected);
}
if (!affected.isEmpty()) {
logger.info("Triggering metadata cleanup from Koji metadata expiration, for GA: {} in groups: {}", key, affected);
String path = ArtifactPathUtils.formatMetadataPath(key, METADATA_NAME);
clearPaths(affected, path);
}
} catch (IndyDataException ex) {
logger.error("Failed to clear group metadata for expired Koji metadata: " + key, ex);
} catch (TransferException ex) {
logger.error("Failed to format metadata path for: " + key, ex);
}
}
Aggregations