-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webflux): implement remote IP header appender for command reques…
…ts (#1112) * feat(webflux): implement remote IP header appender for command requests - Add CommandRequestRemoteIpHeaderAppender to handle X-Forwarded-For header - Implement logic to resolve remote IP from request headers - Update WebFluxAutoConfiguration to include new appender - Add unit tests for CommandRequestRemoteIpHeaderAppender * test(wow-webflux): add tests for CommandRequestExtendHeaderAppender and DefaultCommandMessageParser - Add unit tests for CommandRequestExtendHeaderAppender - Add test cases for CommandRequestUserAgentHeaderAppender - Remove unnecessary test file for DefaultCommandMessageParser * refactor(wow-webflux): optimize remote IP resolution and logging - Remove unnecessary logging for multiple X-Forwarded-For headers - Simplify code by using firstHeader instead of header list - Add test case for empty X-Forwarded-For header - Update existing test cases to use new header resolution approach * test(webflux): add test case for disabling command request appender - Add test case to verify that CommandRequestUserAgentHeaderAppender and CommandRequestRemote
- Loading branch information
Showing
13 changed files
with
306 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
32 changes: 32 additions & 0 deletions
32
...n/kotlin/me/ahoo/wow/webflux/route/command/appender/CommandRequestExtendHeaderAppender.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,32 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.wow.webflux.route.command.appender | ||
|
||
import me.ahoo.wow.api.messaging.Header | ||
import me.ahoo.wow.openapi.command.CommandRequestHeaders | ||
import org.springframework.web.reactive.function.server.ServerRequest | ||
|
||
object CommandRequestExtendHeaderAppender : CommandRequestHeaderAppender { | ||
override fun append(request: ServerRequest, header: Header) { | ||
val extendedHeaders = request.headers().asHttpHeaders() | ||
.filter { (key, _) -> key.startsWith(CommandRequestHeaders.COMMAND_HEADER_X_PREFIX) } | ||
.map { (key, value) -> | ||
key.substring(CommandRequestHeaders.COMMAND_HEADER_X_PREFIX.length) to value.firstOrNull<String>().orEmpty() | ||
}.toMap<String, String>() | ||
if (extendedHeaders.isEmpty()) { | ||
return | ||
} | ||
header.with(extendedHeaders) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...rc/main/kotlin/me/ahoo/wow/webflux/route/command/appender/CommandRequestHeaderAppender.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,21 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.wow.webflux.route.command.appender | ||
|
||
import me.ahoo.wow.api.messaging.Header | ||
import org.springframework.web.reactive.function.server.ServerRequest | ||
|
||
interface CommandRequestHeaderAppender { | ||
fun append(request: ServerRequest, header: Header) | ||
} |
51 changes: 51 additions & 0 deletions
51
...kotlin/me/ahoo/wow/webflux/route/command/appender/CommandRequestRemoteIpHeaderAppender.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,51 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.wow.webflux.route.command.appender | ||
|
||
import me.ahoo.wow.api.messaging.Header | ||
import me.ahoo.wow.messaging.propagation.CommandRequestHeaderPropagator.Companion.withRemoteIp | ||
import org.springframework.web.reactive.function.server.ServerRequest | ||
import kotlin.jvm.optionals.getOrNull | ||
|
||
object CommandRequestRemoteIpHeaderAppender : CommandRequestHeaderAppender { | ||
const val X_FORWARDED_FOR = "X-Forwarded-For" | ||
const val DELIMITER = ',' | ||
|
||
override fun append(request: ServerRequest, header: Header) { | ||
resolveRemoteIp(request)?.let { | ||
header.withRemoteIp(it) | ||
} | ||
} | ||
|
||
private fun getRemoteIp(request: ServerRequest): String? { | ||
return request.remoteAddress().getOrNull()?.hostName | ||
} | ||
|
||
private fun resolveRemoteIp(request: ServerRequest): String? { | ||
val xForwardedHeaderValue = request.headers().firstHeader(X_FORWARDED_FOR) | ||
if (xForwardedHeaderValue.isNullOrBlank()) { | ||
return getRemoteIp(request) | ||
} | ||
|
||
val xForwardedValues = xForwardedHeaderValue | ||
.split(DELIMITER) | ||
.filter { it.isNotBlank() } | ||
.reversed() | ||
if (xForwardedValues.isEmpty()) { | ||
return getRemoteIp(request) | ||
} | ||
val index = xForwardedValues.size.coerceAtMost(Int.MAX_VALUE) - 1 | ||
return xForwardedValues[index] | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,16 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.wow.webflux.route.command | ||
|
||
import io.mockk.every | ||
|
@@ -11,8 +24,9 @@ import me.ahoo.wow.openapi.command.CommandRequestHeaders | |
import me.ahoo.wow.serialization.MessageRecords | ||
import me.ahoo.wow.tck.mock.MOCK_AGGREGATE_METADATA | ||
import me.ahoo.wow.tck.mock.MockCreateAggregate | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.* | ||
import me.ahoo.wow.webflux.route.command.appender.CommandRequestExtendHeaderAppender | ||
import org.hamcrest.CoreMatchers | ||
import org.hamcrest.MatcherAssert | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.util.MultiValueMap | ||
|
@@ -90,7 +104,7 @@ class DefaultCommandMessageParserTest { | |
request | ||
).test() | ||
.consumeNextWith { | ||
assertThat(it.header[headerKey], equalTo(value)) | ||
MatcherAssert.assertThat(it.header[headerKey], CoreMatchers.equalTo(value)) | ||
} | ||
.verifyComplete() | ||
} | ||
|
56 changes: 56 additions & 0 deletions
56
...tlin/me/ahoo/wow/webflux/route/command/appender/CommandRequestExtendHeaderAppenderTest.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,56 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.wow.webflux.route.command.appender | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
import me.ahoo.wow.messaging.DefaultHeader | ||
import me.ahoo.wow.openapi.command.CommandRequestHeaders | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.* | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.util.MultiValueMap | ||
import org.springframework.web.reactive.function.server.ServerRequest | ||
|
||
class CommandRequestExtendHeaderAppenderTest { | ||
@Test | ||
fun append() { | ||
val headerKey = "app" | ||
val key = CommandRequestHeaders.COMMAND_HEADER_X_PREFIX + headerKey | ||
val value = "oms" | ||
val request = mockk<ServerRequest> { | ||
every { headers().asHttpHeaders() } returns HttpHeaders( | ||
MultiValueMap.fromSingleValue<String, String>( | ||
mapOf( | ||
key to value | ||
) | ||
) | ||
) | ||
} | ||
val commandHeader = DefaultHeader.empty() | ||
CommandRequestExtendHeaderAppender.append(request, commandHeader) | ||
assertThat(commandHeader[headerKey], equalTo(value)) | ||
} | ||
|
||
@Test | ||
fun appendEmpty() { | ||
val request = mockk<ServerRequest> { | ||
every { headers().asHttpHeaders() } returns HttpHeaders() | ||
} | ||
val commandHeader = DefaultHeader.empty() | ||
CommandRequestExtendHeaderAppender.append(request, commandHeader) | ||
assertThat(commandHeader.isEmpty(), equalTo(true)) | ||
} | ||
} |
Oops, something went wrong.