Skip to content

Commit

Permalink
rm const
Browse files Browse the repository at this point in the history
  • Loading branch information
katherinelc321 committed May 16, 2024
1 parent aa46870 commit 1a944f6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 76 deletions.
15 changes: 6 additions & 9 deletions frontend/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ const (
APIVersionKey = "api-version"

// Wildcard path segment names for request multiplexing, must be lowercase as we lowercase the request URL pattern when registering handlers
PageSegmentLocation = "location"
PathSegmentSubscriptionID = "subscriptionid"
PathSegmentResourceGroupName = "resourcegroupname"
PathSegmentResourceName = "resourcename"
PathSegmentDeploymentName = "deploymentname"
PathSegmentActionName = "actionname"
PathSegmentOperationID = "operationId"
PathSegmentResourceType = "resourceType"
PathSegmentResourceProviderNamespace = "resourceProviderNamespace"
PageSegmentLocation = "location"
PathSegmentSubscriptionID = "subscriptionid"
PathSegmentResourceGroupName = "resourcegroupname"
PathSegmentResourceName = "resourcename"
PathSegmentDeploymentName = "deploymentname"
PathSegmentActionName = "actionname"
)
28 changes: 3 additions & 25 deletions frontend/middleware_validatestatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,27 @@ package main
// Licensed under the Apache License 2.0.

import (
"errors"
"net/http"
"regexp"
"strings"

"github.com/go-logr/logr"
uuid "github.com/google/uuid"

"github.com/Azure/ARO-HCP/internal/api"
"github.com/Azure/ARO-HCP/internal/api/arm"
)

// Referenced in https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules#microsoftresources
var rxResourceGroupName = regexp.MustCompile(`^[-a-z0-9_().]{0,89}[-a-z0-9_()]$`)
var rxResourceGroupName = regexp.MustCompile(`^[a-zA-Z0-9_()-][a-zA-Z0-9_().-]{0,87}[a-zA-Z0-9_()-]$`)
var rxResourceName = regexp.MustCompile(`^[a-zA-Z0-9-]{3,24}$`)

func MiddlewareValidateStatic(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

subId := r.PathValue(PathSegmentSubscriptionID)
resourceGroupName := r.PathValue(PathSegmentResourceGroupName)
resourceProviderNamespace := r.PathValue(PathSegmentResourceProviderNamespace)
resourceType := r.PathValue(PathSegmentResourceType)
operationId := r.PathValue(PathSegmentOperationID)
resourceName := r.PathValue(PathSegmentResourceName)

if r.URL.Path != strings.ToLower(r.URL.Path) {
if log, ok := r.Context().Value(LoggerFromContext).(logr.Logger); ok {
log.Error(errors.New("LowerCase error"), "path was not lower case")
}
arm.WriteInternalServerError(w)
return
}

if subId != "" {
valid := api.IsValid(subId)
if !valid {
if uuid.Validate(subId) != nil {
arm.WriteError(w, http.StatusBadRequest, arm.CloudErrorCodeInvalidSubscriptionID, "", "The provided subscription identifier '%s' is malformed or invalid.", subId)
return
}
Expand All @@ -58,13 +44,5 @@ func MiddlewareValidateStatic(w http.ResponseWriter, r *http.Request, next http.
}
}

if operationId != "" {
valid := api.IsValid(operationId)
if !valid {
arm.WriteError(w, http.StatusBadRequest, arm.CloudErrorCodeInvalidOperationID, "", "The provided operation identifier '%s' is malformed or invalid.", operationId)
return
}
}

next(w, r)
}
50 changes: 16 additions & 34 deletions frontend/middleware_validatestatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ func TestMiddlewareValidateStatic(t *testing.T) {
})

tests := []struct {
name string
path string
subscriptionID string
resourceGroupName string
resourceProviderNamespace string
resourceType string
resourceName string
operationsId string
expectedStatusCode int
expectedBody string
name string
path string
subscriptionID string
resourceGroupName string

resourceType string
resourceName string
operationsId string
expectedStatusCode int
expectedBody string
}{
{
name: "Valid request",
Expand All @@ -54,28 +54,13 @@ func TestMiddlewareValidateStatic(t *testing.T) {
expectedBody: "Resource group 'resourcegroup!' is invalid.",
},
{
name: "Invalid resource provider namespace",
path: "/providers/invalid",
resourceProviderNamespace: "invalid",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "The resource namespace 'invalid' is invalid.",
},
{
name: "Invalid resource name",
path: "/resourcegroup/providers/microsoft.redhatopenshift/hcpopenshiftcluster/$",
resourceGroupName: "resourcegroup",
resourceProviderNamespace: "microsoft.redhatopenshift",
resourceType: "hcpopenshiftcluster",
resourceName: "$",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "The Resource 'microsoft.redhatopenshift/hcpopenshiftcluster/$' under resource group 'resourcegroup' is invalid.",
},
{
name: "Invalid operation id",
path: "/operations/abc",
operationsId: "abc",
name: "Invalid resource name",
path: "/resourcegroup/providers/microsoft.redhatopenshift/hcpopenshiftcluster/$",
resourceGroupName: "resourcegroup",
resourceType: "hcpOpenShiftClusters",
resourceName: "$",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "The provided operation identifier 'abc' is malformed or invalid.",
expectedBody: "The Resource 'Microsoft.RedHatOpenShift/hcpOpenShiftClusters/$' under resource group 'resourcegroup' is invalid.",
},
}

Expand All @@ -88,10 +73,7 @@ func TestMiddlewareValidateStatic(t *testing.T) {

req.SetPathValue(PathSegmentSubscriptionID, tc.subscriptionID)
req.SetPathValue(PathSegmentResourceGroupName, tc.resourceGroupName)
req.SetPathValue(PathSegmentResourceProviderNamespace, tc.resourceProviderNamespace)
req.SetPathValue(PathSegmentResourceType, tc.resourceType)
req.SetPathValue(PathSegmentResourceName, tc.resourceName)
req.SetPathValue(PathSegmentOperationID, tc.operationsId)

// Execute the middleware
MiddlewareValidateStatic(w, req, nextHandler)
Expand Down
8 changes: 0 additions & 8 deletions internal/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package api

import (
"slices"

uuid "github.com/google/uuid"
)

// Copyright (c) Microsoft Corporation.
Expand Down Expand Up @@ -37,9 +35,3 @@ func StringPtrSliceToStringSlice(s []*string) []string {
}
return out
}

// Check that a string conforms to the UUID format
func IsValid(u string) bool {
err := uuid.Validate(u)
return err == nil
}

0 comments on commit 1a944f6

Please sign in to comment.