Search in sources :

Example 6 with KojiBuildInfo

use of com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo 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 7 with KojiBuildInfo

use of com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo in project indy by Commonjava.

the class KojiContentManagerDecorator method proxyKojiBuild.

private <T> T proxyKojiBuild(final StoreKey inStore, final ArtifactRef artifactRef, final String path, EventMetadata eventMetadata, T defValue, KojiBuildAction<T> consumer) throws IndyWorkflowException {
    Logger logger = LoggerFactory.getLogger(getClass());
    try {
        // return kojiClient.withKojiSession( ( session ) -> {
        KojiSessionInfo session = null;
        List<KojiBuildInfo> builds = kojiContentProvider.listBuildsContaining(artifactRef, session);
        builds.sort((build1, build2) -> build2.getCreationTime().compareTo(build1.getCreationTime()));
        logger.debug("Got {} builds from koji. Looking for best match.", builds.size());
        // use multicall
        Map<Integer, List<KojiTagInfo>> tagsMap = getTagsByBuildIds(builds, session);
        for (KojiBuildInfo build : builds) {
            if (build.getBuildState() != KojiBuildState.COMPLETE) {
                logger.debug("Build: {} is not completed. The state is {}. Skipping.", build.getNvr(), build.getBuildState());
                continue;
            }
            boolean buildAllowed = false;
            if (kojiUtils.isBinaryBuild(build)) {
                // This is not a real build, it's a binary import.
                if (config.isProxyBinaryBuilds()) {
                    logger.info("Trying binary build: {} with id: {}", build.getNvr(), build.getId());
                    buildAllowed = true;
                } else {
                    logger.debug("Skipping binary build: {} with id: {}", build.getNvr(), build.getId());
                }
            } else {
                logger.info("Trying build: {} with id: {}", build.getNvr(), build.getId());
                if (!config.isTagPatternsEnabled()) {
                    buildAllowed = true;
                } else {
                    List<KojiTagInfo> tags = tagsMap.get(build.getId());
                    logger.debug("Build {} is in {} tags.", build.getId(), tags.size());
                    if (logger.isTraceEnabled()) {
                        logTagsForBuild(build.getId(), tags);
                    }
                    for (KojiTagInfo tag : tags) {
                        // If the tags match patterns configured in whitelist, construct a new remote repo.
                        if (config.isTagAllowed(tag.getName())) {
                            logger.info("Koji tag is on whitelist: {}", tag.getName());
                            buildAllowed = true;
                            break;
                        } else {
                            logger.debug("Tag: {} is not in the whitelist.", tag.getName());
                        }
                    }
                }
            }
            if (buildAllowed) {
                // or the artifact matches the one in the authoritative store, go ahead.
                if (buildAuthority.isAuthorized(path, eventMetadata, artifactRef, build, session)) {
                    return consumer.execute(inStore, artifactRef, build, session);
                }
            } else {
                logger.debug("No whitelisted tags found for: {}", build.getNvr());
            }
        }
        logger.trace("No builds were found that matched the restrictions.");
        return defValue;
    // } );
    } catch (KojiClientException e) {
        throw new IndyWorkflowException("Cannot retrieve builds for: %s. Error: %s", e, artifactRef, e.getMessage());
    }
}
Also used : KojiClientException(com.redhat.red.build.koji.KojiClientException) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArrayList(java.util.ArrayList) List(java.util.List) Logger(org.slf4j.Logger) KojiSessionInfo(com.redhat.red.build.koji.model.xmlrpc.KojiSessionInfo) KojiTagInfo(com.redhat.red.build.koji.model.xmlrpc.KojiTagInfo) KojiBuildInfo(com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo)

Example 8 with KojiBuildInfo

use of com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo in project indy by Commonjava.

the class IndyKojiContentProvider method getBuildInfo.

public List<KojiBuildInfo> getBuildInfo(List<Object> args, KojiSessionInfo session) throws KojiClientException {
    logger.debug("getBuildInfo, args: {}", args);
    if (!kojiConfig.isQueryCacheEnabled()) {
        logger.trace("Cache not enabled, run direct kojiClient.multiCall");
        return kojiClient.multiCall(GET_BUILD, args, KojiBuildInfo.class, session);
    }
    CacheHandle<Object, KojiBuildInfo> cache = cacheProducer.getCache(KOJI_BUILD_INFO);
    Map<Object, KojiBuildInfo> m = new HashMap<>();
    List<Object> missed = new ArrayList<>();
    for (Object obj : args) {
        KojiBuildInfo kojiBuildInfo = cache.get(obj);
        if (kojiBuildInfo != null) {
            m.put(obj, kojiBuildInfo);
        } else {
            missed.add(obj);
        }
    }
    if (!missed.isEmpty()) {
        List<KojiBuildInfo> retrieved = kojiClient.multiCall(GET_BUILD, missed, KojiBuildInfo.class, session);
        for (int i = 0; i < missed.size(); i++) {
            Object obj = missed.get(i);
            KojiBuildInfo kojiBuildInfo = retrieved.get(i);
            m.put(obj, kojiBuildInfo);
            cache.put(obj, kojiBuildInfo);
        }
    }
    List<KojiBuildInfo> ret = new ArrayList<>();
    // keep the order of results equal to args
    args.forEach((a) -> ret.add(m.get(a)));
    return ret;
}
Also used : KojiBuildInfo(com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo)

Aggregations

KojiBuildInfo (com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo)8 KojiSessionInfo (com.redhat.red.build.koji.model.xmlrpc.KojiSessionInfo)5 KojiClientException (com.redhat.red.build.koji.KojiClientException)4 StoreKey (org.commonjava.indy.model.core.StoreKey)4 KojiRepairResult (org.commonjava.indy.koji.model.KojiRepairResult)3 Logger (org.slf4j.Logger)3 KojiArchiveInfo (com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)2 IndyDataException (org.commonjava.indy.data.IndyDataException)2 KojiMultiRepairResult (org.commonjava.indy.koji.model.KojiMultiRepairResult)2 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)2 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)2 KojiClient (com.redhat.red.build.koji.KojiClient)1 KojiBuildArchiveCollection (com.redhat.red.build.koji.model.xmlrpc.KojiBuildArchiveCollection)1 KojiTagInfo (com.redhat.red.build.koji.model.xmlrpc.KojiTagInfo)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TRUE (java.lang.Boolean.TRUE)1