Skip to content

Commit 977a4dd

Browse files
pass the context from attrs whenever it\'s empty
1 parent 964f73b commit 977a4dd

7 files changed

Lines changed: 57 additions & 40 deletions

File tree

otoroshi/app/el/el.scala

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ object GlobalExpressionLanguage {
4141
route = None,
4242
apiKey = None,
4343
user = None,
44-
context = context,
44+
context = Some(context).filter(_.nonEmpty).getOrElse(attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty)),
4545
attrs = attrs,
4646
env = env
4747
)
@@ -574,12 +574,12 @@ object HeadersExpressionLanguage {
574574
): String = {
575575
GlobalExpressionLanguage.apply(
576576
value = value,
577-
req = req,
578-
service = service,
579-
route = route,
580-
apiKey = apiKey,
581-
user = user,
582-
context = context,
577+
req = req.orElse(attrs.get(otoroshi.plugins.Keys.RequestKey)),
578+
service = service.orElse(attrs.get(otoroshi.next.plugins.Keys.RouteKey).map(_.legacy)),
579+
route = route.orElse(attrs.get(otoroshi.next.plugins.Keys.RouteKey)),
580+
apiKey = apiKey.orElse(attrs.get(otoroshi.plugins.Keys.ApiKeyKey)),
581+
user = user.orElse(attrs.get(otoroshi.plugins.Keys.UserKey)),
582+
context = Some(context).filter(_.nonEmpty).getOrElse(attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty)),
583583
attrs = attrs,
584584
env = env
585585
)
@@ -601,12 +601,12 @@ object RedirectionExpressionLanguage {
601601
): String = {
602602
GlobalExpressionLanguage.apply(
603603
value = value,
604-
req = req,
605-
service = service,
606-
route = route,
607-
apiKey = apiKey,
608-
user = user,
609-
context = context,
604+
req = req.orElse(attrs.get(otoroshi.plugins.Keys.RequestKey)),
605+
service = service.orElse(attrs.get(otoroshi.next.plugins.Keys.RouteKey).map(_.legacy)),
606+
route = route.orElse(attrs.get(otoroshi.next.plugins.Keys.RouteKey)),
607+
apiKey = apiKey.orElse(attrs.get(otoroshi.plugins.Keys.ApiKeyKey)),
608+
user = user.orElse(attrs.get(otoroshi.plugins.Keys.UserKey)),
609+
context = Some(context).filter(_.nonEmpty).getOrElse(attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty)),
610610
attrs = attrs,
611611
env = env
612612
)
@@ -628,12 +628,12 @@ object TargetExpressionLanguage {
628628
): String = {
629629
GlobalExpressionLanguage.apply(
630630
value = value,
631-
req = req,
632-
service = service,
633-
route = route,
634-
apiKey = apiKey,
635-
user = user,
636-
context = context,
631+
req = req.orElse(attrs.get(otoroshi.plugins.Keys.RequestKey)),
632+
service = service.orElse(attrs.get(otoroshi.next.plugins.Keys.RouteKey).map(_.legacy)),
633+
route = route.orElse(attrs.get(otoroshi.next.plugins.Keys.RouteKey)),
634+
apiKey = apiKey.orElse(attrs.get(otoroshi.plugins.Keys.ApiKeyKey)),
635+
user = user.orElse(attrs.get(otoroshi.plugins.Keys.UserKey)),
636+
context = Some(context).filter(_.nonEmpty).getOrElse(attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty)),
637637
attrs = attrs,
638638
env = env
639639
)
@@ -655,12 +655,12 @@ object JwtExpressionLanguage {
655655
): String = {
656656
GlobalExpressionLanguage.apply(
657657
value = value,
658-
req = req,
659-
service = service,
660-
route = route,
661-
apiKey = apiKey,
662-
user = user,
663-
context = context,
658+
req = req.orElse(attrs.get(otoroshi.plugins.Keys.RequestKey)),
659+
service = service.orElse(attrs.get(otoroshi.next.plugins.Keys.RouteKey).map(_.legacy)),
660+
route = route.orElse(attrs.get(otoroshi.next.plugins.Keys.RouteKey)),
661+
apiKey = apiKey.orElse(attrs.get(otoroshi.plugins.Keys.ApiKeyKey)),
662+
user = user.orElse(attrs.get(otoroshi.plugins.Keys.UserKey)),
663+
context = Some(context).filter(_.nonEmpty).getOrElse(attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty)),
664664
attrs = attrs,
665665
env = env
666666
)

otoroshi/app/next/plugins/external.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class NgExternalValidator extends NgAccessValidator {
9191
route = ctx.route.some,
9292
apiKey = ctx.apikey,
9393
user = ctx.user,
94-
context = Map.empty,
94+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
9595
attrs = ctx.attrs,
9696
env = env
9797
)
@@ -103,7 +103,7 @@ class NgExternalValidator extends NgAccessValidator {
103103
route = ctx.route.some,
104104
apiKey = ctx.apikey,
105105
user = ctx.user,
106-
context = Map.empty,
106+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
107107
attrs = ctx.attrs,
108108
env = env
109109
)
@@ -193,7 +193,7 @@ class NgExternalValidator extends NgAccessValidator {
193193
route = ctx.route.some,
194194
apiKey = ctx.apikey,
195195
user = ctx.user,
196-
context = Map.empty,
196+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
197197
attrs = ctx.attrs,
198198
env = env
199199
)

