Skip to content

Commit

Permalink
Formatting Kotlin sources
Browse files Browse the repository at this point in the history
  • Loading branch information
chkpnt committed Nov 24, 2019
1 parent aabec4c commit 448c775
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
/*
* Copyright 2016 - 2019 Gregor Dschung
*
* 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 de.chkpnt.gradle.plugin.truststorebuilder

import java.nio.file.Files
import java.nio.file.Path
import java.security.KeyStore
import java.security.MessageDigest
Expand All @@ -9,10 +26,6 @@ import java.time.Clock
import java.time.Duration
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.util.stream.Collectors

import groovy.transform.PackageScope
import java.nio.file.Files

interface CertificateService {
fun isCertificateValidInFuture(cert: X509Certificate, duration: Duration): Boolean
Expand Down Expand Up @@ -43,11 +56,11 @@ class DefaultCertificateService : CertificateService {
}

fun getCertificateFromKeystore(ks: KeyStore, alias: String): X509Certificate {
if (! ks.containsAlias(alias)) {
if (!ks.containsAlias(alias)) {
throw IllegalArgumentException("The keystore does not contain a certificate for alias $alias")
}

if (! ks.entryInstanceOf(alias, KeyStore.TrustedCertificateEntry::class.java)) {
if (!ks.entryInstanceOf(alias, KeyStore.TrustedCertificateEntry::class.java)) {
throw UnsupportedOperationException("Certificate extraction is currently only implemented for TrustedCertificateEntry")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package de.chkpnt.gradle.plugin.truststorebuilder
/*
* Copyright 2016 - 2019 Gregor Dschung
*
* 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.
*/

import java.nio.file.Path
import java.security.cert.CertificateException
import java.security.cert.CertificateFactory
import java.time.Duration
package de.chkpnt.gradle.plugin.truststorebuilder

import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.TaskAction
import java.nio.file.Files
import java.nio.file.Path
import java.security.cert.CertificateException
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
import java.time.Duration

open class CheckCertsValidationTask() : DefaultTask() {

Expand Down Expand Up @@ -44,7 +59,7 @@ open class CheckCertsValidationTask() : DefaultTask() {
private fun checkValidation(file: Path) {
val cert = loadX509Certificate(file)

if (! certificateService.isCertificateValidInFuture(cert, atLeastValid)) {
if (!certificateService.isCertificateValidInFuture(cert, atLeastValid)) {
throw CheckCertValidationError(file, INVALID_REASON)
}
}
Expand All @@ -60,8 +75,7 @@ open class CheckCertsValidationTask() : DefaultTask() {
}
}

data class CheckCertValidationError(val file: Path, val reason: String): GradleException() {
data class CheckCertValidationError(val file: Path, val reason: String) : GradleException() {

override val message: String? = "$reason: $file"

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2016 - 2019 Gregor Dschung
*
* 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 de.chkpnt.gradle.plugin.truststorebuilder

import org.gradle.api.DefaultTask
Expand All @@ -8,15 +24,19 @@ import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.TaskExecutionException
import java.io.File
import java.nio.file.*
import java.nio.file.FileSystems
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
import java.util.*
import java.util.Properties

interface FileAdapter {
fun toFile(path: Path): File
}

class DefaultFileAdapter: FileAdapter {
class DefaultFileAdapter : FileAdapter {
override fun toFile(path: Path): File = path.toFile()
}

Expand Down Expand Up @@ -106,7 +126,6 @@ open class ImportCertsTask() : DefaultTask() {
return alias ?: filename.toString()
}
}

}

internal object PathScanner {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package de.chkpnt.gradle.plugin.truststorebuilder
/*
* Copyright 2016 - 2019 Gregor Dschung
*
* 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.
*/

import java.nio.file.Path
package de.chkpnt.gradle.plugin.truststorebuilder

import org.gradle.api.Project
import java.nio.file.Path

open class TrustStoreBuilderExtension(project: Project) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Gregor Dschung
* Copyright 2016 - 2019 Gregor Dschung
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,46 +25,46 @@ class TrustStoreBuilderPlugin : Plugin<Project> {

override fun apply(project: Project) {
project.pluginManager
.apply(BasePlugin::class.java)
.apply(BasePlugin::class.java)

val extension = project.extensions
.create(TRUSTSTOREBUILDER_EXTENSION_NAME, TrustStoreBuilderExtension::class.java, project)
.create(TRUSTSTOREBUILDER_EXTENSION_NAME, TrustStoreBuilderExtension::class.java, project)

project.tasks
.register(CHECK_CERTS_TASK_NAME, CheckCertsValidationTask::class.java) { task ->
task.group = LifecycleBasePlugin.VERIFICATION_GROUP
task.description = "Checks the validation of the certificates to import."
.register(CHECK_CERTS_TASK_NAME, CheckCertsValidationTask::class.java) { task ->
task.group = LifecycleBasePlugin.VERIFICATION_GROUP
task.description = "Checks the validation of the certificates to import."

task.inputDir.set(extension.inputDirPath)
task.acceptedFileEndings.set(extension.acceptedFileEndings)
task.atLeastValidDays.set(Integer(extension.atLeastValidDays))
}
task.inputDir.set(extension.inputDirPath)
task.acceptedFileEndings.set(extension.acceptedFileEndings)
task.atLeastValidDays.set(Integer(extension.atLeastValidDays))
}

project.tasks
.getByName(LifecycleBasePlugin.CHECK_TASK_NAME)
.dependsOn(CHECK_CERTS_TASK_NAME)
.getByName(LifecycleBasePlugin.CHECK_TASK_NAME)
.dependsOn(CHECK_CERTS_TASK_NAME)

project.tasks
.register(BUILD_TRUSTSTORE_TASK_NAME, ImportCertsTask::class.java) { task ->
task.group = BasePlugin.BUILD_GROUP
.register(BUILD_TRUSTSTORE_TASK_NAME, ImportCertsTask::class.java) { task ->
task.group = BasePlugin.BUILD_GROUP

task.keystore.set(extension.trustStorePath)
task.password.set(extension.password)
task.inputDir.set(extension.inputDirPath)
task.acceptedFileEndings.set(extension.acceptedFileEndings)
}
task.keystore.set(extension.trustStorePath)
task.password.set(extension.password)
task.inputDir.set(extension.inputDirPath)
task.acceptedFileEndings.set(extension.acceptedFileEndings)
}

configureTaskDependencies(project)
}

private fun configureTaskDependencies(project: Project) {
project.tasks
.getByName(LifecycleBasePlugin.CHECK_TASK_NAME)
.dependsOn(CHECK_CERTS_TASK_NAME)
.getByName(LifecycleBasePlugin.CHECK_TASK_NAME)
.dependsOn(CHECK_CERTS_TASK_NAME)

project.tasks
.getByName(LifecycleBasePlugin.BUILD_TASK_NAME)
.dependsOn(BUILD_TRUSTSTORE_TASK_NAME)
.getByName(LifecycleBasePlugin.BUILD_TASK_NAME)
.dependsOn(BUILD_TRUSTSTORE_TASK_NAME)
}

companion object {
Expand All @@ -73,5 +73,4 @@ class TrustStoreBuilderPlugin : Plugin<Project> {
private val BUILD_TRUSTSTORE_TASK_NAME = "buildTrustStore"
private val CHECK_CERTS_TASK_NAME = "checkCertificates"
}

}

0 comments on commit 448c775

Please sign in to comment.