Skip to content

Commit

Permalink
feat: add token format checking to yurtadm join process
Browse files Browse the repository at this point in the history
Signed-off-by: Liang Deng <283304489@qq.com>
  • Loading branch information
YTGhost committed Aug 27, 2023
1 parent 68d4079 commit d2d1a49
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/yurtadm/cmd/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ func newJoinData(args []string, opt *joinOptions) (*joinData, error) {
return nil, errors.New("join token is empty, so unable to bootstrap worker node.")
}

if !yurtadmutil.IsValidBootstrapToken(opt.token) {
return nil, errors.Errorf("the bootstrap token %s was not of the form %s", opt.token, yurtconstants.BootstrapTokenPattern)
}

Check warning on line 266 in pkg/yurtadm/cmd/join/join.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurtadm/cmd/join/join.go#L265-L266

Added lines #L265 - L266 were not covered by tests

if opt.nodeType != yurtconstants.EdgeNode && opt.nodeType != yurtconstants.CloudNode {
return nil, errors.Errorf("node type(%s) is invalid, only \"edge and cloud\" are supported", opt.nodeType)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/yurtadm/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const (
KubeletHostname = "--hostname-override=[^\"\\s]*"
KubeletEnvironmentFile = "EnvironmentFile=.*"

BootstrapTokenPattern = `\A([a-z0-9]{6})\.([a-z0-9]{16})\z`

DaemonReload = "systemctl daemon-reload"
RestartKubeletSvc = "systemctl restart kubelet"

Expand Down
10 changes: 10 additions & 0 deletions pkg/yurtadm/util/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -62,6 +63,9 @@ var (
PropagationPolicy = metav1.DeletePropagationBackground

ErrClusterVersionEmpty = errors.New("cluster version should not be empty")

// BootstrapTokenRegexp is a compiled regular expression of TokenRegexpString
BootstrapTokenRegexp = regexp.MustCompile(constants.BootstrapTokenPattern)
)

// RunJobAndCleanup runs the job, wait for it to be complete, and delete it
Expand Down Expand Up @@ -541,3 +545,9 @@ func GetDefaultClientSet() (*kubernetes.Clientset, error) {
}
return cliSet, nil
}

// IsValidBootstrapToken returns whether the given string is valid as a Bootstrap Token and
// in other words satisfies the BootstrapTokenRegexp
func IsValidBootstrapToken(token string) bool {
return BootstrapTokenRegexp.MatchString(token)
}

0 comments on commit d2d1a49

Please sign in to comment.