-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api/vmauth: introduce unauthorizedUserAccessSpec field at vmauth.spec
Previously for unauthorized_user config section of vmauth. Operator used two configuration fields: `unauthorizedAccessConfig` and inlined fields from `VMUserOptions`. This behaviour doesn't aligh with configuration file supported by vmauth. It also incorrectly exposed fields from `VMUserOptions` at `spec`. Which could mislead users, since `spec.default_urls` could be treated as global config option for vmauth, but in fact, it can only be used at `unauthorized_user` section. This commit replaces both fields with the new field `unauthorizedUserAccess`. It combines both config options - `url_map` and `VMUserOptions`. Replaced fields marked as deprecated and will be removed at `v1.0` operator API release. Also `VMauth` now properly validates `unauthorized_user` related configuration and returns proper error to the user, instead of crashing `vmauth` container in runtime. Related issues: - #1168 - #1169 --------- Signed-off-by: f41gh7 <[email protected]> Co-authored-by: Hui Wang <[email protected]>
- Loading branch information
Showing
16 changed files
with
1,103 additions
and
569 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -1,57 +1,108 @@ | ||
package v1beta1 | ||
|
||
import ( | ||
"testing" | ||
"encoding/json" | ||
|
||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func TestVMAuth_sanityCheck(t *testing.T) { | ||
type fields struct { | ||
TypeMeta v1.TypeMeta | ||
ObjectMeta v1.ObjectMeta | ||
Spec VMAuthSpec | ||
Status VMAuthStatus | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "invalid ingress", | ||
fields: fields{ | ||
Spec: VMAuthSpec{ | ||
Ingress: &EmbeddedIngress{ | ||
TlsHosts: []string{"host-1", "host-2"}, | ||
}, | ||
}, | ||
var _ = Describe("VMAuth Webhook", func() { | ||
Context("When creating VMAuth under Validating Webhook", func() { | ||
DescribeTable("fail validation", | ||
func(srcYAML string, wantErrText string) { | ||
var amc VMAuth | ||
Expect(yaml.Unmarshal([]byte(srcYAML), &amc)).To(Succeed()) | ||
cfgJSON, err := json.Marshal(amc) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
Expect(json.Unmarshal(cfgJSON, &amc)).ShouldNot(HaveOccurred()) | ||
Expect(amc.sanityCheck()).To(MatchError(wantErrText)) | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "valid cfg", | ||
fields: fields{ | ||
Spec: VMAuthSpec{ | ||
Ingress: &EmbeddedIngress{ | ||
TlsHosts: []string{"host1"}, | ||
TlsSecretName: "secret-1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cr := &VMAuth{ | ||
TypeMeta: tt.fields.TypeMeta, | ||
ObjectMeta: tt.fields.ObjectMeta, | ||
Spec: tt.fields.Spec, | ||
Status: tt.fields.Status, | ||
} | ||
if err := cr.sanityCheck(); (err != nil) != tt.wantErr { | ||
t.Errorf("sanityCheck() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
Entry("invalid ingress", ` | ||
apiVersion: v1 | ||
kind: VMAuth | ||
metadata: | ||
name: must-fail | ||
spec: | ||
ingress: | ||
tlsHosts: | ||
- host-1 | ||
- host-2 | ||
`, `spec.ingress.tlsSecretName cannot be empty with non-empty spec.ingress.tlsHosts`), | ||
Entry("both configSecret and external config is defined at the same time", ` | ||
apiVersion: v1 | ||
kind: VMAuth | ||
metadata: | ||
name: must-fail | ||
spec: | ||
configSecret: some-value | ||
externalConfig: | ||
secretRef: | ||
key: secret | ||
name: access | ||
`, `spec.configSecret and spec.externalConfig.secretRef cannot be used at the same time`), | ||
Entry("incorrect unauthorized access config, missing backends", ` | ||
apiVersion: v1 | ||
kind: VMAuth | ||
metadata: | ||
name: must-fail | ||
spec: | ||
unauthorizedUserAccessSpec: | ||
default_url: | ||
- http://url-1 | ||
`, "incorrect r.spec.UnauthorizedUserAccess syntax: at least one of `url_map` or `url_prefix` must be defined"), | ||
Entry("incorrect unauthorized access config, bad metric_labels syntax", ` | ||
apiVersion: v1 | ||
kind: VMAuth | ||
metadata: | ||
name: must-fail | ||
spec: | ||
unauthorizedUserAccessSpec: | ||
metric_labels: | ||
124124asff: 12fsaf | ||
url_prefix: http://some-dst | ||
default_url: | ||
- http://url-1 | ||
`, `incorrect r.spec.UnauthorizedUserAccess syntax: incorrect metricLabelName="124124asff", must match pattern="^[a-zA-Z_:.][a-zA-Z0-9_:.]*$"`), | ||
Entry("incorrect unauthorized access config url_map", ` | ||
apiVersion: v1 | ||
kind: VMAuth | ||
metadata: | ||
name: must-fail | ||
spec: | ||
unauthorizedUserAccessSpec: | ||
metric_labels: | ||
label: 12fsaf-value | ||
url_map: | ||
- url_prefix: http://some-url | ||
src_paths: ["/path-1"] | ||
- url_prefix: http://some-url-2 | ||
default_url: | ||
- http://url-1 | ||
`, `incorrect r.spec.UnauthorizedUserAccess syntax: incorrect url_map at idx=1: incorrect url_map config at least of one src_paths,src_hosts,src_query_args or src_headers must be defined`, | ||
), | ||
Entry("both unauthorizedUserAccessSpec and UnauthorizedUserAccess defined", ` | ||
apiVersion: v1 | ||
kind: VMAuth | ||
metadata: | ||
name: must-fail | ||
spec: | ||
unauthorizedAccessConfig: | ||
- url_prefix: http://some-url | ||
src_paths: ["/path-1"] | ||
- url_prefix: http://some-url-2 | ||
src_paths: ["/path-1"] | ||
unauthorizedUserAccessSpec: | ||
metric_labels: | ||
label: 12fsaf-value | ||
url_map: | ||
- url_prefix: http://some-url | ||
src_paths: ["/path-1"] | ||
default_url: | ||
- http://url-1 | ||
`, "at most one option can be used `spec.unauthorizedAccessConfig` or `spec.unauthorizedUserAccessSpec`, got both", | ||
), | ||
) | ||
}) | ||
}) |
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
Oops, something went wrong.