-
Notifications
You must be signed in to change notification settings - Fork 51
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
Showing
13 changed files
with
291 additions
and
43 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 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 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 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 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 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 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 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
59 changes: 59 additions & 0 deletions
59
src/autoscaler/metricsforwarder/server/auth/custom_metrics_strategy.go
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,59 @@ | ||
package auth | ||
|
||
import ( | ||
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/db" | ||
"code.cloudfoundry.org/lager/v3" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type MetricsSubmissionStrategy interface { | ||
validate(appId string, submitterAppIdFromCert string, logger lager.Logger, bindingDB db.BindingDB, r *http.Request) error | ||
} | ||
|
||
var _ MetricsSubmissionStrategy = &DefaultMetricsSubmissionStrategy{} | ||
|
||
type DefaultMetricsSubmissionStrategy struct{} | ||
|
||
func (d *DefaultMetricsSubmissionStrategy) validate(appId string, submitterAppIdFromCert string, logger lager.Logger, bindingDB db.BindingDB, r *http.Request) error { | ||
// check if appID is same as AppIdFromCert | ||
if appId != submitterAppIdFromCert { | ||
return ErrorAppIDWrong | ||
} | ||
return nil | ||
} | ||
|
||
type BoundedMetricsSubmissionStrategy struct{} | ||
|
||
func (c *BoundedMetricsSubmissionStrategy) validate(appId string, submitterAppIdFromCert string, logger lager.Logger, bindingDB db.BindingDB, r *http.Request) error { | ||
// check if appID is same as AppIdFromCert | ||
if appId != submitterAppIdFromCert { | ||
c.verifyMetricSubmissionStrategy(r, logger, bindingDB, submitterAppIdFromCert, appId) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *BoundedMetricsSubmissionStrategy) verifyMetricSubmissionStrategy(r *http.Request, logger lager.Logger, bindingDB db.BindingDB, submitterAppCert string, appID string) (bool, error) { | ||
|
||
customMetricSubmissionStrategy := r.Header.Get("custom-metrics-submission-strategy") | ||
customMetricSubmissionStrategy = strings.ToLower(customMetricSubmissionStrategy) | ||
if customMetricSubmissionStrategy == "" { | ||
logger.Info("custom-metrics-submission-strategy-not-found", lager.Data{"appID": appID}) | ||
return false, nil | ||
} | ||
if customMetricSubmissionStrategy == "bound_app" { | ||
logger.Info("custom-metrics-submission-strategy-found", lager.Data{"appID": appID, "strategy": customMetricSubmissionStrategy}) | ||
// check if the app is bound to same autoscaler instance by check the binding id from the bindingdb | ||
// if the app is bound to the same autoscaler instance, then allow the request to the next handler i.e publish custom metrics | ||
isAppBound, err := bindingDB.IsAppBoundToSameAutoscaler(r.Context(), submitterAppCert, appID) | ||
if err != nil { | ||
logger.Error("error-checking-app-bound-to-same-service", err, lager.Data{"metric-submitter-app-id": submitterAppCert}) | ||
return false, err | ||
} | ||
if isAppBound == false { | ||
logger.Info("app-not-bound-to-same-service", lager.Data{"app-id": submitterAppCert}) | ||
return false, ErrorAppNotBound | ||
} | ||
} | ||
return true, nil | ||
} |
Oops, something went wrong.