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

Add support for security policy #291

Merged
merged 3 commits into from
Jun 19, 2018
Merged

Conversation

MrHohn
Copy link
Member

@MrHohn MrHohn commented May 31, 2018

The first commit is for updating vendor. I will soon split that into another PR. Updated vendor in #293.
/assign @rramkumar1 @bowei

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 31, 2018
@@ -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)
Copy link
Contributor

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"

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

}
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)
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

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)
Copy link
Contributor

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..."

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

}
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)
Copy link
Contributor

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".

Copy link
Member Author

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",
Copy link
Contributor

@rramkumar1 rramkumar1 May 31, 2018

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"

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@@ -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 {
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. PTAL thanks!

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 1, 2018
@MrHohn MrHohn force-pushed the cloud-armor-support branch 2 times, most recently from ca6fda8 to d3facb3 Compare June 1, 2018 17:31
// 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) {
Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@MrHohn
Copy link
Member Author

MrHohn commented Jun 7, 2018

PR needs to be revised after #305.
/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 7, 2018
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 {
Copy link
Member

Choose a reason for hiding this comment

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

do not need else

Copy link
Member Author

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.

}
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)
Copy link
Member

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?

Copy link
Member Author

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.

// service.
beBeta, err := b.cloud.GetBetaGlobalBackendService(beGa.Name)
if err != nil {
glog.Errorf("Failed to get backend service %s through Beta API", beGa.Name)
Copy link
Member

Choose a reason for hiding this comment

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

log the error

Copy link
Member Author

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) {
Copy link
Member

Choose a reason for hiding this comment

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

comment

Copy link
Member Author

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 {
Copy link
Member

Choose a reason for hiding this comment

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

// EnsureSecurityPolicy ...

Copy link
Member Author

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 {
Copy link
Member

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)

Copy link
Member Author

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)
Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

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

t.Run(tc.desc, ...

"k8s.io/ingress-gce/pkg/utils"
)

// BackendService embeds all of the GA, beta and alpha compute
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

File removed.

@MrHohn MrHohn force-pushed the cloud-armor-support branch from e8ba106 to cf8c6a0 Compare June 15, 2018 01:02
@MrHohn
Copy link
Member Author

MrHohn commented Jun 15, 2018

Rebased on top of #305.

@MrHohn
Copy link
Member Author

MrHohn commented Jun 15, 2018

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 15, 2018
@MrHohn
Copy link
Member Author

MrHohn commented Jun 15, 2018

TODO: Define securityPolicy as a feature name in features.go. Will update PR.

@MrHohn
Copy link
Member Author

MrHohn commented Jun 15, 2018

TODO: Define securityPolicy as a feature name in features.go. Will update PR.

Done.

@MrHohn MrHohn force-pushed the cloud-armor-support branch from 291823f to 6061aa5 Compare June 15, 2018 19:16
@aaron-trout aaron-trout mentioned this pull request Jun 18, 2018
@MrHohn MrHohn force-pushed the cloud-armor-support branch from 6061aa5 to cbf463a Compare June 18, 2018 22:38
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jun 18, 2018
@MrHohn MrHohn force-pushed the cloud-armor-support branch from cbf463a to 7743c3e Compare June 18, 2018 22:44
@MrHohn MrHohn force-pushed the cloud-armor-support branch from 7743c3e to 2f87d2c Compare June 18, 2018 23:54
@MrHohn
Copy link
Member Author

MrHohn commented Jun 18, 2018

@rramkumar1 @bowei Rebased PR, PTAL, thanks!

@MrHohn MrHohn force-pushed the cloud-armor-support branch from 2f87d2c to 5e26719 Compare June 18, 2018 23:58
@rramkumar1
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 19, 2018
Copy link
Member

@bowei bowei left a 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 {
Copy link
Member

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

Copy link
Member Author

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)
Copy link
Member

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?

Copy link
Member Author

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)
Copy link
Member

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

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

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

see above

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

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

same

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

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

same

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@MrHohn MrHohn force-pushed the cloud-armor-support branch from 5e26719 to d40ea79 Compare June 19, 2018 21:13
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 19, 2018
@k8s-ci-robot
Copy link
Contributor

New changes are detected. LGTM label has been removed.

@bowei bowei merged commit 8821e6c into kubernetes:master Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants