Search in sources :

Example 1 with ProjectRef

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;
    });
}
Also used : InvalidRefException(org.commonjava.atlas.maven.ident.ref.InvalidRefException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) Logger(org.slf4j.Logger) IndyDataException(org.commonjava.indy.data.IndyDataException) SimpleProjectRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectRef) KojiClientException(com.redhat.red.build.koji.KojiClientException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) SimpleProjectRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.atlas.maven.ident.ref.ProjectRef) File(java.io.File) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 2 with ProjectRef

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;
}
Also used : Arrays(java.util.Arrays) ContentDigester(org.commonjava.indy.content.ContentDigester) StringUtils.trimToEmpty(org.apache.commons.lang3.StringUtils.trimToEmpty) LoggerFactory(org.slf4j.LoggerFactory) TransferMetadata(org.commonjava.maven.galley.io.checksum.TransferMetadata) HashMap(java.util.HashMap) Inject(javax.inject.Inject) Transfer(org.commonjava.maven.galley.model.Transfer) StringUtils.contains(org.apache.commons.lang3.StringUtils.contains) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) FORCE_CHECKSUM(org.commonjava.maven.galley.io.ChecksummingTransferDecorator.FORCE_CHECKSUM) Measure(org.commonjava.o11yphant.metrics.annotation.Measure) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) CacheProducer(org.commonjava.indy.subsys.infinispan.CacheProducer) Map(java.util.Map) ContentDigest(org.commonjava.maven.galley.io.checksum.ContentDigest) KojiArchiveInfo(com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo) KojiClientException(com.redhat.red.build.koji.KojiClientException) StoreKey(org.commonjava.indy.model.core.StoreKey) ArtifactPathUtils(org.commonjava.maven.galley.maven.util.ArtifactPathUtils) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ProjectRef(org.commonjava.atlas.maven.ident.ref.ProjectRef) Predicate(java.util.function.Predicate) KojiClient(com.redhat.red.build.koji.KojiClient) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) KojiSessionInfo(com.redhat.red.build.koji.model.xmlrpc.KojiSessionInfo) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) KojiBuildInfo(com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) TypeMapper(org.commonjava.maven.galley.maven.spi.type.TypeMapper) ApplicationScoped(javax.enterprise.context.ApplicationScoped) KojiBuildArchiveCollection(com.redhat.red.build.koji.model.xmlrpc.KojiBuildArchiveCollection) Collections(java.util.Collections) TRUE(java.lang.Boolean.TRUE) DirectContentAccess(org.commonjava.indy.content.DirectContentAccess) IndyDataException(org.commonjava.indy.data.IndyDataException) StoreDataManager(org.commonjava.indy.data.StoreDataManager) IndyKojiConfig(org.commonjava.indy.koji.conf.IndyKojiConfig) InputStream(java.io.InputStream) KojiBuildArchiveCollection(com.redhat.red.build.koji.model.xmlrpc.KojiBuildArchiveCollection) KojiClientException(com.redhat.red.build.koji.KojiClientException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) Logger(org.slf4j.Logger) KojiArchiveInfo(com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) KojiClientException(com.redhat.red.build.koji.KojiClientException) IOException(java.io.IOException) IndyDataException(org.commonjava.indy.data.IndyDataException) Measure(org.commonjava.o11yphant.metrics.annotation.Measure)

