Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/main/java/com/github/ai/kpdiff/domain/MainInteractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class MainInteractor(
} else {
val options = DiffFormatterOptions(
isColorEnabled = !parsedArgs.isNoColoredOutput,
isVerboseOutput = parsedArgs.isVerboseOutput
isVerboseOutput = parsedArgs.isVerboseOutput,
isReveal = parsedArgs.isReveal
)

printDiffUseCase.printDiff(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class ArgumentParser(
OptionalArgument.HELP -> parseHelp(values)
OptionalArgument.VERSION -> parseVersion(values)
OptionalArgument.VERBOSE -> parseVerbose(values)
OptionalArgument.REVEAL -> parseReveal(values)
OptionalArgument.KEY_FILE_A -> parseLeftKeyPath(queue.poll(), values)
OptionalArgument.KEY_FILE_B -> parseRightKeyPath(queue.poll(), values)
OptionalArgument.PASSWORD -> parsePassword(queue.poll(), values)
Expand Down Expand Up @@ -152,6 +153,11 @@ class ArgumentParser(
return Either.Right(Unit)
}

private fun parseReveal(arguments: MutableArguments): Either<Unit> {
arguments.isReveal = true
return Either.Right(Unit)
}

private fun parseLeftKeyPath(
path: String?,
arguments: MutableArguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fun MutableArguments.toArguments(): Arguments {
isNoColoredOutput = isNoColoredOutput,
isPrintHelp = isPrintHelp,
isPrintVersion = isPrintVersion,
isVerboseOutput = isVerboseOutput
isVerboseOutput = isVerboseOutput,
isReveal = isReveal
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ enum class OptionalArgument(
PASSWORD_B(shortName = null, fullName = "password-b"),
VERBOSE(shortName = "v", fullName = "verbose"),
DIFF_BY(shortName = "d", fullName = "diff-by"),
OUTPUT_FILE(shortName = "f", fullName = "output-file");
OUTPUT_FILE(shortName = "f", fullName = "output-file"),
REVEAL(shortName = null, fullName = "reveal");

val cliShortName: String? = if (shortName != null) {
"-$shortName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ class DiffFormatterImpl(
val formatter = formatterProvider.getFormatter(entity::class)
as EntityFormatter<DatabaseEntity>

val formattedEvent = event.maskProtectedFieldsUnlessRevealed(options)

return terminalOutputFormatter.format(
line = formatter.format(event as DiffEvent<DatabaseEntity>, indent),
line = formatter.format(formattedEvent as DiffEvent<DatabaseEntity>, indent),
color = if (options.isColorEnabled) {
event.getColor()
} else {
Expand All @@ -204,6 +206,46 @@ class DiffFormatterImpl(
)
}

@Suppress("UNCHECKED_CAST")
private fun <T : DatabaseEntity> DiffEvent<T>.maskProtectedFieldsUnlessRevealed(
options: DiffFormatterOptions
): DiffEvent<T> {
if (options.isReveal) {
return this
}

return when (this) {
is DiffEvent.Insert -> DiffEvent.Insert(
parentUuid = parentUuid,
entity = entity.maskProtectedField()
) as DiffEvent<T>

is DiffEvent.Delete -> DiffEvent.Delete(
parentUuid = parentUuid,
entity = entity.maskProtectedField()
) as DiffEvent<T>

is DiffEvent.Update -> DiffEvent.Update(
oldParentUuid = oldParentUuid,
newParentUuid = newParentUuid,
oldEntity = oldEntity.maskProtectedField(),
newEntity = newEntity.maskProtectedField()
) as DiffEvent<T>
}
}

private fun <T : DatabaseEntity> T.maskProtectedField(): T {
if (this !is Field<*> || name != Fields.FIELD_PASSWORD) {
return this
}

return Field(
uuid = uuid,
name = name,
value = PROTECTED_FIELD_PLACEHOLDER
) as T
}

private fun shouldPrintAdditionalInformation(
event: DiffEvent<*>,
options: DiffFormatterOptions
Expand Down Expand Up @@ -276,6 +318,8 @@ class DiffFormatterImpl(
companion object {
private const val INDENT = " "

private const val PROTECTED_FIELD_PLACEHOLDER = "***"

private val DEFAULT_PROPERTIES = setOf(
Fields.FIELD_TITLE,
Fields.FIELD_USERNAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class PrintHelpUseCase(
-d, --diff-by Type of differ, default is 'path'. Possible values:
path - produces more accurate diff, considers entries identical if they have identical content but UUID differs
uuid - considers entries identical if they have identical content and UUID
--reveal Reveal protected field values such as passwords
-v, --verbose Print verbose output (entry fields will be printed)
-V, --version Print version
-h, --help Print help information
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/github/ai/kpdiff/entity/Arguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ data class Arguments(
val isNoColoredOutput: Boolean,
val isPrintHelp: Boolean,
val isPrintVersion: Boolean,
val isVerboseOutput: Boolean
val isVerboseOutput: Boolean,
val isReveal: Boolean
) {

companion object {
Expand All @@ -42,7 +43,8 @@ data class Arguments(
isNoColoredOutput = false,
isPrintHelp = false,
isPrintVersion = false,
isVerboseOutput = false
isVerboseOutput = false,
isReveal = false
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package com.github.ai.kpdiff.entity

data class DiffFormatterOptions(
val isColorEnabled: Boolean = true,
val isVerboseOutput: Boolean = false
)
val isVerboseOutput: Boolean = false,
val isReveal: Boolean = false
)
5 changes: 3 additions & 2 deletions src/main/java/com/github/ai/kpdiff/entity/MutableArguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ data class MutableArguments(
var isNoColoredOutput: Boolean = false,
var isPrintHelp: Boolean = false,
var isPrintVersion: Boolean = false,
var isVerboseOutput: Boolean = false
)
var isVerboseOutput: Boolean = false,
var isReveal: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ class MainInteractorTest {
isNoColoredOutput = isNoColoredOutput,
isPrintHelp = isPrintHelp,
isPrintVersion = isPrintVersion,
isVerboseOutput = false
isVerboseOutput = false,
isReveal = false
)

private fun newInteractor(): MainInteractor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ internal class ArgumentParserTest {
}
}


@Test
fun `parse should return arguments if --reveal is specified`() {
assertParsedSuccessfully(
arguments = arrayOf(
LEFT_FILE_PATH,
RIGHT_FILE_PATH,
OptionalArgument.REVEAL.cliFullName
),
expectedArguments = newArguments(
LEFT_FILE_PATH,
RIGHT_FILE_PATH,
isReveal = true
)
)
}

@Test
fun `parse should return arguments if --one-password is specified`() {
listOf(
Expand Down Expand Up @@ -674,7 +691,8 @@ internal class ArgumentParserTest {
isNoColoredOutput: Boolean = false,
isPrintHelp: Boolean = false,
isPrintVersion: Boolean = false,
isVerboseOutput: Boolean = false
isVerboseOutput: Boolean = false,
isReveal: Boolean = false
): Arguments {
return Arguments(
leftPath = leftPath,
Expand All @@ -694,7 +712,8 @@ internal class ArgumentParserTest {
isNoColoredOutput = isNoColoredOutput,
isPrintHelp = isPrintHelp,
isPrintVersion = isPrintVersion,
isVerboseOutput = isVerboseOutput
isVerboseOutput = isVerboseOutput,
isReveal = isReveal
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class DiffFormatterImplTest {
fun `format should return diff between databases`() {
listOf(
Pair(defaultOptions(), newPathDiffer()) to OUTPUT,
Pair(verboseOptions(), newPathDiffer()) to VERBOSE_OUTPUT
Pair(verboseOptions(), newPathDiffer()) to VERBOSE_OUTPUT,
Pair(revealOptions(), newPathDiffer()) to REVEALED_VERBOSE_OUTPUT
).forEach { (data, expected) ->
// arrange
val (options, differ) = data
Expand Down Expand Up @@ -53,6 +54,13 @@ class DiffFormatterImplTest {
private fun verboseOptions(): DiffFormatterOptions =
DiffFormatterOptions(isColorEnabled = false, isVerboseOutput = true)

private fun revealOptions(): DiffFormatterOptions =
DiffFormatterOptions(
isColorEnabled = false,
isVerboseOutput = true,
isReveal = true
)

companion object {
private val OUTPUT = """
~ Group 'Database'
Expand Down Expand Up @@ -97,13 +105,13 @@ class DiffFormatterImplTest {
- Entry 'Github.com'
- Field 'Title': 'Github.com'
- Field 'UserName': 'john.doe@example.com'
- Field 'Password': 'abc123'
- Field 'Password': '***'
- Field 'URL': 'https://github.com'
- Field 'Notes': ''
+ Entry 'Gitlab'
+ Field 'Title': 'Gitlab'
+ Field 'UserName': 'john.doe@example.com'
+ Field 'Password': 'abc123'
+ Field 'Password': '***'
+ Field 'URL': 'https://gitlab.com'
+ Field 'Notes': ''
~ Group 'Database'
Expand All @@ -116,11 +124,15 @@ class DiffFormatterImplTest {
+ Entry 'Facebook'
+ Field 'Title': 'Facebook'
+ Field 'UserName': 'john.doe@example.com'
+ Field 'Password': 'abc123'
+ Field 'Password': '***'
+ Field 'URL': 'https://facebook.com'
+ Field 'Notes': ''
""".transformOutput()

private val REVEALED_VERBOSE_OUTPUT = VERBOSE_OUTPUT.map { line ->
line.replace("Field 'Password': '***'", "Field 'Password': 'abc123'")
}

private fun String.transformOutput(): List<String> {
return this
.trimIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ class GetKeysUseCaseTest {
isAskRightPassword = isAskRightPassword,
isPrintHelp = false,
isPrintVersion = false,
isVerboseOutput = false
isVerboseOutput = false,
isReveal = false
)
}
}
Loading