Skip to content

Commit

Permalink
Add init command to auditor
Browse files Browse the repository at this point in the history
  • Loading branch information
masomel committed Nov 9, 2017
1 parent ba9b430 commit 7117f50
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
6 changes: 3 additions & 3 deletions coniksauditor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand All @@ -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]
```
Expand Down
61 changes: 61 additions & 0 deletions coniksauditor/cli/internal/cmd/init.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 0 additions & 1 deletion coniksauditor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions coniksserver/cli/internal/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions coniksserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions utils/binutils/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}

0 comments on commit 7117f50

Please sign in to comment.