Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"additionalProperties": {
"type": "string"
}
},
"templateRuleKey": {
"type": "string"
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/br/com/felipezorzo/zpa/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import br.com.felipezorzo.zpa.cli.config.RuleLevel
import br.com.felipezorzo.zpa.cli.exporters.ConsoleExporter
import br.com.felipezorzo.zpa.cli.exporters.GenericIssueFormatExporter
import br.com.felipezorzo.zpa.cli.plugin.PluginManager
import br.com.felipezorzo.zpa.cli.rules.CliActiveRules
import com.beust.jcommander.JCommander
import com.beust.jcommander.ParameterException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
Expand All @@ -15,7 +16,6 @@ import com.felipebz.zpa.api.PlSqlFile
import com.felipebz.zpa.api.ZpaRulesDefinition
import com.felipebz.zpa.api.checks.PlSqlVisitor
import com.felipebz.zpa.metadata.FormsMetadata
import com.felipebz.zpa.rules.ActiveRules
import com.felipebz.zpa.rules.Repository
import com.felipebz.zpa.rules.RuleMetadataLoader
import com.felipebz.zpa.rules.ZpaChecks
Expand Down Expand Up @@ -159,15 +159,16 @@ class Main(private val args: Arguments) {
pluginManager.unloadPlugins()
}

private fun getActiveRules(): ActiveRules {
val activeRules = ActiveRules()
private fun getActiveRules(): CliActiveRules {
val config = if (args.configFile.isNotEmpty()) {
val configFile = File(args.configFile)
mapper.readValue(configFile, ConfigFile::class.java)
} else {
ConfigFile()
}

val activeRules = CliActiveRules(config)

if (config.rules.isNotEmpty()) {
activeRules.addRuleConfigurer { repo, rule, configuration ->
var ruleConfig = config.rules["${repo.key}:${rule.key}"] ?: config.rules[rule.key]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ enum class RuleLevel {
class RuleOptions {
var level: RuleLevel = RuleLevel.ON
var parameters: Map<String, String> = emptyMap()
var templateRuleKey: String? = null
}

class RuleCategoryDeserializer : JsonDeserializer<RuleConfiguration>() {
Expand Down
33 changes: 33 additions & 0 deletions src/main/kotlin/br/com/felipezorzo/zpa/cli/rules/CliActiveRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Z PL/SQL Analyzer
* Copyright (C) 2015-2025 Felipe Zorzo
* mailto:felipe AT felipezorzo DOT com DOT br
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package br.com.felipezorzo.zpa.cli.rules

import com.felipebz.zpa.rules.*

class CliActiveRule(
private val activeRule : ActiveRule,
private val rule: ZpaRule
) : ZpaActiveRule by activeRule {

constructor(repository : ZpaRepository, rule: ZpaRule, configuration: ActiveRuleConfiguration?) : this(ActiveRule(repository, rule, configuration), rule)

override val templateRuleKey: String?
get() = (rule as? CliCustomRule)?.templateRuleKey
}
91 changes: 91 additions & 0 deletions src/main/kotlin/br/com/felipezorzo/zpa/cli/rules/CliActiveRules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Z PL/SQL Analyzer
* Copyright (C) 2015-2026 Felipe Zorzo
* mailto:felipe AT felipezorzo DOT com DOT br
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package br.com.felipezorzo.zpa.cli.rules

import br.com.felipezorzo.zpa.cli.config.ConfigFile
import com.felipebz.zpa.rules.*

class CliActiveRules(val config: ConfigFile?) : ZpaActiveRules {

private val repositories = mutableListOf<Repository>()
private val activeRuleConfigurers = mutableListOf<ActiveRuleConfigurer>()

fun addRepository(repository: Repository): CliActiveRules = apply {
repositories.add(repository)
}

fun addRuleConfigurer(filter: ActiveRuleConfigurer): CliActiveRules = apply {
activeRuleConfigurers.add(filter)
}

override fun findByRepository(repository: String): Collection<ZpaActiveRule> {
val repo = this.repositories.first { it.key == repository }
val repoAvailableRules = repo.availableRules
val customRules = addCustomRulesByConfig(config, repo)

return (repoAvailableRules + customRules)
.mapNotNull { rule ->
val activeRuleConfiguration = ActiveRuleConfiguration(repo.key, rule.key)
if (activeRuleConfigurers.all { it.apply(repo, rule, activeRuleConfiguration) }) {
CliActiveRule(repo, rule, activeRuleConfiguration)
} else {
null
}
}
}

fun addCustomRulesByConfig(config: ConfigFile?, repo: Repository): List<ZpaRule> {
if (config == null) return emptyList()

return config.rules.entries
.mapNotNull { (key, ruleConfig) ->
val templateRuleKey = ruleConfig.options.templateRuleKey?.let { RuleKeyParser.parse(it) } ?: return@mapNotNull null
if (templateRuleKey.repository.isNotEmpty() && templateRuleKey.repository != repo.key) return@mapNotNull null
val templateRule = repo.rule(templateRuleKey.rule) ?: return@mapNotNull null

createCustomRuleFromTemplateRule(key, templateRule)
}
}

private fun createCustomRuleFromTemplateRule(key: String, templateRule: ZpaRule): CliCustomRule {

val rule = Rule(key).apply {
name = templateRule.name
remediationConstant = templateRule.remediationConstant
scope = templateRule.scope
severity = templateRule.severity
status = templateRule.status
tags = templateRule.tags
htmlDescription = templateRule.htmlDescription
isActivatedByDefault = templateRule.isActivatedByDefault
templateRule.params.forEach { param ->
createParam(param.key).apply {
description = param.description
defaultValue = param.defaultValue
}
}
}

return CliCustomRule(rule).apply {
templateRuleKey = templateRule.key
}
}

}
28 changes: 28 additions & 0 deletions src/main/kotlin/br/com/felipezorzo/zpa/cli/rules/CliCustomRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Z PL/SQL Analyzer
* Copyright (C) 2015-2026 Felipe Zorzo
* mailto:felipe AT felipezorzo DOT com DOT br
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package br.com.felipezorzo.zpa.cli.rules

import com.felipebz.zpa.rules.ZpaRule

class CliCustomRule(
rule: ZpaRule
) : ZpaRule by rule {
var templateRuleKey : String? = null
}
33 changes: 33 additions & 0 deletions src/main/kotlin/br/com/felipezorzo/zpa/cli/rules/RuleKeyParser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Z PL/SQL Analyzer
* Copyright (C) 2015-2026 Felipe Zorzo
* mailto:felipe AT felipezorzo DOT com DOT br
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package br.com.felipezorzo.zpa.cli.rules

import com.felipebz.zpa.rules.RuleKey

object RuleKeyParser {
fun parse(key: String): RuleKey {
val parts = key.split(":", limit = 2)
return if (parts.size == 2) {
RuleKey(parts[0], parts[1])
} else {
RuleKey("", parts[0])
}
}
}