Skip to content

Commit 11acf48

Browse files
authored
Pin key server initial STR, load in client (#204)
1 parent dd0db5f commit 11acf48

File tree

8 files changed

+98
-7
lines changed

8 files changed

+98
-7
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ language: go
44
go:
55
- "1.9"
66
- "1.10"
7-
- tip
7+
# Temporarily stop testing for Go tip due to unknown go fmt error - tip
88

99
# TODO: Run `dep ensure` in Travis
1010
# see: https://github.com/coniks-sys/coniks-go/pull/201

application/client/config.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"github.com/coniks-sys/coniks-go/application"
55
"github.com/coniks-sys/coniks-go/crypto/sign"
6+
"github.com/coniks-sys/coniks-go/protocol"
67
)
78

89
// Config contains the client's configuration needed to send a request to a
@@ -17,8 +18,10 @@ type Config struct {
1718
*application.CommonConfig
1819

1920
SignPubkeyPath string `toml:"sign_pubkey_path"`
21+
SigningPubKey sign.PublicKey
2022

21-
SigningPubKey sign.PublicKey
23+
InitSTRPath string `toml:"init_str_path"`
24+
InitSTR *protocol.DirSTR
2225

2326
RegAddress string `toml:"registration_address,omitempty"`
2427
Address string `toml:"address"`
@@ -30,11 +33,12 @@ var _ application.AppConfig = (*Config)(nil)
3033
// given file path, with the given config encoding,
3134
// server signing public key path, registration address, and
3235
// server address.
33-
func NewConfig(file, encoding string, signPubkeyPath, regAddr,
36+
func NewConfig(file, encoding, signPubkeyPath, initSTRPath, regAddr,
3437
serverAddr string) *Config {
3538
var conf = Config{
3639
CommonConfig: application.NewCommonConfig(file, encoding, nil),
3740
SignPubkeyPath: signPubkeyPath,
41+
InitSTRPath: initSTRPath,
3842
RegAddress: regAddr,
3943
Address: serverAddr,
4044
}
@@ -58,6 +62,13 @@ func (conf *Config) Load(file, encoding string) error {
5862
}
5963
conf.SigningPubKey = signPubKey
6064

65+
// load initial STR
66+
initSTR, err := application.LoadInitSTR(conf.InitSTRPath, file)
67+
if err != nil {
68+
return err
69+
}
70+
conf.InitSTR = initSTR
71+
6172
return nil
6273
}
6374

application/config.go

+65
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package application
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"io/ioutil"
68

9+
"github.com/BurntSushi/toml"
710
"github.com/coniks-sys/coniks-go/crypto/sign"
11+
"github.com/coniks-sys/coniks-go/protocol"
812
"github.com/coniks-sys/coniks-go/utils"
913
)
1014

@@ -61,3 +65,64 @@ func LoadSigningPubKey(path, file string) (sign.PublicKey, error) {
6165
}
6266
return signPubKey, nil
6367
}
68+
69+
// LoadInitSTR loads an initial STR at the given path
70+
// specified in the given config file.
71+
// If there is any parsing error or the STR is malformed,
72+
// LoadInitSTR() returns an error with a nil STR.
73+
func LoadInitSTR(path, file string) (*protocol.DirSTR, error) {
74+
initSTRPath := utils.ResolvePath(path, file)
75+
initSTRBytes, err := ioutil.ReadFile(initSTRPath)
76+
if err != nil {
77+
return nil, fmt.Errorf("Cannot read init STR: %v", err)
78+
}
79+
initSTR := new(protocol.DirSTR)
80+
if err := json.Unmarshal(initSTRBytes, &initSTR); err != nil {
81+
return nil, fmt.Errorf("Cannot parse initial STR: %v", err)
82+
}
83+
if initSTR.Epoch != 0 {
84+
return nil, fmt.Errorf("Initial STR epoch must be 0 (got %d)", initSTR.Epoch)
85+
}
86+
return initSTR, nil
87+
}
88+
89+
// SaveSTR serializes the given STR to the given file.
90+
func SaveSTR(file string, str *protocol.DirSTR) error {
91+
strBytes, err := json.Marshal(str)
92+
if err != nil {
93+
return err
94+
}
95+
96+
if err := utils.WriteFile(file, strBytes, 0600); err != nil {
97+
return err
98+
}
99+
100+
return nil
101+
}
102+
103+
// LoadConfig loads an application configuration from the given toml-encoded
104+
// file. If there is any decoding error, an LoadConfig() returns an error
105+
// with a nil config.
106+
func LoadConfig(file string) (AppConfig, error) {
107+
var conf AppConfig
108+
if _, err := toml.DecodeFile(file, &conf); err != nil {
109+
return nil, fmt.Errorf("Failed to load config: %v", err)
110+
}
111+
return conf, nil
112+
}
113+
114+
// SaveConfig stores the given configuration conf in the given
115+
// file using toml encoding.
116+
// If there is any encoding or IO error, SaveConfig() returns an error.
117+
func SaveConfig(file string, conf AppConfig) error {
118+
var confBuf bytes.Buffer
119+
120+
e := toml.NewEncoder(&confBuf)
121+
if err := e.Encode(conf); err != nil {
122+
return err
123+
}
124+
if err := utils.WriteFile(file, confBuf.Bytes(), 0644); err != nil {
125+
return err
126+
}
127+
return nil
128+
}

