Search in sources :

Example 56 with Nullable

use of javax.annotation.Nullable in project keywhiz by square.

the class ExpirationExtractor method expirationFromEncodedCertificateChain.

@Nullable
public static Instant expirationFromEncodedCertificateChain(byte[] content) {
    PemReader reader = new PemReader(new InputStreamReader(new ByteArrayInputStream(content), UTF_8));
    PemObject object;
    try {
        object = reader.readPemObject();
    } catch (IOException e) {
        // Should never occur (reading form byte array)
        throw Throwables.propagate(e);
    }
    Instant earliest = null;
    while (object != null) {
        if (object.getType().equals("CERTIFICATE")) {
            Instant expiry = expirationFromRawCertificate(object.getContent());
            if (earliest == null || expiry.isBefore(earliest)) {
                earliest = expiry;
            }
        }
        try {
            object = reader.readPemObject();
        } catch (IOException e) {
            // Should never occur (reading form byte array)
            throw Throwables.propagate(e);
        }
    }
    return earliest;
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Instant(java.time.Instant) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 57 with Nullable

use of javax.annotation.Nullable in project keywhiz by square.

the class ExpirationExtractor method expirationFromKeystore.

@Nullable
public static Instant expirationFromKeystore(String type, String password, byte[] content) {
    KeyStore ks;
    try {
        ks = KeyStore.getInstance(type);
    } catch (KeyStoreException e) {
        // Should never occur (assuming JCE is installed)
        throw Throwables.propagate(e);
    }
    try {
        ks.load(new ByteArrayInputStream(content), password.toCharArray());
    } catch (IOException | NoSuchAlgorithmException | CertificateException e) {
        // Failed to parse
        logger.info("Failed to parse keystore", e);
        return null;
    }
    Instant earliest = null;
    try {
        for (String alias : list(ks.aliases())) {
            Certificate[] chain = ks.getCertificateChain(alias);
            if (chain == null) {
                Certificate certificate = ks.getCertificate(alias);
                if (certificate == null) {
                    // No certs in this entry
                    continue;
                }
                chain = new Certificate[] { certificate };
            }
            for (Certificate cert : chain) {
                if (cert instanceof X509Certificate) {
                    X509Certificate c = (X509Certificate) cert;
                    if (earliest == null || c.getNotAfter().toInstant().isBefore(earliest)) {
                        earliest = c.getNotAfter().toInstant();
                    }
                }
            }
        }
    } catch (KeyStoreException e) {
        // Should never occur (ks was initialized)
        throw Throwables.propagate(e);
    }
    return earliest;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Instant(java.time.Instant) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Nullable(javax.annotation.Nullable)

Example 58 with Nullable

use of javax.annotation.Nullable in project bazel by bazelbuild.

the class NewHttpArchiveFunction method fetch.

@Nullable
@Override
public RepositoryDirectoryValue.Builder fetch(Rule rule, Path outputDirectory, BlazeDirectories directories, Environment env, Map<String, String> markerData) throws RepositoryFunctionException, InterruptedException {
    NewRepositoryBuildFileHandler buildFileHandler = new NewRepositoryBuildFileHandler(directories.getWorkspace());
    if (!buildFileHandler.prepareBuildFile(rule, env)) {
        return null;
    }
    try {
        FileSystemUtils.createDirectoryAndParents(outputDirectory);
    } catch (IOException e) {
        throw new RepositoryFunctionException(new IOException("Could not create directory for " + rule.getName() + ": " + e.getMessage()), Transience.TRANSIENT);
    }
    // Download.
    Path downloadedPath = downloader.download(rule, outputDirectory, env.getListener(), clientEnvironment);
    // Decompress.
    Path decompressed;
    WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
    String prefix = null;
    if (mapper.isAttributeValueExplicitlySpecified("strip_prefix")) {
        try {
            prefix = mapper.get("strip_prefix", Type.STRING);
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    }
    decompressed = DecompressorValue.decompress(DecompressorDescriptor.builder().setTargetKind(rule.getTargetKind()).setTargetName(rule.getName()).setArchivePath(downloadedPath).setRepositoryPath(outputDirectory).setPrefix(prefix).build());
    // Finally, write WORKSPACE and BUILD files.
    createWorkspaceFile(decompressed, rule.getTargetKind(), rule.getName());
    buildFileHandler.finishBuildFile(outputDirectory);
    return RepositoryDirectoryValue.builder().setPath(outputDirectory);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) NewRepositoryBuildFileHandler(com.google.devtools.build.lib.rules.repository.NewRepositoryBuildFileHandler) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) Nullable(javax.annotation.Nullable)

Example 59 with Nullable

use of javax.annotation.Nullable in project bazel by bazelbuild.

the class RedirectChaser method followRedirects.

/**
   * Follows the 'srcs' attribute of the given label recursively. Keeps repeating as long as the
   * labels are either <code>alias</code> or <code>bind</code> rules.
   *
   * @param env for loading the packages
   * @param label the label to start at
   * @param name user-meaningful description of the content being resolved
   * @return the label which cannot be further resolved
   * @throws InvalidConfigurationException if something goes wrong
   */
@Nullable
public static Label followRedirects(ConfigurationEnvironment env, Label label, String name) throws InvalidConfigurationException, InterruptedException {
    Label oldLabel = null;
    Set<Label> visitedLabels = new HashSet<>();
    visitedLabels.add(label);
    try {
        while (true) {
            Target possibleRedirect = env.getTarget(label);
            if (possibleRedirect == null) {
                return null;
            }
            Label newLabel = getBindOrAliasRedirect(possibleRedirect);
            if (newLabel == null) {
                return label;
            }
            newLabel = label.resolveRepositoryRelative(newLabel);
            oldLabel = label;
            label = newLabel;
            if (!visitedLabels.add(label)) {
                throw new InvalidConfigurationException("The " + name + " points to a rule which " + "recursively references itself. The label " + label + " is part of the loop");
            }
        }
    } catch (NoSuchThingException e) {
        String prefix = oldLabel == null ? "" : "in target '" + oldLabel + "': ";
        throw new InvalidConfigurationException(prefix + e.getMessage(), e);
    }
}
Also used : Target(com.google.devtools.build.lib.packages.Target) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) Label(com.google.devtools.build.lib.cmdline.Label) HashSet(java.util.HashSet) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) Nullable(javax.annotation.Nullable)

