Skip to content

Commit ef00e4b

Browse files
authored
Merge pull request #332 from splitio/task/hash
added hash validation for snapshot
2 parents 2c461bd + d6a2260 commit ef00e4b

File tree

6 files changed

+20
-15
lines changed

6 files changed

+20
-15
lines changed

splitio/admin/admin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Options struct {
4141
FullConfig interface{}
4242
FlagSpecVersion string
4343
LargeSegmentVersion string
44+
Hash string
4445
}
4546

4647
type AdminServer struct {
@@ -96,7 +97,7 @@ func NewServer(options *Options) (*AdminServer, error) {
9697
observabilityController.Register(admin)
9798

9899
if options.Snapshotter != nil {
99-
snapshotController := controllers.NewSnapshotController(options.Logger, options.Snapshotter, options.FlagSpecVersion)
100+
snapshotController := controllers.NewSnapshotController(options.Logger, options.Snapshotter, options.Hash)
100101
snapshotController.Register(admin)
101102
}
102103

splitio/admin/controllers/snapshot.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414

1515
// SnapshotController bundles endpoints associated to snapshot management
1616
type SnapshotController struct {
17-
logger logging.LoggerInterface
18-
db storage.Snapshotter
19-
version string
17+
logger logging.LoggerInterface
18+
db storage.Snapshotter
19+
hash string
2020
}
2121

2222
// NewSnapshotController constructs a new snapshot controller
23-
func NewSnapshotController(logger logging.LoggerInterface, db storage.Snapshotter, version string) *SnapshotController {
24-
return &SnapshotController{logger: logger, db: db, version: version}
23+
func NewSnapshotController(logger logging.LoggerInterface, db storage.Snapshotter, hash string) *SnapshotController {
24+
return &SnapshotController{logger: logger, db: db, hash: hash}
2525
}
2626

2727
// Register mounts the endpoints int he provided router
@@ -39,7 +39,7 @@ func (c *SnapshotController) downloadSnapshot(ctx *gin.Context) {
3939
return
4040
}
4141

42-
s, err := snapshot.New(snapshot.Metadata{Version: 1, Storage: snapshot.StorageBoltDB, SpecVersion: c.version}, b)
42+
s, err := snapshot.New(snapshot.Metadata{Version: 1, Storage: snapshot.StorageBoltDB, Hash: c.hash}, b)
4343
if err != nil {
4444
c.logger.Error("error building snapshot: ", err)
4545
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error building snapshot"})

splitio/admin/controllers/snapshot_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestDownloadProxySnapshot(t *testing.T) {
2929
dbInstance, err := persistent.NewBoltWrapper(tmpDataFile, nil)
3030
assert.Nil(t, err)
3131

32-
ctrl := NewSnapshotController(logging.NewLogger(nil), dbInstance, "1.3")
32+
ctrl := NewSnapshotController(logging.NewLogger(nil), dbInstance, "123456")
3333

3434
resp := httptest.NewRecorder()
3535
ctx, router := gin.CreateTestContext(resp)
@@ -46,7 +46,7 @@ func TestDownloadProxySnapshot(t *testing.T) {
4646

4747
assert.Equal(t, uint64(1), snapRes.Meta().Version)
4848
assert.Equal(t, uint64(1), snapRes.Meta().Storage)
49-
assert.Equal(t, "1.3", snapRes.Meta().SpecVersion)
49+
assert.Equal(t, "123456", snapRes.Meta().Hash)
5050

5151
dat, err := snap.Data()
5252
assert.Nil(t, err)

splitio/commitversion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ This file is created automatically, please do not edit
55
*/
66

77
// CommitVersion is the version of the last commit previous to release
8-
const CommitVersion = "88290d5"
8+
const CommitVersion = "829fcf8"

splitio/common/snapshot/snapshot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ var ErrMetadataRead = errors.New("snapshot metadata cannot be decoded")
3939

4040
// Metadata represents the Snapshot metadata object
4141
type Metadata struct {
42-
Version uint64
43-
Storage uint64
44-
SpecVersion string
42+
Version uint64
43+
Storage uint64
44+
Hash string
4545
}
4646

4747
// Snapshot represents a snapshot struct with metadata and data

splitio/proxy/initialization.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"net/url"
8+
"strconv"
89
"strings"
910
"time"
1011

@@ -57,8 +58,9 @@ func Start(logger logging.LoggerInterface, cfg *pconf.Main) error {
5758
return fmt.Errorf("error writing temporary snapshot file: %w", err)
5859
}
5960

60-
if snap.Meta().SpecVersion != cfg.FlagSpecVersion {
61-
return common.NewInitError(fmt.Errorf("snapshot spec version %s does not match config spec version %s", snap.Meta().SpecVersion, cfg.FlagSpecVersion), common.ExitErrorDB)
61+
currentHash := util.HashAPIKey(cfg.Apikey + cfg.FlagSpecVersion + strings.Join(cfg.FlagSetsFilter, "::"))
62+
if snap.Meta().Hash != strconv.Itoa(int(currentHash)) {
63+
return common.NewInitError(errors.New("snapshot cfg (apikey, version, flagsets) does not match the provided one"), common.ExitErrorDB)
6264
}
6365

6466
logger.Debug("Database created from snapshot at", dbpath)
@@ -221,6 +223,7 @@ func Start(logger logging.LoggerInterface, cfg *pconf.Main) error {
221223

222224
// --------------------------- ADMIN DASHBOARD ------------------------------
223225
cfgForAdmin := *cfg
226+
hash := util.HashAPIKey(cfgForAdmin.Apikey + cfg.FlagSpecVersion + strings.Join(cfg.FlagSetsFilter, "::"))
224227
cfgForAdmin.Apikey = logging.ObfuscateAPIKey(cfgForAdmin.Apikey)
225228

226229
adminTLSConfig, err := util.TLSConfigForServer(&cfg.Admin.TLS)
@@ -244,6 +247,7 @@ func Start(logger logging.LoggerInterface, cfg *pconf.Main) error {
244247
FullConfig: cfgForAdmin,
245248
TLS: adminTLSConfig,
246249
FlagSpecVersion: cfg.FlagSpecVersion,
250+
Hash: strconv.Itoa(int(hash)),
247251
})
248252
if err != nil {
249253
return common.NewInitError(fmt.Errorf("error starting admin server: %w", err), common.ExitAdminError)

0 commit comments

Comments
 (0)