Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
816bc95905 | ||
|
|
58b9b17944 | ||
|
|
a66cfc094a | ||
|
|
8fd6eddc16 | ||
|
|
402525d572 |
No files matched your search
@@ -81,6 +81,9 @@ func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) {
|
|||||||
yamlBytes = []byte(os.ExpandEnv(string(yamlBytes)))
|
yamlBytes = []byte(os.ExpandEnv(string(yamlBytes)))
|
||||||
// Parse configuration file
|
// Parse configuration file
|
||||||
err = yaml.Unmarshal(yamlBytes, &config)
|
err = yaml.Unmarshal(yamlBytes, &config)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
// Check if the configuration file at least has services.
|
// Check if the configuration file at least has services.
|
||||||
if config == nil || config.Services == nil || len(config.Services) == 0 {
|
if config == nil || config.Services == nil || len(config.Services) == 0 {
|
||||||
err = ErrNoServiceInConfig
|
err = ErrNoServiceInConfig
|
||||||
|
|||||||
+12
-2
@@ -82,7 +82,15 @@ func (c *Condition) evaluate(result *Result) bool {
|
|||||||
// If the condition isn't a success, return what the resolved condition was too
|
// If the condition isn't a success, return what the resolved condition was too
|
||||||
if !success {
|
if !success {
|
||||||
log.Printf("[Condition][evaluate] Condition '%s' did not succeed because '%s' is false", condition, resolvedCondition)
|
log.Printf("[Condition][evaluate] Condition '%s' did not succeed because '%s' is false", condition, resolvedCondition)
|
||||||
conditionToDisplay = fmt.Sprintf("%s (%s)", condition, resolvedCondition)
|
// Check if the resolved condition was an invalid path
|
||||||
|
isResolvedConditionInvalidPath := strings.ReplaceAll(resolvedCondition, fmt.Sprintf("%s ", InvalidConditionElementSuffix), "") == condition
|
||||||
|
if isResolvedConditionInvalidPath {
|
||||||
|
// Since, in the event of an invalid path, the resolvedCondition contains the condition itself,
|
||||||
|
// we'll only display the resolvedCondition
|
||||||
|
conditionToDisplay = resolvedCondition
|
||||||
|
} else {
|
||||||
|
conditionToDisplay = fmt.Sprintf("%s (%s)", condition, resolvedCondition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success})
|
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success})
|
||||||
return success
|
return success
|
||||||
@@ -138,7 +146,9 @@ func sanitizeAndResolve(list []string, result *Result) []string {
|
|||||||
}
|
}
|
||||||
resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.Replace(element, fmt.Sprintf("%s.", BodyPlaceHolder), "", 1), result.Body)
|
resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.Replace(element, fmt.Sprintf("%s.", BodyPlaceHolder), "", 1), result.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.Errors = append(result.Errors, err.Error())
|
if err.Error() != "unexpected end of JSON input" {
|
||||||
|
result.Errors = append(result.Errors, err.Error())
|
||||||
|
}
|
||||||
element = fmt.Sprintf("%s %s", element, InvalidConditionElementSuffix)
|
element = fmt.Sprintf("%s %s", element, InvalidConditionElementSuffix)
|
||||||
} else {
|
} else {
|
||||||
if wantLength {
|
if wantLength {
|
||||||
|
|||||||
@@ -113,6 +113,19 @@ func TestCondition_evaluateWithBodyJsonPathComplex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithInvalidBodyJsonPathComplex(t *testing.T) {
|
||||||
|
expectedResolvedCondition := "[BODY].data.name (INVALID) == john"
|
||||||
|
condition := Condition("[BODY].data.name == john")
|
||||||
|
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure, because the path was invalid", condition)
|
||||||
|
}
|
||||||
|
if result.ConditionResults[0].Condition != expectedResolvedCondition {
|
||||||
|
t.Errorf("Condition '%s' should have resolved to '%s', but resolved to '%s' instead", condition, expectedResolvedCondition, result.ConditionResults[0].Condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJsonPathDoublePlaceholders(t *testing.T) {
|
func TestCondition_evaluateWithBodyJsonPathDoublePlaceholders(t *testing.T) {
|
||||||
condition := Condition("[BODY].user.firstName != [BODY].user.lastName")
|
condition := Condition("[BODY].user.firstName != [BODY].user.lastName")
|
||||||
result := &Result{Body: []byte("{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}")}
|
result := &Result{Body: []byte("{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}")}
|
||||||
|
|||||||
@@ -13,6 +13,16 @@ data:
|
|||||||
interval: 5m
|
interval: 5m
|
||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
|
- name: cat-fact
|
||||||
|
url: "https://cat-fact.herokuapp.com/facts/random"
|
||||||
|
interval: 5m
|
||||||
|
conditions:
|
||||||
|
- "[STATUS] == 200"
|
||||||
|
- "[BODY].deleted == false"
|
||||||
|
- "len([BODY].text) > 0"
|
||||||
|
- "[BODY].text == pat(*cat*)"
|
||||||
|
- "[STATUS] == pat(2*)"
|
||||||
|
- "[CONNECTED] == true"
|
||||||
- name: Example
|
- name: Example
|
||||||
url: https://example.com/
|
url: https://example.com/
|
||||||
conditions:
|
conditions:
|
||||||
|
|||||||
+2
-1
@@ -2,12 +2,13 @@ package security
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Handler(handler http.HandlerFunc, security *Config) http.HandlerFunc {
|
func Handler(handler http.HandlerFunc, security *Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
usernameEntered, passwordEntered, ok := r.BasicAuth()
|
usernameEntered, passwordEntered, ok := r.BasicAuth()
|
||||||
if !ok || usernameEntered != security.Basic.Username || Sha512(passwordEntered) != security.Basic.PasswordSha512Hash {
|
if !ok || usernameEntered != security.Basic.Username || Sha512(passwordEntered) != strings.ToLower(security.Basic.PasswordSha512Hash) {
|
||||||
w.Header().Set("WWW-Authenticate", "Basic")
|
w.Header().Set("WWW-Authenticate", "Basic")
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
_, _ = w.Write([]byte("Unauthorized"))
|
_, _ = w.Write([]byte("Unauthorized"))
|
||||||
|
|||||||
Reference in new issue
Block a user