Example 3 with ProjectRef

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);
    }
}
Also used : Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) KojiTagInfo(com.redhat.red.build.koji.model.xmlrpc.KojiTagInfo) BasicCacheHandle(org.commonjava.indy.subsys.infinispan.BasicCacheHandle) Cache(org.infinispan.Cache) RemoteCache(org.infinispan.client.hotrod.RemoteCache) WeftExecutorService(org.commonjava.cdi.util.weft.WeftExecutorService) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) WeftManaged(org.commonjava.cdi.util.weft.WeftManaged) CacheProducer(org.commonjava.indy.subsys.infinispan.CacheProducer) Map(java.util.Map) KojiArchiveInfo(com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo) StoreKey(org.commonjava.indy.model.core.StoreKey) StoreType.group(org.commonjava.indy.model.core.StoreType.group) GroupMergeHelper(org.commonjava.indy.core.content.group.GroupMergeHelper) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Listener(org.infinispan.notifications.Listener) ClientListener(org.infinispan.client.hotrod.annotation.ClientListener) TimeZone(java.util.TimeZone) KojiClient(com.redhat.red.build.koji.KojiClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CacheEntryExpired(org.infinispan.notifications.cachelistener.annotation.CacheEntryExpired) Collectors(java.util.stream.Collectors) List(java.util.List) InvalidVersionSpecificationException(org.commonjava.atlas.maven.ident.version.InvalidVersionSpecificationException) KojiBuildInfo(com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) PostConstruct(javax.annotation.PostConstruct) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Pattern(java.util.regex.Pattern) KojiBuildArchiveCollection(com.redhat.red.build.koji.model.xmlrpc.KojiBuildArchiveCollection) DirectContentAccess(org.commonjava.indy.content.DirectContentAccess) IndyDataException(org.commonjava.indy.data.IndyDataException) PoolUtils.detectOverloadVoid(org.commonjava.indy.core.ctl.PoolUtils.detectOverloadVoid) IndyKojiConfig(org.commonjava.indy.koji.conf.IndyKojiConfig) Locker(org.commonjava.cdi.util.weft.Locker) MavenMetadataProvider(org.commonjava.indy.pkg.maven.content.group.MavenMetadataProvider) SimpleDateFormat(java.text.SimpleDateFormat) SimpleProjectRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectRef) Callable(java.util.concurrent.Callable) AtomicReference(java.util.concurrent.atomic.AtomicReference) VersionUtils(org.commonjava.atlas.maven.ident.util.VersionUtils) Group(org.commonjava.indy.model.core.Group) ConcurrentHashSet(org.infinispan.commons.util.concurrent.ConcurrentHashSet) ArrayList(java.util.ArrayList) InvalidRefException(org.commonjava.atlas.maven.ident.ref.InvalidRefException) AbstractStoreDataManager(org.commonjava.indy.db.common.AbstractStoreDataManager) ClientCacheEntryExpired(org.infinispan.client.hotrod.annotation.ClientCacheEntryExpired) Inject(javax.inject.Inject) Transfer(org.commonjava.maven.galley.model.Transfer) MetadataCacheManager(org.commonjava.indy.pkg.maven.content.MetadataCacheManager) CacheEntryExpiredEvent(org.infinispan.notifications.cachelistener.event.CacheEntryExpiredEvent) Calendar(java.util.Calendar) Measure(org.commonjava.o11yphant.metrics.annotation.Measure) ExecutorConfig(org.commonjava.cdi.util.weft.ExecutorConfig) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) KojiMavenVersionMetadataLocks(org.commonjava.indy.koji.inject.KojiMavenVersionMetadataLocks) DrainingExecutorCompletionService(org.commonjava.cdi.util.weft.DrainingExecutorCompletionService) KojiClientException(com.redhat.red.build.koji.KojiClientException) ArtifactPathUtils(org.commonjava.maven.galley.maven.util.ArtifactPathUtils) METADATA_NAME(org.commonjava.indy.pkg.maven.content.group.MavenMetadataMerger.METADATA_NAME) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) LocationUtils(org.commonjava.indy.util.LocationUtils) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ProjectRef(org.commonjava.atlas.maven.ident.ref.ProjectRef) IOException(java.io.IOException) NotFoundCache(org.commonjava.maven.galley.spi.nfc.NotFoundCache) CacheHandle(org.commonjava.indy.subsys.infinispan.CacheHandle) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) KojiBuildState(com.redhat.red.build.koji.model.xmlrpc.KojiBuildState) ClientCacheEntryExpiredEvent(org.infinispan.client.hotrod.event.ClientCacheEntryExpiredEvent) KojiMavenVersionMetadataCache(org.commonjava.indy.koji.inject.KojiMavenVersionMetadataCache) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) Collections(java.util.Collections) StoreDataManager(org.commonjava.indy.data.StoreDataManager) TransferException(org.commonjava.maven.galley.TransferException) IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) TransferException(org.commonjava.maven.galley.TransferException) AbstractStoreDataManager(org.commonjava.indy.db.common.AbstractStoreDataManager) Logger(org.slf4j.Logger)

Aggregations

KojiClientException (com.redhat.red.build.koji.KojiClientException)3 ProjectRef (org.commonjava.atlas.maven.ident.ref.ProjectRef)3 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)3 IndyDataException (org.commonjava.indy.data.IndyDataException)3 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)3 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 Measure (org.commonjava.o11yphant.metrics.annotation.Measure)3 Logger (org.slf4j.Logger)3 KojiClient (com.redhat.red.build.koji.KojiClient)2 KojiArchiveInfo (com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo)2 KojiBuildArchiveCollection (com.redhat.red.build.koji.model.xmlrpc.KojiBuildArchiveCollection)2 KojiBuildInfo (com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo)2 File (java.io.File)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Inject (javax.inject.Inject)2