本文整理汇总了Golang中mig.Action类的典型用法代码示例。如果您正苦于以下问题:Golang Action类的具体用法?Golang Action怎么用?Golang Action使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Action类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: computeSignature
func computeSignature(a mig.Action, ctx Context) (pgpsig string, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("computeSignature() -> %v", e)
}
}()
// do a round trip through the json marshaller, this is voodoo that
// fixes signature verification issues down the road
b, err := json.Marshal(a)
if err != nil {
panic(err)
}
err = json.Unmarshal(b, &a)
if err != nil {
panic(err)
}
secringFile, err := os.Open(ctx.GPG.Home + "/secring.gpg")
if err != nil {
panic(err)
}
defer secringFile.Close()
// compute the signature
str, err := a.String()
if err != nil {
panic(err)
}
pgpsig, err = sign.Sign(str, ctx.GPG.KeyID, secringFile)
if err != nil {
panic(err)
}
fmt.Println("Signature computed successfully")
return
}
示例2: invalidAction
// invalidAction marks actions that have failed to run
func invalidAction(ctx Context, a mig.Action, origin string) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("invalidAction() -> %v", e)
}
ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: "leaving invalidAction()"}.Debug()
}()
// move action to invalid dir
jsonA, err := json.Marshal(a)
if err != nil {
panic(err)
}
dest := fmt.Sprintf("%s/%.0f.json", ctx.Directories.Action.Invalid, a.ID)
err = safeWrite(ctx, dest, jsonA)
if err != nil {
panic(err)
}
// remove the action from its origin
os.Remove(origin)
if err != nil {
panic(err)
}
a.Status = "invalid"
a.LastUpdateTime = time.Now().UTC()
a.FinishTime = time.Now().UTC()
a.Counters.Sent = 0
err = ctx.DB.UpdateAction(a)
if err != nil {
panic(err)
}
desc := fmt.Sprintf("invalidAction(): Action '%s' has been marked as invalid.", a.Name)
ctx.Channels.Log <- mig.Log{OpID: ctx.OpID, ActionID: a.ID, Desc: desc}.Debug()
return
}
示例3: issueKillAction
// issueKillAction issues an `agentdestroy` action targetted to a specific agent
// and updates the status of the agent in the database
func issueKillAction(agent mig.Agent, ctx Context) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("issueKillAction() -> %v", e)
}
ctx.Channels.Log <- mig.Log{OpID: ctx.OpID, Desc: "leaving issueKillAction()"}.Debug()
}()
// generate an `agentdestroy` action for this agent
killAction := mig.Action{
ID: mig.GenID(),
Name: fmt.Sprintf("Kill agent %s", agent.Name),
Target: fmt.Sprintf("queueloc='%s'", agent.QueueLoc),
ValidFrom: time.Now().Add(-60 * time.Second).UTC(),
ExpireAfter: time.Now().Add(30 * time.Minute).UTC(),
SyntaxVersion: 2,
}
var opparams struct {
PID int `json:"pid"`
Version string `json:"version"`
}
opparams.PID = agent.PID
opparams.Version = agent.Version
killOperation := mig.Operation{
Module: "agentdestroy",
Parameters: opparams,
}
killAction.Operations = append(killAction.Operations, killOperation)
// sign the action with the scheduler PGP key
secring, err := getSecring(ctx)
if err != nil {
panic(err)
}
pgpsig, err := killAction.Sign(ctx.PGP.PrivKeyID, secring)
if err != nil {
panic(err)
}
killAction.PGPSignatures = append(killAction.PGPSignatures, pgpsig)
var jsonAction []byte
jsonAction, err = json.Marshal(killAction)
if err != nil {
panic(err)
}
// write the action to the spool for scheduling
dest := fmt.Sprintf("%s/%.0f.json", ctx.Directories.Action.New, killAction.ID)
err = safeWrite(ctx, dest, jsonAction)
if err != nil {
panic(err)
}
// mark the agent as `destroyed` in the database
err = ctx.DB.MarkAgentDestroyed(agent)
if err != nil {
panic(err)
}
ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("issued kill action for agent '%s' with PID '%d'", agent.Name, agent.PID)}.Warning()
return
}
示例4: FinishAction
// FinishAction updates the action fields to mark it as done
func (db *DB) FinishAction(a mig.Action) (err error) {
a.FinishTime = time.Now()
a.Status = "completed"
_, err = db.c.Exec(`UPDATE actions SET (finishtime, lastupdatetime, status) = ($1, $2, $3) WHERE id=$4`,
a.FinishTime, a.LastUpdateTime, a.Status, a.ID)
if err != nil {
return fmt.Errorf("Failed to update action: '%v'", err)
}
return
}
示例5: FinishAction
// FinishAction updates the action fields to mark it as done
func (db *DB) FinishAction(a mig.Action) (err error) {
a.FinishTime = time.Now()
a.Status = "completed"
_, err = db.c.Exec(`UPDATE actions SET (finishtime, lastupdatetime, status,
returnedctr, donectr, cancelledctr, failedctr, timeoutctr)
= ($1, $2, $3, $4, $5, $6, $7, $8) WHERE id=$9`,
a.FinishTime, a.LastUpdateTime, a.Status, a.Counters.Returned,
a.Counters.Done, a.Counters.Cancelled, a.Counters.Failed, a.Counters.TimeOut, a.ID)
if err != nil {
return fmt.Errorf("Failed to update action: '%v'", err)
}
return
}
示例6: flyAction
// flyAction moves an action file to the InFlight directory and
// write it to database
func flyAction(ctx Context, a mig.Action, origin string) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("flyAction() -> %v", e)
}
ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: "leaving flyAction()"}.Debug()
}()
// move action to inflight dir
jsonA, err := json.Marshal(a)
if err != nil {
panic(err)
}
dest := fmt.Sprintf("%s/%.0f.json", ctx.Directories.Action.InFlight, a.ID)
err = safeWrite(ctx, dest, jsonA)
if err != nil {
panic(err)
}
// remove the action from its origin
os.Remove(origin)
if err != nil {
panic(err)
}
a.Status = "inflight"
err = ctx.DB.UpdateActionStatus(a)
if err != nil {
panic(err)
}
desc := fmt.Sprintf("flyAction(): Action '%s' is in flight", a.Name)
ctx.Channels.Log <- mig.Log{OpID: ctx.OpID, ActionID: a.ID, Desc: desc}.Debug()
return
}
示例7: SignAction
// SignAction takes a MIG Action, signs it with the key identified in the configuration
// and returns the signed action
func (cli Client) SignAction(a mig.Action) (signed_action mig.Action, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("SignAction() -> %v", e)
}
}()
secring, err := os.Open(cli.Conf.GPG.Home + "/secring.gpg")
if err != nil {
panic(err)
}
defer secring.Close()
sig, err := a.Sign(cli.Conf.GPG.KeyID, secring)
if err != nil {
panic(err)
}
a.PGPSignatures = append(a.PGPSignatures, sig)
signed_action = a
return
}
示例8: checkActionAuthorization
// 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()
}()
var keys [][]byte
for _, pk := range PUBLICPGPKEYS {
keys = append(keys, []byte(pk))
}
// get an io.Reader from the public pgp key
keyring, keycount, err := pgp.ArmoredKeysToKeyring(keys)
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
}
示例9: landAction
// landAction moves an action file to the Done directory and
// updates it in database
func landAction(ctx Context, a mig.Action) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("landAction() -> %v", e)
}
ctx.Channels.Log <- mig.Log{ActionID: a.ID, Desc: "leaving landAction()"}.Debug()
}()
// update status and timestamps
a.Status = "done"
a.FinishTime = time.Now().UTC()
duration := a.FinishTime.Sub(a.StartTime)
// log
desc := fmt.Sprintf("action has completed in %s", duration.String())
ctx.Channels.Log <- mig.Log{OpID: ctx.OpID, ActionID: a.ID, Desc: desc}
// move action to done dir
jsonA, err := json.Marshal(a)
if err != nil {
panic(err)
}
dest := fmt.Sprintf("%s/%.0f.json", ctx.Directories.Action.Done, a.ID)
err = safeWrite(ctx, dest, jsonA)
if err != nil {
panic(err)
}
// remove the action from its origin
origin := fmt.Sprintf("%s/%.0f.json", ctx.Directories.Action.InFlight, a.ID)
os.Remove(origin)
if err != nil {
panic(err)
}
err = ctx.DB.FinishAction(a)
if err != nil {
panic(err)
}
desc = fmt.Sprintf("landAction(): Action '%s' has landed", a.Name)
ctx.Channels.Log <- mig.Log{OpID: ctx.OpID, ActionID: a.ID, Desc: desc}.Debug()
return
}
示例10: main
func main() {
var a2 mig.Action
a, err := mig.ActionFromFile(os.Args[1])
if err != nil {
panic(err)
}
a2 = a
a2.SyntaxVersion = 2
for i, op := range a.Operations {
if op.Module == "filechecker" {
input, err := json.Marshal(op.Parameters)
if err != nil {
panic(err)
}
a2.Operations[i].Parameters = filechecker.ConvertParametersV1toV2(input)
}
}
out, err := json.Marshal(a2)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", out)
}
示例11: validateAction
func validateAction(a mig.Action, ctx Context) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("validateAction() -> %v", e)
}
}()
// syntax checking
err = a.Validate()
if err != nil {
panic(err)
}
// signature checking
pubringFile, err := os.Open(ctx.GPG.Home + "/pubring.gpg")
if err != nil {
panic(err)
}
err = a.VerifySignatures(pubringFile)
if err != nil {
panic(err)
}
pubringFile.Close()
return
}
示例12: PostAction
// PostAction submits a MIG Action to the API and returns the reflected action with API ID
func (cli Client) PostAction(a mig.Action) (a2 mig.Action, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("PostAction() -> %v", e)
}
}()
a.SyntaxVersion = mig.ActionVersion
// serialize
ajson, err := json.Marshal(a)
if err != nil {
panic(err)
}
actionstr := string(ajson)
data := url.Values{"action": {actionstr}}
r, err := http.NewRequest("POST", cli.Conf.API.URL+"action/create/", strings.NewReader(data.Encode()))
if err != nil {
panic(err)
}
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := cli.Do(r)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if resp.StatusCode != 202 {
err = fmt.Errorf("error: HTTP %d. action creation failed.", resp.StatusCode)
panic(err)
}
var resource *cljs.Resource
err = json.Unmarshal(body, &resource)
if err != nil {
panic(err)
}
a2, err = ValueToAction(resource.Collection.Items[0].Data[0].Value)
if err != nil {
panic(err)
}
return
}
示例13: createAction
// createAction receives a signed action in a POST request, validates it,
// and write it into the scheduler spool
func createAction(respWriter http.ResponseWriter, request *http.Request) {
var (
err error
action mig.Action
)
opid := getOpID(request)
loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
resource := cljs.New(loc)
defer func() {
if e := recover(); e != nil {
ctx.Channels.Log <- mig.Log{OpID: opid, ActionID: action.ID, Desc: fmt.Sprintf("%v", e)}.Err()
resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: fmt.Sprintf("%v", e)})
respond(500, resource, respWriter, request)
}
ctx.Channels.Log <- mig.Log{OpID: opid, ActionID: action.ID, Desc: "leaving createAction()"}.Debug()
}()
// parse the POST body into a mig action
err = request.ParseForm()
if err != nil {
panic(err)
}
postAction := request.FormValue("action")
err = json.Unmarshal([]byte(postAction), &action)
if err != nil {
panic(err)
}
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("Received action for creation '%s'", action)}.Debug()
// Init action fields
action.ID = mig.GenID()
date0 := time.Date(0011, time.January, 11, 11, 11, 11, 11, time.UTC)
date1 := time.Date(9998, time.January, 11, 11, 11, 11, 11, time.UTC)
action.StartTime = date0
action.FinishTime = date1
action.LastUpdateTime = date0
action.Status = "pending"
// load keyring and validate action
keyring, err := getKeyring()
if err != nil {
panic(err)
}
err = action.Validate()
if err != nil {
panic(err)
}
err = action.VerifySignatures(keyring)
if err != nil {
panic(err)
}
ctx.Channels.Log <- mig.Log{OpID: opid, ActionID: action.ID, Desc: "Received new action with valid signature"}
// write action to database
err = ctx.DB.InsertAction(action)
if err != nil {
panic(err)
}
// write signatures to database
astr, err := action.String()
if err != nil {
panic(err)
}
for _, sig := range action.PGPSignatures {
k, err := getKeyring()
if err != nil {
panic(err)
}
fp, err := pgp.GetFingerprintFromSignature(astr, sig, k)
if err != nil {
panic(err)
}
inv, err := ctx.DB.InvestigatorByFingerprint(fp)
if err != nil {
panic(err)
}
err = ctx.DB.InsertSignature(action.ID, inv.ID, sig)
if err != nil {
panic(err)
}
}
ctx.Channels.Log <- mig.Log{OpID: opid, ActionID: action.ID, Desc: "Action written to database"}
err = resource.AddItem(cljs.Item{
Href: fmt.Sprintf("%s/action?actionid=%.0f", ctx.Server.BaseURL, action.ID),
Data: []cljs.Data{{Name: "action ID " + fmt.Sprintf("%.0f", action.ID), Value: action}},
})
if err != nil {
panic(err)
}
// return a 202 Accepted. the action will be processed asynchronously, and may fail later.
respond(202, resource, respWriter, request)
}
示例14: createAction
// createAction receives a signed action in a POST request, validates it,
// and write it into the scheduler spool
func createAction(respWriter http.ResponseWriter, request *http.Request) {
var err error
opid := mig.GenID()
var action mig.Action
resource := cljs.New(request.URL.String())
defer func() {
if e := recover(); e != nil {
ctx.Channels.Log <- mig.Log{OpID: opid, ActionID: action.ID, Desc: fmt.Sprintf("%v", e)}.Err()
resource.SetError(cljs.Error{Code: fmt.Sprintf("%d", opid), Message: fmt.Sprintf("%v", e)})
respond(500, resource, respWriter, request, opid)
}
ctx.Channels.Log <- mig.Log{OpID: opid, ActionID: action.ID, Desc: "leaving createAction()"}.Debug()
}()
// parse the POST body into a mig action
request.ParseForm()
postAction := request.FormValue("action")
err = json.Unmarshal([]byte(postAction), &action)
if err != nil {
panic(err)
}
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("Received action for creation '%s'", action)}.Debug()
// load keyring and validate action
keyring, err := os.Open(ctx.OpenPGP.PubRing)
if err != nil {
panic(err)
}
defer keyring.Close()
err = action.Validate()
if err != nil {
panic(err)
}
err = action.VerifySignature(keyring)
if err != nil {
panic(err)
}
action.ID = mig.GenID()
ctx.Channels.Log <- mig.Log{OpID: opid, ActionID: action.ID, Desc: "Received new action with valid signature"}
// write action to disk
destdir := fmt.Sprintf("%s/%d.json", ctx.Directories.Action.New, action.ID)
newAction, err := json.Marshal(action)
if err != nil {
panic(err)
}
err = safeWrite(opid, destdir, newAction)
if err != nil {
panic(err)
}
ctx.Channels.Log <- mig.Log{OpID: opid, ActionID: action.ID, Desc: "Action committed to spool"}
err = resource.AddItem(cljs.Item{
Href: "/api/action?actionid=" + fmt.Sprintf("%d", action.ID),
Data: []cljs.Data{{Name: "action ID " + fmt.Sprintf("%d", action.ID), Value: action}},
})
if err != nil {
panic(err)
}
respond(201, resource, respWriter, request, opid)
}
示例15: main
func main() {
var err error
var Usage = func() {
fmt.Fprintf(os.Stderr,
"Mozilla InvestiGator Action Verifier\n"+
"usage: %s <-a action file> <-c command file>\n\n"+
"Command line to verify an action *or* command.\n"+
"Options:\n",
os.Args[0])
flag.PrintDefaults()
}
hasaction := false
hascommand := false
// command line options
var actionfile = flag.String("a", "/path/to/action", "Load action from file")
var commandfile = flag.String("c", "/path/to/command", "Load command from file")
var pubring = flag.String("pubring", "/path/to/pubring", "Use pubring at <path>")
flag.Parse()
// if a file is defined, load action from that
if *actionfile != "/path/to/action" {
hasaction = true
}
if *commandfile != "/path/to/command" {
hascommand = true
}
if (hasaction && hascommand) || (!hasaction && !hascommand) {
Usage()
panic(err)
}
var a mig.Action
if hasaction {
a, err = mig.ActionFromFile(*actionfile)
if err != nil {
panic(err)
}
} else {
c, err := mig.CmdFromFile(*commandfile)
if err != nil {
panic(err)
}
a = c.Action
}
fmt.Printf("%s\n", a)
err = a.Validate()
if err != nil {
fmt.Println(err)
}
// find keyring in default location
u, err := user.Current()
if err != nil {
panic(err)
}
if *pubring != "/path/to/pubring" {
// load keyring
var gnupghome string
gnupghome = os.Getenv("GNUPGHOME")
if gnupghome == "" {
gnupghome = "/.gnupg"
}
*pubring = u.HomeDir + gnupghome + "/pubring.gpg"
}
keyring, err := os.Open(*pubring)
if err != nil {
panic(err)
}
defer keyring.Close()
// syntax checking
err = a.VerifySignatures(keyring)
if err != nil {
panic(err)
}
fmt.Println("Valid signature")
}