From 3c0623cce17ad08275a8a361410ccdefc5ebb998 Mon Sep 17 00:00:00 2001 From: stkrwork Date: Tue, 27 Jun 2017 20:48:53 +0200 Subject: [PATCH] ApacheConfGen for HTTP Basic Authentication Proposed implementation (#5800) * - Added Restbed Generator * - Added Json processing functions to model - Removed unnused code from restbed codegen class - Added response header processing to api template * Changed it to respect alphabetical order * Made the string joining java 7 compatible * Added samples * Auto stash before merge of "master" and "origin/master" * added missing apache2 config gen in services file * - Added new Config Type - Added Samples - Added sample generating scripts - Renamed Apache2Confgen to Apache2ConfigCodegen * - Added Samples - Added new config type - Added sample generating scripts - Renamed Apache2Confgen to Apache2ConfigCodegen --- bin/apache2-petstore-config.sh | 31 +++++ bin/windows/apache2-petstore-config.bat | 10 ++ .../java/io/swagger/codegen/CodegenType.java | 2 +- .../languages/Apache2ConfigCodegen.java | 106 ++++++++++++++++++ .../services/io.swagger.codegen.CodegenConfig | 1 + .../resources/apache2/apache-config.mustache | 44 ++++++++ .../main/resources/apache2/authConf.mustache | 5 + .../petstore/apache2/.swagger-codegen-ignore | 23 ++++ .../petstore/apache2/.swagger-codegen/VERSION | 1 + samples/config/petstore/apache2/PetApi.conf | 78 +++++++++++++ samples/config/petstore/apache2/StoreApi.conf | 39 +++++++ samples/config/petstore/apache2/UserApi.conf | 84 ++++++++++++++ 12 files changed, 423 insertions(+), 1 deletion(-) create mode 100644 bin/apache2-petstore-config.sh create mode 100644 bin/windows/apache2-petstore-config.bat create mode 100644 modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Apache2ConfigCodegen.java create mode 100644 modules/swagger-codegen/src/main/resources/apache2/apache-config.mustache create mode 100644 modules/swagger-codegen/src/main/resources/apache2/authConf.mustache create mode 100644 samples/config/petstore/apache2/.swagger-codegen-ignore create mode 100644 samples/config/petstore/apache2/.swagger-codegen/VERSION create mode 100644 samples/config/petstore/apache2/PetApi.conf create mode 100644 samples/config/petstore/apache2/StoreApi.conf create mode 100644 samples/config/petstore/apache2/UserApi.conf diff --git a/bin/apache2-petstore-config.sh b/bin/apache2-petstore-config.sh new file mode 100644 index 00000000000..e0c04ba2cc5 --- /dev/null +++ b/bin/apache2-petstore-config.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +SCRIPT="$0" + +while [ -h "$SCRIPT" ] ; do + ls=`ls -ld "$SCRIPT"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + SCRIPT="$link" + else + SCRIPT=`dirname "$SCRIPT"`/"$link" + fi +done + +if [ ! -d "${APP_DIR}" ]; then + APP_DIR=`dirname "$SCRIPT"`/.. + APP_DIR=`cd "${APP_DIR}"; pwd` +fi + +executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" + +if [ ! -f "$executable" ] +then + mvn clean package +fi + +# if you've executed sbt assembly previously it will use that instead. +export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" +ags="$@ generate -l apache2 -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -o samples/config/petstore/apache2" + +java $JAVA_OPTS -jar $executable $ags diff --git a/bin/windows/apache2-petstore-config.bat b/bin/windows/apache2-petstore-config.bat new file mode 100644 index 00000000000..5e61f95586d --- /dev/null +++ b/bin/windows/apache2-petstore-config.bat @@ -0,0 +1,10 @@ +set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar + +If Not Exist %executable% ( + mvn clean package +) + +REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties +set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -l apache2 -o samples\config\petstore\apache2\ + +java %JAVA_OPTS% -jar %executable% %ags% diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenType.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenType.java index 8d65290e8f8..5d348b946b6 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenType.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenType.java @@ -7,7 +7,7 @@ import java.util.HashMap; import java.util.Map; public enum CodegenType { - CLIENT, SERVER, DOCUMENTATION, OTHER; + CLIENT, SERVER, DOCUMENTATION, CONFIG, OTHER; private static Map names = new HashMap(); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Apache2ConfigCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Apache2ConfigCodegen.java new file mode 100644 index 00000000000..0027e6920cf --- /dev/null +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Apache2ConfigCodegen.java @@ -0,0 +1,106 @@ +package io.swagger.codegen.languages; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + +import io.swagger.codegen.CliOption; +import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenConstants; +import io.swagger.codegen.CodegenOperation; +import io.swagger.codegen.CodegenType; +import io.swagger.codegen.DefaultCodegen; +import io.swagger.codegen.SupportingFile; + +public class Apache2ConfigCodegen extends DefaultCodegen implements CodegenConfig { + public static final String USER_INFO_PATH = "userInfoPath"; + protected String userInfoPath = "/var/www/html/"; + + @Override + public CodegenType getTag() { + return CodegenType.CONFIG; + } + + @Override + public String getName() { + return "apache2"; + } + + @Override + public String getHelp() { + return "Generates an Apache2 Config file with the permissions"; + } + + public Apache2ConfigCodegen() { + super(); + apiTemplateFiles.put("apache-config.mustache", ".conf"); + + embeddedTemplateDir = templateDir = "apache2"; + + cliOptions.add(new CliOption(USER_INFO_PATH, "Path to the user and group files")); + } + + + @Override + public void processOpts() { + if (additionalProperties.containsKey(USER_INFO_PATH)) { + userInfoPath = additionalProperties.get(USER_INFO_PATH).toString(); + } + } + + @Override + public String escapeQuotationMark(String input) { + // remove " to avoid code injection + return input.replace("\"", ""); + } + + @Override + public String escapeUnsafeCharacters(String input) { + return input.replace("*/", "*_/").replace("/*", "/_*"); + } + + @SuppressWarnings("unchecked") + @Override + public Map postProcessOperations(Map objs) { + Map operations = (Map) objs.get("operations"); + List operationList = (List) operations.get("operation"); + List newOpList = new ArrayList(); + for (CodegenOperation op : operationList) { + String path = new String(op.path); + + String[] items = path.split("/", -1); + List splitPath = new ArrayList(); + for (String item: items) { + if (item.matches("^\\{(.*)\\}$")) { + item = "*"; + } + splitPath.add(item); + } + op.path = String.join("/", splitPath); + op.vendorExtensions.put("x-codegen-userInfoPath", userInfoPath); + boolean foundInNewList = false; + for (CodegenOperation op1 : newOpList) { + if (!foundInNewList) { + if (op1.path.equals(op.path)) { + foundInNewList = true; + List currentOtherMethodList = (List) op1.vendorExtensions.get("x-codegen-otherMethods"); + if (currentOtherMethodList == null) { + currentOtherMethodList = new ArrayList(); + } + op.operationIdCamelCase = op1.operationIdCamelCase; + currentOtherMethodList.add(op); + op1.vendorExtensions.put("x-codegen-otherMethods", currentOtherMethodList); + } + } + } + if (!foundInNewList) { + newOpList.add(op); + } + } + operations.put("operation", newOpList); + return objs; + } +} diff --git a/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig b/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig index b8c4c7e9397..0242440fbad 100644 --- a/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig +++ b/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig @@ -1,5 +1,6 @@ io.swagger.codegen.languages.AkkaScalaClientCodegen io.swagger.codegen.languages.AndroidClientCodegen +io.swagger.codegen.languages.Apache2ConfigCodegen io.swagger.codegen.languages.ApexClientCodegen io.swagger.codegen.languages.AspNet5ServerCodegen io.swagger.codegen.languages.AspNetCoreServerCodegen diff --git a/modules/swagger-codegen/src/main/resources/apache2/apache-config.mustache b/modules/swagger-codegen/src/main/resources/apache2/apache-config.mustache new file mode 100644 index 00000000000..92fad06e97d --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/apache2/apache-config.mustache @@ -0,0 +1,44 @@ +{{#operations}} +{{#operation}} + + {{>authConf}} + + Require valid-user + +{{#hasAuthMethods}} +{{#authMethods}} + {{#isOAuth}} + + {{#scopes}} + Require group {{scope}} + {{/scopes}} + + {{/isOAuth}} +{{/authMethods}} +{{/hasAuthMethods}} +{{^hasAuthMethods}} + + Require valid-user + +{{/hasAuthMethods}} +{{#vendorExtensions.x-codegen-otherMethods}} +{{#hasAuthMethods}} +{{#authMethods}} + {{#isOAuth}} + + {{#scopes}} + Require group {{scope}} + {{/scopes}} + + {{/isOAuth}} +{{/authMethods}} +{{/hasAuthMethods}} +{{^hasAuthMethods}} + + Require valid-user + +{{/hasAuthMethods}} +{{/vendorExtensions.x-codegen-otherMethods}} + +{{/operation}} +{{/operations}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/apache2/authConf.mustache b/modules/swagger-codegen/src/main/resources/apache2/authConf.mustache new file mode 100644 index 00000000000..27306808878 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/apache2/authConf.mustache @@ -0,0 +1,5 @@ +AuthBasicProvider file + AuthUserFile "{{vendorExtensions.x-codegen-userInfoPath}}htpwd" + AuthGroupFile "{{vendorExtensions.x-codegen-userInfoPath}}groups" + AuthType basic + AuthName "api" \ No newline at end of file diff --git a/samples/config/petstore/apache2/.swagger-codegen-ignore b/samples/config/petstore/apache2/.swagger-codegen-ignore new file mode 100644 index 00000000000..c5fa491b4c5 --- /dev/null +++ b/samples/config/petstore/apache2/.swagger-codegen-ignore @@ -0,0 +1,23 @@ +# Swagger Codegen Ignore +# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen + +# Use this file to prevent files from being overwritten by the generator. +# The patterns follow closely to .gitignore or .dockerignore. + +# As an example, the C# client generator defines ApiClient.cs. +# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line: +#ApiClient.cs + +# You can match any string of characters against a directory, file or extension with a single asterisk (*): +#foo/*/qux +# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux + +# You can recursively match patterns against a directory, file or extension with a double asterisk (**): +#foo/**/qux +# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux + +# You can also negate patterns with an exclamation (!). +# For example, you can ignore all files in a docs folder with the file extension .md: +#docs/*.md +# Then explicitly reverse the ignore rule for a single file: +#!docs/README.md diff --git a/samples/config/petstore/apache2/.swagger-codegen/VERSION b/samples/config/petstore/apache2/.swagger-codegen/VERSION new file mode 100644 index 00000000000..7fea99011a6 --- /dev/null +++ b/samples/config/petstore/apache2/.swagger-codegen/VERSION @@ -0,0 +1 @@ +2.2.3-SNAPSHOT \ No newline at end of file diff --git a/samples/config/petstore/apache2/PetApi.conf b/samples/config/petstore/apache2/PetApi.conf new file mode 100644 index 00000000000..c92843e2610 --- /dev/null +++ b/samples/config/petstore/apache2/PetApi.conf @@ -0,0 +1,78 @@ + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require group write:pets + Require group read:pets + + + Require group write:pets + Require group read:pets + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require group write:pets + Require group read:pets + + + Require group write:pets + Require group read:pets + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require group write:pets + Require group read:pets + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require group write:pets + Require group read:pets + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require group write:pets + Require group read:pets + + diff --git a/samples/config/petstore/apache2/StoreApi.conf b/samples/config/petstore/apache2/StoreApi.conf new file mode 100644 index 00000000000..f0fb742f55a --- /dev/null +++ b/samples/config/petstore/apache2/StoreApi.conf @@ -0,0 +1,39 @@ + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require valid-user + + + Require valid-user + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require valid-user + + diff --git a/samples/config/petstore/apache2/UserApi.conf b/samples/config/petstore/apache2/UserApi.conf new file mode 100644 index 00000000000..b01bb509728 --- /dev/null +++ b/samples/config/petstore/apache2/UserApi.conf @@ -0,0 +1,84 @@ + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require valid-user + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require valid-user + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require valid-user + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require valid-user + + + Require valid-user + + + Require valid-user + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require valid-user + + + + AuthBasicProvider file + AuthUserFile "/var/www/html/htpwd" + AuthGroupFile "/var/www/html/groups" + AuthType basic + AuthName "api" + + Require valid-user + + + Require valid-user + +