-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add git-secrets to verification tasks (#4072)
- Loading branch information
Showing
8 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,48 @@ | ||
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import de.undercouch.gradle.tasks.download.Download | ||
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform | ||
|
||
plugins { | ||
id("de.undercouch.download") | ||
} | ||
|
||
val downloadGitSecrets = tasks.register<Download>("downloadGitSecrets") { | ||
src("https://raw.githubusercontent.com/awslabs/git-secrets/master/git-secrets") | ||
dest("$buildDir/git-secrets") | ||
onlyIfModified(true) | ||
useETag(true) | ||
} | ||
|
||
val gitSecrets = tasks.register<Exec>("gitSecrets") { | ||
onlyIf { | ||
!DefaultNativePlatform.getCurrentOperatingSystem().isWindows | ||
} | ||
|
||
dependsOn(downloadGitSecrets) | ||
workingDir(project.rootDir) | ||
val path = "$buildDir${File.pathSeparator}" | ||
val patchendEnv = environment.apply { replace("PATH", path + getOrDefault("PATH", "")) } | ||
environment = patchendEnv | ||
|
||
commandLine("/bin/sh", "$buildDir/git-secrets", "--register-aws") | ||
|
||
// cleaner than having multiple separate exec tasks | ||
doLast { | ||
exec { | ||
workingDir(project.rootDir) | ||
commandLine("git", "config", "--add", "secrets.allowed", "123456789012") | ||
} | ||
|
||
exec { | ||
workingDir(project.rootDir) | ||
environment = patchendEnv | ||
commandLine("/bin/sh", "$buildDir/git-secrets", "--scan") | ||
} | ||
} | ||
} | ||
|
||
tasks.findByName("check")?.let { | ||
it.dependsOn(gitSecrets) | ||
} |
95 changes: 95 additions & 0 deletions
95
buildSrc/src/test/kotlin/software/aws/toolkits/gradle/GitSecretsTest.kt
This file contains 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,95 @@ | ||
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package software.aws.toolkits.gradle | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder | ||
import org.gradle.testfixtures.ProjectBuilder | ||
import org.gradle.testkit.runner.GradleRunner | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import org.gradle.testkit.runner.UnexpectedBuildFailure | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import org.junit.jupiter.api.io.TempDir | ||
import java.io.File | ||
import kotlin.io.path.writeText | ||
|
||
class GitSecretsTest { | ||
@Test | ||
fun `plugin can be applied`() { | ||
val project = ProjectBuilder.builder().build() | ||
project.getPluginManager().apply("toolkit-git-secrets") | ||
} | ||
|
||
@Test | ||
fun `passes when no secrets`(@TempDir tempDir: File) { | ||
tempDir.mkdirs() | ||
val repo = FileRepositoryBuilder() | ||
.setWorkTree(tempDir) | ||
.build() | ||
repo.create() | ||
|
||
tempDir | ||
.resolve("build.gradle.kts") | ||
.writeText( | ||
""" | ||
plugins { | ||
id("toolkit-git-secrets") | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
Git.wrap(repo).add().addFilepattern(".").call() | ||
|
||
val result = GradleRunner.create() | ||
.withProjectDir(tempDir) | ||
.withArguments("gitSecrets") | ||
.withPluginClasspath() | ||
.build() | ||
|
||
assertThat(result.task(":gitSecrets")?.outcome).isEqualTo(TaskOutcome.SUCCESS) | ||
} | ||
|
||
@Test | ||
fun `fails when contains secrets`(@TempDir tempDir: File) { | ||
tempDir.mkdirs() | ||
val repo = FileRepositoryBuilder() | ||
.setWorkTree(tempDir) | ||
.build() | ||
repo.create() | ||
|
||
tempDir | ||
.resolve("build.gradle.kts") | ||
.apply { | ||
writeText( | ||
""" | ||
plugins { | ||
id("toolkit-git-secrets") | ||
} | ||
""".trimIndent() | ||
) | ||
|
||
appendText( | ||
buildString { | ||
appendLine() | ||
// split to avoid tripping git-secrets | ||
append("// AKI") | ||
append("AXXXXXXXXXXXXXXXX") | ||
} | ||
) | ||
|
||
Git.wrap(repo).add().addFilepattern(".").call() | ||
} | ||
|
||
val failure = assertThrows<UnexpectedBuildFailure> { | ||
GradleRunner.create() | ||
.withProjectDir(tempDir) | ||
.withArguments("gitSecrets") | ||
.withPluginClasspath() | ||
.build() | ||
} | ||
assertThat(failure.message).contains("Matched one or more prohibited patterns") | ||
} | ||
} |
This file contains 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 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 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 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