Skip to content

Security Scans: Improving telemetry error messages #4500

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 21 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import software.aws.toolkits.resources.message

open class CodeWhispererCodeScanException(override val message: String?) : RuntimeException()

open class UploadCodeScanException(override val message: String?) : Exception()
open class CodeWhispererCodeScanServerException(override val message: String?) : RuntimeException()

internal fun noFileOpenError(): Nothing =
throw CodeWhispererCodeScanException(message("codewhisperer.codescan.no_file_open"))
Expand All @@ -27,8 +27,8 @@ internal fun fileFormatNotSupported(format: String): Nothing =
internal fun fileTooLarge(): Nothing =
throw CodeWhispererCodeScanException(message("codewhisperer.codescan.file_too_large"))

internal fun uploadArtifactFailedError(errorMessage: String): Nothing =
throw UploadCodeScanException(errorMessage)
internal fun codeScanServerException(errorMessage: String): Nothing =
throw CodeWhispererCodeScanServerException(errorMessage)

internal fun invalidSourceZipError(): Nothing =
throw CodeWhispererCodeScanException(message("codewhisperer.codescan.invalid_source_zip_telemetry"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,26 @@ class CodeWhispererCodeScanManager(val project: Project) {
return errorMessage
}

private fun getCodeScanExceptionMessage(e: CodeWhispererCodeScanException): String? {
val message = e.message
return when {
message.isNullOrBlank() -> null
message == message("codewhisperer.codescan.invalid_source_zip_telemetry") -> {
message("codewhisperer.codescan.run_scan_error")
}
else -> message
}
}

private fun getCodeScanServerExceptionMessage(e: CodeWhispererCodeScanServerException): String? =
e.message?.takeIf { it.startsWith("UploadArtifactToS3Exception:") }
?.let { message("codewhisperer.codescan.upload_to_s3_failed") }

fun handleException(coroutineContext: CoroutineContext, e: Exception, scope: CodeWhispererConstants.CodeAnalysisScope): String {
val errorMessage = when (e) {
is CodeWhispererException -> e.awsErrorDetails().errorMessage() ?: message("codewhisperer.codescan.run_scan_error")
is CodeWhispererCodeScanException -> when (e.message) {
message("codewhisperer.codescan.invalid_source_zip_telemetry") -> message("codewhisperer.codescan.run_scan_error")
else -> e.message
}
is UploadCodeScanException -> message("codewhisperer.codescan.upload_to_s3_failed")
is CodeWhispererCodeScanException -> getCodeScanExceptionMessage(e)
is CodeWhispererCodeScanServerException -> getCodeScanServerExceptionMessage(e)
is WaiterTimeoutException, is TimeoutCancellationException -> message("codewhisperer.codescan.scan_timed_out")
is CancellationException -> message("codewhisperer.codescan.cancelled_by_user_exception")
else -> null
Expand Down Expand Up @@ -372,10 +384,17 @@ class CodeWhispererCodeScanManager(val project: Project) {
message("codewhisperer.codescan.file_too_large") -> message("codewhisperer.codescan.file_too_large_telemetry")
else -> e.message
}
is UploadCodeScanException -> e.message
is CodeWhispererCodeScanServerException -> e.message
is WaiterTimeoutException, is TimeoutCancellationException -> message("codewhisperer.codescan.scan_timed_out")
is CancellationException -> message("codewhisperer.codescan.cancelled_by_user_exception")
else -> e.message
else -> when {
/**
* Error message has text with user details(like requestId) which is specific so sending a custom error message to calculate the occurence of this event.
*/
e.message?.startsWith("Too many requests, please wait before trying again.") == true ->
"Too many requests, please wait before trying again."
else -> e.message
}
} ?: message("codewhisperer.codescan.run_scan_error_telemetry")

return telemetryErrorMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ class CodeWhispererCodeScanSession(val sessionContext: CodeScanSessionContext) {
.build()
)
} catch (e: Exception) {
throw e
LOG.debug { "Create Upload URL failed: ${e.message}" }
throw codeScanServerException("CreateUploadUrlException: " + e.message?.let { it } ?: message("codewhisperer.codescan.run_scan_error_telemetry"))
}

private fun getUploadIntent(scope: CodeWhispererConstants.CodeAnalysisScope): UploadIntent = when (scope) {
Expand Down Expand Up @@ -317,8 +318,8 @@ class CodeWhispererCodeScanSession(val sessionContext: CodeScanSessionContext) {
IoUtils.copy(fileToUpload.inputStream(), connection.outputStream)
}
} catch (e: Exception) {
val errorMessage = e.message?.let { it } ?: message("codewhisperer.codescan.run_scan_error_telemetry")
throw uploadArtifactFailedError(errorMessage)
LOG.debug { "Artifact failed to upload in the S3 bucket: ${e.message}" }
throw codeScanServerException("UploadArtifactToS3Exception: " + e.message?.let { it } ?: message("codewhisperer.codescan.run_scan_error_telemetry"))
}
}

Expand All @@ -340,7 +341,7 @@ class CodeWhispererCodeScanSession(val sessionContext: CodeScanSessionContext) {
)
} catch (e: Exception) {
LOG.debug { "Creating security scan failed: ${e.message}" }
throw e
throw codeScanServerException("CreateCodeScanException: " + e.message?.let { it } ?: message("codewhisperer.codescan.run_scan_error_telemetry"))
}
}