Example 60 with Nullable

use of javax.annotation.Nullable in project bazel by bazelbuild.

the class ConfigurationFactory method getConfiguration.

/**
   * Returns a {@link com.google.devtools.build.lib.analysis.config.BuildConfiguration} based on the
   * given set of build options.
   *
   * <p>If the configuration has already been created, re-uses it, otherwise, creates a new one.
   */
@Nullable
public BuildConfiguration getConfiguration(PackageProviderForConfigurations loadedPackageProvider, BuildOptions buildOptions, boolean actionsDisabled, Cache<String, BuildConfiguration> cache) throws InvalidConfigurationException, InterruptedException {
    String cacheKey = buildOptions.computeCacheKey();
    BuildConfiguration result = cache.getIfPresent(cacheKey);
    if (result != null) {
        return result;
    }
    Map<Class<? extends Fragment>, Fragment> fragments = new HashMap<>();
    // Create configuration fragments
    for (ConfigurationFragmentFactory factory : configurationFragmentFactories) {
        Class<? extends Fragment> fragmentType = factory.creates();
        Fragment fragment = loadedPackageProvider.getFragment(buildOptions, fragmentType);
        if (fragment != null && fragments.get(fragment.getClass()) == null) {
            fragments.put(fragment.getClass(), fragment);
        }
    }
    BlazeDirectories directories = loadedPackageProvider.getDirectories();
    if (loadedPackageProvider.valuesMissing()) {
        return null;
    }
    result = new BuildConfiguration(directories, fragments, buildOptions, actionsDisabled);
    cache.put(cacheKey, result);
    return result;
}
Also used : BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) HashMap(java.util.HashMap) Fragment(com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment) Nullable(javax.annotation.Nullable)

Aggregations

Nullable (javax.annotation.Nullable)859 IOException (java.io.IOException)94 Map (java.util.Map)77 List (java.util.List)67 Test (org.junit.Test)62 Function (com.google.common.base.Function)60 File (java.io.File)53 ArrayList (java.util.ArrayList)52 HashMap (java.util.HashMap)47 ImmutableList (com.google.common.collect.ImmutableList)37 ItemStack (net.minecraft.item.ItemStack)35 ImmutableMap (com.google.common.collect.ImmutableMap)33 Nonnull (javax.annotation.Nonnull)31 Predicate (com.google.common.base.Predicate)29 SkyKey (com.google.devtools.build.skyframe.SkyKey)28 URL (java.net.URL)24 Collectors (java.util.stream.Collectors)24 HashSet (java.util.HashSet)22 Label (com.google.devtools.build.lib.cmdline.Label)21 Set (java.util.Set)20