From fc44a4c7e4a9ba5778db757e3107697529380ede Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Wed, 20 May 2020 14:29:54 +0200 Subject: [PATCH 1/3] Deprecate Remote-Address header Migration note is already part of #3173 --- .../java/akka/http/javadsl/model/headers/RemoteAddress.java | 3 +++ akka-http-core/src/main/resources/reference.conf | 1 + .../akka/http/impl/engine/server/HttpServerBluePrint.scala | 2 ++ .../scala/akka/http/javadsl/settings/ServerSettings.scala | 5 +++++ akka-http-core/src/main/scala/akka/http/scaladsl/Http.scala | 2 ++ .../scala/akka/http/scaladsl/model/headers/headers.scala | 2 ++ .../scala/akka/http/scaladsl/settings/ServerSettings.scala | 1 + .../scala/akka/http/impl/engine/http2/RequestParsing.scala | 2 ++ 8 files changed, 18 insertions(+) diff --git a/akka-http-core/src/main/java/akka/http/javadsl/model/headers/RemoteAddress.java b/akka-http-core/src/main/java/akka/http/javadsl/model/headers/RemoteAddress.java index cbcf5ef3409..bb19c4bdf79 100644 --- a/akka-http-core/src/main/java/akka/http/javadsl/model/headers/RemoteAddress.java +++ b/akka-http-core/src/main/java/akka/http/javadsl/model/headers/RemoteAddress.java @@ -7,7 +7,10 @@ /** * Model for the `Remote-Address` header. * Custom header we use for optionally transporting the peer's IP in an HTTP header. + * + * Deprecated since 10.2.0: use the remote-header-attribute instead. */ +@Deprecated public abstract class RemoteAddress extends akka.http.scaladsl.model.HttpHeader { public abstract akka.http.javadsl.model.RemoteAddress address(); diff --git a/akka-http-core/src/main/resources/reference.conf b/akka-http-core/src/main/resources/reference.conf index 1d089c985a0..d0b5b25af9c 100644 --- a/akka-http-core/src/main/resources/reference.conf +++ b/akka-http-core/src/main/resources/reference.conf @@ -99,6 +99,7 @@ akka.http { # Enables/disables the addition of a `Remote-Address` header # holding the clients (remote) IP address. + # Deprecated since 10.2.0: please use `remote-address-attribute` instead. remote-address-header = off # Enables/disables the addition of a remote-address attribute in HttpRequest diff --git a/akka-http-core/src/main/scala/akka/http/impl/engine/server/HttpServerBluePrint.scala b/akka-http-core/src/main/scala/akka/http/impl/engine/server/HttpServerBluePrint.scala index e4fcbb06ee5..419b8af56df 100644 --- a/akka-http-core/src/main/scala/akka/http/impl/engine/server/HttpServerBluePrint.scala +++ b/akka-http-core/src/main/scala/akka/http/impl/engine/server/HttpServerBluePrint.scala @@ -34,6 +34,7 @@ import akka.http.scaladsl.model.headers.`Timeout-Access` import akka.http.javadsl.model import akka.http.scaladsl.model._ import akka.http.impl.util.LogByteStringTools._ +import com.github.ghik.silencer.silent import scala.util.Failure @@ -125,6 +126,7 @@ private[http] object HttpServerBluePrint { case RequestStart(method, uri, protocol, hdrs, entityCreator, _, _) => val effectiveMethod = if (method == HttpMethods.HEAD && settings.transparentHeadRequests) HttpMethods.GET else method + @silent("use remote-address-attribute instead") val effectiveHeaders = if (settings.remoteAddressHeader && remoteAddressOpt.isDefined) headers.`Remote-Address`(RemoteAddress(remoteAddressOpt.get)) +: hdrs diff --git a/akka-http-core/src/main/scala/akka/http/javadsl/settings/ServerSettings.scala b/akka-http-core/src/main/scala/akka/http/javadsl/settings/ServerSettings.scala index f72c7a36be2..ceecc68eb51 100644 --- a/akka-http-core/src/main/scala/akka/http/javadsl/settings/ServerSettings.scala +++ b/akka-http-core/src/main/scala/akka/http/javadsl/settings/ServerSettings.scala @@ -30,6 +30,11 @@ import scala.concurrent.duration.{ Duration, FiniteDuration } def getTimeouts: ServerSettings.Timeouts def getMaxConnections: Int def getPipeliningLimit: Int + + /** + * Deprecated since 10.2.0, use remoteAddressAttribute instead + */ + @Deprecated def getRemoteAddressHeader: Boolean def getRemoteAddressAttribute: Boolean def getRawRequestUriHeader: Boolean diff --git a/akka-http-core/src/main/scala/akka/http/scaladsl/Http.scala b/akka-http-core/src/main/scala/akka/http/scaladsl/Http.scala index 268f7506b93..c0cbc4434f9 100644 --- a/akka-http-core/src/main/scala/akka/http/scaladsl/Http.scala +++ b/akka-http-core/src/main/scala/akka/http/scaladsl/Http.scala @@ -34,6 +34,7 @@ import akka.stream.TLSProtocol._ import akka.stream.scaladsl._ import akka.util.ByteString import akka.util.ManifestInfo +import com.github.ghik.silencer.silent import com.typesafe.config.Config import com.typesafe.sslconfig.akka._ import com.typesafe.sslconfig.akka.util.AkkaLoggerFactory @@ -1030,6 +1031,7 @@ object Http extends ExtensionId[HttpExt] with ExtensionIdProvider { def createExtension(system: ExtendedActorSystem): HttpExt = new HttpExt(system.settings.config getConfig "akka.http")(system) + @silent("use remote-address-attribute instead") @InternalApi private[akka] def prepareAttributes(settings: ServerSettings, incoming: Tcp.IncomingConnection) = if (settings.remoteAddressHeader || settings.remoteAddressAttribute) HttpAttributes.remoteAddress(incoming.remoteAddress) diff --git a/akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/headers.scala b/akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/headers.scala index dfa0850bd2b..3ad2eae1635 100644 --- a/akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/headers.scala +++ b/akka-http-core/src/main/scala/akka/http/scaladsl/model/headers/headers.scala @@ -759,7 +759,9 @@ final case class `Raw-Request-URI`(uri: String) extends jm.headers.RawRequestURI protected def companion = `Raw-Request-URI` } +@deprecated("use remote-address-attribute instead", since = "10.2.0") object `Remote-Address` extends ModeledCompanion[`Remote-Address`] +@deprecated("use remote-address-attribute instead", since = "10.2.0") final case class `Remote-Address`(address: RemoteAddress) extends jm.headers.RemoteAddress with SyntheticHeader { def renderValue[R <: Rendering](r: R): r.type = r ~~ address protected def companion = `Remote-Address` diff --git a/akka-http-core/src/main/scala/akka/http/scaladsl/settings/ServerSettings.scala b/akka-http-core/src/main/scala/akka/http/scaladsl/settings/ServerSettings.scala index 3a8a0d89e6e..67c5315d3ce 100644 --- a/akka-http-core/src/main/scala/akka/http/scaladsl/settings/ServerSettings.scala +++ b/akka-http-core/src/main/scala/akka/http/scaladsl/settings/ServerSettings.scala @@ -35,6 +35,7 @@ abstract class ServerSettings private[akka] () extends akka.http.javadsl.setting def timeouts: ServerSettings.Timeouts def maxConnections: Int def pipeliningLimit: Int + @deprecated("use remote-address-attribute instead", since = "10.2.0") def remoteAddressHeader: Boolean def remoteAddressAttribute: Boolean def rawRequestUriHeader: Boolean diff --git a/akka-http2-support/src/main/scala/akka/http/impl/engine/http2/RequestParsing.scala b/akka-http2-support/src/main/scala/akka/http/impl/engine/http2/RequestParsing.scala index 137cf203716..6ce0aed0236 100644 --- a/akka-http2-support/src/main/scala/akka/http/impl/engine/http2/RequestParsing.scala +++ b/akka-http2-support/src/main/scala/akka/http/impl/engine/http2/RequestParsing.scala @@ -17,6 +17,7 @@ import akka.http.scaladsl.settings.ServerSettings import akka.stream.Attributes import akka.stream.scaladsl.Source import akka.util.ByteString +import com.github.ghik.silencer.silent import scala.annotation.tailrec import scala.collection.immutable.VectorBuilder @@ -27,6 +28,7 @@ import scala.collection.immutable.VectorBuilder @InternalApi private[http2] object RequestParsing { + @silent("use remote-address-attribute instead") def parseRequest(httpHeaderParser: HttpHeaderParser, serverSettings: ServerSettings, attributes: Attributes): Http2SubStream => HttpRequest = { val remoteAddressHeader: Option[`Remote-Address`] = if (serverSettings.remoteAddressHeader) { From e221f6db559446a5be033caaf892a62d05bd687d Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Wed, 20 May 2020 14:56:57 +0200 Subject: [PATCH 2/3] Don't show deprecated synthetic header in docs --- .../http/scaladsl/server/directives/MiscDirectivesSpec.scala | 3 +++ .../server/directives/MiscDirectivesExamplesSpec.scala | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/MiscDirectivesSpec.scala b/akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/MiscDirectivesSpec.scala index 593a03e5eb8..75249d659c9 100644 --- a/akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/MiscDirectivesSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/MiscDirectivesSpec.scala @@ -12,8 +12,11 @@ import akka.http.scaladsl.model._ import akka.testkit._ import headers._ import java.net.InetAddress + import akka.http.scaladsl.server.util.VarArgsFunction1 +import com.github.ghik.silencer.silent +@silent("use remote-address-attribute instead") class MiscDirectivesSpec extends RoutingSpec { "the extractClientIP directive" should { diff --git a/docs/src/test/scala/docs/http/scaladsl/server/directives/MiscDirectivesExamplesSpec.scala b/docs/src/test/scala/docs/http/scaladsl/server/directives/MiscDirectivesExamplesSpec.scala index e63cf84285e..221adf76c67 100644 --- a/docs/src/test/scala/docs/http/scaladsl/server/directives/MiscDirectivesExamplesSpec.scala +++ b/docs/src/test/scala/docs/http/scaladsl/server/directives/MiscDirectivesExamplesSpec.scala @@ -20,7 +20,7 @@ class MiscDirectivesExamplesSpec extends RoutingSpec with CompileOnlySpec { } // tests: - Get("/").withHeaders(`Remote-Address`(RemoteAddress(InetAddress.getByName("192.168.3.12")))) ~> route ~> check { + Get("/").withHeaders(`X-Forwarded-For`(RemoteAddress(InetAddress.getByName("192.168.3.12")))) ~> route ~> check { responseAs[String] shouldEqual "Client's ip is 192.168.3.12" } //#extractClientIP-example From 1c968462e1538d0c4884c58789a8d2b56438a447 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Wed, 20 May 2020 14:58:07 +0200 Subject: [PATCH 3/3] and for java --- .../server/directives/MiscDirectivesExamplesTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/docs/src/test/java/docs/http/javadsl/server/directives/MiscDirectivesExamplesTest.java b/docs/src/test/java/docs/http/javadsl/server/directives/MiscDirectivesExamplesTest.java index 0f95618679a..29c9301bf6c 100644 --- a/docs/src/test/java/docs/http/javadsl/server/directives/MiscDirectivesExamplesTest.java +++ b/docs/src/test/java/docs/http/javadsl/server/directives/MiscDirectivesExamplesTest.java @@ -6,10 +6,7 @@ import akka.http.javadsl.model.HttpRequest; import akka.http.javadsl.model.StatusCodes; -import akka.http.javadsl.model.headers.AcceptLanguage; -import akka.http.javadsl.model.headers.Language; -import akka.http.javadsl.model.headers.LanguageRanges; -import akka.http.javadsl.model.headers.RemoteAddress; +import akka.http.javadsl.model.headers.*; import akka.http.javadsl.server.PathMatchers; import akka.http.javadsl.server.Route; import akka.http.javadsl.testkit.JUnitRouteTest; @@ -150,11 +147,11 @@ public void testExtractClientIP() throws UnknownHostException { // tests: final String ip = "192.168.1.2"; - final akka.http.javadsl.model.RemoteAddress remoteAddress = + final akka.http.javadsl.model.RemoteAddress remoteAddress = akka.http.javadsl.model.RemoteAddress.create(InetAddress.getByName(ip)); final HttpRequest request = HttpRequest.GET("/") - .addHeader(RemoteAddress.create(remoteAddress)); // + .addHeader(XForwardedFor.create(remoteAddress)); // testRoute(route).run(request) .assertEntity("Client's IP is " + ip);