|
| 1 | +package dev.protsenko.securityLinter.kubernetes |
| 2 | + |
| 3 | +import com.intellij.codeInspection.LocalInspectionTool |
| 4 | +import com.intellij.codeInspection.ProblemHighlightType |
| 5 | +import com.intellij.codeInspection.ProblemsHolder |
| 6 | +import com.intellij.psi.PsiElementVisitor |
| 7 | +import dev.protsenko.securityLinter.core.HtmlProblemDescriptor |
| 8 | +import dev.protsenko.securityLinter.core.SecurityPluginBundle |
| 9 | +import dev.protsenko.securityLinter.utils.YamlPath |
| 10 | +import org.jetbrains.yaml.psi.YAMLDocument |
| 11 | +import org.jetbrains.yaml.psi.YAMLMapping |
| 12 | +import org.jetbrains.yaml.psi.YAMLSequence |
| 13 | + |
| 14 | +class DisallowedVolumeType : LocalInspectionTool() { |
| 15 | + override fun buildVisitor( |
| 16 | + holder: ProblemsHolder, |
| 17 | + isOnTheFly: Boolean, |
| 18 | + ): PsiElementVisitor { |
| 19 | + return object : BaseKubernetesVisitor() { |
| 20 | + override fun analyze( |
| 21 | + specPrefix: String, |
| 22 | + document: YAMLDocument, |
| 23 | + ) { |
| 24 | + val specVolumes = |
| 25 | + YamlPath.findByYamlPath("${specPrefix}spec.volumes", document) as? YAMLSequence ?: return |
| 26 | + |
| 27 | + for (volume in specVolumes.items) { |
| 28 | + val volumeValue = volume.value as? YAMLMapping ?: continue |
| 29 | + val prohibitedValues = |
| 30 | + volumeValue |
| 31 | + .keyValues |
| 32 | + .filter { |
| 33 | + if (it.value !is YAMLMapping) return@filter false |
| 34 | + if (it.keyText !in allowedVolumeTypes) { |
| 35 | + return@filter true |
| 36 | + } |
| 37 | + return@filter false |
| 38 | + } |
| 39 | + |
| 40 | + if (prohibitedValues.isNotEmpty()) { |
| 41 | + prohibitedValues.forEach { |
| 42 | + val descriptor = |
| 43 | + HtmlProblemDescriptor( |
| 44 | + it, |
| 45 | + SecurityPluginBundle.message("kube012.documentation"), |
| 46 | + SecurityPluginBundle.message("kube012.problem-text"), |
| 47 | + ProblemHighlightType.ERROR, |
| 48 | + emptyArray(), |
| 49 | + ) |
| 50 | + holder.registerProblem(descriptor) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +private val allowedVolumeTypes = |
| 60 | + setOf("configMap", "csi", "downwardAPI", "emptyDir", "ephemeral", "persistentVolumeClaim", "projected", "secret") |
0 commit comments