Skip to content

Commit

Permalink
Merge pull request #1836 from MAIF/wasmo-tls
Browse files Browse the repository at this point in the history
Wasmo tls
  • Loading branch information
Zwiterrion authored Feb 22, 2024
2 parents 046fa09 + 5996b4e commit bedd8ca
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 43 deletions.
18 changes: 9 additions & 9 deletions otoroshi/app/controllers/BackOfficeController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1980,15 +1980,15 @@ class BackOfficeController(
.singleton()
.flatMap { globalConfig =>
globalConfig.wasmoSettings match {
case Some(settings @ WasmoSettings(url, _, _, pluginsFilter, _)) =>
val (header, token) = ApikeyHelper.generate(settings)
case Some(config) =>
val (header, token) = ApikeyHelper.generate(config.settings)
Try {
env.MtlsWs
.url(s"$url/plugins", MtlsConfig())
.url(s"${config.settings.url}/plugins", config.tlsConfig)
.withFollowRedirects(false)
.withHttpHeaders(
header -> token,
"kind" -> pluginsFilter.getOrElse("*")
"kind" -> config.settings.pluginsFilter.getOrElse("*")
)
.get()
.map(res => {
Expand Down Expand Up @@ -2019,16 +2019,16 @@ class BackOfficeController(
def getWasmFilesFromBodyConfiguration() = BackOfficeActionAuth.async(parse.json) { ctx =>
val jsonBody = ctx.request.body

val wasmoSettings = WasmoSettings.format.reads(jsonBody).get
val (header, token) = ApikeyHelper.generate(wasmoSettings)
val wasmoConfiguration = TlsWasmoSettings.format.reads(jsonBody).get
val (header, token) = ApikeyHelper.generate(wasmoConfiguration.settings)

Try {
env.Ws
.url(s"${wasmoSettings.url}/plugins")
env.MtlsWs
.url(s"${wasmoConfiguration.settings.url}/plugins", wasmoConfiguration.tlsConfig)
.withFollowRedirects(false)
.withHttpHeaders(
header -> token,
"kind" -> wasmoSettings.pluginsFilter.getOrElse("*")
"kind" -> wasmoConfiguration.settings.pluginsFilter.getOrElse("*")
)
.get()
.map(res => {
Expand Down
76 changes: 59 additions & 17 deletions otoroshi/app/models/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,39 @@ object DefaultTemplates {
}
}

case class TlsWasmoSettings(settings: WasmoSettings = WasmoSettings(), tlsConfig: MtlsConfig = MtlsConfig()) {
def json: JsValue = TlsWasmoSettings.format.writes(this)
}

object TlsWasmoSettings {
val format = new Format[TlsWasmoSettings] {
override def writes(o: TlsWasmoSettings): JsValue =
Json.obj(
"settings" -> o.settings.json,
"tlsConfig" -> o.tlsConfig.json
)

override def reads(json: JsValue): JsResult[TlsWasmoSettings] = {
Try {
WasmoSettings.format.reads(json) match {
case JsSuccess(value, _) => TlsWasmoSettings(
settings = value
)
case JsError(_) =>
TlsWasmoSettings(
settings = (json \ "settings").as[WasmoSettings](WasmoSettings.format.reads),
tlsConfig = (json \ "tlsConfig").as[MtlsConfig](MtlsConfig.format.reads)
)
}

} match {
case Failure(e) => JsError(e.getMessage)
case Success(ac) => JsSuccess(ac)
}
}
}
}

case class GlobalConfig(
letsEncryptSettings: LetsEncryptSettings = LetsEncryptSettings(),
lines: Seq[String] = Seq("prod"),
Expand Down Expand Up @@ -626,7 +659,7 @@ case class GlobalConfig(
quotasSettings: QuotasAlmostExceededSettings = QuotasAlmostExceededSettings(false, 0.8, 0.8),
plugins: Plugins = Plugins(),
templates: DefaultTemplates = DefaultTemplates(),
wasmoSettings: Option[WasmoSettings] = None,
wasmoSettings: Option[TlsWasmoSettings] = None,
tags: Seq[String] = Seq.empty,
metadata: Map[String, String] = Map.empty,
env: JsObject = Json.obj(),
Expand Down Expand Up @@ -681,6 +714,30 @@ object GlobalConfig {
lazy val logger = Logger("otoroshi-global-config")

val _fmt: Format[GlobalConfig] = new Format[GlobalConfig] {

def readWasmoSettings(json: JsValue): Option[TlsWasmoSettings] = {
TlsWasmoSettings.format.reads(json) match {
case JsSuccess(value, path) => value.some
case JsError(errors) => {
val wasmoSettings: JsResult[WasmoSettings] = WasmoSettings.format.reads(
(json \ "wasmoSettings")
.asOpt[JsValue]
.getOrElse(JsNull)
)
val wasmManagerSettings: JsResult[WasmoSettings] = WasmoSettings.format
.reads(
(json \ "wasmManagerSettings")
.asOpt[JsValue]
.getOrElse(JsNull)
)

wasmoSettings.map(r => r.some)
.getOrElse(wasmManagerSettings.map(r => r.some).getOrElse(None))
.map(value => TlsWasmoSettings(settings = value))
}
}
}

override def writes(o: GlobalConfig): JsValue = {
val mailerSettings: JsValue = o.mailerSettings match {
case None => JsNull
Expand Down Expand Up @@ -906,22 +963,7 @@ object GlobalConfig {
.flatMap(str => DefaultTemplates.format.reads(Json.parse(str)).asOpt)
.orElse(json.select("templates").asOpt(DefaultTemplates.format))
.getOrElse(DefaultTemplates()),
wasmoSettings = WasmoSettings.format
.reads(
(json \ "wasmoSettings")
.asOpt[JsValue]
.getOrElse(JsNull)
)
.getOrElse(
WasmoSettings.format
.reads(
(json \ "wasmManagerSettings")
.asOpt[JsValue]
.getOrElse(JsNull)
)
.getOrElse(WasmoSettings())
)
.some,
wasmoSettings = readWasmoSettings(json),
metadata = (json \ "metadata").asOpt[Map[String, String]].getOrElse(Map.empty),
env = (json \ "env").asOpt[JsObject].getOrElse(Json.obj()),
extensions = (json \ "extensions").asOpt[Map[String, JsValue]].getOrElse(Map.empty),
Expand Down
2 changes: 1 addition & 1 deletion otoroshi/app/wasm/wasm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class OtoroshiWasmIntegrationContext(env: Env) extends WasmIntegrationContext {
}

override def wasmoSettings: Future[Option[WasmoSettings]] =
env.datastores.globalConfigDataStore.latest().wasmoSettings.vfuture
env.datastores.globalConfigDataStore.latest().wasmoSettings.flatMap(_.settings.some).vfuture

override def wasmConfig(path: String): Future[Option[WasmConfiguration]] =
env.proxyState.wasmPlugin(path).map(_.config).vfuture
Expand Down
92 changes: 76 additions & 16 deletions otoroshi/javascript/src/pages/DangerZonePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function WasmoTester(props) {
...props.rawValue.wasmoSettings,
}),
})
.catch((_) => {})
.catch((_) => { })
.then((r) => {
console.log(r.status);
if (r.status !== 200) {
Expand All @@ -347,7 +347,7 @@ function WasmoTester(props) {
height={window.innerHeight - 320}
label=""
help="..."
onChange={() => {}}
onChange={() => { }}
value={value}
/>
<p className="text-center" style={{ fontWeight: 'bold' }}>
Expand Down Expand Up @@ -1086,36 +1086,90 @@ export class DangerZonePage extends Component {
help: 'When the SNI domain cannot be found, this one will be used to find the matching certificate',
},
},
'wasmoSettings.url': {
'wasmoSettings.settings.url': {
type: 'string',
props: {
label: 'URL',
},
},
'wasmoSettings.clientId': {
'wasmoSettings.settings.clientId': {
type: 'string',
props: {
label: 'Apikey id',
},
},
'wasmoSettings.clientSecret': {
'wasmoSettings.settings.clientSecret': {
type: 'string',
props: {
label: 'Apikey secret',
},
},
'wasmoSettings.pluginsFilter': {
'wasmoSettings.settings.pluginsFilter': {
type: 'string',
props: {
label: 'User(s)',
},
},
'wasmoSettings.legacyAuth': {
'wasmoSettings.settings.legacyAuth': {
type: 'bool',
props: {
label: 'Use legacy auth.',
},
},
'wasmoSettings.tlsConfig.mtls': {
type: 'bool',
props: { label: 'Custom TLS Settings' },
},
'wasmoSettings.tlsConfig.loose': {
type: 'bool',
display: (v) => tryOrTrue(() => v.wasmoSettings.tlsConfig.mtls),
props: { label: 'TLS loose' },
},
'wasmoSettings.tlsConfig.trustAll': {
type: 'bool',
display: (v) => tryOrTrue(() => v.wasmoSettings.tlsConfig.mtls),
props: { label: 'TrustAll' },
},
'wasmoSettings.tlsConfig.certs': {
type: 'array',
display: (v) => tryOrTrue(() => v.wasmoSettings.tlsConfig.mtls),
props: {
label: 'Client certificates',
placeholder: 'Choose a client certificate',
valuesFrom: '/bo/api/proxy/api/certificates',
transformer: (a) => ({
value: a.id,
label: (
<span>
<span className="badge bg-success" style={{ minWidth: 63 }}>
{a.certType}
</span>{' '}
{a.name} - {a.description}
</span>
),
}),
},
},
'wasmoSettings.tlsConfig.trustedCerts': {
type: 'array',
display: (v) => tryOrTrue(() => v.wasmoSettings.tlsConfig.mtls && !v.wasmoSettings.tlsConfig.trustAll),
props: {
label: 'Trusted certificates',
placeholder: 'Choose a trusted certificate',
valuesFrom: '/bo/api/proxy/api/certificates',
transformer: (a) => ({
value: a.id,
label: (
<span>
<span className="badge bg-success" style={{ minWidth: 63 }}>
{a.certType}
</span>{' '}
{a.name} - {a.description}
</span>
),
}),
},
},
testing: {
type: WasmoTester,
},
Expand Down Expand Up @@ -1275,11 +1329,16 @@ export class DangerZonePage extends Component {
'>>>Default templates',
'templates',
'>>>Wasmo',
'wasmoSettings.url',
'wasmoSettings.clientId',
'wasmoSettings.clientSecret',
'wasmoSettings.pluginsFilter',
'wasmoSettings.legacyAuth',
'wasmoSettings.settings.url',
'wasmoSettings.settings.clientId',
'wasmoSettings.settings.clientSecret',
'wasmoSettings.settings.pluginsFilter',
'wasmoSettings.settings.legacyAuth',
'wasmoSettings.tlsConfig.mtls',
'wasmoSettings.tlsConfig.loose',
'wasmoSettings.tlsConfig.trustAll',
'wasmoSettings.tlsConfig.certs',
'wasmoSettings.tlsConfig.trustedCerts',
'testing',
'>>>Global metadata',
'tags',
Expand Down Expand Up @@ -1343,7 +1402,9 @@ export class DangerZonePage extends Component {

updateState = (raw) => {
const value = { ...raw };
delete value.elasticReadsConfig.clusterUri;

if (value.elasticReadsConfig)
delete value.elasticReadsConfig.clusterUri;
this.setState({ value, changed: shallowDiffers(this.state.originalValue, value) });
};

Expand Down Expand Up @@ -1846,11 +1907,10 @@ const GlobalPluginInformation = ({ plugin, open }) => {
'https://maif.github.io/otoroshi/manual/plugins/built-in-plugins.html';

const getNgPluginDocumentationUrl = () => {
return `https://maif.github.io/otoroshi/manual/next/built-in-plugins.html#${
plugin.id.replace('cp:', '')
return `https://maif.github.io/otoroshi/manual/next/built-in-plugins.html#${plugin.id.replace('cp:', '')
// .replace(/\./g, '-')
// .toLowerCase()
}`;
}`;
};

return (
Expand Down

0 comments on commit bedd8ca

Please sign in to comment.