feature: Docker OAuth block support (via #4987)
* add `onFound` callback to schemas * add warning to method docs (for #4957) * implement Docker OAuth2 init block support * update docs * add OAUTH_SCOPE_SEPARATOR * drop OAuth env from Dockerfile and run script * don't indent the first oauth block line * drop unused `dedent` import * touch up warning message * add more test cases * return an empty block if no OAuth content is generated * fix broken doc line
This commit is contained in:
13
docker/configurator/helpers.js
Normal file
13
docker/configurator/helpers.js
Normal file
@@ -0,0 +1,13 @@
|
||||
module.exports.indent = function indent(str, len, fromLine = 0) {
|
||||
|
||||
return str
|
||||
.split("\n")
|
||||
.map((line, i) => {
|
||||
if (i + 1 >= fromLine) {
|
||||
return `${Array(len + 1).join(" ")}${line}`
|
||||
} else {
|
||||
return line
|
||||
}
|
||||
})
|
||||
.join("\n")
|
||||
}
|
||||
@@ -2,7 +2,8 @@ const fs = require("fs")
|
||||
const path = require("path")
|
||||
|
||||
const translator = require("./translator")
|
||||
const configSchema = require("./variables")
|
||||
const oauthBlockBuilder = require("./oauth")
|
||||
const indent = require("./helpers").indent
|
||||
|
||||
const START_MARKER = "// Begin Swagger UI call region"
|
||||
const END_MARKER = "// End Swagger UI call region"
|
||||
@@ -22,19 +23,7 @@ fs.writeFileSync(targetPath, `${beforeStartMarkerContent}
|
||||
const ui = SwaggerUIBundle({
|
||||
${indent(translator(process.env, { injectBaseConfig: true }), 8, 2)}
|
||||
})
|
||||
|
||||
${indent(oauthBlockBuilder(process.env), 6, 2)}
|
||||
${END_MARKER}
|
||||
${afterEndMarkerContent}`)
|
||||
|
||||
function indent(str, len, fromLine) {
|
||||
|
||||
return str
|
||||
.split("\n")
|
||||
.map((line, i) => {
|
||||
if(i + 1 >= fromLine) {
|
||||
return `${Array(len + 1).join(" ")}${line}`
|
||||
} else {
|
||||
return line
|
||||
}
|
||||
})
|
||||
.join("\n")
|
||||
}
|
||||
${afterEndMarkerContent}`)
|
||||
43
docker/configurator/oauth.js
Normal file
43
docker/configurator/oauth.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const translator = require("./translator")
|
||||
const indent = require("./helpers").indent
|
||||
|
||||
const oauthBlockSchema = {
|
||||
OAUTH_CLIENT_ID: {
|
||||
type: "string",
|
||||
name: "clientId"
|
||||
},
|
||||
OAUTH_CLIENT_SECRET: {
|
||||
type: "string",
|
||||
name: "clientSecret",
|
||||
onFound: () => console.warn("Swagger UI warning: don't use `OAUTH_CLIENT_SECRET` in production!")
|
||||
},
|
||||
OAUTH_REALM: {
|
||||
type: "string",
|
||||
name: "realm"
|
||||
},
|
||||
OAUTH_APP_NAME: {
|
||||
type: "string",
|
||||
name: "appName"
|
||||
},
|
||||
OAUTH_SCOPE_SEPARATOR: {
|
||||
type: "string",
|
||||
name: "scopeSeparator"
|
||||
},
|
||||
OAUTH_ADDITIONAL_PARAMS: {
|
||||
type: "object",
|
||||
name: "additionalQueryStringParams"
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function oauthBlockBuilder(env) {
|
||||
const translatorResult = translator(env, { schema: oauthBlockSchema })
|
||||
|
||||
if(translatorResult) {
|
||||
return (
|
||||
`ui.initOAuth({
|
||||
${indent(translatorResult, 2)}
|
||||
})`)
|
||||
}
|
||||
|
||||
return ``
|
||||
}
|
||||
@@ -55,6 +55,10 @@ function objectToKeyValueString(env, { injectBaseConfig = false, schema = config
|
||||
|
||||
if(!varSchema) return
|
||||
|
||||
if(varSchema.onFound) {
|
||||
varSchema.onFound()
|
||||
}
|
||||
|
||||
const storageContents = valueStorage[varSchema.name]
|
||||
|
||||
if(storageContents) {
|
||||
|
||||
@@ -28,13 +28,6 @@ if [ "${BASE_URL}" ]; then
|
||||
fi
|
||||
|
||||
replace_in_index myApiKeyXXXX123456789 $API_KEY
|
||||
replace_or_delete_in_index your-client-id $OAUTH_CLIENT_ID
|
||||
replace_or_delete_in_index your-client-secret-if-required $OAUTH_CLIENT_SECRET
|
||||
replace_or_delete_in_index your-realms $OAUTH_REALM
|
||||
replace_or_delete_in_index your-app-name $OAUTH_APP_NAME
|
||||
if [ "$OAUTH_ADDITIONAL_PARAMS" != "**None**" ]; then
|
||||
replace_in_index "additionalQueryStringParams: {}" "additionalQueryStringParams: {$OAUTH_ADDITIONAL_PARAMS}"
|
||||
fi
|
||||
|
||||
if [[ -f $SWAGGER_JSON ]]; then
|
||||
cp -s $SWAGGER_JSON $NGINX_ROOT
|
||||
|
||||
Reference in New Issue
Block a user