-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom validation for BackendConfig + hook validation into Transl…
…ator
- Loading branch information
1 parent
d5aa264
commit 19a28b8
Showing
8 changed files
with
276 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters