View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.owasp.dependencycheck.utils.scarf;
20  
21  import org.owasp.dependencycheck.utils.Downloader;
22  import org.owasp.dependencycheck.utils.Settings;
23  
24  import java.net.URL;
25  import java.net.URLEncoder;
26  import java.nio.charset.StandardCharsets;
27  import java.util.concurrent.atomic.AtomicBoolean;
28  
29  
30  /**
31   * A utility class to collect and send telemetry data to scarf.
32   * <p>
33   * Originally from https://github.com/apache/sedona/blob/4e4791d08ddafcf0b46c3d2c092f750eb5dcf2ef/common/src/main/java/org/apache/sedona/common/utils/TelemetryCollector.java#L26
34   */
35  public class TelemetryCollector {
36  
37      private static final String BASE_URL = "https://dependency-check.gateway.scarf.sh/scan/";
38      private static final AtomicBoolean telemetrySubmitted = new AtomicBoolean(false);
39  
40      public static void send(Settings settings) {
41          try {
42              String tool = settings.getString(Settings.KEYS.APPLICATION_NAME, "dependency-check");
43              String version = settings.getString(Settings.KEYS.APPLICATION_VERSION, "Unknown");
44              send(settings, tool, version);
45          } catch (Exception e) {
46              // Silent catch block
47          }
48      }
49      public static void send(Settings settings, String tool, String version) {
50          if (!telemetrySubmitted.compareAndSet(false, true)) {
51              return;
52          }
53          // Check for user opt-out
54          if (System.getenv("SCARF_NO_ANALYTICS") != null
55                  && System.getenv("SCARF_NO_ANALYTICS").equalsIgnoreCase("true")
56                  || System.getenv("DO_NOT_TRACK") != null
57                  && System.getenv("DO_NOT_TRACK").equalsIgnoreCase("true")
58                  || System.getProperty("SCARF_NO_ANALYTICS") != null
59                  && System.getProperty("SCARF_NO_ANALYTICS").equalsIgnoreCase("true")
60                  || System.getProperty("DO_NOT_TRACK") != null
61                  && System.getProperty("DO_NOT_TRACK").equalsIgnoreCase("true")) {
62              return;
63          }
64          try {
65              URL telemetryUrl = new URL(BASE_URL
66                      + URLEncoder.encode(tool, StandardCharsets.UTF_8)
67                      + "/"
68                      + URLEncoder.encode(version, StandardCharsets.UTF_8));
69              Thread telemetryThread = createThread(settings, telemetryUrl);
70              telemetryThread.start();
71          } catch (Exception e) {
72              // Silent catch block
73          }
74      }
75  
76      private static Thread createThread(Settings settings, URL url) {
77          Thread telemetryThread =
78                  new Thread("telemetry-thread") {
79                      @Override
80                      public void run() {
81                          try {
82                              Downloader downloader = Downloader.getInstance();
83                              downloader.configure(settings);
84                              downloader.fetchContent(url, StandardCharsets.UTF_8);
85                          } catch (Exception e) {
86                              // Silent catch block
87                          }
88                      }
89                  };
90          telemetryThread.setDaemon(true);
91          return telemetryThread;
92      }
93  }