-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathConfigInversionLinter.kt
More file actions
128 lines (115 loc) · 5.04 KB
/
ConfigInversionLinter.kt
File metadata and controls
128 lines (115 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package datadog.gradle.plugin.config
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.GradleException
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.internal.impldep.kotlinx.metadata.impl.extensions.KmExtension
import org.gradle.kotlin.dsl.accessors.runtime.externalModuleDependencyFor
import org.gradle.kotlin.dsl.getByType
import java.net.URLClassLoader
import java.nio.file.Path
class ConfigInversionLinter : Plugin<Project> {
override fun apply(target: Project) {
val extension = target.extensions.create("supportedTracerConfigurations", SupportedTracerConfigurations::class.java)
registerLogEnvVarUsages(target, extension)
registerCheckEnvironmentVariablesUsage(target)
}
}
/** Registers `logEnvVarUsages` (scan for DD_/OTEL_ tokens and fail if unsupported). */
private fun registerLogEnvVarUsages(target: Project, extension: SupportedTracerConfigurations) {
val ownerPath = extension.configOwnerPath
val generatedFile = extension.className
// token check that uses the generated class instead of JSON
target.tasks.register("logEnvVarUsages") {
group = "verification"
description = "Scan Java files for DD_/OTEL_ tokens and fail if unsupported (using generated constants)"
val mainSourceSetOutput = ownerPath.map {
target.project(it)
.extensions.getByType<SourceSetContainer>()
.named(SourceSet.MAIN_SOURCE_SET_NAME)
.map { main -> main.output }
}
inputs.files(mainSourceSetOutput)
// inputs for incrementality (your own source files, not the owner’s)
val javaFiles = target.fileTree(target.projectDir) {
include("**/src/main/java/**/*.java")
exclude("**/build/**", "**/dd-smoke-tests/**")
}
inputs.files(javaFiles)
outputs.upToDateWhen { true }
doLast {
// 1) Build classloader from the owner project’s runtime classpath
val urls = mainSourceSetOutput.get().get().files.map { it.toURI().toURL() }.toTypedArray()
val supported: Set<String> = URLClassLoader(urls, javaClass.classLoader).use { cl ->
// 2) Load the generated class + read static field
val clazz = Class.forName(generatedFile.get(), true, cl)
@Suppress("UNCHECKED_CAST")
clazz.getField("SUPPORTED").get(null) as Set<String>
}
// 3) Scan our sources and compare
val repoRoot = target.projectDir.toPath()
val tokenRegex = Regex("\"(?:DD_|OTEL_)[A-Za-z0-9_]+\"")
val violations = buildList {
javaFiles.files.forEach { f ->
val rel = repoRoot.relativize(f.toPath()).toString()
var inBlock = false
f.readLines().forEachIndexed { i, raw ->
val trimmed = raw.trim()
if (trimmed.startsWith("//")) return@forEachIndexed
if (!inBlock && trimmed.contains("/*")) inBlock = true
if (inBlock) {
if (trimmed.contains("*/")) inBlock = false
return@forEachIndexed
}
tokenRegex.findAll(raw).forEach { m ->
val token = m.value.trim('"')
if (token !in supported) add("$rel:${i + 1} -> Unsupported token'$token'")
}
}
}
}
if (violations.isNotEmpty()) {
violations.forEach { target.logger.error(it) }
throw GradleException("Unsupported DD_/OTEL_ tokens found! See errors above.")
} else {
target.logger.info("All DD_/OTEL_ tokens are supported.")
}
}
}
}
/** Registers `checkEnvironmentVariablesUsage` (forbid EnvironmentVariables.get(...)). */
private fun registerCheckEnvironmentVariablesUsage(project: Project) {
project.tasks.register("checkEnvironmentVariablesUsage") {
group = "verification"
description = "Scans src/main/java for direct usages of EnvironmentVariables.get(...)"
doLast {
val repoRoot: Path = project.projectDir.toPath()
val javaFiles = project.fileTree(project.projectDir) {
include("**/src/main/java/**/*.java")
exclude("**/build/**")
exclude("internal-api/src/main/java/datadog/trace/api/ConfigHelper.java")
exclude("dd-java-agent/agent-bootstrap/**")
exclude("dd-java-agent/src/main/java/datadog/trace/bootstrap/BootstrapInitializationTelemetry.java")
}
val pattern = Regex("""EnvironmentVariables\.get\s*\(""")
val matches = buildList {
javaFiles.forEach { f ->
val relative = repoRoot.relativize(f.toPath())
f.readLines().forEachIndexed { idx, line ->
if (pattern.containsMatchIn(line)) {
add("$relative:${idx + 1} -> ${line.trim()}")
}
}
}
}
if (matches.isNotEmpty()) {
project.logger.lifecycle("\nFound forbidden usages of EnvironmentVariables.get(...):")
matches.forEach { project.logger.lifecycle(it) }
throw GradleException("Forbidden usage of EnvironmentVariables.get(...) found in Java files.")
} else {
project.logger.info("No forbidden EnvironmentVariables.get(...) usages found in src/main/java.")
}
}
}
}