Skip to content

Commit d2fc5e7

Browse files
committed
New manual processing for URL path matching GCP #65
1 parent c937bb6 commit d2fc5e7

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

iamlivecore/proxy.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
mxj "github.com/clbanning/mxj/v2"
2828
"github.com/iann0036/goproxy"
2929
"github.com/mitchellh/go-homedir"
30-
"github.com/ucarion/urlpath"
3130
)
3231

3332
//go:embed service/*
@@ -587,7 +586,7 @@ func handleAWSRequest(req *http.Request, body []byte, respCode int) {
587586

588587
pathMatches := regexp.MustCompile(regexStr).FindAllStringSubmatch(path, -1)
589588

590-
if len(pathMatches) > 0 && len(pathMatches) > 0 && len(templateMatches) == len(pathMatches[0])-1 {
589+
if len(pathMatches) > 0 && len(templateMatches) > 0 && len(templateMatches) == len(pathMatches[0])-1 {
591590
for i := 0; i < len(templateMatches); i++ {
592591
uriparams[templateMatches[i][1]] = pathMatches[0][1:][i]
593592
}
@@ -748,6 +747,8 @@ func handleAWSRequest(req *http.Request, body []byte, respCode int) {
748747
handleLoggedCall()
749748
}
750749

750+
var azurermregex = regexp.MustCompile(`^/subscriptions/.+/resourcegroups/.+/providers/Microsoft\.Resources/deployments/.+`)
751+
751752
func handleAzureRequest(req *http.Request, body []byte, respCode int) {
752753
host := req.Host
753754

@@ -764,9 +765,7 @@ func handleAzureRequest(req *http.Request, body []byte, respCode int) {
764765

765766
// Handle AzureRM deployments (inline only)
766767
if req.Method == "PUT" { // TODO: other similar methods
767-
pathmatch := urlpath.New("/subscriptions/:subscriptionId/resourcegroups/:resourceGroupName/providers/Microsoft.Resources/deployments/:deploymentName")
768-
_, ok := pathmatch.Match(req.URL.Path)
769-
if ok {
768+
if azurermregex.MatchString(req.URL.Path) {
770769
data := struct {
771770
Properties struct {
772771
Template interface{} `json:"template"`
@@ -820,6 +819,27 @@ func handleAzureRequest(req *http.Request, body []byte, respCode int) {
820819
handleLoggedCall()
821820
}
822821

822+
func generateMethodTemplate(path string) string {
823+
path = regexp.QuoteMeta(path)
824+
i := strings.Index(path, "\\{")
825+
for i != -1 {
826+
j := strings.Index(path[i:], "\\}")
827+
if j != -1 {
828+
path = path[:i] + ".+" + path[i+j+len("\\}"):]
829+
} else {
830+
break
831+
}
832+
i = strings.Index(path, "\\{")
833+
}
834+
835+
pathtemplate := "^/" + path
836+
if pathtemplate[0:3] == "^//" {
837+
pathtemplate = "^" + pathtemplate[2:]
838+
}
839+
840+
return pathtemplate
841+
}
842+
823843
func gcpProcessResource(req *http.Request, gcpResource GCPResourceDefinition, basePath string) string {
824844
for _, gcpSubresource := range gcpResource.Resources {
825845
apiID := gcpProcessResource(req, gcpSubresource, basePath)
@@ -830,14 +850,10 @@ func gcpProcessResource(req *http.Request, gcpResource GCPResourceDefinition, ba
830850

831851
for _, gcpMethod := range gcpResource.Methods {
832852
if req.Method == gcpMethod.HTTPMethod {
833-
pathtemplate := "/" + strings.ReplaceAll(strings.ReplaceAll(basePath+gcpMethod.FlatPath, "{", ":"), "}", "")
834-
if pathtemplate[0:2] == "//" {
835-
pathtemplate = pathtemplate[1:]
836-
}
837-
pathmatch := urlpath.New(pathtemplate)
853+
pathtemplate := generateMethodTemplate(basePath+gcpMethod.FlatPath)
838854

839-
_, ok := pathmatch.Match(req.URL.Path)
840-
if ok {
855+
r, err := regexp.Compile(pathtemplate)
856+
if err == nil && r.MatchString(req.URL.Path) {
841857
return gcpMethod.ID
842858
}
843859
}

0 commit comments

Comments
 (0)