Search in sources :

Example 31 with VisibleForTesting

use of com.google.common.annotations.VisibleForTesting in project keywhiz by square.

the class GenerateAesKeyCommand method generate.

@VisibleForTesting
static void generate(char[] password, Path destination, int keySize, String alias, SecureRandom random) throws Exception {
    KeyGenerator generator = KeyGenerator.getInstance("AES");
    generator.init(keySize, random);
    SecretKey key = generator.generateKey();
    KeyStore keyStore = KeyStore.getInstance("JCEKS");
    // KeyStores must be initialized before use.
    keyStore.load(null);
    keyStore.setKeyEntry(alias, key, password, null);
    try (OutputStream out = Files.newOutputStream(destination)) {
        keyStore.store(out, password);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) OutputStream(java.io.OutputStream) KeyGenerator(javax.crypto.KeyGenerator) KeyStore(java.security.KeyStore) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 32 with VisibleForTesting

use of com.google.common.annotations.VisibleForTesting in project keywhiz by square.

the class SecretDAO method createSecret.

@VisibleForTesting
public long createSecret(String name, String encryptedSecret, String hmac, String creator, Map<String, String> metadata, long expiry, String description, @Nullable String type, @Nullable Map<String, String> generationOptions) {
    return dslContext.transactionResult(configuration -> {
        SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
        SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
        Optional<SecretSeries> secretSeries = secretSeriesDAO.getSecretSeriesByName(name);
        long secretId;
        if (secretSeries.isPresent()) {
            SecretSeries secretSeries1 = secretSeries.get();
            if (secretSeries1.currentVersion().isPresent()) {
                throw new DataAccessException(format("secret already present: %s", name));
            }
            secretId = secretSeries1.id();
            secretSeriesDAO.updateSecretSeries(secretId, name, creator, description, type, generationOptions);
        } else {
            secretId = secretSeriesDAO.createSecretSeries(name, creator, description, type, generationOptions);
        }
        long secretContentId = secretContentDAO.createSecretContent(secretId, encryptedSecret, hmac, creator, metadata, expiry);
        secretSeriesDAO.setCurrentVersion(secretId, secretContentId);
        return secretId;
    });
}
Also used : SecretSeries(keywhiz.api.model.SecretSeries) DataAccessException(org.jooq.exception.DataAccessException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 33 with VisibleForTesting

use of com.google.common.annotations.VisibleForTesting in project keywhiz by square.

the class SecretDAO method partialUpdateSecret.

@VisibleForTesting
public long partialUpdateSecret(String name, String creator, PartialUpdateSecretRequestV2 request) {
    return dslContext.transactionResult(configuration -> {
        SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
        SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
        SecretSeries secretSeries = secretSeriesDAO.getSecretSeriesByName(name).orElseThrow(NotFoundException::new);
        Long currentVersion = secretSeries.currentVersion().orElseThrow(NotFoundException::new);
        SecretContent secretContent = secretContentDAO.getSecretContentById(currentVersion).orElseThrow(NotFoundException::new);
        long secretId = secretSeries.id();
        String description = request.descriptionPresent() ? request.description() : secretSeries.description();
        String type = request.typePresent() ? request.type() : secretSeries.type().orElse("");
        ImmutableMap<String, String> metadata = request.metadataPresent() ? request.metadata() : secretContent.metadata();
        Long expiry = request.expiryPresent() ? request.expiry() : secretContent.expiry();
        String encryptedContent = secretContent.encryptedContent();
        String hmac = secretContent.hmac();
        if (request.contentPresent()) {
            hmac = cryptographer.computeHmac(request.content().getBytes(UTF_8));
            if (hmac == null) {
                throw new ContentEncodingException("Error encoding content for SecretBuilder!");
            }
            encryptedContent = cryptographer.encryptionKeyDerivedFrom(name).encrypt(request.content());
        }
        secretSeriesDAO.updateSecretSeries(secretId, name, creator, description, type, secretSeries.generationOptions());
        long secretContentId = secretContentDAO.createSecretContent(secretId, encryptedContent, hmac, creator, metadata, expiry);
        secretSeriesDAO.setCurrentVersion(secretId, secretContentId);
        return secretId;
    });
}
Also used : ContentEncodingException(keywhiz.service.crypto.ContentEncodingException) SecretSeries(keywhiz.api.model.SecretSeries) SecretContent(keywhiz.api.model.SecretContent) NotFoundException(javax.ws.rs.NotFoundException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 34 with VisibleForTesting

use of com.google.common.annotations.VisibleForTesting in project bazel by bazelbuild.

the class SkyframeExecutor method maybeInjectEmbeddedArtifacts.

@VisibleForTesting
void maybeInjectEmbeddedArtifacts() throws AbruptExitException {
    if (!needToInjectEmbeddedArtifacts) {
        return;
    }
    Preconditions.checkNotNull(artifactFactory.get());
    Preconditions.checkNotNull(binTools);
    Map<SkyKey, SkyValue> values = Maps.newHashMap();
    // Blaze separately handles the symlinks that target these binaries. See BinTools#setupTool.
    for (Artifact artifact : binTools.getAllEmbeddedArtifacts(artifactFactory.get())) {
        FileArtifactValue fileArtifactValue;
        try {
            fileArtifactValue = FileArtifactValue.create(artifact);
        } catch (IOException e) {
            // See ExtractData in blaze.cc.
            String message = "Error: corrupt installation: file " + artifact.getPath() + " missing. " + "Please remove '" + directories.getInstallBase() + "' and try again.";
            throw new AbruptExitException(message, ExitCode.LOCAL_ENVIRONMENTAL_ERROR, e);
        }
        values.put(ArtifactSkyKey.key(artifact, /*isMandatory=*/
        true), fileArtifactValue);
    }
    injectable().inject(values);
    needToInjectEmbeddedArtifacts = false;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) SkyValue(com.google.devtools.build.skyframe.SkyValue) IOException(java.io.IOException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException) Artifact(com.google.devtools.build.lib.actions.Artifact) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 35 with VisibleForTesting

use of com.google.common.annotations.VisibleForTesting in project bazel by bazelbuild.

the class RepositoryDelegatorFunction method unescape.

// Unescape a value from the marker file
@VisibleForTesting
static String unescape(String str) {
    if (str.equals("\\0")) {
        // \0 == null string
        return null;
    }
    StringBuffer result = new StringBuffer();
    boolean escaped = false;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (escaped) {
            if (c == 'n') {
                // n means new line
                result.append("\n");
            } else if (c == 's') {
                // s means space
                result.append(" ");
            } else {
                // Any other escaped characters are just un-escaped
                result.append(c);
            }
            escaped = false;
        } else if (c == '\\') {
            escaped = true;
        } else {
            result.append(c);
        }
    }
    return result.toString();
}
Also used : Fingerprint(com.google.devtools.build.lib.util.Fingerprint) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)808 IOException (java.io.IOException)135 ArrayList (java.util.ArrayList)67 Map (java.util.Map)52 Path (java.nio.file.Path)47 File (java.io.File)41 HashMap (java.util.HashMap)37 Path (org.apache.hadoop.fs.Path)33 List (java.util.List)29 ImmutableList (com.google.common.collect.ImmutableList)28 Matcher (java.util.regex.Matcher)26 HashSet (java.util.HashSet)23 ImmutableMap (com.google.common.collect.ImmutableMap)21 FileStatus (org.apache.hadoop.fs.FileStatus)21 SourcePath (com.facebook.buck.rules.SourcePath)20 FileHandle (org.apache.hadoop.nfs.nfs3.FileHandle)19 DFSClient (org.apache.hadoop.hdfs.DFSClient)18 Nfs3FileAttributes (org.apache.hadoop.nfs.nfs3.Nfs3FileAttributes)18 ImmutableSet (com.google.common.collect.ImmutableSet)17 LinkedHashMap (java.util.LinkedHashMap)17