mirror of
https://github.com/elastic/logstash.git
synced 2025-06-27 17:08:55 -04:00
Simplifies aliass registry marking task config and move into separate class (#14138)
Introduce some default configs for Alias registry signing task, extract into separate class and cover with tests.
This commit is contained in:
parent
da68ff3803
commit
69107a5bc6
3 changed files with 105 additions and 35 deletions
39
build.gradle
39
build.gradle
|
@ -43,6 +43,7 @@ import de.undercouch.gradle.tasks.download.Download
|
|||
import groovy.json.JsonSlurper
|
||||
import org.logstash.gradle.tooling.ListProjectDependencies
|
||||
import org.logstash.gradle.tooling.ExtractBundledJdkVersion
|
||||
import org.logstash.gradle.tooling.SignAliasDefinitions
|
||||
import org.logstash.gradle.tooling.ToolingUtils
|
||||
|
||||
allprojects {
|
||||
|
@ -190,46 +191,14 @@ tasks.register("configureArtifactInfo") {
|
|||
}
|
||||
}
|
||||
|
||||
abstract class SignAliasDefinitionsTask extends DefaultTask {
|
||||
|
||||
/**
|
||||
* Relative path to the AliasRegistry.yml file to use, relative to project's root
|
||||
* */
|
||||
@Input
|
||||
String registryPath
|
||||
|
||||
@InputFile
|
||||
File getRegistryFullPath() {
|
||||
project.file("${project.projectDir}/${registryPath}")
|
||||
}
|
||||
|
||||
/**
|
||||
* Full file path to the file containing the marked AliasRegistry file
|
||||
* */
|
||||
@OutputFile
|
||||
File hashedFile
|
||||
|
||||
@TaskAction
|
||||
def sign() {
|
||||
String aliasesDefs = registryFullPath.text
|
||||
String hash = aliasesDefs.digest('SHA-256')
|
||||
hashedFile.withWriter('utf-8') { writer ->
|
||||
writer.writeLine "#CHECKSUM: ${hash}"
|
||||
writer.writeLine "# DON'T EDIT THIS FILE, PLEASE REFER TO ${registryPath}"
|
||||
writer.write aliasesDefs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.register("markAliasDefinitions", SignAliasDefinitionsTask) {
|
||||
tasks.register("markAliasDefinitions", SignAliasDefinitions) {
|
||||
description "Create an hashes aliases file from original aliases yml definition"
|
||||
registryPath = 'logstash-core/src/main/resources/org/logstash/plugins/AliasRegistry.yml'
|
||||
hashedFile = project.file("${project.buildDir}/plugin_aliases_hashed.yml")
|
||||
}
|
||||
|
||||
tasks.register("markTestAliasDefinitions", SignAliasDefinitionsTask) {
|
||||
tasks.register("markTestAliasDefinitions", SignAliasDefinitions) {
|
||||
description "Create an hashes aliases file for testing aliases yml definition"
|
||||
registryPath = 'logstash-core/src/test/resources/org/logstash/plugins/AliasRegistry.yml'
|
||||
stage SignAliasDefinitions.Stage.test
|
||||
hashedFile = project.file("${project.buildDir}/plugin_aliases_hashed_test.yml")
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package org.logstash.gradle.tooling
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.InputFile
|
||||
import org.gradle.api.tasks.OutputFile
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
|
||||
abstract class SignAliasDefinitions extends DefaultTask {
|
||||
|
||||
enum Stage {
|
||||
main, test;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relative path to the AliasRegistry.yml file to use, relative to project's root
|
||||
* */
|
||||
@Input
|
||||
String registry = 'org/logstash/plugins/AliasRegistry.yml'
|
||||
|
||||
@InputFile
|
||||
File getRegistryFullPath() {
|
||||
String stagedRegistry = "logstash-core/src/${stage}/resources/${registry}"
|
||||
project.file("${project.projectDir}/${stagedRegistry}")
|
||||
}
|
||||
|
||||
/**
|
||||
* Full file path to the file containing the marked AliasRegistry file
|
||||
* */
|
||||
@OutputFile
|
||||
File hashedFile
|
||||
|
||||
private Stage stage = Stage.main
|
||||
|
||||
@TaskAction
|
||||
def sign() {
|
||||
String aliasesDefs = registryFullPath.text
|
||||
String hash = aliasesDefs.digest('SHA-256')
|
||||
String stagedRegistry = "logstash-core/src/${stage}/resources/${registry}"
|
||||
hashedFile.withWriter('utf-8') { writer ->
|
||||
writer.writeLine "#CHECKSUM: ${hash}"
|
||||
writer.writeLine "# DON'T EDIT THIS FILE, PLEASE REFER TO ${stagedRegistry}"
|
||||
writer.write aliasesDefs
|
||||
}
|
||||
}
|
||||
|
||||
SignAliasDefinitions stage(Stage stage) {
|
||||
this.stage = stage
|
||||
return this
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package org.logstash.gradle.tooling
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.testfixtures.ProjectBuilder
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
|
||||
class SignAliasDefinitionsTest {
|
||||
Project project
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
project = ProjectBuilder.builder().build()
|
||||
}
|
||||
|
||||
@Test
|
||||
void "test sign registry file adds header definition and SHA"() {
|
||||
Path toSignFile = project.rootDir.toPath().resolve("logstash-core/src/test/resources/ToSign.yml")
|
||||
Files.createDirectories(toSignFile.parent)
|
||||
|
||||
String someYaml = """
|
||||
|input:
|
||||
| - alias: elastic_agent
|
||||
| from: beats
|
||||
""".stripMargin().stripIndent()
|
||||
|
||||
toSignFile.toFile() << someYaml
|
||||
assert Files.exists(toSignFile)
|
||||
|
||||
Path signedFile = project.buildDir.toPath().resolve("hashed_output.yml")
|
||||
// necessary to avoid FileNotFoundException when the task try to write on the output file
|
||||
Files.createDirectories(signedFile.parent)
|
||||
|
||||
def task = project.task("signerTask", type: SignAliasDefinitions) {
|
||||
stage SignAliasDefinitions.Stage.test
|
||||
registry = "ToSign.yml"
|
||||
hashedFile = project.file("${project.buildDir}/hashed_output.yml")
|
||||
}
|
||||
|
||||
// Exercise
|
||||
task.sign()
|
||||
|
||||
// Verify
|
||||
String hash = someYaml.digest('SHA-256')
|
||||
assert signedFile.toFile().text.startsWith("#CHECKSUM: ${hash}")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue