Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Commit

Permalink
add cors policy
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed May 23, 2021
1 parent e505a70 commit c239806
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
7 changes: 6 additions & 1 deletion bundlegen/generateapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func isFileYaml(name string) bool {
return false
}

func GenerateAPIProxyDefFromOAS(name string, oasDocName string, skipPolicy bool) (err error) {
func GenerateAPIProxyDefFromOAS(name string, oasDocName string, skipPolicy bool, addCORS bool) (err error) {

if doc == nil {
return fmt.Errorf("Open API document not loaded")
Expand Down Expand Up @@ -141,6 +141,11 @@ func GenerateAPIProxyDefFromOAS(name string, oasDocName string, skipPolicy bool)

proxies.NewProxyEndpoint(u.Path)

if addCORS {
proxies.AddStepToPreFlowRequest("Add-CORS")
apiproxy.AddPolicy("Add-CORS")
}

if !skipPolicy {
proxies.AddStepToPreFlowRequest("OpenAPI-Spec-Validation-1")
}
Expand Down
17 changes: 17 additions & 0 deletions bundlegen/policies/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ var oauth2Policy = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Tokens/>
</OAuthV2>`

var corsPolicy = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CORS async="false" continueOnError="false" enabled="true" name="Add-CORS">
<DisplayName>Add CORS</DisplayName>
<AllowOrigins>{request.header.origin}</AllowOrigins>
<AllowMethods>GET, PUT, POST, DELETE</AllowMethods>
<AllowHeaders>origin, x-requested-with, accept, content-type</AllowHeaders>
<ExposeHeaders>*</ExposeHeaders>
<MaxAge>3628800</MaxAge>
<AllowCredentials>false</AllowCredentials>
<GeneratePreflightResponse>true</GeneratePreflightResponse>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</CORS>`

func AddOpenAPIValidatePolicy(name string) string {
return replaceTemplateWithPolicy(name)
}
Expand All @@ -57,6 +70,10 @@ func AddOAuth2Policy() string {
return oauth2Policy
}

func AddCORSPolicy() string {
return corsPolicy
}

func replaceTemplateWithPolicy(name string) string {
re := regexp.MustCompile(`{(.*?)}`)
return re.ReplaceAllLiteralString(oasPolicyTemplate, name)
Expand Down
15 changes: 11 additions & 4 deletions bundlegen/proxybundle/proxybundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
target "github.com/srinandan/apigeecli/bundlegen/targetendpoint"
)

func GenerateAPIProxyBundle(name string, content string, fileName string, skipPolicy bool) (err error) {
func GenerateAPIProxyBundle(name string, content string, fileName string, resourceType string, skipPolicy bool, addCORS bool) (err error) {
const rootDir = "apiproxy"
var apiProxyData, proxyEndpointData, targetEndpointData string

Expand All @@ -49,7 +49,7 @@ func GenerateAPIProxyBundle(name string, content string, fileName string, skipPo
proxiesDirPath := rootDir + string(os.PathSeparator) + "proxies"
policiesDirPath := rootDir + string(os.PathSeparator) + "policies"
targetDirPath := rootDir + string(os.PathSeparator) + "targets"
oasDirPath := rootDir + string(os.PathSeparator) + "resources" + string(os.PathSeparator) + "oas"
resDirPath := rootDir + string(os.PathSeparator) + "resources" + string(os.PathSeparator) + resourceType //"oas"

if err = os.Mkdir(proxiesDirPath, os.ModePerm); err != nil {
return err
Expand Down Expand Up @@ -77,10 +77,10 @@ func GenerateAPIProxyBundle(name string, content string, fileName string, skipPo
}

if !skipPolicy {
if err = os.MkdirAll(oasDirPath, os.ModePerm); err != nil {
if err = os.MkdirAll(resDirPath, os.ModePerm); err != nil {
return err
}
if err = writeXMLData(oasDirPath+string(os.PathSeparator)+fileName, content); err != nil {
if err = writeXMLData(resDirPath+string(os.PathSeparator)+fileName, content); err != nil {
return err
}
}
Expand Down Expand Up @@ -110,6 +110,13 @@ func GenerateAPIProxyBundle(name string, content string, fileName string, skipPo
}
}

if addCORS {
//add cors policy
if err = writeXMLData(policiesDirPath+string(os.PathSeparator)+"Add-CORS.xml", policies.AddCORSPolicy()); err != nil {
return err
}
}

if err = archiveBundle(rootDir, name+".zip"); err != nil {
return err
}
Expand Down
21 changes: 16 additions & 5 deletions cmd/apis/crtapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ var CreateCmd = &cobra.Command{
return fmt.Errorf("Importing a bundle (--proxy) cannot be combined with importing via an OAS file")
}

if proxy != "" && (gqlFile != "" || gqlURI != "") {
return fmt.Errorf("Importing a bundle (--proxy) cannot be combined with importing via an GraphQL file")
}

if oasFile != "" && oasURI != "" {
return fmt.Errorf("Cannot be combined with importing via an OAS through a file and URI")
return fmt.Errorf("Cannot combine importing an OAS through a file and URI")
}

if gqlFile != "" && gqlURI != "" {
return fmt.Errorf("Cannot combine importing a GraphQL schema through a file and URI")
}

return apiclient.SetApigeeOrg(org)
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand All @@ -54,12 +63,12 @@ var CreateCmd = &cobra.Command{
return err
}

err = bundle.GenerateAPIProxyDefFromOAS(name, oasDocName, skipPolicy)
err = bundle.GenerateAPIProxyDefFromOAS(name, oasDocName, skipPolicy, addCORS)
if err != nil {
return err
}

err = proxybundle.GenerateAPIProxyBundle(name, string(content), oasDocName, skipPolicy)
err = proxybundle.GenerateAPIProxyBundle(name, string(content), oasDocName, "oas", skipPolicy, addCORS)
if err != nil {
return err
}
Expand All @@ -76,8 +85,8 @@ var CreateCmd = &cobra.Command{
},
}

var proxy, oasFile, oasURI string
var importProxy, validateSpec, skipPolicy bool
var proxy, oasFile, oasURI, gqlFile, gqlURI string
var importProxy, validateSpec, skipPolicy, addCORS bool

func init() {

Expand All @@ -95,6 +104,8 @@ func init() {
true, "Validate Spec before generating proxy")
CreateCmd.Flags().BoolVarP(&skipPolicy, "skip-policy", "",
false, "Skip adding the OAS Validate policy")
CreateCmd.Flags().BoolVarP(&addCORS, "add-cors", "",
false, "Add a CORS policy")

_ = CreateCmd.MarkFlagRequired("name")
}

0 comments on commit c239806

Please sign in to comment.