<commit_before>/*
 * Copyright 2020 Google LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.cloud.tools.jib.plugins.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.cloud.tools.jib.ProjectInfo;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.filesystem.XdgDirectories;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
import com.google.cloud.tools.jib.http.Request;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.json.JsonTemplate;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.base.Verify;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.function.Consumer;

/** Checks if Jib is up-to-date. */
public class UpdateChecker {

  /** JSON template for the configuration file used to enable/disable update checks. */
  @VisibleForTesting
  static class ConfigJsonTemplate implements JsonTemplate {
    private boolean disableUpdateCheck;

    @VisibleForTesting
    void setDisableUpdateCheck(boolean disableUpdateCheck) {
      this.disableUpdateCheck = disableUpdateCheck;
    }
  }

  /** JSON template for content downloaded during version check. */
  @JsonIgnoreProperties(ignoreUnknown = true)
  private static class VersionJsonTemplate implements JsonTemplate {
    private String latest = "";
  }

  /**
   * Begins checking for an update in a separate thread.
   *
   * @param executorService the {@link ExecutorService}
   * @param log {@link Consumer} used to log messages
   * @param versionUrl the location to check for the latest version
   * @param toolName the tool name
   * @return a new {@link UpdateChecker}
   */
  public static Future<Optional<String>> checkForUpdate(
      ExecutorService executorService, Consumer<LogEvent> log, String versionUrl, String toolName) {
    return executorService.submit(
        () ->
            performUpdateCheck(
                log,
                Verify.verifyNotNull(ProjectInfo.VERSION),
                versionUrl,
                getConfigDir(),
                toolName));
  }

