-
Notifications
You must be signed in to change notification settings - Fork 260
Internal Bugfix - add telemetry guards for string overflow #4538
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
0ad89ad
be773f4
648f2a4
9ed9c3f
d9d51b1
b0656e9
b710532
a0baad9
db256c7
5ce8f32
ed9e067
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package software.aws.toolkits.jetbrains.services.codemodernizer.model | ||
|
||
import com.fasterxml.jackson.databind.module.SimpleModule | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
|
||
const val CODETRANSFORM_METADATA_MAX_STRINGIFIED_LENGTH = 65536 | ||
|
||
data class CodeTransformTelemetryMetadata( | ||
var dependencyVersionSelected: String? = null, | ||
var cancelledFromChat: Boolean = false, | ||
) { | ||
private val propertyValues = listOf( | ||
"dependencyVersionSelected" to dependencyVersionSelected, | ||
"cancelledFromChat" to cancelledFromChat | ||
) | ||
|
||
operator fun iterator(): Iterator<Pair<String, Any?>> = propertyValues.iterator() | ||
|
||
fun toJsonString(): String { | ||
var trimmedJsonString = trimJsonString(CODETRANSFORM_METADATA_MAX_STRINGIFIED_LENGTH) | ||
return trimmedJsonString | ||
} | ||
|
||
fun resetDefaults() { | ||
dependencyVersionSelected = null | ||
cancelledFromChat = false | ||
} | ||
|
||
/** | ||
* @description We have a truncation function for all fields to be less than 1000 characters. | ||
* If this fails, we try to completely remove fields to limit the size sent to backend to prevent | ||
* an overflow when submitting data. | ||
*/ | ||
private fun trimJsonString(maxLength: Int): String { | ||
val objectMapper = jacksonObjectMapper() | ||
objectMapper.registerModule( | ||
SimpleModule().addSerializer(String::class.java, MaxLengthTelemetryStringSerializer()) | ||
) | ||
val jsonString = objectMapper.writeValueAsString(this) | ||
if (jsonString.length <= maxLength) { | ||
return jsonString | ||
} | ||
|
||
val trimmedPropertyValues = mutableListOf<Pair<String, Any?>>() | ||
var currentLength = 0 | ||
for ((key, value) in propertyValues) { | ||
val elementLength = key.length + value.toString().length + 5 // add 5 for quotes and comma around key-value pairs | ||
if (currentLength + elementLength <= maxLength) { | ||
trimmedPropertyValues.add(Pair(key, value)) | ||
currentLength += elementLength | ||
} | ||
// else we omit the key/value pair as a way of "trimming" the object that is too large | ||
} | ||
|
||
return objectMapper.writeValueAsString(trimmedPropertyValues.toMap()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package software.aws.toolkits.jetbrains.services.codemodernizer.model | ||
|
||
object CodeTransformTelemetryMetadataSingleton { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like the modularization for testing, but now your project service is sharing state across all instances, which is probably not what you want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only have one instance at a time right now and we do want to share this across any reference to it. For simplicity we are not managing the state of the object within any of our manager classes. If we move to a multi instance service, we can make this a variable with session or sessionContext. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the interest of avoiding global state that isn't managed by the platform then, can you convert this into a light service? your test will need to have an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I'll check out adding these changes to convert to:
|
||
private val instance = CodeTransformTelemetryMetadata() | ||
|
||
fun getInstance() = instance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getInstance() in this project implies the platform "service" construct, rather than a singleton There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you saying the coding style for this repo of If its service level, then it would fit because this is a single instance shared across our service preventing duplicates. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. service as in the intellij platform service construct |
||
|
||
fun setDependencyVersionSelected(version: String?) { | ||
instance.dependencyVersionSelected = version | ||
} | ||
|
||
fun setCancelledFromChat(cancelled: Boolean) { | ||
instance.cancelledFromChat = cancelled | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package software.aws.toolkits.jetbrains.services.codemodernizer.model | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.databind.JsonSerializer | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
|
||
const val MAX_SERIALIZABLE_STRING_LENGTH = 1000 | ||
class MaxLengthTelemetryStringSerializer : JsonSerializer<String>() { | ||
override fun serialize(value: String, gen: JsonGenerator, provider: SerializerProvider) { | ||
val truncatedValue = if (value.length > MAX_SERIALIZABLE_STRING_LENGTH) { | ||
value.substring(0, MAX_SERIALIZABLE_STRING_LENGTH) | ||
} else { | ||
value | ||
} | ||
gen.writeString(truncatedValue) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package software.aws.toolkits.jetbrains.services.codemodernizer.model | ||
|
||
import junit.framework.TestCase.assertEquals | ||
import org.junit.Test | ||
|
||
open class CodeTransformTelemetryMetadataSingletonTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't need to be |
||
@Test | ||
fun `CodeTransformTelemetryMetadata will set values and resets defaults properly`() { | ||
CodeTransformTelemetryMetadataSingleton.setDependencyVersionSelected("1.2.3") | ||
CodeTransformTelemetryMetadataSingleton.setCancelledFromChat(true) | ||
assertEquals(CodeTransformTelemetryMetadataSingleton.getInstance().dependencyVersionSelected, "1.2.3") | ||
assertEquals(CodeTransformTelemetryMetadataSingleton.getInstance().cancelledFromChat, true) | ||
|
||
// check reset defaults works | ||
CodeTransformTelemetryMetadataSingleton.getInstance().resetDefaults() | ||
assertEquals(CodeTransformTelemetryMetadataSingleton.getInstance().dependencyVersionSelected, null) | ||
assertEquals(CodeTransformTelemetryMetadataSingleton.getInstance().cancelledFromChat, false) | ||
} | ||
|
||
@Test | ||
fun `CodeTransformTelemetryMetadataSingletonTest toJsonString() will serialize object correctly`() { | ||
CodeTransformTelemetryMetadataSingleton.setDependencyVersionSelected("1.2.3") | ||
CodeTransformTelemetryMetadataSingleton.setCancelledFromChat(true) | ||
val expectedJsonString = """{"dependencyVersionSelected":"1.2.3","cancelledFromChat":true}""" | ||
assertEquals(expectedJsonString, CodeTransformTelemetryMetadataSingleton.getInstance().toJsonString()) | ||
} | ||
|
||
@Test | ||
fun `CodeTransformTelemetryMetadataSingletonTest trimJsonString() trims single field JSON string to specified length`() { | ||
val longString = "a".repeat(CODETRANSFORM_METADATA_MAX_STRINGIFIED_LENGTH) | ||
CodeTransformTelemetryMetadataSingleton.setDependencyVersionSelected(longString) | ||
CodeTransformTelemetryMetadataSingleton.setCancelledFromChat(true) | ||
|
||
val expectedTrimmedJsonString = """{"dependencyVersionSelected":"${"a".repeat(MAX_SERIALIZABLE_STRING_LENGTH)}","cancelledFromChat":true}""" | ||
assertEquals(expectedTrimmedJsonString, CodeTransformTelemetryMetadataSingleton.getInstance().toJsonString()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't need to fix, but in future avoid unnecessary diffs