2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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 v1beta1
|
|
|
|
|
|
|
|
import (
|
2020-03-26 21:07:15 +00:00
|
|
|
"crypto/x509"
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
certificatesv1beta1 "k8s.io/api/certificates/v1beta1"
|
2020-08-10 17:43:49 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2019-01-12 04:58:27 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2020-03-26 21:07:15 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2020-08-10 17:43:49 +00:00
|
|
|
certificates "k8s.io/kubernetes/pkg/apis/certificates"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func addDefaultingFuncs(scheme *runtime.Scheme) error {
|
|
|
|
return RegisterDefaults(scheme)
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
func SetDefaults_CertificateSigningRequestSpec(obj *certificatesv1beta1.CertificateSigningRequestSpec) {
|
|
|
|
if obj.Usages == nil {
|
|
|
|
obj.Usages = []certificatesv1beta1.KeyUsage{certificatesv1beta1.UsageDigitalSignature, certificatesv1beta1.UsageKeyEncipherment}
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
|
|
|
|
if obj.SignerName == nil {
|
|
|
|
signerName := DefaultSignerNameFromSpec(obj)
|
|
|
|
obj.SignerName = &signerName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func SetDefaults_CertificateSigningRequestCondition(obj *certificatesv1beta1.CertificateSigningRequestCondition) {
|
|
|
|
if len(obj.Status) == 0 {
|
|
|
|
obj.Status = v1.ConditionTrue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
// DefaultSignerNameFromSpec will determine the signerName that should be set
|
|
|
|
// by attempting to inspect the 'request' content and the spec options.
|
|
|
|
func DefaultSignerNameFromSpec(obj *certificatesv1beta1.CertificateSigningRequestSpec) string {
|
|
|
|
csr, err := ParseCSR(obj.Request)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
// Set the signerName to 'legacy-unknown' as the CSR could not be
|
|
|
|
// recognised.
|
|
|
|
return certificatesv1beta1.LegacyUnknownSignerName
|
|
|
|
case IsKubeletClientCSR(csr, obj.Usages):
|
|
|
|
return certificatesv1beta1.KubeAPIServerClientKubeletSignerName
|
|
|
|
case IsKubeletServingCSR(csr, obj.Usages):
|
|
|
|
return certificatesv1beta1.KubeletServingSignerName
|
|
|
|
default:
|
|
|
|
return certificatesv1beta1.LegacyUnknownSignerName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsKubeletServingCSR(req *x509.CertificateRequest, usages []certificatesv1beta1.KeyUsage) bool {
|
2020-08-10 17:43:49 +00:00
|
|
|
return certificates.IsKubeletServingCSR(req, usagesToSet(usages))
|
|
|
|
}
|
|
|
|
func ValidateKubeletServingCSR(req *x509.CertificateRequest, usages []certificatesv1beta1.KeyUsage) error {
|
|
|
|
return certificates.ValidateKubeletServingCSR(req, usagesToSet(usages))
|
2020-03-26 21:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsKubeletClientCSR(req *x509.CertificateRequest, usages []certificatesv1beta1.KeyUsage) bool {
|
2020-08-10 17:43:49 +00:00
|
|
|
return certificates.IsKubeletClientCSR(req, usagesToSet(usages))
|
|
|
|
}
|
|
|
|
func ValidateKubeletClientCSR(req *x509.CertificateRequest, usages []certificatesv1beta1.KeyUsage) error {
|
|
|
|
return certificates.ValidateKubeletClientCSR(req, usagesToSet(usages))
|
2020-03-26 21:07:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func usagesToSet(usages []certificatesv1beta1.KeyUsage) sets.String {
|
|
|
|
result := sets.NewString()
|
|
|
|
for _, usage := range usages {
|
|
|
|
result.Insert(string(usage))
|
2020-03-26 21:07:15 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
return result
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|