Expand All @@ -352,7 +353,7 @@ class CodeWhispererCodeScanSession(val sessionContext: CodeScanSessionContext) {
)
} catch (e: Exception) {
LOG.debug { "Getting security scan failed: ${e.message}" }
throw e
throw codeScanServerException("GetCodeScanException: " + e.message?.let { it } ?: message("codewhisperer.codescan.run_scan_error_telemetry"))
}

fun listCodeScanFindings(jobId: String, nextToken: String?): ListCodeScanFindingsResponse = try {
Expand All @@ -365,7 +366,7 @@ class CodeWhispererCodeScanSession(val sessionContext: CodeScanSessionContext) {
)
} catch (e: Exception) {
LOG.debug { "Listing security scan failed: ${e.message}" }
throw e
throw codeScanServerException("ListCodeScanFindingsException: " + e.message?.let { it } ?: message("codewhisperer.codescan.run_scan_error_telemetry"))
}

fun mapToCodeScanIssues(recommendations: List<String>): List<CodeWhispererCodeScanIssue> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,14 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
@Test
fun `test run() - createCodeScan error`() {
mockClient.stub {
onGeneric { createCodeScan(any(), any()) }.thenThrow(CodeWhispererCodeScanException::class.java)
onGeneric { createCodeScan(any(), any()) }.thenThrow(CodeWhispererCodeScanServerException::class.java)
}

runBlocking {
val codeScanResponse = codeScanSessionSpy.run()
assertThat(codeScanResponse).isInstanceOf<CodeScanResponse.Failure>()
assertThat(codeScanResponse.responseContext.payloadContext).isEqualTo(payloadContext)
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererCodeScanException>()
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererCodeScanServerException>()
}
}

Expand Down Expand Up @@ -361,28 +361,28 @@ class CodeWhispererCodeFileScanTest : CodeWhispererCodeScanTestBase(PythonCodeIn
@Test
fun `test run() - getCodeScan error`() {
mockClient.stub {
onGeneric { getCodeScan(any(), any()) }.thenThrow(CodeWhispererException::class.java)
onGeneric { getCodeScan(any(), any()) }.thenThrow(CodeWhispererCodeScanServerException::class.java)
}

runBlocking {
val codeScanResponse = codeScanSessionSpy.run()
assertThat(codeScanResponse).isInstanceOf<CodeScanResponse.Failure>()
assertThat(codeScanResponse.responseContext.payloadContext).isEqualTo(payloadContext)
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererException>()
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererCodeScanServerException>()
}
}

@Test
fun `test run() - listCodeScanFindings error`() {
mockClient.stub {
onGeneric { listCodeScanFindings(any(), any()) }.thenThrow(CodeWhispererException::class.java)
onGeneric { listCodeScanFindings(any(), any()) }.thenThrow(CodeWhispererCodeScanServerException::class.java)
}

runBlocking {
val codeScanResponse = codeScanSessionSpy.run()
assertThat(codeScanResponse).isInstanceOf<CodeScanResponse.Failure>()
assertThat(codeScanResponse.responseContext.payloadContext).isEqualTo(payloadContext)
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererException>()
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererCodeScanServerException>()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ class CodeWhispererCodeScanTest : CodeWhispererCodeScanTestBase(PythonCodeInsigh
@Test
fun `test run() - createCodeScan error`() {
mockClient.stub {
onGeneric { createCodeScan(any(), any()) }.thenThrow(CodeWhispererCodeScanException::class.java)
onGeneric { createCodeScan(any(), any()) }.thenThrow(CodeWhispererCodeScanServerException::class.java)
}

runBlocking {
val codeScanResponse = codeScanSessionSpy.run()
assertThat(codeScanResponse).isInstanceOf<CodeScanResponse.Failure>()
assertThat(codeScanResponse.responseContext.payloadContext).isEqualTo(payloadContext)
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererCodeScanException>()
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererCodeScanServerException>()
}
}

Expand Down Expand Up @@ -272,28 +272,28 @@ class CodeWhispererCodeScanTest : CodeWhispererCodeScanTestBase(PythonCodeInsigh
@Test
fun `test run() - getCodeScan error`() {
mockClient.stub {
onGeneric { getCodeScan(any(), any()) }.thenThrow(CodeWhispererException::class.java)
onGeneric { getCodeScan(any(), any()) }.thenThrow(CodeWhispererCodeScanServerException::class.java)
}

runBlocking {
val codeScanResponse = codeScanSessionSpy.run()
assertThat(codeScanResponse).isInstanceOf<CodeScanResponse.Failure>()
assertThat(codeScanResponse.responseContext.payloadContext).isEqualTo(payloadContext)
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererException>()
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererCodeScanServerException>()
}
}

@Test
fun `test run() - listCodeScanFindings error`() {
mockClient.stub {
onGeneric { listCodeScanFindings(any(), any()) }.thenThrow(CodeWhispererException::class.java)
onGeneric { listCodeScanFindings(any(), any()) }.thenThrow(CodeWhispererCodeScanServerException::class.java)
}

runBlocking {
val codeScanResponse = codeScanSessionSpy.run()
assertThat(codeScanResponse).isInstanceOf<CodeScanResponse.Failure>()
assertThat(codeScanResponse.responseContext.payloadContext).isEqualTo(payloadContext)
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererException>()
assertThat((codeScanResponse as CodeScanResponse.Failure).failureReason).isInstanceOf<CodeWhispererCodeScanServerException>()
}
}

Expand Down
Loading