-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc7f003
commit 1b1974b
Showing
3 changed files
with
91 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |