Skip to content

Commit

Permalink
Add configuration cookie.oldName config option (close snowplow#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-j committed Jun 29, 2023
1 parent d9ee385 commit 963424d
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,75 +51,78 @@ trait CollectorRoute {
def routes: Route =
doNotTrack(collectorService.doNotTrackCookie) { dnt =>
cookieIfWanted(collectorService.cookieName) { reqCookie =>
val cookie = reqCookie.map(_.toCookie)
headers { (userAgent, refererURI, rawRequestURI, spAnonymous) =>
val qs = queryString(rawRequestURI)
extractors(spAnonymous) { (host, ip, request) =>
// get the adapter vendor and version from the path
path(Segment / Segment) { (vendor, version) =>
val path = collectorService.determinePath(vendor, version)
post {
extractContentType { ct =>
entity(as[String]) { body =>
cookieIfWanted(collectorService.oldCookieName) { oldReqCookie =>
val cookie = reqCookie.map(_.toCookie).orElse(oldReqCookie.map((_.toCookie)))
headers { (userAgent, refererURI, rawRequestURI, spAnonymous) =>
val qs = queryString(rawRequestURI)
extractors(spAnonymous) { (host, ip, request) =>
// get the adapter vendor and version from the path
path(Segment / Segment) { (vendor, version) =>
val path = collectorService.determinePath(vendor, version)
post {
extractContentType { ct =>
entity(as[String]) { body =>
val r = collectorService.cookie(
qs,
Some(body),
path,
cookie,
userAgent,
refererURI,
host,
ip,
request,
pixelExpected = false,
doNotTrack = dnt,
Some(ct),
spAnonymous
)
complete(r)
}
}
} ~
(get | head) {
val r = collectorService.cookie(
qs,
Some(body),
None,
path,
cookie,
userAgent,
refererURI,
host,
ip,
request,
pixelExpected = false,
pixelExpected = true,
doNotTrack = dnt,
Some(ct),
None,
spAnonymous
)
complete(r)
}
}
} ~
(get | head) {
val r = collectorService.cookie(
qs,
None,
path,
cookie,
userAgent,
refererURI,
host,
ip,
request,
pixelExpected = true,
doNotTrack = dnt,
None,
spAnonymous
)
complete(r)
}
} ~
path("""ice\.png""".r | "i".r) { path =>
(get | head) {
val r = collectorService.cookie(
qs,
None,
"/" + path,
cookie,
userAgent,
refererURI,
host,
ip,
request,
pixelExpected = true,
doNotTrack = dnt,
None,
spAnonymous
)
complete(r)
path("""ice\.png""".r | "i".r) { path =>
(get | head) {
val r = collectorService.cookie(
qs,
None,
"/" + path,
cookie,
userAgent,
refererURI,
host,
ip,
request,
pixelExpected = true,
doNotTrack = dnt,
None,
spAnonymous
)
complete(r)
}
}
}
}
}

}
}
} ~ corsRoute ~ healthRoute ~ sinkHealthRoute ~ crossDomainRoute ~ rootRoute ~ robotsRoute ~ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ trait Service {
spAnonymous: Option[String] = None
): HttpResponse
def cookieName: Option[String]
def oldCookieName: Option[String]
def doNotTrackCookie: Option[DntCookieMatcher]
def determinePath(vendor: String, version: String): String
def enableDefaultRedirect: Boolean
Expand All @@ -83,6 +84,7 @@ class CollectorService(
config.streams.sink.getClass.getSimpleName.toLowerCase

override val cookieName = config.cookieName
override val oldCookieName = config.oldCookieName
override val doNotTrackCookie = config.doNotTrackHttpCookie
override val enableDefaultRedirect = config.enableDefaultRedirect
override def sinksHealthy = sinks.good.isHealthy && sinks.bad.isHealthy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ package model {
final case class CookieConfig(
enabled: Boolean,
name: String,
oldName: Option[String],
expiration: FiniteDuration,
domains: Option[List[String]],
fallbackDomain: Option[String],
Expand Down Expand Up @@ -245,6 +246,7 @@ package model {
None

def cookieName = cookieConfig.map(_.name)
def oldCookieName = cookieConfig.flatMap(_.oldName)
def cookieDomain = cookieConfig.flatMap(_.domains)
def fallbackDomain = cookieConfig.flatMap(_.fallbackDomain)
def cookieExpiration = cookieConfig.map(_.expiration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CollectorRouteSpec extends Specification with Specs2RouteTest {
spAnonymous: Option[String] = spAnonymous
): HttpResponse = HttpResponse(200, entity = s"cookie")
def cookieName: Option[String] = Some("name")
def oldCookieName: Option[String] = None
def doNotTrackCookie: Option[DntCookieMatcher] = None
def determinePath(vendor: String, version: String): String = "/p1/p2"
def enableDefaultRedirect: Boolean = withRedirects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ class CollectorServiceSpec extends Specification {
val conf = CookieConfig(
true,
"name",
None,
5.seconds,
Some(List("domain")),
None,
Expand All @@ -627,6 +628,7 @@ class CollectorServiceSpec extends Specification {
val conf = CookieConfig(
true,
"name",
None,
5.seconds,
Some(List("domain")),
None,
Expand All @@ -640,6 +642,7 @@ class CollectorServiceSpec extends Specification {
val conf = CookieConfig(
true,
"name",
None,
5.seconds,
Some(List("domain")),
None,
Expand All @@ -654,6 +657,7 @@ class CollectorServiceSpec extends Specification {
val conf = CookieConfig(
true,
"name",
None,
5.seconds,
Some(List("domain")),
None,
Expand Down Expand Up @@ -820,34 +824,56 @@ class CollectorServiceSpec extends Specification {
"not return a domain" in {
"if a list of domains is not supplied in the config and there is no fallback domain" in {
val request = HttpRequest()
val cookieConfig = CookieConfig(true, "name", 5.seconds, None, None, false, false, None)
val cookieConfig = CookieConfig(true, "name", None, 5.seconds, None, None, false, false, None)
service.cookieDomain(request.headers, cookieConfig.domains, cookieConfig.fallbackDomain) shouldEqual None
}
"if a list of domains is supplied in the config but the Origin request header is empty and there is no fallback domain" in {
val request = HttpRequest()
val cookieConfig = CookieConfig(true, "name", 5.seconds, Some(List("domain.com")), None, false, false, None)
val request = HttpRequest()
val cookieConfig =
CookieConfig(true, "name", None, 5.seconds, Some(List("domain.com")), None, false, false, None)
service.cookieDomain(request.headers, cookieConfig.domains, cookieConfig.fallbackDomain) shouldEqual None
}
"if none of the domains in the request's Origin header has a match in the list of domains supplied with the config and there is no fallback domain" in {
val origins = Seq(HttpOrigin("http", Host("origin.com")), HttpOrigin("http", Host("otherorigin.com", 8080)))
val request = HttpRequest().withHeaders(`Origin`(origins))
val cookieConfig =
CookieConfig(true, "name", 5.seconds, Some(List("domain.com", "otherdomain.com")), None, false, false, None)
CookieConfig(
true,
"name",
None,
5.seconds,
Some(List("domain.com", "otherdomain.com")),
None,
false,
false,
None
)
service.cookieDomain(request.headers, cookieConfig.domains, cookieConfig.fallbackDomain) shouldEqual None
}
}
"return the fallback domain" in {
"if a list of domains is not supplied in the config but a fallback domain is configured" in {
val request = HttpRequest()
val cookieConfig = CookieConfig(true, "name", 5.seconds, None, Some("fallbackDomain"), false, false, None)
val request = HttpRequest()
val cookieConfig =
CookieConfig(true, "name", None, 5.seconds, None, Some("fallbackDomain"), false, false, None)
service.cookieDomain(request.headers, cookieConfig.domains, cookieConfig.fallbackDomain) shouldEqual Some(
"fallbackDomain"
)
}
"if the Origin header is empty and a fallback domain is configured" in {
val request = HttpRequest()
val cookieConfig =
CookieConfig(true, "name", 5.seconds, Some(List("domain.com")), Some("fallbackDomain"), false, false, None)
CookieConfig(
true,
"name",
None,
5.seconds,
Some(List("domain.com")),
Some("fallbackDomain"),
false,
false,
None
)
service.cookieDomain(request.headers, cookieConfig.domains, cookieConfig.fallbackDomain) shouldEqual Some(
"fallbackDomain"
)
Expand All @@ -858,6 +884,7 @@ class CollectorServiceSpec extends Specification {
val cookieConfig = CookieConfig(
true,
"name",
None,
5.seconds,
Some(List("domain.com", "otherdomain.com")),
Some("fallbackDomain"),
Expand All @@ -877,6 +904,7 @@ class CollectorServiceSpec extends Specification {
val cookieConfig = CookieConfig(
true,
"name",
None,
5.seconds,
Some(List("domain.com", "otherdomain.com")),
Some("fallbackDomain"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ object TestUtils {
cookie = CookieConfig(
true,
"sp",
None,
365.days,
None,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ abstract class ConfigSpec extends Specification {
enabled = true,
expiration = 365.days,
name = "sp",
oldName = None,
domains = None,
fallbackDomain = None,
secure = true,
Expand Down

0 comments on commit 963424d

Please sign in to comment.