Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement security policy validator for real #393

Merged
merged 1 commit into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion pkg/fuzz/features/security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ limitations under the License.
package features

import (
"context"
"fmt"
"net/http"

"k8s.io/api/extensions/v1beta1"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce/cloud/meta"

"k8s.io/ingress-gce/pkg/annotations"
"k8s.io/ingress-gce/pkg/fuzz"
)

Expand Down Expand Up @@ -63,7 +68,50 @@ func (v *securityPolicyValidator) ConfigureAttributes(env fuzz.ValidatorEnv, ing

// CheckResponse implements fuzz.FeatureValidator.
func (v *securityPolicyValidator) CheckResponse(host, path string, resp *http.Response, body []byte) (fuzz.CheckResponseAction, error) {
// There isn't anything interesting to check in response.
backendConfig, err := fuzz.BackendConfigForPath(host, path, v.ing, v.env)
if err != nil {
if err == annotations.ErrBackendConfigAnnotationMissing {
// Don't fail this test if the service associated
// with the host + path has no BackendConfig annotation.
return fuzz.CheckResponseContinue, nil
}
return fuzz.CheckResponseContinue, err
}
if backendConfig.Spec.SecurityPolicy == nil || backendConfig.Spec.SecurityPolicy.Name == "" {
// Don't check on response if security policy isn't configured
// in BackendConfig or is set to none.
return fuzz.CheckResponseContinue, nil
}

policy, err := v.env.Cloud().BetaSecurityPolicies().Get(context.Background(), meta.GlobalKey(backendConfig.Spec.SecurityPolicy.Name))
if err != nil {
return fuzz.CheckResponseContinue, fmt.Errorf("error getting security policy %q: %v", backendConfig.Spec.SecurityPolicy.Name, err)
}
// Check for the exact response code we are expecting.
// For simplicity, the test assumes only the default rule exists.
if len(policy.Rules) == 0 {
return fuzz.CheckResponseContinue, fmt.Errorf("found 0 rule for security policy %q", backendConfig.Spec.SecurityPolicy.Name)
}
var expectedCode int
switch policy.Rules[0].Action {
case "allow":
expectedCode = 200
case "deny(403)":
expectedCode = 403
case "deny(404)":
expectedCode = 404
case "deny(502)":
expectedCode = 502
default:
return fuzz.CheckResponseContinue, fmt.Errorf("unrecognized rule %q", policy.Rules[0].Action)
}
if resp.StatusCode != expectedCode {
return fuzz.CheckResponseContinue, fmt.Errorf("unexpected status code %d, want %d", resp.StatusCode, expectedCode)
}
// Skip standard check in case of deny policy.
if expectedCode != 200 {
return fuzz.CheckResponseSkip, nil
}
return fuzz.CheckResponseContinue, nil
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/fuzz/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ func ServiceMapFromIngress(ing *v1beta1.Ingress) ServiceMap {
for _, path := range rule.HTTP.Paths {
hp := HostPath{Host: rule.Host, Path: path.Path}
if _, ok := ret[hp]; !ok {
ret[hp] = &path.Backend
// Copy the value over to a new struct so that we won't be
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the purpose for this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During the test I found we are saving the same pointer for path.Backend in this for loop because path is defined in the init statement.

Ref https://golang.org/ref/spec#For_statements: "Variables declared by the init statement are re-used in each iteration."

// saving the same pointer.
cloneBackend := path.Backend
ret[hp] = &cloneBackend
}
}
}
Expand Down