// checkActionAuthorization verifies the PGP signatures of a given action // against the Access Control List of the agent. func checkActionAuthorization(a mig.Action, ctx Context) (err error) { defer func() { if e := recover(); e != nil { err = fmt.Errorf("checkActionAuthorization() -> %v", e) } ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: "leaving checkActionAuthorization()"}.Debug() }() // get an io.Reader from the public pgp key keyring, keycount, err := pgp.ArmoredPubKeysToKeyring(PUBLICPGPKEYS[0:]) if err != nil { panic(err) } ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: fmt.Sprintf("loaded %d keys", keycount)}.Debug() // Check the action syntax and signature err = a.Validate() if err != nil { desc := fmt.Sprintf("action validation failed: %v", err) ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: desc}.Err() panic(desc) } // Validate() checks that the action hasn't expired, but we need to // check the start time ourselves if time.Now().Before(a.ValidFrom) { ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: "action is scheduled for later"}.Err() panic("Action ValidFrom date is in the future") } // check ACLs, includes verifying signatures err = a.VerifyACL(ctx.ACL, keyring) if err != nil { desc := fmt.Sprintf("action ACL verification failed: %v", err) ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: desc}.Err() panic(desc) } ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: "ACL verification succeeded."}.Debug() return }
// parseCommands transforms a message into a MIG Command struct, performs validation // and run the command func parseCommands(ctx Context, msg []byte) (err error) { var cmd mig.Command cmd.ID = 0 // safety net defer func() { if e := recover(); e != nil { err = fmt.Errorf("parseCommands() -> %v", e) // if we have a command to return, update status and send back if cmd.ID > 0 { errLog := mig.Log{CommandID: cmd.ID, ActionID: cmd.Action.ID, Desc: fmt.Sprintf("%v", err)}.Err() cmd.Results = append(cmd.Results, errLog) cmd.Status = "failed" ctx.Channels.Results <- cmd } } ctx.Channels.Log <- mig.Log{CommandID: cmd.ID, ActionID: cmd.Action.ID, Desc: "leaving parseCommands()"}.Debug() }() // unmarshal the received command into a command struct // if this fails, inform the scheduler and skip this message err = json.Unmarshal(msg, &cmd) if err != nil { panic(err) } // get an io.Reader from the public pgp key keyring, keycount, err := pgp.ArmoredPubKeysToKeyring(PUBLICPGPKEYS[0:]) if err != nil { panic(err) } ctx.Channels.Log <- mig.Log{CommandID: cmd.ID, ActionID: cmd.Action.ID, Desc: fmt.Sprintf("loaded %d keys", keycount)}.Debug() // Check the action syntax and signature err = cmd.Action.Validate() if err != nil { desc := fmt.Sprintf("action validation failed: %v", err) ctx.Channels.Log <- mig.Log{CommandID: cmd.ID, ActionID: cmd.Action.ID, Desc: desc}.Err() panic(desc) } err = cmd.Action.VerifySignature(keyring) if err != nil { desc := fmt.Sprintf("action signature verification failed: %v", err) ctx.Channels.Log <- mig.Log{CommandID: cmd.ID, ActionID: cmd.Action.ID, Desc: desc}.Err() panic(desc) } // Expiration is verified by the Validate() call above, but we need // to verify the ScheduledDate ourselves if time.Now().Before(cmd.Action.ValidFrom) { ctx.Channels.Log <- mig.Log{CommandID: cmd.ID, ActionID: cmd.Action.ID, Desc: "action is scheduled for later"}.Err() panic("ScheduledDateInFuture") } // Each operation is ran separately by a module, a channel is created to receive the results from each module // a goroutine is created to read from the result channel, and when all modules are done, build the response resultChan := make(chan moduleResult) opsCounter := 0 for counter, operation := range cmd.Action.Operations { // create an module operation object currentOp := moduleOp{id: mig.GenID(), mode: operation.Module, params: operation.Parameters, resultChan: resultChan} desc := fmt.Sprintf("sending operation %d to module %s", counter, operation.Module) ctx.Channels.Log <- mig.Log{OpID: currentOp.id, ActionID: cmd.Action.ID, CommandID: cmd.ID, Desc: desc} // pass the module operation object to the proper channel switch operation.Module { case "filechecker": // send the operation to the module ctx.Channels.RunAgentCommand <- currentOp opsCounter++ case "shell": // send to the external execution path ctx.Channels.RunExternalCommand <- currentOp opsCounter++ case "terminate": ctx.Channels.Terminate <- fmt.Errorf("Terminate order received from scheduler") opsCounter++ default: ctx.Channels.Log <- mig.Log{CommandID: cmd.ID, ActionID: cmd.Action.ID, Desc: fmt.Sprintf("module '%s' is invalid", operation.Module)} } } // start the goroutine that will receive the results go receiveModuleResults(ctx, cmd, resultChan, opsCounter) return }