1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
//go:build !no_workceptor
// +build !no_workceptor
package workceptor
import (
"context"
"crypto/tls"
"fmt"
"io"
"io/fs"
"net"
"os"
"path"
"reflect"
"regexp"
"strings"
"sync"
"time"
"github.com/ansible/receptor/pkg/certificates"
"github.com/ansible/receptor/pkg/controlsvc"
"github.com/ansible/receptor/pkg/logger"
"github.com/ansible/receptor/pkg/netceptor"
"github.com/ansible/receptor/pkg/randstr"
"github.com/ansible/receptor/pkg/utils"
"github.com/golang-jwt/jwt/v4"
)
// NetceptorForWorkceptor is a interface to decouple workceptor from netceptor.
// it includes only the functions that workceptor uses.
type NetceptorForWorkceptor interface {
NodeID() string
AddWorkCommand(typeName string, verifySignature bool) error
GetClientTLSConfig(name string, expectedHostName string, expectedHostNameType netceptor.ExpectedHostnameType) (*tls.Config, error) // have a common pkg for types
GetLogger() *logger.ReceptorLogger
DialContext(ctx context.Context, node string, service string, tlscfg *tls.Config) (*netceptor.Conn, error) // create an interface for Conn
}
type ServerForWorkceptor interface {
AddControlFunc(name string, cType controlsvc.ControlCommandType) error
ConnectionListener(ctx context.Context, listener net.Listener)
RunControlSession(conn net.Conn)
RunControlSvc(ctx context.Context, service string, tlscfg *tls.Config, unixSocket string, unixSocketPermissions fs.FileMode, tcpListen string, tcptls *tls.Config) error
SetServerNet(n controlsvc.Neter)
SetServerTLS(t controlsvc.Tlser)
SetServerUtils(u controlsvc.Utiler)
SetupConnection(conn net.Conn)
}
// Workceptor is the main object that handles unit-of-work management.
type Workceptor struct {
ctx context.Context
Cancel context.CancelFunc
nc NetceptorForWorkceptor
dataDir string
workTypesLock *sync.RWMutex
workTypes map[string]*workType
activeUnitsLock *sync.RWMutex
activeUnits map[string]WorkUnit
SigningKey string
SigningExpiration time.Duration
VerifyingKey string
}
// workType is the record for a registered type of work.
type workType struct {
newWorkerFunc NewWorkerFunc
verifySignature bool
}
// New constructs a new Workceptor instance.
func New(ctx context.Context, nc NetceptorForWorkceptor, dataDir string) (*Workceptor, error) {
if dataDir == "" {
dataDir = path.Join(os.TempDir(), "receptor")
}
dataDir = path.Join(dataDir, nc.NodeID())
c, cancel := context.WithCancel(ctx)
w := &Workceptor{
ctx: c,
Cancel: cancel,
nc: nc,
dataDir: dataDir,
workTypesLock: &sync.RWMutex{},
workTypes: make(map[string]*workType),
activeUnitsLock: &sync.RWMutex{},
activeUnits: make(map[string]WorkUnit),
SigningKey: "",
SigningExpiration: 5 * time.Minute,
VerifyingKey: "",
}
err := w.RegisterWorker("remote", newRemoteWorker, false)
if err != nil {
return nil, fmt.Errorf("could not register remote worker function: %s", err)
}
return w, nil
}
// MainInstance is the global instance of Workceptor instantiated by the command-line main() function.
var MainInstance *Workceptor
// stdoutSize returns size of stdout, if it exists, or 0 otherwise.
func stdoutSize(unitdir string) int64 {
stat, err := os.Stat(path.Join(unitdir, "stdout"))
if err != nil {
return 0
}
return stat.Size()
}
// RegisterWithControlService registers this workceptor instance with a control service instance.
func (w *Workceptor) RegisterWithControlService(cs ServerForWorkceptor) error {
err := cs.AddControlFunc("work", &workceptorCommandType{
w: w,
})
if err != nil {
return fmt.Errorf("could not add work control function: %s", err)
}
return nil
}
// RegisterWorker notifies the Workceptor of a new kind of work that can be done.
func (w *Workceptor) RegisterWorker(typeName string, newWorkerFunc NewWorkerFunc, verifySignature bool) error {
w.workTypesLock.Lock()
_, ok := w.workTypes[typeName]
if ok {
w.workTypesLock.Unlock()
return fmt.Errorf("work type %s already registered", typeName)
}
w.workTypes[typeName] = &workType{
newWorkerFunc: newWorkerFunc,
verifySignature: verifySignature,
}
if typeName != "remote" { // all workceptors get a remote command by default
w.nc.AddWorkCommand(typeName, verifySignature)
}
w.workTypesLock.Unlock()
// Check if any unknown units have now become known
w.activeUnitsLock.Lock()
for id, worker := range w.activeUnits {
_, ok := worker.(*unknownUnit)
if ok && worker.Status().WorkType == typeName {
delete(w.activeUnits, id)
}
}
w.activeUnitsLock.Unlock()
w.scanForUnits()
return nil
}
func (w *Workceptor) generateUnitID(lock bool, workUnitID string) (string, error) {
if lock {
w.activeUnitsLock.RLock()
defer w.activeUnitsLock.RUnlock()
}
var ident string
for {
if workUnitID == "" {
rstr := randstr.RandomString(8)
nid := regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(w.nc.NodeID(), "")
ident = fmt.Sprintf("%s%s", nid, rstr)
} else {
ident = workUnitID
unitdir := path.Join(w.dataDir, ident)
_, err := os.Stat(unitdir)
if err == nil {
return "", fmt.Errorf("workunit ID %s is already in use, cannot use the same workunit ID more than once", ident)
}
}
_, ok := w.activeUnits[ident]
if !ok {
unitdir := path.Join(w.dataDir, ident)
_, err := os.Stat(unitdir)
if err == nil {
continue
}
return ident, os.MkdirAll(unitdir, 0o700)
}
}
}
func (w *Workceptor) createSignature(nodeID string) (string, error) {
if w.SigningKey == "" {
return "", fmt.Errorf("cannot sign work: signing key is empty")
}
exp := time.Now().Add(w.SigningExpiration)
claims := &jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(exp),
Audience: []string{nodeID},
}
rsaPrivateKey, err := certificates.LoadPrivateKey(w.SigningKey, &certificates.OsWrapper{})
if err != nil {
return "", fmt.Errorf("could not load signing key file: %s", err.Error())
}
token := jwt.NewWithClaims(jwt.SigningMethodRS512, claims)
tokenString, err := token.SignedString(rsaPrivateKey)
if err != nil {
return "", err
}
return tokenString, nil
}
func (w *Workceptor) ShouldVerifySignature(workType string, signWork bool) bool {
// if work unit is remote, just get the signWork boolean from the
// remote extra data field
if workType == "remote" {
return signWork
}
w.workTypesLock.RLock()
wt, ok := w.workTypes[workType]
w.workTypesLock.RUnlock()
if ok && wt.verifySignature {
return true
}
return false
}
func (w *Workceptor) VerifySignature(signature string) error {
if signature == "" {
return fmt.Errorf("could not verify signature: signature is empty")
}
if w.VerifyingKey == "" {
return fmt.Errorf("could not verify signature: verifying key not specified")
}
rsaPublicKey, err := certificates.LoadPublicKey(w.VerifyingKey, &certificates.OsWrapper{})
if err != nil {
return fmt.Errorf("could not load verifying key file: %s", err.Error())
}
token, err := jwt.ParseWithClaims(signature, &jwt.RegisteredClaims{}, func(token *jwt.Token) (interface{}, error) {
return rsaPublicKey, nil
})
if err != nil {
return fmt.Errorf("could not verify signature: %s", err.Error())
}
if !token.Valid {
return fmt.Errorf("token not valid")
}
claims := token.Claims.(*jwt.RegisteredClaims)
ok := claims.VerifyAudience(w.nc.NodeID(), true)
if !ok {
return fmt.Errorf("token audience did not match node ID")
}
return nil
}
// AllocateUnit creates a new local work unit and generates an identifier for it.
func (w *Workceptor) AllocateUnit(workTypeName string, workUnitID string, params map[string]string) (WorkUnit, error) {
w.workTypesLock.RLock()
wt, ok := w.workTypes[workTypeName]
w.workTypesLock.RUnlock()
if !ok {
return nil, fmt.Errorf("unknown work type %s", workTypeName)
}
w.activeUnitsLock.Lock()
defer w.activeUnitsLock.Unlock()
ident, err := w.generateUnitID(false, workUnitID)
if err != nil {
return nil, err
}
worker := wt.newWorkerFunc(nil, w, ident, workTypeName)
err = worker.SetFromParams(params)
if err == nil {
err = worker.Save()
}
if err != nil {
return nil, err
}
w.activeUnits[ident] = worker
return worker, nil
}
// AllocateRemoteUnit creates a new remote work unit and generates a local identifier for it.
func (w *Workceptor) AllocateRemoteUnit(remoteNode, remoteWorkType, workUnitID string, tlsClient, ttl string, signWork bool, params map[string]string) (WorkUnit, error) {
if tlsClient != "" {
_, err := w.nc.GetClientTLSConfig(tlsClient, "testhost", netceptor.ExpectedHostnameTypeReceptor)
if err != nil {
return nil, err
}
}
hasSecrets := false
for k := range params {
if strings.HasPrefix(strings.ToLower(k), "secret_") {
hasSecrets = true
break
}
}
if hasSecrets && tlsClient == "" {
return nil, fmt.Errorf("cannot send secrets over a non-TLS connection")
}
rw, err := w.AllocateUnit("remote", workUnitID, params)
if err != nil {
return nil, err
}
var expiration time.Time
if ttl != "" {
duration, err := time.ParseDuration(ttl)
if err != nil {
w.nc.GetLogger().Error("Failed to parse provided ttl -- valid examples include '1.5h', '30m', '30m10s'")
return nil, err
}
if signWork && duration > w.SigningExpiration {
w.nc.GetLogger().Warning("json web token expires before ttl")
}
expiration = time.Now().Add(duration)
} else {
expiration = time.Time{}
}
rw.UpdateFullStatus(func(status *StatusFileData) {
ed := status.ExtraData.(*RemoteExtraData)
ed.RemoteNode = remoteNode
ed.RemoteWorkType = remoteWorkType
ed.TLSClient = tlsClient
ed.Expiration = expiration
ed.SignWork = signWork
})
if rw.LastUpdateError() != nil {
return nil, rw.LastUpdateError()
}
return rw, nil
}
func (w *Workceptor) scanForUnit(unitID string) {
unitdir := path.Join(w.dataDir, unitID)
fi, _ := os.Stat(unitdir)
if fi == nil || !fi.IsDir() {
w.nc.GetLogger().Error("Error locating unit: %s", unitID)
return
}
ident := fi.Name()
w.activeUnitsLock.RLock()
_, ok := w.activeUnits[ident] //nolint:ifshort
w.activeUnitsLock.RUnlock()
if !ok {
statusFilename := path.Join(unitdir, "status")
sfd := &StatusFileData{}
serr := sfd.Load(statusFilename)
if serr != nil {
w.nc.GetLogger().Error("Error loading %s: %s", statusFilename, serr)
}
w.workTypesLock.RLock()
wt, ok := w.workTypes[sfd.WorkType]
w.workTypesLock.RUnlock()
var worker WorkUnit
if ok {
worker = wt.newWorkerFunc(nil, w, ident, sfd.WorkType)
} else {
worker = newUnknownWorker(w, ident, sfd.WorkType)
}
if _, err := os.Stat(statusFilename); os.IsNotExist(err) {
w.nc.GetLogger().Error("Status file has disappeared for %s.", ident)
return
}
err := worker.Load()
if err != nil {
w.nc.GetLogger().Warning("Failed to restart worker %s due to read error: %s", unitdir, err)
worker.UpdateBasicStatus(WorkStateFailed, fmt.Sprintf("Failed to restart: %s", err), stdoutSize(unitdir))
}
err = worker.Restart()
if err != nil && !IsPending(err) {
w.nc.GetLogger().Warning("Failed to restart worker %s: %s", unitdir, err)
worker.UpdateBasicStatus(WorkStateFailed, fmt.Sprintf("Failed to restart: %s", err), stdoutSize(unitdir))
}
w.activeUnitsLock.Lock()
defer w.activeUnitsLock.Unlock()
w.activeUnits[ident] = worker
}
}
func (w *Workceptor) scanForUnits() {
files, err := os.ReadDir(w.dataDir)
if err != nil {
return
}
for i := range files {
fi := files[i]
w.scanForUnit(fi.Name())
}
}
func (w *Workceptor) findUnit(unitID string) (WorkUnit, error) {
w.activeUnitsLock.RLock()
defer w.activeUnitsLock.RUnlock()
unit, ok := w.activeUnits[unitID]
if ok {
return unit, nil
}
// if not in active units, rescan work unit dir and recheck
w.scanForUnit(unitID)
unit, ok = w.activeUnits[unitID]
if !ok {
return nil, fmt.Errorf("unknown work unit %s", unitID)
}
return unit, nil
}
// StartUnit starts a unit of work.
func (w *Workceptor) StartUnit(unitID string) error {
unit, err := w.findUnit(unitID)
if err != nil {
return err
}
return unit.Start()
}
// ListKnownUnitIDs returns a slice containing the known unit IDs.
func (w *Workceptor) ListKnownUnitIDs() []string {
w.activeUnitsLock.RLock()
defer w.activeUnitsLock.RUnlock()
result := make([]string, 0, len(w.activeUnits))
for id := range w.activeUnits {
result = append(result, id)
}
return result
}
// UnitStatus returns the state of a unit.
func (w *Workceptor) UnitStatus(unitID string) (*StatusFileData, error) {
unit, err := w.findUnit(unitID)
if err != nil {
return nil, err
}
return unit.Status(), nil
}
// CancelUnit cancels a unit of work, killing any processes.
func (w *Workceptor) CancelUnit(unitID string) error {
unit, err := w.findUnit(unitID)
if err != nil {
return err
}
return unit.Cancel()
}
// ReleaseUnit releases (deletes) resources from a unit of work, including stdout. Release implies Cancel.
func (w *Workceptor) ReleaseUnit(unitID string, force bool) error {
unit, err := w.findUnit(unitID)
if err != nil {
return err
}
return unit.Release(force)
}
// unitStatusForCFR returns status information as a map, suitable for a control function return value.
func (w *Workceptor) unitStatusForCFR(unitID string) (map[string]interface{}, error) {
status, err := w.UnitStatus(unitID)
if err != nil {
return nil, err
}
retMap := make(map[string]interface{})
v := reflect.ValueOf(*status)
t := reflect.TypeOf(*status)
for i := 0; i < v.NumField(); i++ {
retMap[t.Field(i).Name] = v.Field(i).Interface()
}
retMap["StateName"] = WorkStateToString(status.State)
return retMap, nil
}
// sleepOrDone sleeps until a timeout or the done channel is signaled.
func sleepOrDone(doneChan <-chan struct{}, interval time.Duration) bool {
select {
case <-doneChan:
return true
case <-time.After(interval):
return false
}
}
// GetResults returns a live stream of the results of a unit.
func (w *Workceptor) GetResults(ctx context.Context, unitID string, startPos int64) (chan []byte, error) {
unit, err := w.findUnit(unitID)
if err != nil {
return nil, err
}
resultChan := make(chan []byte)
closeOnce := sync.Once{}
resultClose := func() {
closeOnce.Do(func() {
close(resultChan)
})
}
unitdir := path.Join(w.dataDir, unitID)
stdoutFilename := path.Join(unitdir, "stdout")
var stdout *os.File
ctxChild, cancel := context.WithCancel(ctx)
go func() {
defer func() {
err = stdout.Close()
if err != nil {
w.nc.GetLogger().Error("Error closing stdout %s", stdoutFilename)
}
resultClose()
cancel()
}()
// Wait for stdout file to exist
for {
stdout, err = os.Open(stdoutFilename)
switch {
case err == nil:
case os.IsNotExist(err):
if IsComplete(unit.Status().State) {
w.nc.GetLogger().Warning("Unit completed without producing any stdout\n")
return
}
if sleepOrDone(ctx.Done(), 500*time.Millisecond) {
return
}
continue
default:
w.nc.GetLogger().Error("Error accessing stdout file: %s\n", err)
return
}
break
}
filePos := startPos
statChan := make(chan struct{}, 1)
go func() {
failures := 0
for {
select {
case <-ctxChild.Done():
return
case <-time.After(1 * time.Second):
_, err := os.Stat(stdoutFilename)
if os.IsNotExist(err) {
failures++
if failures > 3 {
w.nc.GetLogger().Error("Exceeded retries for reading stdout %s", stdoutFilename)
statChan <- struct{}{}
return
}
} else {
failures = 0
}
}
}
}()
for {
if sleepOrDone(ctx.Done(), 250*time.Millisecond) {
return
}
for {
select {
case <-ctx.Done():
return
case <-statChan:
return
default:
var newPos int64
newPos, err = stdout.Seek(filePos, 0)
if err != nil {
w.nc.GetLogger().Warning("Seek error processing stdout: %s\n", err)
return
}
if newPos != filePos {
w.nc.GetLogger().Warning("Seek error processing stdout\n")
return
}
var n int
buf := make([]byte, utils.NormalBufferSize)
n, err = stdout.Read(buf)
if n > 0 {
filePos += int64(n)
select {
case <-ctx.Done():
return
case resultChan <- buf[:n]:
}
}
}
if err != nil {
break
}
}
if err == io.EOF {
unitStatus := unit.Status()
if IsComplete(unitStatus.State) && filePos >= unitStatus.StdoutSize {
w.nc.GetLogger().Debug("Stdout complete - closing channel for: %s \n", unitID)
return
}
} else if err != nil {
w.nc.GetLogger().Error("Error reading stdout: %s\n", err)
return
}
}
}()
return resultChan, nil
}
|