All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.seeq.utilities.process.LogBundler Maven / Gradle / Ivy

The newest version!
package com.seeq.utilities.process;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;

import org.apache.commons.io.FileUtils;

import com.seeq.utilities.configuration.Configuration;
import com.seeq.utilities.encryption.SimpleHybrid;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class LogBundler {

    // Utility class
    private LogBundler() {}

    private static final String LOG_EXPORT_KEY_FILE = "log-export-public-key.der";
    private static final String SEEQ_CONFIG_JSON = "seeq_config.json";
    private static final String GLOBAL_CONFIG_JSON = "global_config.json";

    private static Path getLogExportKey(Path dataFolder) throws IOException {
        Path logExportKey = dataFolder.resolve("keys").resolve(LOG_EXPORT_KEY_FILE);
        // If the log export key file does not exist, recreate it using the default packaged with appserver
        if (!Files.exists(logExportKey)) {
            LOG.debug("Getting default log export public key from resources");
            try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(LOG_EXPORT_KEY_FILE)) {
                if (inputStream == null) {
                    throw new IOException(String.format("Missing resource %s", LOG_EXPORT_KEY_FILE));
                }
                Files.createDirectories(logExportKey.getParent());
                Files.copy(inputStream, logExportKey);
            }
        }
        return logExportKey;
    }

    /**
     * Bundles the Seeq server logs into a single zip file for export.
     *
     * @param configuration
     *         The Configuration object to use to find the data folder and global folder.
     * @param encrypted
     *         When true, the zipped log bundle will be encrypted using {@link SimpleHybrid#encrypt}. The resulting
     *         encrypted data and key files will then be zipped and returned.
     * @return a zip file containing server logs
     * @throws IOException
     *         If there is an error reading from the data folder
     *         If encryption is enabled and there is an error reading the log export public key file
     * @throws InvalidKeyException
     *         If encryption is enabled and the log export public key file does not contain a valid public key
     * @throws InvalidKeySpecException
     *         If encryption is enabled and the log export public key file does not contain a valid public key
     */
    public static File getLogBundle(Configuration configuration, boolean encrypted)
            throws IOException, InvalidKeyException, InvalidKeySpecException {
        LOG.debug("Zipping logs for export");

        Path dataPath = Paths.get(configuration.get(Configuration.Folders.Data));
        Path logFolder = dataPath.resolve("log");

        Path temporaryConfigFolder = Files.createTempDirectory(null).resolve("configuration");
        Files.createDirectories(temporaryConfigFolder);
        Files.copy(dataPath.resolve("configuration").resolve(SEEQ_CONFIG_JSON),
                temporaryConfigFolder.resolve(SEEQ_CONFIG_JSON));

        Path globalFolderPath = Paths.get(configuration.get(Configuration.Folders.Global));
        Files.copy(globalFolderPath.resolve(GLOBAL_CONFIG_JSON), temporaryConfigFolder.resolve(GLOBAL_CONFIG_JSON));

        File zippedLogFolder = FileZipper.zipDirectory(logFolder, temporaryConfigFolder);

        if (!encrypted) {
            return zippedLogFolder;
        }

        try {
            LOG.debug("Encrypting logs for export");
            Path logExportKey = getLogExportKey(dataPath);
            Path bundleDirectory = Files.createTempDirectory(null).resolve("bundle");
            Files.createDirectories(bundleDirectory);

            SimpleHybrid.encrypt(logExportKey.toFile(), zippedLogFolder, bundleDirectory.toFile());
            File zippedEncryptedDir = FileZipper.zipDirectory(bundleDirectory);
            FileUtils.deleteDirectory(bundleDirectory.toFile());

            return zippedEncryptedDir;
        } catch (InvalidKeyException e) {
            String message = "The log export public key is not valid. Please contact a Seeq administrator.";
            throw new InvalidKeyException(message, e);
        } finally {
            Files.delete(zippedLogFolder.toPath());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy