-
Notifications
You must be signed in to change notification settings - Fork 303
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
Add support for security policy #291
Conversation
pkg/annotations/service.go
Outdated
@@ -117,10 +117,10 @@ func (svc Service) GetBackendConfigs() (*BackendConfigs, error) { | |||
|
|||
configs := BackendConfigs{} | |||
if err := json.Unmarshal([]byte(val), &configs); err != nil { | |||
return nil, err | |||
return nil, fmt.Errorf("invalid format in backend config annotation: %v, error: %v", val, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use BackendConfigKey instead of "backend config annotation"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
pkg/annotations/service.go
Outdated
} | ||
if configs.Default == "" && len(configs.Ports) == 0 { | ||
return nil, fmt.Errorf("no backendConfigs found in annotation: %v", val) | ||
return nil, fmt.Errorf("either one of `ports` or `default` should be specified in annotation: %v", val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use BackendConfigKey to specify exact annotation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
pkg/backendconfig/backendconfig.go
Outdated
if err != nil { | ||
return nil, fmt.Errorf("failed to get BackendConfig %s, referenced by service %s/%s: %v", configName, svc.Namespace, svc.Name, err) | ||
} else if !exists { | ||
return nil, fmt.Errorf("BackendConfig %s not exist, but is referenced by service %s/%s", configName, svc.Namespace, svc.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"BackendConfig %s does not exist..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
pkg/backendconfig/backendconfig.go
Outdated
} | ||
backendConfig, ok := obj.(*backendconfigv1beta1.BackendConfig) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid format detected in BackendConfig %s, referenced by service %s/%s", configName, svc.Namespace, svc.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just say "failed to parse BackendConfig %s" instead of "invalid format detected".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -323,6 +323,18 @@ func TestGetBackendConfigForServicePort(t *testing.T) { | |||
}, | |||
expectErr: true, | |||
}, | |||
{ | |||
desc: "invalid format in backend config", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"invalid field in backend config"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
pkg/backends/backends.go
Outdated
@@ -263,6 +283,38 @@ func (b *Backends) ensureHealthCheck(sp utils.ServicePort) (string, error) { | |||
return b.healthChecker.Sync(hc) | |||
} | |||
|
|||
// TODO(mrhohn): Emit event when attach/detach security policy to backend service. | |||
func (b *Backends) ensureSecurityPolicy(sp utils.ServicePort, be *BackendService, beName string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed offline, can we move this to pkg/backends/features/securitypolicy.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. PTAL thanks!
ca6fda8
to
d3facb3
Compare
pkg/backends/backends.go
Outdated
// The idea is to avoid impacting the usual workflow when security policy | ||
// is irrelevant, especially when the Beta endpoint goes down. | ||
if unknown && desiredPolicyLink != "" || | ||
retrieveObjectName(existingPolicyLink) != retrieveObjectName(desiredPolicyLink) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Humm..Seems like I put a wrong condition here. Will push a new commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
PR needs to be revised after #305. |
pkg/backendconfig/backendconfig.go
Outdated
return nil, fmt.Errorf("failed to get BackendConfig %s for service %s/%s: %v", configName, svc.Namespace, svc.Name, err) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get BackendConfig %s, referenced by service %s/%s: %v", configName, svc.Namespace, svc.Name, err) | ||
} else if !exists { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not need else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will separate to another PR.
pkg/backendconfig/backendconfig.go
Outdated
} | ||
backendConfig, ok := obj.(*backendconfigv1beta1.BackendConfig) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to parse BackendConfig %s, referenced by service %s/%s", configName, svc.Namespace, svc.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this actually a parse error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will separate to another PR.
pkg/backends/backends.go
Outdated
// service. | ||
beBeta, err := b.cloud.GetBetaGlobalBackendService(beGa.Name) | ||
if err != nil { | ||
glog.Errorf("Failed to get backend service %s through Beta API", beGa.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log the error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will separate to another PR.
ProjectID() string | ||
} | ||
|
||
func getSecurityPolicyLink(be *unity.BackendService) (securityPolicyLink string, unknown bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function removed.
} | ||
|
||
// TODO(mrhohn): Emit event when attach/detach security policy to backend service. | ||
func EnsureSecurityPolicy(cloud SecurityPolicySetter, sp utils.ServicePort, be *unity.BackendService, beName string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// EnsureSecurityPolicy ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
) | ||
|
||
func TestEnsureSecurityPolicy(t *testing.T) { | ||
testCases := []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.Parallel() (on all unit tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
} | ||
|
||
for _, tc := range testCases { | ||
fakeCloud := fake.NewFakeBackendServices(tc.errFunc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.Run(tc.desc, func(t*testing.T) {
} | ||
|
||
for _, tc := range testCases { | ||
policyLink, unknown := getSecurityPolicyLink(tc.backendService) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.Run(tc.desc, ...
pkg/backends/unity/backendservice.go
Outdated
"k8s.io/ingress-gce/pkg/utils" | ||
) | ||
|
||
// BackendService embeds all of the GA, beta and alpha compute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you describe what the semantics of the storing some combination of Alpha, Beta, GA is? which one is preferred? What if there is a conflict etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File removed.
e8ba106
to
cf8c6a0
Compare
Rebased on top of #305. |
/hold cancel |
TODO: Define securityPolicy as a feature name in features.go. Will update PR. |
Done. |
291823f
to
6061aa5
Compare
6061aa5
to
cbf463a
Compare
cbf463a
to
7743c3e
Compare
7743c3e
to
2f87d2c
Compare
@rramkumar1 @bowei Rebased PR, PTAL, thanks! |
2f87d2c
to
5e26719
Compare
/lgtm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other than the test comments, lgtm
) | ||
|
||
// TODO(mrhohn): Upstream this mock hook to gce provider. | ||
func setSecurityPolicyHook(_ context.Context, key *meta.Key, ref *computebeta.SecurityPolicyReference, _ *cloud.MockBetaBackendServices) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this inside the test if possible, avoid global locking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, done.
t.Parallel() | ||
|
||
fakeGCE := gce.FakeGCECloud(gce.DefaultTestClusterValues()) | ||
fakeBeName := fmt.Sprintf("be-name-XXX-%s", tc.desc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need tc.desc in the backend name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose is to avoid tests conflicting with each other given that they run in parallel. Changed to use index instead.
(fakeGCE.Compute().(*cloud.MockGCE)).MockBetaBackendServices.SetSecurityPolicyHook = setSecurityPolicyHook | ||
|
||
if err := EnsureSecurityPolicy(fakeGCE, utils.ServicePort{BackendConfig: tc.desiredConfig}, tc.currentBackendService, fakeBeName); err != nil { | ||
t.Errorf("%s: EnsureSecurityPolicy()=%v, want nil", tc.desc, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove tc.desc, not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
// Verify whether the desired policy is set. | ||
policyRef, ok := mockSecurityPolcies[fakeBeName] | ||
if !ok { | ||
t.Errorf("%s: policy not set for backend service %s", tc.desc, fakeBeName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
desiredPolicyName = tc.desiredConfig.Spec.SecurityPolicy.Name | ||
} | ||
if utils.EqualResourceIDs(policyLink, desiredPolicyName) { | ||
t.Errorf("%s: got policy %q, want %q", tc.desc, policyLink, desiredPolicyName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
// Verify not set call is made. | ||
policyRef, ok := mockSecurityPolcies[fakeBeName] | ||
if ok { | ||
t.Errorf("%s: unexpected policy %q is set for backend service %s", tc.desc, policyRef, fakeBeName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
5e26719
to
d40ea79
Compare
New changes are detected. LGTM label has been removed. |
The first commit is for updating vendor. I will soon split that into another PR.Updated vendor in #293./assign @rramkumar1 @bowei