Search in sources :

Example 1 with Resources

use of com.google.common.io.Resources in project curiostack by curioswitch.

the class GrpcApiPlugin method apply.

@Override
public void apply(Project project) {
    project.getPluginManager().apply(JavaLibraryPlugin.class);
    project.getExtensions().create(ImmutableGrpcExtension.NAME, GrpcExtension.class);
    GRPC_DEPENDENCIES.forEach(dep -> project.getDependencies().add("api", "io.grpc:" + dep));
    project.afterEvaluate(p -> {
        ImmutableGrpcExtension config = project.getExtensions().getByType(GrpcExtension.class);
        Map<String, String> managedVersions = project.getExtensions().getByType(DependencyManagementExtension.class).getManagedVersions();
        ProtobufConfigurator protobuf = project.getConvention().getPlugin(ProtobufConvention.class).getProtobuf();
        protobuf.protoc(LambdaClosure.of((ExecutableLocator locator) -> locator.setArtifact("com.google.protobuf:protoc:" + managedVersions.get("com.google.protobuf:protoc"))));
        protobuf.plugins(LambdaClosure.of((NamedDomainObjectContainer<ExecutableLocator> locators) -> {
            locators.create("grpc").setArtifact("io.grpc:protoc-gen-grpc-java:" + managedVersions.get("io.grpc:grpc-core"));
            if (config.web()) {
                locators.create("ts").setPath(project.file("node_modules/.bin/protoc-gen-ts-resolved" + (IS_WINDOWS ? ".cmd" : "")).getAbsolutePath());
            }
        }));
        String archivesBaseName = project.getConvention().getPlugin(BasePluginConvention.class).getArchivesBaseName();
        String descriptorSetOutputPath = project.getBuildDir() + "/resources/main/META-INF/armeria/grpc/" + project.getGroup() + "." + archivesBaseName + ".dsc";
        protobuf.generateProtoTasks(LambdaClosure.of((JavaGenerateProtoTaskCollection tasks) -> {
            tasks.all().forEach(task -> {
                task.getBuiltins().getByName("java").setOutputSubDir("");
                task.getPlugins().create("grpc").setOutputSubDir("");
                if (config.web()) {
                    // We generate web protos into build/web to make it easier to
                    // reference from other projects.
                    task.getBuiltins().create("js").option("import_style=commonjs,binary").setOutputSubDir("../../../../web");
                    task.getPlugins().create("ts").option("service=true").setOutputSubDir("../../../../web");
                }
            });
            tasks.ofSourceSet("main").forEach(task -> {
                task.getOutputs().file(descriptorSetOutputPath);
                task.setGenerateDescriptorSet(true);
                DescriptorSetOptions options = task.getDescriptorSetOptions();
                options.setIncludeSourceInfo(true);
                options.setIncludeImports(true);
                options.setPath(new GStringImpl(new Object[] {}, new String[] { descriptorSetOutputPath }));
            });
        }));
    });
    // Add the protobuf plugin last to make sure our afterEvaluate runs before it.
    project.getPluginManager().apply(ProtobufPlugin.class);
    // Additional configuration of tasks created by protobuf plugin.
    project.afterEvaluate(p -> {
        ImmutableGrpcExtension config = project.getExtensions().getByType(GrpcExtension.class);
        if (config.web()) {
            String currentProjectPath = project.getPath().replace(':', '_');
            NpmTask installTsProtocGen = project.getRootProject().getTasks().create("installTsProtocGen_" + currentProjectPath, NpmTask.class);
            installTsProtocGen.setWorkingDir(project.getProjectDir());
            installTsProtocGen.setArgs(ImmutableList.of("install", "--no-save", "ts-protoc-gen@" + TS_PROTOC_GEN_VERSION));
            installTsProtocGen.getInputs().property("ts-protoc-gen-version", TS_PROTOC_GEN_VERSION);
            installTsProtocGen.getOutputs().dir("node_modules/ts-protoc-gen");
            // gradle-protobuf-plugin does not allow manipulating PATH for protoc invocation, so
            // there's no way
            // to point it at our downloaded nodejs. We go ahead and create our own plugin
            // executable with the
            // nodejs path resolved.
            Task addResolvedPluginScript = project.getTasks().create("addResolvedPluginScript").dependsOn(installTsProtocGen).doFirst(t -> {
                String nodePath = project.getRootProject().getExtensions().getByType(NodeExtension.class).getVariant().getNodeExec();
                writeResolvedScript(project, nodePath, "protoc-gen-ts-resolved", "ts-protoc-gen/lib/ts_index");
            });
            addResolvedPluginScript.getOutputs().files(ImmutableMap.of("protoc-gen-ts-resolved", "node_modules/.bin/protoc-gen-ts-resolved", "protoc-gen-ts-resolved-cmd", "node_modules/.bin/protoc-gen-ts-resolved.cmd"));
            String packageName = config.webPackageName().isEmpty() ? project.getConvention().getPlugin(BasePluginConvention.class).getArchivesBaseName() : config.webPackageName();
            Path packageJsonPath = Paths.get(project.getBuildDir().getAbsolutePath(), "web", "package.json");
            Path indexJsPath = Paths.get(project.getBuildDir().getAbsolutePath(), "web", "index.ts");
            Path tsConfigPath = Paths.get(project.getBuildDir().getAbsolutePath(), "web", "tsconfig.json");
            Task addPackageJson = project.getTasks().create("packageJson").dependsOn("generateProto").doFirst(t -> {
                try {
                    Files.write(packageJsonPath, PACKAGE_JSON_TEMPLATE.replaceFirst("\\|PACKAGE_NAME\\|", packageName).replaceFirst("\\|TYPES_GOOGLE_PROTOBUF_VERSION\\|", TYPES_GOOGLE_PROTOBUF_VERSION).replaceFirst("\\|GOOGLE_PROTOBUF_VERSION\\|", GOOGLE_PROTOBUF_VERSION).replaceFirst("\\|GRPC_WEB_CLIENT_VERSION\\|", GRPC_WEB_CLIENT_VERSION).replaceFirst("\\|CURIOSTACK_BASE_NODE_DEV_VERSION\\|", CURIOSTACK_BASE_NODE_DEV_VERSION).getBytes(StandardCharsets.UTF_8));
                    Files.write(tsConfigPath, TSCONFIG_TEMPLATE.getBytes(StandardCharsets.UTF_8));
                    Files.write(indexJsPath, new byte[0]);
                } catch (IOException e) {
                    throw new UncheckedIOException("Could not write package.json.", e);
                }
            });
            addPackageJson.getOutputs().files(ImmutableMap.of("PACKAGE_JSON", packageJsonPath.toFile(), "INDEX_TS", indexJsPath.toFile(), "TS_CONFIG", tsConfigPath.toFile()));
            Task generateProto = project.getTasks().getByName("generateProto");
            generateProto.dependsOn(addResolvedPluginScript).finalizedBy(addPackageJson);
            project.getRootProject().getTasks().findByName("yarn").dependsOn(generateProto);
            // Unclear why sometimes compileTestJava fails with "no source files" instead of being
            // skipped (usually when activating web), but it's not that hard to at least check the
            // source set directory.
            SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
            if (sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME).getAllJava().isEmpty()) {
                project.getTasks().getByName("compileTestJava").setEnabled(false);
            }
        }
    });
}
Also used : BasePluginConvention(org.gradle.api.plugins.BasePluginConvention) Arrays(java.util.Arrays) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) ProtobufConvention(com.google.protobuf.gradle.ProtobufConvention) SourceSet(org.gradle.api.tasks.SourceSet) Task(org.gradle.api.Task) ImmutableList(com.google.common.collect.ImmutableList) DependencyManagementExtension(io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) Map(java.util.Map) Path(java.nio.file.Path) JavaGenerateProtoTaskCollection(com.google.protobuf.gradle.ProtobufConfigurator.JavaGenerateProtoTaskCollection) NodeExtension(com.moowork.gradle.node.NodeExtension) GStringImpl(org.codehaus.groovy.runtime.GStringImpl) ImmutableMap(com.google.common.collect.ImmutableMap) Resources(com.google.common.io.Resources) NpmTask(com.moowork.gradle.node.npm.NpmTask) Files(java.nio.file.Files) Project(org.gradle.api.Project) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) ProtobufPlugin(com.google.protobuf.gradle.ProtobufPlugin) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) JavaLibraryPlugin(org.gradle.api.plugins.JavaLibraryPlugin) List(java.util.List) DescriptorSetOptions(com.google.protobuf.gradle.GenerateProtoTask.DescriptorSetOptions) Os(org.apache.tools.ant.taskdefs.condition.Os) Paths(java.nio.file.Paths) BasePluginConvention(org.gradle.api.plugins.BasePluginConvention) ExecutableLocator(com.google.protobuf.gradle.ExecutableLocator) ProtobufConfigurator(com.google.protobuf.gradle.ProtobufConfigurator) Collections(java.util.Collections) LambdaClosure(org.curioswitch.gradle.common.LambdaClosure) Plugin(org.gradle.api.Plugin) Path(java.nio.file.Path) Task(org.gradle.api.Task) NpmTask(com.moowork.gradle.node.npm.NpmTask) DependencyManagementExtension(io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) ProtobufConfigurator(com.google.protobuf.gradle.ProtobufConfigurator) GStringImpl(org.codehaus.groovy.runtime.GStringImpl) ProtobufConvention(com.google.protobuf.gradle.ProtobufConvention) ExecutableLocator(com.google.protobuf.gradle.ExecutableLocator) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) DescriptorSetOptions(com.google.protobuf.gradle.GenerateProtoTask.DescriptorSetOptions) JavaGenerateProtoTaskCollection(com.google.protobuf.gradle.ProtobufConfigurator.JavaGenerateProtoTaskCollection) NpmTask(com.moowork.gradle.node.npm.NpmTask)

