diff --git a/cmd/globals.go b/cmd/globals.go index 5aa28140..95f63cd2 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -39,8 +39,9 @@ const ( globalMCCAsDir = "CAs" // session config and shared urls related constants - globalSessionDir = "session" - globalSharedURLsDataDir = "share" + globalSessionDir = "session" + globalSharedURLsDataDir = "share" + globalSessionConfigVersion = "8" // Profile directory for dumping profiler outputs. globalProfileDir = "profile" diff --git a/cmd/session-migrate.go b/cmd/session-migrate.go index a3a9f5b2..ac999fbb 100644 --- a/cmd/session-migrate.go +++ b/cmd/session-migrate.go @@ -48,7 +48,7 @@ func migrateSessionV7ToV8() { // Initialize v7 header and migrate to new config. sV8Header := &sessionV8Header{} - sV8Header.Version = "8" + sV8Header.Version = globalSessionConfigVersion sV8Header.When = sV7.Header.When sV8Header.RootPath = sV7.Header.RootPath sV8Header.GlobalBoolFlags = sV7.Header.GlobalBoolFlags diff --git a/cmd/session-v8.go b/cmd/session-v8.go index 1a38f158..467570fc 100644 --- a/cmd/session-v8.go +++ b/cmd/session-v8.go @@ -20,6 +20,7 @@ package cmd import ( "encoding/json" + "errors" "fmt" "io" "os" @@ -117,21 +118,34 @@ func loadSessionV8(sid string) (*sessionV8, *probe.Error) { return nil, probe.NewError(e) } - s := &sessionV8{} - s.Header = &sessionV8Header{} - s.SessionID = sid - s.Header.Version = "8" + // Initialize new session. + s := &sessionV8{ + Header: &sessionV8Header{ + Version: globalSessionConfigVersion, + }, + SessionID: sid, + } + + // Initialize session config loader. qs, e := quick.New(s.Header) if e != nil { return nil, probe.NewError(e).Trace(sid, s.Header.Version) } - e = qs.Load(sessionFile) - if e != nil { + + if e = qs.Load(sessionFile); e != nil { return nil, probe.NewError(e).Trace(sid, s.Header.Version) } + // Validate if the version matches with expected current version. + sV8Header := qs.Data().(*sessionV8Header) + if sV8Header.Version != globalSessionConfigVersion { + msg := fmt.Sprintf("Session header version %s does not match mc session version %s.\n", + sV8Header.Version, globalSessionConfigVersion) + return nil, probe.NewError(errors.New(msg)).Trace(sid, sV8Header.Version) + } + s.mutex = new(sync.Mutex) - s.Header = qs.Data().(*sessionV8Header) + s.Header = sV8Header sessionDataFile, err := getSessionDataFile(s.SessionID) if err != nil { @@ -151,7 +165,7 @@ func loadSessionV8(sid string) (*sessionV8, *probe.Error) { func newSessionV8() *sessionV8 { s := &sessionV8{} s.Header = &sessionV8Header{} - s.Header.Version = "8" + s.Header.Version = globalSessionConfigVersion // map of command and files copied. s.Header.GlobalBoolFlags = make(map[string]bool) s.Header.GlobalIntFlags = make(map[string]int)