Skip to content

Commit

Permalink
rename func
Browse files Browse the repository at this point in the history
  • Loading branch information
katherinelc321 committed May 8, 2024
1 parent dc7f003 commit 1b1974b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 92 deletions.
2 changes: 1 addition & 1 deletion frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewFrontend(logger *slog.Logger, listener net.Listener, emitter metrics.Emi
MiddlewareBody,
MiddlewareLowercase,
MiddlewareSystemData,
MiddlewareValidate(),
MiddlewareValidateStatic,
metricsMiddleware.Metrics(),
)

Expand Down
91 changes: 0 additions & 91 deletions frontend/middleware_validate.go

This file was deleted.

90 changes: 90 additions & 0 deletions frontend/middleware_validatestatic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

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

"github.com/go-chi/chi/v5"
"github.com/go-logr/logr"

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

const (
wantedResourceProviderNamespace = "Microsoft.RedHatOpenShift"
resourceTypeHCPOpenshiftCluster = "HcpOpenShiftClusters"
)

var rxResourceGroupName = regexp.MustCompile(`^[-a-z0-9_().]{0,89}[-a-z0-9_()]$`)

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

subId := chi.URLParam(r, "subscriptionId")
resourceGroupName := chi.URLParam(r, "resourceGroupName")
resourceProviderNamespace := chi.URLParam(r, "resourceProviderNamespace")
resourceType := chi.URLParam(r, "resourceType")
operationId := chi.URLParam(r, "operationId")
resourceName := chi.URLParam(r, "resourceName")

apiVersion := r.URL.Query().Get(APIVersionKey)

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

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

if resourceGroupName != "" {
if !rxResourceGroupName.MatchString(resourceGroupName) {
arm.WriteError(w, http.StatusBadRequest, arm.CloudErrorCodeResourceGroupNotFound, "", "Resource group '%s' could not be found.", resourceGroupName)
return
}
}

if resourceProviderNamespace != "" {
if resourceProviderNamespace != strings.ToLower(wantedResourceProviderNamespace) {
arm.WriteError(w, http.StatusBadRequest, arm.CloudErrorCodeInvalidResourceNamespace, "", "The resource namespace '%s' is invalid.", resourceProviderNamespace)
return
}
}

if resourceType != "" {
if resourceType != strings.ToLower(resourceTypeHCPOpenshiftCluster) {
arm.WriteError(w, http.StatusBadRequest, arm.CloudErrorCodeInvalidResourceType, "", "The resource type '%s' could not be found in the namespace '%s' for api version '%s'.", resourceType, resourceProviderNamespace, apiVersion)
return
}
}

if resourceName != "" {
if !rxResourceGroupName.MatchString(resourceName) {
arm.WriteError(w, http.StatusBadRequest, arm.CloudErrorCodeResourceNotFound, "", "The Resource '%s/%s/%s' under resource group '%s' was not found.", resourceProviderNamespace, resourceType, resourceName, resourceGroupName)
return
}
}

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)
}

0 comments on commit 1b1974b

Please sign in to comment.