-
Notifications
You must be signed in to change notification settings - Fork 74
Basic implementation of compliance scan
command
#1940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2a52e6f
Working on new Codyze
oxisto 3e0f340
Basic implementation of `compliance scan` command
oxisto a85fb1e
Spotless
oxisto 3da7b57
Adjused package path
oxisto 09a199c
List security goals first
oxisto 4b26c2c
Also connect security objectives
oxisto 3387fd8
Exposing clikt as API dependency, otherwise its hard to interact with…
oxisto 373327b
Trying to outside things into a ProjectCommand so we can access it fr…
oxisto 05ee88a
Appling neo4j dependency hack also to Codyze
oxisto 06fb5e6
Added Kotlin scripting support for queries
oxisto 491e02a
Kotlin script works (at least with K2)
oxisto 140aa0d
Executing query statement
oxisto 50c173e
Added a --components flag
oxisto d27fea4
Add support exclusion patterns
oxisto c5000d8
Spotless
oxisto 08802d7
Improved code after review
oxisto 78fb721
Update qualified name to Application main()
fwendland 25a7565
Adjust group name in CLI as colon is added internally
fwendland 75201d5
Merge branch 'main' into scan-command
oxisto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
buildSrc/src/main/kotlin/codyze.module-conventions.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import org.gradle.accessors.dm.LibrariesForLibs | ||
|
||
plugins { | ||
id("cpg.common-conventions") | ||
id("cpg.frontend-conventions") | ||
} | ||
|
||
val libs = the<LibrariesForLibs>() // necessary to be able to use the version catalog in buildSrc | ||
dependencies { | ||
api(project(":codyze-core")) | ||
api(libs.clikt) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ompliance/src/integrationTest/kotlin/de/fraunhofer/aisec/codyze/compliance/CommandTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2025, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* 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.fraunhofer.aisec.codyze.compliance | ||
|
||
import com.github.ajalt.clikt.testing.test | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class CommandIntegrationTest { | ||
@Test | ||
fun testScanCommand() { | ||
val command = ScanCommand() | ||
val result = | ||
command.test( | ||
"--project-dir src/integrationTest/resources/demo-app --components webapp --components auth" | ||
) | ||
assertEquals( | ||
"Message(arguments=null, id=null, markdown=This is a **finding**, properties=null, text=null)\n", | ||
result.output, | ||
) | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
codyze-compliance/src/integrationTest/resources/demo-app/components/auth/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
print("Hello World") | ||
|
||
def encrypt(): | ||
return very_good_encryption() |
4 changes: 4 additions & 0 deletions
4
codyze-compliance/src/integrationTest/resources/demo-app/components/webapp/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
print("Hello World") | ||
|
||
def encrypt(): | ||
return very_good_encryption() |
16 changes: 16 additions & 0 deletions
16
codyze-compliance/src/integrationTest/resources/demo-app/queries/good-encryption.query.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import de.fraunhofer.aisec.cpg.TranslationResult | ||
import de.fraunhofer.aisec.cpg.graph.calls | ||
import de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration | ||
import de.fraunhofer.aisec.cpg.query.QueryTree | ||
import de.fraunhofer.aisec.cpg.query.allExtended | ||
|
||
fun statement1(tr: TranslationResult): QueryTree<Boolean> { | ||
val result = tr.allExtended<FunctionDeclaration>(sel = { | ||
it.name.localName.contains("encrypt") && !it.isInferred | ||
}) { | ||
QueryTree(it.calls.any { | ||
it.name.contains("very_good") | ||
}) | ||
} | ||
return result | ||
} |
12 changes: 12 additions & 0 deletions
12
codyze-compliance/src/integrationTest/resources/demo-app/security-goals/goal1.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: Goal1 | ||
description: Make it very secure | ||
components: | ||
- auth | ||
- webserver | ||
assumptions: | ||
- Third party code is very good | ||
objectives: | ||
- name: Good encryption | ||
description: Encryption used is very good | ||
statements: | ||
- For each algorithm A, if A is used, then A must be a very good cryptographic algorithm |
113 changes: 113 additions & 0 deletions
113
codyze-compliance/src/main/kotlin/de/fraunhofer/aisec/codyze/compliance/Command.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (c) 2025, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* 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.fraunhofer.aisec.codyze.compliance | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.core.subcommands | ||
import com.github.ajalt.clikt.parameters.groups.provideDelegate | ||
import de.fraunhofer.aisec.codyze.* | ||
|
||
/** The main `compliance` command. */ | ||
class ComplianceCommand : CliktCommand() { | ||
override fun run() {} | ||
} | ||
|
||
/** | ||
* A command that operates on a project. This class provides the common options and functions for | ||
* all commands. | ||
*/ | ||
abstract class ProjectCommand : CliktCommand() { | ||
private val projectOptions by ProjectOptions() | ||
private val translationOptions by TranslationOptions() | ||
|
||
/** Loads the security goals from the project. */ | ||
fun loadSecurityGoals(): List<SecurityGoal> { | ||
return loadSecurityGoals(projectOptions.directory.resolve("security-goals")) | ||
} | ||
|
||
/** | ||
* This method is called by the `run` method to perform the actual analysis. It is separated to | ||
* allow for easier access from overriding applications. | ||
*/ | ||
protected fun analyze(): AnalysisResult { | ||
// Load the security goals from the project | ||
val goals = loadSecurityGoals(projectOptions.directory.resolve("security-goals")) | ||
oxisto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Analyze the project | ||
val project = AnalysisProject.fromOptions(projectOptions, translationOptions) | ||
val result = project.analyze() | ||
val tr = result.translationResult | ||
|
||
// Connect the security goals to the translation result for now. Later we will add them to | ||
// individual concepts | ||
for (goal in goals) { | ||
goal.underlyingNode = tr | ||
|
||
// Load and execute queries associated to the goals | ||
for (objective in goal.objectives) { | ||
objective.underlyingNode = tr | ||
|
||
val scriptFile = | ||
projectOptions.directory | ||
.resolve("queries") | ||
.resolve( | ||
"${objective.name.localName.lowercase().replace(" ", "-")}.query.kts" | ||
) | ||
for (stmt in objective.statements.withIndex()) { | ||
tr.evalQuery(scriptFile.toFile(), "statement${stmt.index + 1}") | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} | ||
|
||
/** The `scan` command. This will scan the project for compliance violations in the future. */ | ||
open class ScanCommand : ProjectCommand() { | ||
override fun run() { | ||
val result = analyze() | ||
|
||
result.run.results?.forEach { echo(it.message) } | ||
} | ||
} | ||
|
||
/** | ||
* The `list-security-goals` command. This will list the names of all security goals in the | ||
* specified project. | ||
* | ||
* This command assumes that the project contains a folder named `security-goals` that contains YAML | ||
* files with the security goals. | ||
*/ | ||
class ListSecurityGoals : ProjectCommand() { | ||
oxisto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
override fun run() { | ||
val goals = loadSecurityGoals() | ||
// Print the name of each security goal | ||
goals.forEach { echo(it.name.localName) } | ||
} | ||
} | ||
|
||
var Command = ComplianceCommand().subcommands(ScanCommand(), ListSecurityGoals()) | ||
Check warning on line 113 in codyze-compliance/src/main/kotlin/de/fraunhofer/aisec/codyze/compliance/Command.kt
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 0 additions & 71 deletions
71
codyze-compliance/src/main/kotlin/de/fraunhofer/aisec/cpg/codyze/compliance/Command.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Configuration status="WARN"> | ||
<Appenders> | ||
<Console name="STDOUT" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d{HH:mm:ss,SSS} %-5p %C{1} %m%n"/> | ||
<ThresholdFilter level="DEBUG"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Logger level="DEBUG" name="de.fraunhofer.aisec"/> | ||
<Root level="DEBUG"> | ||
<AppenderRef ref="STDOUT"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.