Skip to content
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

feat: add log masking feature #138

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import java.io.IOException
* @param maxBodyLogSize The maximum size of the request/response body to log. Defaults to 1MB.
*/
class LoggingInterceptor(
private val logger: LoggerDecorator,
private val maxBodyLogSize: Long = DEFAULT_MAX_BODY_SIZE,
) : Interceptor {
@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.slf4j.Logger

class LoggerDecorator(
private val logger: Logger,
private val masker: (String) -> String = { it },
) : Logger by logger {
override fun info(msg: String) = logger.info(decorate(msg))

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.sdk.core.logging.masking

import com.ebay.ejmask.api.MaskingPattern

internal class LogMasker(
globalMaskedFields: Set<String> = emptySet(),
pathMaskedFields: Set<List<String>> = emptySet(),
) : (String) -> String {
private val patterns: List<MaskingPattern> =
MaskingPatternBuilder()
.apply {
globalFields(globalMaskedFields)
pathFields(pathMaskedFields)
}.build()

/**
* Applies all masking patterns to the input string.
*
* @param input The input string to be masked.
* @return The masked string.
*/
override fun invoke(input: String): String {
var masked = input

patterns.forEach { pattern: MaskingPattern ->
masked = pattern.replaceAll(masked)
}

return masked
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.sdk.core.logging.masking

import com.ebay.ejmask.api.MaskingPattern

/**
* Builder class for creating masking patterns.
*/
internal class MaskingPatternBuilder {
private var globalFields: Set<String> = setOf()
private var pathFields: Set<List<String>> = setOf()

/**
* Adds global fields to be masked.
*
* @param fields Vararg of field names to be masked globally.
* @return The current instance of MaskingPatternBuilder.
*/
fun globalFields(fields: Set<String>): MaskingPatternBuilder =
apply {
globalFields += fields.toSortedSet()
}

/**
* Adds path-specific fields to be masked.
* This method updates the `globalFields` set by adding the provided field names.
*
* Takes the last two elements of each list to be used as the path and ignores the rest. Temporary solution
* until ejmask add support for unlimited-fields path-based masking.
*
* @param paths Vararg of lists of field names to be masked by path.
* @return The current instance of MaskingPatternBuilder.
*/
fun pathFields(paths: Set<List<String>>) =
apply {
pathFields += paths.map { it.takeLast(2) }
}

/**
* Builds the list of MaskingPattern based on the added global and path fields.
*
* @return A list of MaskingPattern.
*/
fun build(): List<MaskingPattern> =
buildList {
/**
* Builds masking patterns for global fields.
*
* @return A list of MaskingPattern for global fields.
*/
fun buildGlobalFieldsMaskingPattern(): List<MaskingPattern> =
if (globalFields.isEmpty()) {
emptyList()
} else {
val patternGenerator = CustomJsonFullValuePatternBuilder()
listOf(
MaskingPattern(
0,
patternGenerator.buildPattern(0, *globalFields.toTypedArray()),
patternGenerator.buildReplacement(0, *globalFields.toTypedArray()),
),
)
}

/**
* Builds masking patterns for path-specific fields.
*
* @return A list of MaskingPattern for path-specific fields.
*/
fun buildPathFieldsMaskingPattern(): List<MaskingPattern> =
buildList {
pathFields.forEachIndexed { index, path ->
CustomJsonRelativeFieldPatternBuilder().also { patternGenerator ->
add(
MaskingPattern(
index + 1,
patternGenerator.buildPattern(1, *path.toTypedArray()),
patternGenerator.buildReplacement(1, *path.toTypedArray()),
),
)
}
}
}

addAll(buildGlobalFieldsMaskingPattern())
addAll(buildPathFieldsMaskingPattern())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.sdk.core.logging.masking

import com.ebay.ejmask.extenstion.builder.json.JsonFullValuePatternBuilder
import com.ebay.ejmask.extenstion.builder.json.JsonRelativeFieldPatternBuilder
import com.expediagroup.sdk.core.logging.common.Constant.OMITTED

/**
* Custom implementation of JsonFieldPatternBuilder for building JSON field patterns.
*/
internal class CustomJsonFullValuePatternBuilder : JsonFullValuePatternBuilder() {
companion object {
@JvmStatic
val REPLACEMENT_TEMPLATE = "\"$1$2$OMITTED\""
}

/**
* Builds the replacement string for the given field names.
* Ignores the visibleCharacters parameter.
*
* @param visibleCharacters Number of visible characters. Value is ignored.
* @param fieldNames Vararg of field names.
* @return The replacement string.
*/
override fun buildReplacement(
visibleCharacters: Int,
vararg fieldNames: String?,
): String = REPLACEMENT_TEMPLATE
}

internal class CustomJsonRelativeFieldPatternBuilder : JsonRelativeFieldPatternBuilder() {
companion object {
@JvmStatic
val REPLACEMENT_TEMPLATE = "$1$OMITTED$3"
}

/**
* Builds the replacement string for the given field names.
* Ignores the visibleCharacters parameter.
*
* @param visibleCharacters Number of visible characters. Value is ignored.
* @param fieldNames Vararg of field names.
* @return The replacement string.
*/
override fun buildReplacement(
visibleCharacters: Int,
vararg fieldNames: String?,
): String = REPLACEMENT_TEMPLATE
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.expediagroup.sdk.core.client.Transport
import com.expediagroup.sdk.core.common.RequestHeadersInterceptor
import com.expediagroup.sdk.core.interceptor.Interceptor
import com.expediagroup.sdk.core.logging.LoggingInterceptor
import com.expediagroup.sdk.core.logging.common.LoggerDecorator
import com.expediagroup.sdk.core.okhttp.BaseOkHttpClient
import com.expediagroup.sdk.core.okhttp.OkHttpTransport
import com.expediagroup.sdk.lodgingconnectivity.configuration.ApiEndpoint
Expand All @@ -24,11 +25,12 @@ internal fun getHttpTransport(configuration: ClientConfiguration): Transport =
class RequestExecutor(
configuration: ClientConfiguration,
apiEndpoint: ApiEndpoint,
logger: LoggerDecorator,
) : AbstractRequestExecutor(getHttpTransport(configuration)) {
override val interceptors: List<Interceptor> =
listOf(
RequestHeadersInterceptor(),
LoggingInterceptor(),
LoggingInterceptor(logger),
BearerAuthenticationInterceptor(
BearerAuthenticationManager(
requestExecutor = this,
Expand Down
Loading
Loading