application/server/config.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Config struct {
2121
LoadedHistoryLength uint64 `toml:"loaded_history_length"`
2222
// Policies contains the server's CONIKS policies configuration.
2323
Policies *Policies `toml:"policies"`
24+
// Path to store the initial STR
25+
InitSTRPath string `toml:"init_str_path"`
2426
// Addresses contains the server's connections configuration.
2527
Addresses []*Address `toml:"addresses"`
2628
// The server's epoch interval for updating the directory
@@ -34,12 +36,13 @@ var _ application.AppConfig = (*Config)(nil)
3436
// loaded history length and server application policies.
3537
func NewConfig(file, encoding string, addrs []*Address,
3638
logConfig *application.LoggerConfig,
37-
loadedHistLen uint64, policies *Policies) *Config {
39+
loadedHistLen uint64, policies *Policies, initSTRPath string) *Config {
3840
var conf = Config{
3941
CommonConfig: application.NewCommonConfig(file, encoding, logConfig),
4042
LoadedHistoryLength: loadedHistLen,
4143
Addresses: addrs,
4244
Policies: policies,
45+
InitSTRPath: initSTRPath,
4346
}
4447

4548
return &conf

application/server/server.go

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/coniks-sys/coniks-go/application"
55
"github.com/coniks-sys/coniks-go/protocol"
66
"github.com/coniks-sys/coniks-go/protocol/directory"
7+
"github.com/coniks-sys/coniks-go/utils"
78
)
89

910
// An Address describes a server's connection.
@@ -64,6 +65,13 @@ func NewConiksServer(conf *Config) *ConiksServer {
6465
epochTimer: application.NewEpochTimer(conf.EpochDeadline),
6566
}
6667

68+
// save the initial STR to be used for initializing auditors
69+
// FIXME: this saving should happen in protocol/ (i.e., when the
70+
// server starts and updates), because eventually we'll need
71+
// persistent storage.
72+
initSTRPath := utils.ResolvePath(conf.InitSTRPath, conf.Path)
73+
application.SaveSTR(initSTRPath, server.dir.LatestSTR())
74+
6775
return server
6876
}
6977

cli/coniksclient/internal/cmd/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func init() {
2121
func mkConfigOrExit(cmd *cobra.Command, args []string) {
2222
dir := cmd.Flag("dir").Value.String()
2323
file := path.Join(dir, "config.toml")
24-
2524
conf := client.NewConfig(file, "toml", "../coniksserver/sign.pub",
25+
"../../keyserver/coniksserver/init.str",
2626
"tcp://127.0.0.1:3000", "tcp://127.0.0.1:3000")
2727

2828
if err := conf.Save(); err != nil {

cli/coniksclient/internal/cmd/run.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ func init() {
4242
func run(cmd *cobra.Command, args []string) {
4343
isDebugging, _ := strconv.ParseBool(cmd.Flag("debug").Value.String())
4444
conf := loadConfigOrExit(cmd)
45-
cc := client.New(nil, true, conf.SigningPubKey)
45+
46+
// FIXME: right now we're passing the initSTR, but we should really
47+
// be passing the latest pinned STR here
48+
cc := client.New(conf.InitSTR, true, conf.SigningPubKey)
4649

4750
state, err := terminal.MakeRaw(int(os.Stdin.Fd()))
4851
if err != nil {

cli/coniksserver/internal/cmd/init.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func mkConfig(dir string) {
6666
SignKeyPath: "sign.priv",
6767
}
6868

69-
conf := server.NewConfig(file, "toml", addrs, logger, 1000000, policies)
69+
conf := server.NewConfig(file, "toml", addrs, logger, 1000000, policies,
70+
"init.str")
7071

7172
if err := conf.Save(); err != nil {
7273
log.Println(err)

0 commit comments

Comments
 (0)