fix: validate k3s profile names against derived release name
`scheduler-k3s:profiles:add` accepted profile names it could not later derive a valid helm release name from, so a profile was created successfully and only failed on commands with no obvious relationship to it: `profiles:remove` could not delete the profile it created, and a single bad name broke every subsequent node sysctls change, including the global scope and unrelated profiles. Names are now refused at add time against the release name they derive rather than against the profile name alone, and the paths that have to cope with a profile stored before this check keep working instead of erroring.
This commit is contained in:
6 files changed
+308
-27
No files matched your search
@@ -9,7 +9,6 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -18,6 +17,7 @@ import (
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
resty "github.com/go-resty/resty/v2"
|
||||
"github.com/ryanuber/columnize"
|
||||
"helm.sh/helm/v3/pkg/chartutil"
|
||||
)
|
||||
|
||||
// CommandAnnotationsSet set or clear a scheduler-k3s annotation for an app
|
||||
@@ -55,6 +55,10 @@ func CommandNodeSysctlsSet(profileName string, key string, value string) error {
|
||||
if err := verifyNodeProfileExists(profileName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := verifyNodeProfileSysctlsSupported(profileName); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
property := getNodeSysctlsProperty(profileName)
|
||||
@@ -134,6 +138,17 @@ func verifyNodeProfileExists(profileName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// verifyNodeProfileSysctlsSupported returns an error when a node profile is stored under a
|
||||
// name dokku cannot derive a node sysctls release name from. Such a profile predates the
|
||||
// validation in profiles:add and must be recreated before it can carry node sysctls.
|
||||
func verifyNodeProfileSysctlsSupported(profileName string) error {
|
||||
if err := chartutil.ValidateReleaseName(getNodeSysctlsReleaseName(profileName)); err != nil {
|
||||
return fmt.Errorf("Node profile %s cannot carry node sysctls, recreate it with a name of at most %d lowercase alphanumeric characters and dashes: %w", profileName, maxNodeProfileNameLength, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandAutoscalingAuthSet set or clear a scheduler-k3s autoscaling keda trigger authentication object for an app
|
||||
func CommandAutoscalingAuthSet(appName string, trigger string, metadata map[string]string, global bool) error {
|
||||
if global {
|
||||
@@ -1253,18 +1268,8 @@ func CommandProfilesAdd(profileName string, role string, allowUknownHosts bool,
|
||||
return fmt.Errorf("Invalid role: %s", role)
|
||||
}
|
||||
|
||||
if profileName == "" {
|
||||
return fmt.Errorf("Missing profile name")
|
||||
}
|
||||
|
||||
// profile names must only contain alphanumeric characters and dashes and cannot start with a dash
|
||||
if !regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$`).MatchString(profileName) {
|
||||
return fmt.Errorf("Invalid profile name, must only contain alphanumeric characters and dashes and cannot start with a dash: %s", profileName)
|
||||
}
|
||||
|
||||
// ensure profile names are no longer than 32 characters
|
||||
if len(profileName) > 32 {
|
||||
return fmt.Errorf("Profile name is too long, must be less than 32 characters: %s", profileName)
|
||||
if err := validateNodeProfileName(profileName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
profile := NodeProfile{
|
||||
@@ -1338,18 +1343,8 @@ func CommandProfilesList(format string) error {
|
||||
|
||||
// CommandProfilesRemove removes a node profile from the k3s cluster
|
||||
func CommandProfilesRemove(profileName string) error {
|
||||
if profileName == "" {
|
||||
return fmt.Errorf("Missing profile name")
|
||||
}
|
||||
|
||||
// profile names must only contain alphanumeric characters and dashes and cannot start with a dash
|
||||
if !regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$`).MatchString(profileName) {
|
||||
return fmt.Errorf("Invalid profile name, must only contain alphanumeric characters and dashes and cannot start with a dash: %s", profileName)
|
||||
}
|
||||
|
||||
// ensure profile names are no longer than 32 characters
|
||||
if len(profileName) > 32 {
|
||||
return fmt.Errorf("Profile name is too long, must be less than 32 characters: %s", profileName)
|
||||
if err := validateStoredNodeProfileName(profileName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := common.PropertyDelete("scheduler-k3s", "--global", fmt.Sprintf("node-profile-%s.json", profileName)); err != nil {
|
||||
|
||||
Reference in new issue
Block a user