Example 2 with Resources

use of com.google.common.io.Resources in project rpki-validator-3 by RIPE-NCC.

the class TrustAnchorsFactory method createCertificateAuthority.

public X509ResourceCertificate createCertificateAuthority(CertificateAuthority ca, CertificateAuthority issuer, ValidityPeriod mftValidityPeriod) {
    ManifestCmsBuilder manifestBuilder = new ManifestCmsBuilder();
    X509ResourceCertificate caCertificate = createCaCertificate(ca, ca.keyPair.getPublic(), issuer.dn, issuer.crlDistributionPoint, issuer.keyPair);
    X509Crl crl = new X509CrlBuilder().withIssuerDN(caCertificate.getSubject()).withThisUpdateTime(DateTime.now()).withNextUpdateTime(DateTime.now().plusHours(8)).withAuthorityKeyIdentifier(ca.keyPair.getPublic()).withNumber(nextSerial()).build(ca.keyPair.getPrivate());
    rpkiObjects.add(new RpkiObject(ca.crlDistributionPoint, crl));
    manifestBuilder.addFile(ca.crlDistributionPoint.substring(ca.crlDistributionPoint.lastIndexOf('/') + 1), crl.getEncoded());
    if (ca.children != null) {
        for (CertificateAuthority child : ca.children) {
            X509ResourceCertificate childCertificate = createCertificateAuthority(child, ca);
            rpkiObjects.add(new RpkiObject(ca.repositoryURI + "/" + child.dn + ".cer", childCertificate));
            manifestBuilder.addFile(child.dn + ".cer", childCertificate.getEncoded());
        }
    }
    if (ca.roaPrefixes != null) {
        ca.roaPrefixes.stream().collect(groupingBy(RoaPrefix::getAsn)).forEach((asn, roaPrefix) -> {
            KeyPair roaKeyPair = KEY_PAIR_FACTORY.generate();
            IpResourceSet resources = new IpResourceSet();
            roaPrefix.stream().forEach(p -> resources.add(IpRange.parse(p.getPrefix())));
            X509ResourceCertificate roaCertificate = new X509ResourceCertificateBuilder().withResources(resources).withIssuerDN(new X500Principal(ca.dn)).withSubjectDN(new X500Principal("CN=AS" + asn + ", CN=roa, " + ca.dn)).withValidityPeriod(typicalValidityPeriod()).withPublicKey(roaKeyPair.getPublic()).withSigningKeyPair(ca.keyPair).withCa(false).withKeyUsage(KeyUsage.digitalSignature).withSerial(nextSerial()).withCrlDistributionPoints(URI.create(ca.crlDistributionPoint)).build();
            RoaCms roaCms = new RoaCmsBuilder().withAsn(new Asn(asn)).withPrefixes(roaPrefix.stream().map(p -> new net.ripe.rpki.commons.crypto.cms.roa.RoaPrefix(IpRange.parse(p.getPrefix()), p.getMaximumLength())).collect(toList())).withCertificate(roaCertificate).withSignatureProvider(BouncyCastleProvider.PROVIDER_NAME).build(roaKeyPair.getPrivate());
            rpkiObjects.add(new RpkiObject(ca.repositoryURI + "/" + "AS" + asn + ".roa", roaCms));
            manifestBuilder.addFile("AS" + asn + ".roa", roaCms.getEncoded());
        });
    }
    KeyPair manifestKeyPair = KEY_PAIR_FACTORY.generate();
    X509ResourceCertificate manifestCertificate = new X509ResourceCertificateBuilder().withInheritedResourceTypes(EnumSet.allOf(IpResourceType.class)).withIssuerDN(caCertificate.getSubject()).withSubjectDN(new X500Principal("CN=manifest, " + caCertificate.getSubject())).withValidityPeriod(mftValidityPeriod).withPublicKey(manifestKeyPair.getPublic()).withSigningKeyPair(ca.keyPair).withCa(false).withKeyUsage(KeyUsage.digitalSignature).withSerial(nextSerial()).withCrlDistributionPoints(URI.create(ca.crlDistributionPoint)).build();
    manifestBuilder.withCertificate(manifestCertificate).withManifestNumber(nextSerial()).withThisUpdateTime(DateTime.now()).withNextUpdateTime(DateTime.now().plusHours(8));
    ManifestCms manifest = manifestBuilder.build(manifestKeyPair.getPrivate());
    rpkiObjects.add(new RpkiObject(ca.manifestURI, manifest));
    return caCertificate;
}
Also used : KeyPair(java.security.KeyPair) X500Principal(javax.security.auth.x500.X500Principal) Duration(org.joda.time.Duration) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Autowired(org.springframework.beans.factory.annotation.Autowired) Security(java.security.Security) ValidityPeriod(net.ripe.rpki.commons.crypto.ValidityPeriod) Value(lombok.Value) CertificateRepositoryObjectFactory(net.ripe.rpki.commons.crypto.util.CertificateRepositoryObjectFactory) ArrayList(java.util.ArrayList) Asn(net.ripe.ipresource.Asn) IpResourceType(net.ripe.ipresource.IpResourceType) RoaCms(net.ripe.rpki.commons.crypto.cms.roa.RoaCms) CertificateTreeValidationServiceTest(net.ripe.rpki.validator3.domain.validation.CertificateTreeValidationServiceTest) X509CrlBuilder(net.ripe.rpki.commons.crypto.crl.X509CrlBuilder) X509ResourceCertificate(net.ripe.rpki.commons.crypto.x509cert.X509ResourceCertificate) RoaCmsBuilder(net.ripe.rpki.commons.crypto.cms.roa.RoaCmsBuilder) X509CertificateInformationAccessDescriptor(net.ripe.rpki.commons.crypto.x509cert.X509CertificateInformationAccessDescriptor) X509ResourceCertificateBuilder(net.ripe.rpki.commons.crypto.x509cert.X509ResourceCertificateBuilder) BigInteger(java.math.BigInteger) URI(java.net.URI) IpResourceSet(net.ripe.ipresource.IpResourceSet) KeyPairFactory(net.ripe.rpki.commons.crypto.util.KeyPairFactory) EnumSet(java.util.EnumSet) Resources(com.google.common.io.Resources) Transactional(javax.transaction.Transactional) IpRange(net.ripe.ipresource.IpRange) DateTime(org.joda.time.DateTime) TrustAnchorValidationServiceTest(net.ripe.rpki.validator3.domain.validation.TrustAnchorValidationServiceTest) IOException(java.io.IOException) PublicKey(java.security.PublicKey) X509Crl(net.ripe.rpki.commons.crypto.crl.X509Crl) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Builder(lombok.Builder) ManifestCmsBuilder(net.ripe.rpki.commons.crypto.cms.manifest.ManifestCmsBuilder) Instant(org.joda.time.Instant) PostConstruct(javax.annotation.PostConstruct) ValidationResult(net.ripe.rpki.commons.validation.ValidationResult) ManifestCms(net.ripe.rpki.commons.crypto.cms.manifest.ManifestCms) X509CertificateUtil(net.ripe.rpki.commons.crypto.x509cert.X509CertificateUtil) Collections(java.util.Collections) KeyUsage(org.bouncycastle.asn1.x509.KeyUsage) X509CrlBuilder(net.ripe.rpki.commons.crypto.crl.X509CrlBuilder) KeyPair(java.security.KeyPair) X509Crl(net.ripe.rpki.commons.crypto.crl.X509Crl) ManifestCmsBuilder(net.ripe.rpki.commons.crypto.cms.manifest.ManifestCmsBuilder) RoaCmsBuilder(net.ripe.rpki.commons.crypto.cms.roa.RoaCmsBuilder) RoaCms(net.ripe.rpki.commons.crypto.cms.roa.RoaCms) IpResourceSet(net.ripe.ipresource.IpResourceSet) X500Principal(javax.security.auth.x500.X500Principal) ManifestCms(net.ripe.rpki.commons.crypto.cms.manifest.ManifestCms) X509ResourceCertificate(net.ripe.rpki.commons.crypto.x509cert.X509ResourceCertificate) X509ResourceCertificateBuilder(net.ripe.rpki.commons.crypto.x509cert.X509ResourceCertificateBuilder) IpResourceType(net.ripe.ipresource.IpResourceType) Asn(net.ripe.ipresource.Asn)

Aggregations

Resources (com.google.common.io.Resources)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ExecutableLocator (com.google.protobuf.gradle.ExecutableLocator)1 DescriptorSetOptions (com.google.protobuf.gradle.GenerateProtoTask.DescriptorSetOptions)1 ProtobufConfigurator (com.google.protobuf.gradle.ProtobufConfigurator)1 JavaGenerateProtoTaskCollection (com.google.protobuf.gradle.ProtobufConfigurator.JavaGenerateProtoTaskCollection)1 ProtobufConvention (com.google.protobuf.gradle.ProtobufConvention)1 ProtobufPlugin (com.google.protobuf.gradle.ProtobufPlugin)1 NodeExtension (com.moowork.gradle.node.NodeExtension)1 NpmTask (com.moowork.gradle.node.npm.NpmTask)1 DependencyManagementExtension (io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension)1 UncheckedIOException (java.io.UncheckedIOException)1 BigInteger (java.math.BigInteger)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1