-
Notifications
You must be signed in to change notification settings - Fork 306
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 custom validation for BackendConfig + hook validation into Translator #289
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package backendconfig | ||
|
||
import ( | ||
"fmt" | ||
|
||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1" | ||
) | ||
|
||
const ( | ||
OAuthClientIDKey = "client_id" | ||
OAuthClientSecretKey = "client_secret" | ||
) | ||
|
||
func Validate(kubeClient kubernetes.Interface, beConfig *backendconfigv1beta1.BackendConfig) error { | ||
if beConfig == nil { | ||
return nil | ||
} | ||
return validateIAP(kubeClient, beConfig) | ||
} | ||
|
||
// TODO(rramkumar): Return errors as constants so that the unit tests can distinguish | ||
// between which error is returned. | ||
func validateIAP(kubeClient kubernetes.Interface, beConfig *backendconfigv1beta1.BackendConfig) error { | ||
// If IAP settings are not found or IAP is not enabled then don't bother continuing. | ||
if beConfig.Spec.Iap == nil || beConfig.Spec.Iap.Enabled == false { | ||
return nil | ||
} | ||
// If necessary, get the OAuth credentials stored in the K8s secret. | ||
if beConfig.Spec.Iap.OAuthClientCredentials != nil && beConfig.Spec.Iap.OAuthClientCredentials.SecretName != "" { | ||
secretName := beConfig.Spec.Iap.OAuthClientCredentials.SecretName | ||
secret, err := kubeClient.Core().Secrets(beConfig.Namespace).Get(secretName, meta_v1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("error retrieving secret %v: %v", secretName, err) | ||
} | ||
clientID, ok := secret.Data[OAuthClientIDKey] | ||
if !ok { | ||
return fmt.Errorf("secret %v missing %v data", secretName, OAuthClientIDKey) | ||
} | ||
clientSecret, ok := secret.Data[OAuthClientSecretKey] | ||
if !ok { | ||
return fmt.Errorf("secret %v missing %v data'", secretName, OAuthClientSecretKey) | ||
} | ||
beConfig.Spec.Iap.OAuthClientCredentials.ClientID = string(clientID) | ||
beConfig.Spec.Iap.OAuthClientCredentials.ClientSecret = string(clientSecret) | ||
} | ||
|
||
if beConfig.Spec.Cdn != nil && beConfig.Spec.Cdn.Enabled { | ||
return fmt.Errorf("iap and cdn cannot be enabled at the same time") | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package backendconfig | ||
|
||
import ( | ||
"testing" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/kubernetes/fake" | ||
backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1" | ||
) | ||
|
||
var ( | ||
beConfig = &backendconfigv1beta1.BackendConfig{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Namespace: "default", | ||
}, | ||
Spec: backendconfigv1beta1.BackendConfigSpec{ | ||
Iap: &backendconfigv1beta1.IAPConfig{ | ||
Enabled: true, | ||
OAuthClientCredentials: &backendconfigv1beta1.OAuthClientCredentials{ | ||
SecretName: "foo", | ||
}, | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
func TestValidateIAP(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
init func(kubeClient kubernetes.Interface) | ||
expectError bool | ||
}{ | ||
{ | ||
desc: "secret does not exist", | ||
init: func(kubeClient kubernetes.Interface) { | ||
secret := &v1.Secret{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Namespace: "wrong-namespace", | ||
Name: "foo", | ||
}, | ||
} | ||
kubeClient.Core().Secrets("wrong-namespace").Create(secret) | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
desc: "secret does not contain client_id", | ||
init: func(kubeClient kubernetes.Interface) { | ||
secret := &v1.Secret{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "foo", | ||
}, | ||
Data: map[string][]byte{ | ||
"client_secret": []byte("my-secret"), | ||
}, | ||
} | ||
kubeClient.Core().Secrets("default").Create(secret) | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
desc: "secret does not contain client_secret", | ||
init: func(kubeClient kubernetes.Interface) { | ||
secret := &v1.Secret{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "foo", | ||
}, | ||
Data: map[string][]byte{ | ||
"client_id": []byte("my-id"), | ||
}, | ||
} | ||
kubeClient.Core().Secrets("default").Create(secret) | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
desc: "validation passes", | ||
init: func(kubeClient kubernetes.Interface) { | ||
secret := &v1.Secret{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "foo", | ||
}, | ||
Data: map[string][]byte{ | ||
"client_id": []byte("my-id"), | ||
"client_secret": []byte("my-secret"), | ||
}, | ||
} | ||
kubeClient.Core().Secrets("default").Create(secret) | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
desc: "iap and cdn enabled at the same time", | ||
init: func(kubeClient kubernetes.Interface) { | ||
// TODO(rramkumar): Don't modify in-flight. | ||
// This works now since this is the last test in the | ||
// list of cases. | ||
beConfig.Spec.Cdn = &backendconfigv1beta1.CDNConfig{ | ||
Enabled: true, | ||
} | ||
secret := &v1.Secret{ | ||
ObjectMeta: meta_v1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "foo", | ||
}, | ||
Data: map[string][]byte{ | ||
"client_id": []byte("my-id"), | ||
"client_secret": []byte("my-secret"), | ||
}, | ||
} | ||
kubeClient.Core().Secrets("default").Create(secret) | ||
}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
kubeClient := fake.NewSimpleClientset() | ||
testCase.init(kubeClient) | ||
err := Validate(kubeClient, beConfig) | ||
if testCase.expectError && err == nil { | ||
t.Errorf("%v: Expected error but got nil", testCase.desc) | ||
} | ||
if !testCase.expectError && err != nil { | ||
t.Errorf("%v: Did not expect error but got: %v", testCase.desc, err) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,16 +112,17 @@ PortLoop: | |
return nil, errors.ErrSvcNotNodePort{Service: id.Service} | ||
} | ||
|
||
var backendConfig *backendconfigv1beta1.BackendConfig | ||
var beConfig *backendconfigv1beta1.BackendConfig | ||
if t.ctx.BackendConfigInformer != nil { | ||
backendConfigInStore, err := backendconfig.GetBackendConfigForServicePort(t.ctx.BackendConfigInformer.GetIndexer(), svc, port) | ||
beConfig, err := backendconfig.GetBackendConfigForServicePort(t.ctx.BackendConfigInformer.GetIndexer(), svc, port) | ||
if err != nil { | ||
return nil, err | ||
return nil, errors.ErrSvcBackendConfig{ServicePortID: id, Err: err} | ||
} | ||
if backendConfigInStore != nil { | ||
// Object in cache could be changed in-flight. Deepcopy to | ||
// reduce race conditions. | ||
backendConfig = backendConfigInStore.DeepCopy() | ||
// Object in cache could be changed in-flight. Deepcopy to | ||
// reduce race conditions. | ||
beConfig = beConfig.DeepCopy() | ||
if err = backendconfig.Validate(t.ctx.KubeClient, beConfig); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to do this validation in an earlier stage (e.g. on BackendConfig creation/update)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Humm..Though ingress can still be put into queue triggered by service/ingress update so we need it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, without a webhook, we cannot do it on creation/update. |
||
return nil, errors.ErrBackendConfigValidation{BackendConfig: *beConfig, Err: err} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should check nil before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If its nil, then Validate() would not return an error so I dont think we would need that check. |
||
} | ||
} | ||
|
||
|
@@ -131,7 +132,7 @@ PortLoop: | |
Protocol: proto, | ||
SvcTargetPort: port.TargetPort.String(), | ||
NEGEnabled: t.negEnabled && negEnabled, | ||
BackendConfig: backendConfig, | ||
BackendConfig: beConfig, | ||
}, nil | ||
} | ||
|
||
|
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.
This might panic if
beConfig
is nil?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.
I checked the DeepCopy() code and it returns nil if the parameter is nil
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.
Ah good point, yeah it should work.