From 2b46b3d2077a4820e2a38d71b43eb22d8b48a8e7 Mon Sep 17 00:00:00 2001 From: Julien Vehent Date: Mon, 16 Mar 2015 10:43:39 -0400 Subject: [PATCH] [minor] capture client version in API logs --- src/mig/api/api.go | 13 +++++++++++-- src/mig/client/client.go | 18 ++++++++++-------- src/mig/client/cmd/main.go | 10 +++++++++- src/mig/client/console/console.go | 11 +++++++++-- src/mig/client/generator/generator.go | 11 ++++++++++- 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/mig/api/api.go b/src/mig/api/api.go index a0f1c0a6..5c043c33 100644 --- a/src/mig/api/api.go +++ b/src/mig/api/api.go @@ -18,6 +18,9 @@ import ( "strings" ) +// build version +var version string + var ctx Context func main() { @@ -27,8 +30,14 @@ func main() { // command line options var config = flag.String("c", "/etc/mig/api.cfg", "Load configuration from file") + var showversion = flag.Bool("V", false, "Show build version and exit") flag.Parse() + if *showversion { + fmt.Println(version) + os.Exit(0) + } + // The context initialization takes care of parsing the configuration, // and creating connections to database, syslog, ... fmt.Fprintf(os.Stderr, "Initializing API context...") @@ -215,9 +224,9 @@ func respond(code int, response interface{}, respWriter http.ResponseWriter, r * ctx.Channels.Log <- mig.Log{ OpID: getOpID(r), - Desc: fmt.Sprintf("src=%s auth=[%s %.0f] %s %s %s resp_code=%d resp_size=%d", + Desc: fmt.Sprintf("src=%s auth=[%s %.0f] %s %s %s resp_code=%d resp_size=%d user-agent=%s", remoteAddresses(r), getInvName(r), getInvID(r), r.Method, r.Proto, - r.URL.String(), code, len(body)), + r.URL.String(), code, len(body), r.UserAgent()), } return } diff --git a/src/mig/client/client.go b/src/mig/client/client.go index 57aa9d6c..c0a6e0ae 100644 --- a/src/mig/client/client.go +++ b/src/mig/client/client.go @@ -31,16 +31,17 @@ var version string // A Client provides all the needed functionalities to interact with the MIG API. // It should be initialized with a proper configuration file. type Client struct { - API *http.Client - Token string - Conf Configuration + API *http.Client + Token string + Conf Configuration + Version string } // Configuration stores the live configuration and global parameters of a client type Configuration struct { - API ApiConf - Homedir string - GPG GpgConf + API ApiConf // location of the MIG API + Homedir string // location of the user's home directory + GPG GpgConf // location of the user's secring } type ApiConf struct { @@ -54,8 +55,9 @@ type GpgConf struct { } // NewClient initiates a new instance of a Client -func NewClient(conf Configuration) Client { +func NewClient(conf Configuration, version string) Client { var cli Client + cli.Version = version cli.Conf = conf tr := &http.Transport{ DisableCompression: false, @@ -140,7 +142,7 @@ func (cli Client) Do(r *http.Request) (resp *http.Response, err error) { err = fmt.Errorf("Do() -> %v", e) } }() - r.Header.Set("User-Agent", "MIG Client v"+version) + r.Header.Set("User-Agent", "MIG Client "+cli.Version) if cli.Token == "" { cli.Token, err = cli.MakeSignedToken() if err != nil { diff --git a/src/mig/client/cmd/main.go b/src/mig/client/cmd/main.go index eaf2785a..9177d9aa 100644 --- a/src/mig/client/cmd/main.go +++ b/src/mig/client/cmd/main.go @@ -15,6 +15,9 @@ import ( "time" ) +// build version +var version string + func usage() { fmt.Printf(`%s - Mozilla InvestiGator command line client usage: %s @@ -87,6 +90,11 @@ func main() { usage() } + if len(os.Args) < 2 || os.Args[1] == "-V" { + fmt.Println(version) + os.Exit(0) + } + // when reading the action from a file, go directly to launch if os.Args[1] == "-i" { err = fs.Parse(os.Args[1:]) @@ -162,7 +170,7 @@ readytolaunch: if err != nil { panic(err) } - cli = client.NewClient(conf) + cli = client.NewClient(conf, "cmd-"+version) // set the validity 60 second in the past to deal with clock skew a.ValidFrom = time.Now().Add(-60 * time.Second).UTC() diff --git a/src/mig/client/console/console.go b/src/mig/client/console/console.go index 2fcfa35d..7b412e41 100644 --- a/src/mig/client/console/console.go +++ b/src/mig/client/console/console.go @@ -19,7 +19,8 @@ import ( "strings" ) -var useShortNames bool +// build version +var version string func main() { var err error @@ -32,8 +33,14 @@ func main() { // command line options var config = flag.String("c", homedir+"/.migrc", "Load configuration from file") var quiet = flag.Bool("q", false, "don't display banners and prompts") + var showversion = flag.Bool("V", false, "show build version and exit") flag.Parse() + if *showversion { + fmt.Println(version) + os.Exit(0) + } + // silence extra output out := os.Stdout if *quiet { @@ -63,7 +70,7 @@ func main() { if err != nil { panic(err) } - cli := client.NewClient(conf) + cli := client.NewClient(conf, "console-"+version) // print platform status err = printStatus(cli) if err != nil { diff --git a/src/mig/client/generator/generator.go b/src/mig/client/generator/generator.go index 6147a06f..6711a635 100644 --- a/src/mig/client/generator/generator.go +++ b/src/mig/client/generator/generator.go @@ -17,6 +17,9 @@ import ( "time" ) +// build version +var version string + func main() { var err error defer func() { @@ -45,8 +48,14 @@ func main() { 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')") + var showversion = flag.Bool("V", false, "Show build version and exit") flag.Parse() + if *showversion { + fmt.Println(version) + os.Exit(0) + } + if *nolaunch { *pretty = true } @@ -56,7 +65,7 @@ func main() { if err != nil { panic(err) } - cli := client.NewClient(conf) + cli := client.NewClient(conf, "generator-"+version) // We need a file to load the action from if *file == "/path/to/file" {