  @VisibleForTesting
  static Optional<String> performUpdateCheck(
      Consumer<LogEvent> log,
      String currentVersion,
      String versionUrl,
      Path configDir,
      String toolName) {
    // Abort if offline or update checks are disabled
    if (Boolean.getBoolean(PropertyNames.DISABLE_UPDATE_CHECKS)) {
      return Optional.empty();
    }

    Path configFile = configDir.resolve("config.json");
    Path lastUpdateCheck = configDir.resolve("lastUpdateCheck");

    try {
      // Check global config
      if (Files.exists(configFile) && Files.size(configFile) > 0) {
        // Abort if update checks are disabled
        try {
          ConfigJsonTemplate config =
              JsonTemplateMapper.readJsonFromFile(configFile, ConfigJsonTemplate.class);
          if (config.disableUpdateCheck) {
            return Optional.empty();
          }
        } catch (IOException ex) {
          log.accept(
              LogEvent.warn(
                  "Failed to read global Jib config; you may need to fix or delete "
                      + configFile
                      + ": "
                      + ex.getMessage()));
          return Optional.empty();
        }
      } else {
        // Generate config file if it doesn't exist
        ConfigJsonTemplate config = new ConfigJsonTemplate();
        Files.createDirectories(configDir);
        try (OutputStream outputStream = Files.newOutputStream(configFile)) {
          JsonTemplateMapper.writeTo(config, outputStream);
        } catch (IOException ex) {
          // If attempt to generate new config file failed, delete so we can try again next time
          log.accept(LogEvent.debug("Failed to generate global Jib config; " + ex.getMessage()));
          Files.deleteIfExists(configFile);
        }
      }

      // Check time of last update check
      if (Files.exists(lastUpdateCheck)) {
        try {
          String fileContents =
              new String(Files.readAllBytes(lastUpdateCheck), StandardCharsets.UTF_8);
          Instant modifiedTime = Instant.parse(fileContents);
          if (modifiedTime.plus(Duration.ofDays(1)).isAfter(Instant.now())) {
            return Optional.empty();
          }
        } catch (DateTimeParseException | IOException ex) {
          // If reading update time failed, file might be corrupt, so delete it
          log.accept(LogEvent.debug("Failed to read lastUpdateCheck; " + ex.getMessage()));
          Files.delete(lastUpdateCheck);
        }
      }

      // Check for update
      FailoverHttpClient httpClient = new FailoverHttpClient(true, false, ignored -> {});
      try {
        Response response =
            httpClient.get(
                new URL(versionUrl),
                Request.builder()
                    .setHttpTimeout(3000)
                    .setUserAgent("jib " + currentVersion + " " + toolName)
                    .build());
        VersionJsonTemplate version =
            JsonTemplateMapper.readJson(response.getBody(), VersionJsonTemplate.class);<commit_msg>For config generation use create and move of temp file (#2341)<commit_after>/*
 * Copyright 2020 Google LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.cloud.tools.jib.plugins.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.cloud.tools.jib.ProjectInfo;
import com.google.cloud.tools.jib.api.LogEvent;
import com.google.cloud.tools.jib.filesystem.XdgDirectories;
import com.google.cloud.tools.jib.http.FailoverHttpClient;
import com.google.cloud.tools.jib.http.Request;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.json.JsonTemplate;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.base.Verify;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.function.Consumer;

/** Checks if Jib is up-to-date. */
public class UpdateChecker {

  private static final String CONFIG_FILENAME = "config.json";

  /** JSON template for the configuration file used to enable/disable update checks. */
  @VisibleForTesting
  static class ConfigJsonTemplate implements JsonTemplate {
    private boolean disableUpdateCheck;

    @VisibleForTesting
    void setDisableUpdateCheck(boolean disableUpdateCheck) {
      this.disableUpdateCheck = disableUpdateCheck;
    }
  }

  /** JSON template for content downloaded during version check. */
  @JsonIgnoreProperties(ignoreUnknown = true)
  private static class VersionJsonTemplate implements JsonTemplate {
    private String latest = "";
  }

  /**
   * Begins checking for an update in a separate thread.
   *
   * @param executorService the {@link ExecutorService}
   * @param log {@link Consumer} used to log messages
   * @param versionUrl the location to check for the latest version
   * @param toolName the tool name
   * @return a new {@link UpdateChecker}
   */
  public static Future<Optional<String>> checkForUpdate(
      ExecutorService executorService, Consumer<LogEvent> log, String versionUrl, String toolName) {
    return executorService.submit(
        () ->
            performUpdateCheck(
                log,
                Verify.verifyNotNull(ProjectInfo.VERSION),
                versionUrl,
                getConfigDir(),
                toolName));
  }

  @VisibleForTesting
  static Optional<String> performUpdateCheck(
      Consumer<LogEvent> log,
      String currentVersion,
      String versionUrl,
      Path configDir,
      String toolName) {
    // Abort if offline or update checks are disabled
    if (Boolean.getBoolean(PropertyNames.DISABLE_UPDATE_CHECKS)) {
      return Optional.empty();
    }

    Path configFile = configDir.resolve(CONFIG_FILENAME);
    Path lastUpdateCheck = configDir.resolve("lastUpdateCheck");

    try {
      // Check global config
      if (Files.exists(configFile) && Files.size(configFile) > 0) {
        // Abort if update checks are disabled
        try {
          ConfigJsonTemplate config =
              JsonTemplateMapper.readJsonFromFile(configFile, ConfigJsonTemplate.class);
          if (config.disableUpdateCheck) {
            return Optional.empty();
          }
        } catch (IOException ex) {
          log.accept(
              LogEvent.warn(
                  "Failed to read global Jib config; you may need to fix or delete "
                      + configFile
                      + ": "
                      + ex.getMessage()));
          return Optional.empty();
        }
      } else {
        // Generate config file if it doesn't exist
        ConfigJsonTemplate config = new ConfigJsonTemplate();
        Files.createDirectories(configDir);
        Path tempConfigFile = configDir.resolve(CONFIG_FILENAME + ".tmp");
        try (OutputStream outputStream = Files.newOutputStream(tempConfigFile)) {
          JsonTemplateMapper.writeTo(config, outputStream);
          // Attempts an atomic move first, and falls back to non-atomic if the file system does not
          // support atomic moves.
          try {
            Files.move(
                tempConfigFile,
                configFile,
                StandardCopyOption.ATOMIC_MOVE,
                StandardCopyOption.REPLACE_EXISTING);
          } catch (AtomicMoveNotSupportedException ignored) {
            Files.move(tempConfigFile, configFile, StandardCopyOption.REPLACE_EXISTING);
          }
        } catch (IOException ex) {
          // If attempt to generate new config file failed, delete so we can try again next time
          log.accept(LogEvent.debug("Failed to generate global Jib config; " + ex.getMessage()));
          try {
            Files.deleteIfExists(tempConfigFile);
          } catch (IOException cleanupEx) {
            log.accept(
                LogEvent.debug(
                    "Failed to cleanup "
                        + tempConfigFile.toString()
                        + " -- "
                        + cleanupEx.getMessage()));
          }
        }
      }

      // Check time of last update check
      if (Files.exists(lastUpdateCheck)) {
        try {
          String fileContents =
              new String(Files.readAllBytes(lastUpdateCheck), StandardCharsets.UTF_8);
          Instant modifiedTime = Instant.parse(fileContents);
          if (modifiedTime.plus(Duration.ofDays(1)).isAfter(Instant.now())) {
            return Optional.empty();
          }
        } catch (DateTimeParseException | IOException ex) {
          // If reading update time failed, file might be corrupt, so delete it
          log.accept(LogEvent.debug("Failed to read lastUpdateCheck; " + ex.getMessage()));
          Files.delete(lastUpdateCheck);
        }
      }

      // Check for update
      FailoverHttpClient httpClient = new FailoverHttpClient(true, false, ignored -> {});
      try {
        Response response =
            httpClient.get(
                new URL(versionUrl),
                Request.builder()
                    .setHttpTimeout(3000)
                    .setUserAgent("jib " + currentVersion + " " + toolName)
                    .build());
        VersionJsonTemplate version =
            JsonTemplateMapper.readJson(response.getBody(), VersionJsonTemplate.class);
{'fraction_non_alphanumeric': 0.08916394900681886, 'fraction_numerical': 0.002520011858879336, 'mean_word_length': 2.6516914749661704, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3441588', 'n_tokens_mistral': 3496, 'n_tokens_neox': 3405, 'n_words': 955}