OssindexClientFactory.java

  1. /*
  2.  * This file is part of dependency-check-core.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  * Copyright (c) 2019 Jason Dillon. All Rights Reserved.
  17.  */
  18. package org.owasp.dependencycheck.data.ossindex;

  19. import java.io.File;
  20. import org.sonatype.goodies.packageurl.RenderFlavor;
  21. import org.sonatype.ossindex.service.client.OssindexClient;
  22. import org.sonatype.ossindex.service.client.OssindexClientConfiguration;
  23. import org.sonatype.ossindex.service.client.marshal.Marshaller;
  24. import org.sonatype.ossindex.service.client.marshal.GsonMarshaller;
  25. import org.sonatype.ossindex.service.client.internal.OssindexClientImpl;
  26. import org.sonatype.ossindex.service.client.transport.Transport;
  27. import org.sonatype.ossindex.service.client.transport.UserAgentSupplier;
  28. import org.owasp.dependencycheck.utils.Settings;

  29. import java.io.IOException;
  30. import org.joda.time.Duration;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import org.sonatype.ossindex.service.client.cache.DirectoryCache;
  34. import org.sonatype.ossindex.service.client.transport.AuthConfiguration;

  35. /**
  36.  * Produces {@link OssindexClient} instances.
  37.  *
  38.  * @author Jason Dillon
  39.  * @since 5.0.0
  40.  */
  41. public final class OssindexClientFactory {

  42.     /**
  43.      * Static logger.
  44.      */
  45.     private static final Logger LOGGER = LoggerFactory.getLogger(OssindexClientFactory.class);

  46.     static {
  47.         // prefer pkg scheme vs scheme-less variant
  48.         RenderFlavor.setDefault(RenderFlavor.SCHEME);
  49.     }

  50.     /**
  51.      * Private constructor for utility class.
  52.      */
  53.     private OssindexClientFactory() {
  54.         //private constructor for utility class
  55.     }

  56.     /**
  57.      * Constructs a new OSS Index Client.
  58.      *
  59.      * @param settings the configured settings
  60.      * @return a new OSS Index Client
  61.      */
  62.     public static OssindexClient create(final Settings settings) {
  63.         final OssindexClientConfiguration config = new OssindexClientConfiguration();

  64.         final String baseUrl = settings.getString(Settings.KEYS.ANALYZER_OSSINDEX_URL, null);
  65.         if (baseUrl != null) {
  66.             config.setBaseUrl(baseUrl);
  67.         }

  68.         final String username = settings.getString(Settings.KEYS.ANALYZER_OSSINDEX_USER);
  69.         final String password = settings.getString(Settings.KEYS.ANALYZER_OSSINDEX_PASSWORD);

  70.         if (username != null && password != null) {
  71.             final AuthConfiguration auth = new AuthConfiguration(username, password);
  72.             config.setAuthConfiguration(auth);
  73.         }

  74.         final int batchSize = settings.getInt(Settings.KEYS.ANALYZER_OSSINDEX_BATCH_SIZE, OssindexClientConfiguration.DEFAULT_BATCH_SIZE);
  75.         config.setBatchSize(batchSize);

  76.         if (settings.getBoolean(Settings.KEYS.ANALYZER_OSSINDEX_USE_CACHE, true)) {
  77.             final DirectoryCache.Configuration cache = new DirectoryCache.Configuration();
  78.             final File data;
  79.             try {
  80.                 data = settings.getDataDirectory();
  81.                 final File cacheDir = new File(data, "oss_cache");
  82.                 if (cacheDir.isDirectory() || cacheDir.mkdirs()) {
  83.                     cache.setBaseDir(cacheDir.toPath());
  84.                     cache.setExpireAfter(Duration.standardHours(24));
  85.                     config.setCacheConfiguration(cache);
  86.                     LOGGER.debug("OSS Index Cache: {}", cache);
  87.                 } else {
  88.                     LOGGER.warn("Unable to use a cache for the OSS Index");
  89.                 }
  90.             } catch (IOException ex) {
  91.                 LOGGER.warn("Unable to use a cache for the OSS Index", ex);
  92.             }
  93.         }
  94.         // customize User-Agent for use with dependency-check
  95.         final UserAgentSupplier userAgent = new UserAgentSupplier(
  96.                 "dependency-check",
  97.                 settings.getString(Settings.KEYS.APPLICATION_VERSION, "unknown")
  98.         );

  99.         final Transport transport = new ODCConnectionTransport(config, userAgent);

  100.         final Marshaller marshaller = new GsonMarshaller();

  101.         return new OssindexClientImpl(config, transport, marshaller);
  102.     }
  103. }