From be900a44448f9a350f49286238e14fb3a8ea33d9 Mon Sep 17 00:00:00 2001 From: Marcela M Date: Thu, 9 Nov 2017 17:51:39 -0500 Subject: [PATCH] Add init command to auditor --- coniksauditor/README.md | 6 +-- coniksauditor/cli/internal/cmd/init.go | 61 ++++++++++++++++++++++++++ coniksauditor/config.go | 1 - coniksserver/cli/internal/cmd/init.go | 1 + coniksserver/server.go | 7 +++ utils/binutils/encoding.go | 16 +++++++ 6 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 coniksauditor/cli/internal/cmd/init.go diff --git a/coniksauditor/README.md b/coniksauditor/README.md index 1943910..8aaa281 100644 --- a/coniksauditor/README.md +++ b/coniksauditor/README.md @@ -34,7 +34,7 @@ Use "coniksauditor [command] --help" for more information about a command. - Make sure you have at least one running CONIKS directory for your auditor to track. For information on setting up a CONIKS directory, -see our [CONIKS server setup guide](https://github.com/coniks-sys/coniks-go/tree/master/coniksserver/README.md). +see our [CONIKS server setup guide](https://github.com/coniks-sys/coniks-go/blob/master/coniksserver/README.md). - Generate the configuration file: ``` @@ -55,9 +55,9 @@ we currently only configure the test auditor with a single directory for simplc ⇒ coniksauditor test # this will open a REPL ``` -##### Retrieve and verify the latest STR history from the given directory +##### Update the auditor with the latest STR history from the given directory ``` -> getlatest [dir] +> update [dir] # The auditor should display something like this if the request is successful [+] Valid! The auditor is up-to-date on the STR history of [dir] ``` diff --git a/coniksauditor/cli/internal/cmd/init.go b/coniksauditor/cli/internal/cmd/init.go new file mode 100644 index 0000000..cac39bf --- /dev/null +++ b/coniksauditor/cli/internal/cmd/init.go @@ -0,0 +1,61 @@ +package cmd + +import ( + "fmt" + "path" + + "bytes" + "os" + + "github.com/BurntSushi/toml" + "github.com/coniks-sys/coniks-go/coniksauditor" + "github.com/coniks-sys/coniks-go/utils" + "github.com/spf13/cobra" +) + +var initCmd = &cobra.Command{ + Use: "init", + Short: "Creates a config file for the auditor.", + Long: `Creates a file config.toml in the current working directory with +the following content: + +sign_pubkey_path = "../../keyserver/coniksserver/sign.pub" +init_str_path = "../../keyserver/coniksserver/init_str" +address = "tcp://127.0.0.1:3000" + +If the keyserver's public keys are somewhere else, you will have to modify the +config file accordingly. +`, + Run: func(cmd *cobra.Command, args []string) { + dir := cmd.Flag("dir").Value.String() + mkConfigOrExit(dir) + }, +} + +func init() { + RootCmd.AddCommand(initCmd) + initCmd.Flags().StringP("dir", "d", ".", + "Location of directory for storing generated files") +} + +func mkConfigOrExit(dir string) { + file := path.Join(dir, "config.toml") + var conf = coniksauditor.DirectoryConfig{ + SignPubkeyPath: "../../keyserver/coniksserver/sign.pub", + InitSTRPath: "../../keyserver/coniksserver/init_str", + Address: "tcp://127.0.0.1:3000", + } + + var confBuf bytes.Buffer + enc := toml.NewEncoder(&confBuf) + if err := enc.Encode(conf); err != nil { + fmt.Println("Coulnd't encode config. Error message: [" + + err.Error() + "]") + os.Exit(-1) + } + if err := utils.WriteFile(file, confBuf.Bytes(), 0644); err != nil { + fmt.Println("Coulnd't write config. Error message: [" + + err.Error() + "]") + os.Exit(-1) + } +} diff --git a/coniksauditor/config.go b/coniksauditor/config.go index d6c9804..a06a992 100644 --- a/coniksauditor/config.go +++ b/coniksauditor/config.go @@ -36,7 +36,6 @@ type Config []*DirectoryConfig // If there is any parsing or IO-error it returns an error (and the returned // config will be nil). func LoadConfig(file string) (*Config, error) { - var conf Config // FIXME: Currently assuming there is only one tracked directory // Add a loop here to iterate over multiple directory diff --git a/coniksserver/cli/internal/cmd/init.go b/coniksserver/cli/internal/cmd/init.go index e2539b9..dce1b1d 100644 --- a/coniksserver/cli/internal/cmd/init.go +++ b/coniksserver/cli/internal/cmd/init.go @@ -56,6 +56,7 @@ func mkConfig(dir string) { var conf = coniksserver.ServerConfig{ LoadedHistoryLength: 1000000, Addresses: addrs, + InitSTRPath: "init_str", Policies: &coniksserver.ServerPolicies{ EpochDeadline: 60, VRFKeyPath: "vrf.priv", diff --git a/coniksserver/server.go b/coniksserver/server.go index 5797daa..bc58e6d 100644 --- a/coniksserver/server.go +++ b/coniksserver/server.go @@ -30,6 +30,8 @@ type ServerConfig struct { LoadedHistoryLength uint64 `toml:"loaded_history_length"` // Policies contains the server's CONIKS policies configuration. Policies *ServerPolicies `toml:"policies"` + // Path to store the initial STR + InitSTRPath string `toml:"init_str_path"` // Addresses contains the server's connections configuration. Addresses []*Address `toml:"addresses"` Logger *binutils.LoggerConfig `toml:"logger"` @@ -153,6 +155,11 @@ func NewConiksServer(conf *ServerConfig) *ConiksServer { conf.Policies.signKey, conf.LoadedHistoryLength, true) + + // save the initial STR to be used for initializing auditors + initSTRPath := utils.ResolvePath(conf.InitSTRPath, conf.configFilePath) + binutils.MarshalSTRToFile(server.dir.LatestSTR(), initSTRPath) + server.stop = make(chan struct{}) server.configFilePath = conf.configFilePath server.reloadChan = make(chan os.Signal, 1) diff --git a/utils/binutils/encoding.go b/utils/binutils/encoding.go index d85aa32..eaa8e8a 100644 --- a/utils/binutils/encoding.go +++ b/utils/binutils/encoding.go @@ -2,8 +2,10 @@ package binutils import ( "encoding/json" + "log" "github.com/coniks-sys/coniks-go/protocol" + "github.com/coniks-sys/coniks-go/utils" ) // MarshalResponse returns a JSON encoding of the server's response. @@ -66,3 +68,17 @@ func UnmarshalResponse(t int, msg []byte) *protocol.Response { panic("Unknown request type") } } + +// MarshalSTRToFile serializes the given STR to the given path. +func MarshalSTRToFile(str *protocol.DirSTR, path string) { + strBytes, err := json.Marshal(str) + if err != nil { + log.Print(err) + return + } + + if err := utils.WriteFile(path, strBytes, 0600); err != nil { + log.Println(err) + return + } +}