kube-openapi: Validator for `byte` does not match mashalling behavior

kube-openapi code generator encodes all []byte types as a string with byte format: https://github.com/kubernetes/kube-openapi/blob/34a5c6ab6a70d85502e2e15190fa5cf1b8f29d37/pkg/common/common.go#L218-L240 (Side-note: Most of the formats referred to in this list arent even supported by our openapi-validator and have no meaning: int32, int64, double, float)

The schema validator uses IsBase64 from github.com/asaskevich/govalidator to determine if a string fits byte format.

This validates the string against the regex:

^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$

However, native types deserialize []byte types using the standard go library, which internally uses base64.StdEncoding. base64.StdEncoding follows RFC4648.

Problem: RFC4648 allows empty string, but the pattern does not. There may be other differences.

Paths forward:

  1. Change IsBase64 to accept empty string and conform to RFC4648
    1. Pros:
      • byte format has a well-defined meaning
      • byte has same meaning between k8s native and CRD types
    2. Cons:
      • API Breaking without CRD Validation Ratcheting. Even with ratcheting, regex change would be surprising to users.
        • Mitigation (suggested by @apelisse): Inject maxLength: 1 into all usages of format: byte by users upon decode.
      • If we wait for CRD Validation Ratcheting we will have a long time before this is fixed
  2. Add new base64-rfc4648 format. Change code-generator to use new different format for []byte
    1. Pros:
      • API compatible change
    2. Cons:
      • New format addition might require we wait for CRD validation ratcheting
        1. Mitigation: Use an inline schema rather than adding a new format, and refer to it using $ref until format is added
      • CRDs who want to use RFC4648 are stuck with unspecified byte format
        1. Mitigation: After CRD-Validation Ratcheting is stable, we add new formats for the specific byte RFC. We then migrate native types to use the new format and deprecate old byte format.

Impact: Making the schemas accurate for native types is a prerequisite to KEP-4153: Declarative Validation. This is a blocker to using schemas to validate native types.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (13 by maintainers)

Most upvoted comments

Offline discussion between Jordan, Alex and I. Conclusions:

  • Don’t change what we return in openapi from the server. built-in types are accurate, CRDs are (buggily) stricter server-side but it’s ok for the server to be more strict than the declarative validation
  • Fix the byte bug in our validation fork in kube-openapi to allow “”, with a way to opt back into the stricter buggy behavior
  • kubectl-validate can update to use the fixed relaxed library
  • kube-apiserver can update to use the fixed relaxed library but opt back into stricter buggy validation for CRDs until we land ratcheting

we should obviously only do that once ratcheting would keep from breaking existing data