[medium] use mig/client in action generator

This commit is contained in:
Julien Vehent 2014-11-15 13:19:56 -05:00
Родитель e38dd0bdff
Коммит 36750ce969
1 изменённых файлов: 30 добавлений и 79 удалений

Просмотреть файл

@ -9,53 +9,59 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"mig"
"mig/pgp"
"net/http"
"mig/client"
"net/url"
"os"
"os/user"
"runtime"
"time"
)
func main() {
var err error
defer func() {
if e := recover(); e != nil {
fmt.Printf("FATAL: %v\n", e)
}
}()
homedir := client.FindHomedir()
var Usage = func() {
fmt.Fprintf(os.Stderr,
"Mozilla InvestiGator Action Generator\n"+
"usage: %s -k=<key id> (-i <input file)\n\n"+
"usage: %s -i <input file>\n\n"+
"Command line to generate and sign MIG Actions.\n"+
"The resulting actions are display on stdout.\n\n"+
"Configuration is read from ~/.migrc by default.\n\n"+
"Options:\n",
os.Args[0])
flag.PrintDefaults()
}
// command line options
var key = flag.String("k", "key identifier", "Key identifier used to sign the action (ex: B75C2346)")
var config = flag.String("c", homedir+"/.migrc", "Load configuration from file")
var pretty = flag.Bool("p", false, "Print signed action in pretty JSON format")
var urlencode = flag.Bool("urlencode", false, "URL Encode marshalled JSON before output")
var posturl = flag.String("posturl", "", "POST action to <url> (enforces urlencode)")
var urlencode = flag.Bool("urlencode", false, "URL Encode marshalled JSON before printing it (implies '-p')")
var file = flag.String("i", "/path/to/file", "Load action from file")
var target = flag.String("t", "some.target.example.net", "Set the target of the action")
var validfrom = flag.String("validfrom", "now", "(optional) set an ISO8601 date the action will be valid from. If unset, use 'now'.")
var expireafter = flag.String("expireafter", "30m", "(optional) set a validity duration for the action. If unset, use '30m'.")
var nolaunch = flag.Bool("nolaunch", false, "Don't launch the action. Print it and exit. (implies '-p')")
flag.Parse()
// We need a key, if none is set on the command line, fail
if *key == "key identifier" {
Usage()
os.Exit(-1)
if *nolaunch {
*pretty = true
}
var err error
// instanciate an API client
conf, err := client.ReadConfiguration(*config)
if err != nil {
panic(err)
}
cli := client.NewClient(conf)
// if a file is defined, load action from that
// We need a file to load the action from
if *file == "/path/to/file" {
fmt.Println("Missing action file")
fmt.Println("ERROR: Missing action file")
Usage()
os.Exit(1)
}
a, err := mig.ActionFromFile(*file)
@ -83,61 +89,11 @@ func main() {
a.Target = *target
}
// find homedir
var homedir string
if runtime.GOOS == "darwin" {
homedir = os.Getenv("HOME")
} else {
// find keyring in default location
u, err := user.Current()
if err != nil {
panic(err)
}
homedir = u.HomeDir
}
// load keyrings
var gnupghome string
gnupghome = os.Getenv("GNUPGHOME")
if gnupghome == "" {
gnupghome = "/.gnupg"
}
pubringFile, err := os.Open(homedir + gnupghome + "/pubring.gpg")
if err != nil {
panic(err)
}
defer pubringFile.Close()
secringFile, err := os.Open(homedir + gnupghome + "/secring.gpg")
if err != nil {
panic(err)
}
defer secringFile.Close()
// compute the signature
str, err := a.String()
if err != nil {
panic(err)
}
pgpsig, err := pgp.Sign(str, *key, secringFile)
if err != nil {
panic(err)
}
// store the signature in the action signature array
a.PGPSignatures = append(a.PGPSignatures, pgpsig)
// syntax checking
err = a.Validate()
if err != nil {
panic(err)
}
// signature checking
err = a.VerifySignatures(pubringFile)
asig, err := cli.SignAction(a)
if err != nil {
panic(err)
}
a = asig
// if asked, pretty print the action
var jsonAction []byte
@ -161,17 +117,12 @@ func main() {
}
}
// http post the action to the posturl endpoint
if *posturl != "" {
resp, err := http.PostForm(*posturl, url.Values{"action": {actionstr}})
defer resp.Body.Close()
if !*nolaunch {
a2, err := cli.PostAction(a)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("%s", body)
fmt.Printf("Successfully launched action %.0f\n", a2.ID)
}
}