-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from snyk/feat/trust
feat: add workspace trust feature
- Loading branch information
Showing
14 changed files
with
363 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
# Snyk Changelog | ||
|
||
## [2.4.48] | ||
### Added | ||
|
||
- Project trust feature. | ||
|
||
## [2.4.47] | ||
|
||
### Fixed | ||
|
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
59 changes: 59 additions & 0 deletions
59
src/integTest/kotlin/snyk/trust/WorkspaceTrustServiceTest.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,59 @@ | ||
@file:Suppress("FunctionName") | ||
|
||
package snyk.trust | ||
|
||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.testFramework.fixtures.BasePlatformTestCase | ||
import com.intellij.testFramework.replaceService | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.unmockkAll | ||
import org.hamcrest.core.IsEqual.equalTo | ||
import org.junit.Assert.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import java.nio.file.Paths | ||
|
||
class WorkspaceTrustServiceTest : BasePlatformTestCase() { | ||
|
||
private val workspaceTrustSettingsMock = mockk<WorkspaceTrustSettings>() | ||
private lateinit var cut: WorkspaceTrustService | ||
|
||
private class IntegTestDisposable : Disposable { | ||
override fun dispose() {} | ||
} | ||
|
||
@Before | ||
public override fun setUp() { | ||
super.setUp() | ||
unmockkAll() | ||
|
||
val application = ApplicationManager.getApplication() | ||
application.replaceService( | ||
WorkspaceTrustSettings::class.java, | ||
workspaceTrustSettingsMock, | ||
IntegTestDisposable() | ||
) | ||
|
||
cut = WorkspaceTrustService() | ||
} | ||
|
||
@Test | ||
fun `test isPathTrusted should return false if no trusted path in settings available`() { | ||
every { workspaceTrustSettingsMock.getTrustedPaths() } returns listOf() | ||
|
||
val path = Paths.get("/project") | ||
|
||
assertThat(cut.isPathTrusted(path), equalTo(false)) | ||
} | ||
|
||
@Test | ||
fun `test isPathTrusted should return true if trusted path in settings available`() { | ||
every { workspaceTrustSettingsMock.getTrustedPaths() } returns listOf("/project") | ||
|
||
val path = Paths.get("/project") | ||
|
||
assertThat(cut.isPathTrusted(path), equalTo(true)) | ||
} | ||
} |
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,73 @@ | ||
@file:JvmName("TrustedProjectsKt") | ||
package snyk.trust | ||
|
||
import com.intellij.openapi.application.invokeAndWaitIfNeeded | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.ui.MessageDialogBuilder | ||
import com.intellij.openapi.ui.Messages | ||
import snyk.SnykBundle | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
|
||
private val LOG = Logger.getInstance("snyk.trust.TrustedProjects") | ||
|
||
/** | ||
* Shows the "Trust and Scan Project?" dialog, if the user wasn't asked yet if they trust this project, | ||
* and sets the project trusted state according to the user choice. | ||
* | ||
* @return `false` if the user chose not to scan the project at all; `true` otherwise | ||
*/ | ||
fun confirmScanningAndSetWorkspaceTrustedStateIfNeeded(projectFileOrDir: Path): Boolean { | ||
val projectDir = if (Files.isDirectory(projectFileOrDir)) projectFileOrDir else projectFileOrDir.parent | ||
|
||
val trustService = service<WorkspaceTrustService>() | ||
val trustedState = trustService.isPathTrusted(projectDir) | ||
if (!trustedState) { | ||
LOG.info("Asking user to trust the project ${projectDir.fileName}") | ||
return when (confirmScanningUntrustedProject(projectDir)) { | ||
ScanUntrustedProjectChoice.TRUST_AND_SCAN -> { | ||
trustService.addTrustedPath(projectDir) | ||
true | ||
} | ||
|
||
ScanUntrustedProjectChoice.CANCEL -> false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
private fun confirmScanningUntrustedProject(projectDir: Path): ScanUntrustedProjectChoice { | ||
val fileName = projectDir.fileName ?: projectDir.toString() | ||
val title = SnykBundle.message("snyk.trust.dialog.warning.title", fileName) | ||
val message = SnykBundle.message("snyk.trust.dialog.warning.text") | ||
val trustButton = SnykBundle.message("snyk.trust.dialog.warning.button.trust") | ||
val distrustButton = SnykBundle.message("snyk.trust.dialog.warning.button.distrust") | ||
|
||
var choice = ScanUntrustedProjectChoice.CANCEL | ||
|
||
invokeAndWaitIfNeeded { | ||
val result = MessageDialogBuilder | ||
.yesNo(title, message) | ||
.icon(Messages.getWarningIcon()) | ||
.yesText(trustButton) | ||
.noText(distrustButton) | ||
.show() | ||
|
||
choice = if (result == Messages.YES) { | ||
LOG.info("User trusts the project $fileName for scans") | ||
ScanUntrustedProjectChoice.TRUST_AND_SCAN | ||
} else { | ||
LOG.info("User doesn't trust the project $fileName for scans") | ||
ScanUntrustedProjectChoice.CANCEL | ||
} | ||
} | ||
|
||
return choice | ||
} | ||
|
||
enum class ScanUntrustedProjectChoice { | ||
TRUST_AND_SCAN, | ||
CANCEL; | ||
} |
Oops, something went wrong.