Skip to content

Commit

Permalink
add prefix node ID to workUnit ID, validation for node ID and add doc…
Browse files Browse the repository at this point in the history
…s for node ID validation
  • Loading branch information
matoval committed Oct 31, 2024
1 parent e810de0 commit 4f6d051
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/source/user_guide/configuration_options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Node
- Default value
- Type
* - ``id``
- Node ID
- Node ID can only contain a-z, A-Z, 0-9 or special characters . - _
- local hostname
- string
* - ``datadir``
Expand Down
11 changes: 11 additions & 0 deletions pkg/types/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"regexp"
"strings"

"github.com/ansible/receptor/pkg/controlsvc"
Expand Down Expand Up @@ -34,6 +35,16 @@ func (cfg NodeCfg) Init() error {
return fmt.Errorf("no node ID specified and local host name is localhost")
}
cfg.ID = host
} else {
submitIDRegex := regexp.MustCompile(`^[.\-_a-zA-Z0-9]*$`)
match := submitIDRegex.FindSubmatch([]byte(cfg.ID))
for _, m := range match {
fmt.Println("HERE: " + string(m))
fmt.Println(cfg.ID)
}
if match == nil {
return fmt.Errorf("node id can only contain a-z, A-Z, 0-9 or special characters . - _ but received: %s", cfg.ID)
}
}
if strings.ToLower(cfg.ID) == "localhost" {
return fmt.Errorf("node ID \"localhost\" is reserved")
Expand Down
9 changes: 6 additions & 3 deletions pkg/workceptor/remote_work.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,15 @@ func (rw *remoteUnit) startRemoteUnit(ctx context.Context, conn net.Conn, reader
if err != nil {
return fmt.Errorf("read error reading from %s: %s", red.RemoteNode, err)
}
submitIDRegex := regexp.MustCompile(`with ID ([a-zA-Z0-9]+)\.`)
submitIDRegex := regexp.MustCompile(`([.\-_a-zA-Z0-9]+)~([a-zA-Z0-9]+){8}`)
match := submitIDRegex.FindSubmatch([]byte(response))
if match == nil || len(match) != 2 {
for _, m := range match {
fmt.Println("HERE: " + string(m))
}
if match == nil || len(match) != 3 {
return fmt.Errorf("could not parse response: %s", strings.TrimRight(response, "\n"))
}
red.RemoteUnitID = string(match[1])
red.RemoteUnitID = string(match[0])
rw.UpdateFullStatus(func(status *StatusFileData) {
ed := status.ExtraData.(*RemoteExtraData)
ed.RemoteUnitID = red.RemoteUnitID
Expand Down
4 changes: 3 additions & 1 deletion pkg/workceptor/workceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func (w *Workceptor) generateUnitID(lock bool, workUnitID string) (string, error
var ident string
for {
if workUnitID == "" {
ident = randstr.RandomString(8)
rstr := randstr.RandomString(8)
nid := w.nc.NodeID()
ident = fmt.Sprintf("%s~%s", nid, rstr)
} else {
ident = workUnitID
unitdir := path.Join(w.dataDir, ident)
Expand Down

0 comments on commit 4f6d051

Please sign in to comment.