otoroshi/app/next/plugins/lists.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class NgGenericAllowedList extends NgAccessValidator {
5959
route = ctx.route.some,
6060
apiKey = ctx.apikey,
6161
user = ctx.user,
62-
context = Map.empty,
62+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
6363
attrs = ctx.attrs,
6464
env = env
6565
)
@@ -126,7 +126,7 @@ class NgGenericBlockList extends NgAccessValidator {
126126
route = ctx.route.some,
127127
apiKey = ctx.apikey,
128128
user = ctx.user,
129-
context = Map.empty,
129+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
130130
attrs = ctx.attrs,
131131
env = env
132132
)

otoroshi/app/next/plugins/quotas.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ case class NgCustomQuotasConfig(
326326
route = ctx.route.some,
327327
apiKey = ctx.apikey,
328328
user = ctx.user,
329-
context = Map.empty,
329+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
330330
attrs = ctx.attrs,
331331
env = env
332332
)
@@ -341,7 +341,7 @@ case class NgCustomQuotasConfig(
341341
route = ctx.route.some,
342342
apiKey = ctx.apikey,
343343
user = ctx.user,
344-
context = Map.empty,
344+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
345345
attrs = ctx.attrs,
346346
env = env
347347
)
@@ -498,7 +498,7 @@ case class NgCustomThrottlingConfig(
498498
route = ctx.route.some,
499499
apiKey = ctx.apikey,
500500
user = ctx.user,
501-
context = Map.empty,
501+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
502502
attrs = ctx.attrs,
503503
env = env
504504
)
@@ -513,7 +513,7 @@ case class NgCustomThrottlingConfig(
513513
route = ctx.route.some,
514514
apiKey = ctx.apikey,
515515
user = ctx.user,
516-
context = Map.empty,
516+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
517517
attrs = ctx.attrs,
518518
env = env
519519
)

otoroshi/app/next/plugins/response.scala

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import java.util.UUID
2323
import scala.concurrent.{ExecutionContext, Future}
2424
import scala.util.{Either, Failure, Success, Try}
2525

26-
case class StaticResponseConfig(status: Int = 200, headers: Map[String, String] = Map.empty, body: String = "")
26+
case class StaticResponseConfig(status: Int = 200, headers: Map[String, String] = Map.empty, body: String = "", applyEl: Boolean = false)
2727
extends NgPluginConfig {
2828
def json: JsValue = StaticResponseConfig.format.writes(this)
2929
}
@@ -33,13 +33,15 @@ object StaticResponseConfig {
3333
override def writes(o: StaticResponseConfig): JsValue = Json.obj(
3434
"status" -> o.status,
3535
"headers" -> o.headers,
36-
"body" -> o.body
36+
"body" -> o.body,
37+
"apply_el" -> o.applyEl,
3738
)
3839
override def reads(json: JsValue): JsResult[StaticResponseConfig] = Try {
3940
StaticResponseConfig(
4041
status = json.select("status").asOpt[Int].getOrElse(200),
4142
headers = json.select("headers").asOpt[Map[String, String]].getOrElse(Map.empty),
42-
body = json.select("body").asOpt[String].getOrElse("")
43+
body = json.select("body").asOpt[String].getOrElse(""),
44+
applyEl = json.select("apply_el").asOpt[Boolean].getOrElse(false),
4345
)
4446
} match {
4547
case Failure(ex) => JsError(ex.getMessage())
@@ -72,6 +74,17 @@ class StaticResponse extends NgBackendCall {
7274
val config = ctx.cachedConfig(internalName)(StaticResponseConfig.format).getOrElse(StaticResponseConfig())
7375
val body: ByteString = config.body match {
7476
case str if str.startsWith("Base64(") => str.substring(7).init.byteString.decodeBase64
77+
case str if config.applyEl => GlobalExpressionLanguage.apply(
78+
value = str,
79+
req = ctx.rawRequest.some,
80+
service = None,
81+
route = ctx.route.some,
82+
apiKey = ctx.apikey,
83+
user = ctx.user,
84+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
85+
attrs = ctx.attrs,
86+
env = env
87+
).byteString
7588
case str => str.byteString
7689
}
7790
inMemoryBodyResponse(config.status, config.headers, body).future
@@ -326,7 +339,7 @@ class MockResponses extends NgBackendCall {
326339
route = ctx.route.some,
327340
apiKey = ctx.apikey,
328341
user = ctx.user,
329-
context = Map.empty,
342+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
330343
attrs = ctx.attrs,
331344
env = env
332345
)

otoroshi/app/next/plugins/zip.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class ZipFileBackend extends NgBackendCall {
184184
route = ctx.route.some,
185185
apiKey = ctx.apikey,
186186
user = ctx.user,
187-
context = Map.empty,
187+
context = ctx.attrs.get(otoroshi.plugins.Keys.ElCtxKey).getOrElse(Map.empty),
188188
attrs = ctx.attrs,
189189
env = env
190190
)

otoroshi/javascript/src/forms/ng_plugins/StaticResponse.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export default {
1717
label: 'status',
1818
type: 'number',
1919
},
20+
apply_el: {
21+
label: "Apply EL.",
22+
type: "boolean",
23+
}
2024
},
21-
config_flow: ['body', 'headers', 'status'],
25+
config_flow: ['body', 'headers', 'status', 'apply_el'],
2226
};

0 commit comments

Comments
 (0)