mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-11-21 18:54:41 +00:00
Validate usage strings when creating bootstrap tokens via kubeadm
This commit is contained in:
@@ -18,7 +18,9 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var bootstrapGroupRegexp = regexp.MustCompile(`\A` + BootstrapGroupPattern + `\z`)
|
||||
@@ -32,3 +34,19 @@ func ValidateBootstrapGroupName(name string) error {
|
||||
}
|
||||
return fmt.Errorf("bootstrap group %q is invalid (must match %s)", name, BootstrapGroupPattern)
|
||||
}
|
||||
|
||||
// ValidateUsages validates that the passed in string are valid usage strings for bootstrap tokens.
|
||||
func ValidateUsages(usages []string) error {
|
||||
usageAuthentication := strings.TrimPrefix(BootstrapTokenUsageAuthentication, BootstrapTokenUsagePrefix)
|
||||
usageSigning := strings.TrimPrefix(BootstrapTokenUsageSigningKey, BootstrapTokenUsagePrefix)
|
||||
invalidUsages := sets.NewString()
|
||||
for _, usage := range usages {
|
||||
if usage != usageAuthentication && usage != usageSigning {
|
||||
invalidUsages.Insert(usage)
|
||||
}
|
||||
}
|
||||
if len(invalidUsages) > 0 {
|
||||
return fmt.Errorf("invalide bootstrap token usage string: %s, valid usage option: %s, %s", strings.Join(invalidUsages.List(), ","), usageAuthentication, usageSigning)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user