Skip to content

Commit

Permalink
ct-fetch: Migrate old log::* keys with trailing slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
jschanck committed Jan 12, 2022
1 parent df4cb9f commit 418a2e3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
4 changes: 4 additions & 0 deletions go/cmd/ct-fetch/ct-fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ func (ld *LogSyncEngine) SyncLog(ctx context.Context, enrolledLogs *EnrolledLogs
ld.DownloaderWaitGroup.Add(1)
defer ld.DownloaderWaitGroup.Done()

if err := ld.database.Migrate(&logMeta); err != nil {
return err
}

for {
if !enrolledLogs.IsEnrolled(logMeta.LogID) {
return nil
Expand Down
4 changes: 4 additions & 0 deletions go/storage/certdatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (db *CertDatabase) GetIssuerAndDatesFromCache() ([]types.IssuerDate, error)
return issuerList, nil
}

func (db *CertDatabase) Migrate(aLogData *types.CTLogMetadata) error {
return db.extCache.Migrate(aLogData)
}

func (db *CertDatabase) SaveLogState(aLogObj *types.CTLogState) error {
return db.extCache.StoreLogState(aLogObj)
}
Expand Down
4 changes: 4 additions & 0 deletions go/storage/mockcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,7 @@ func (ec *MockRemoteCache) LoadLogState(shortUrl string) (*types.CTLogState, err
func (ec *MockRemoteCache) LoadAllLogStates() ([]types.CTLogState, error) {
return nil, fmt.Errorf("Unimplemented")
}

func (ec *MockRemoteCache) Migrate(logData *types.CTLogMetadata) error {
return nil
}
46 changes: 45 additions & 1 deletion go/storage/rediscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,51 @@ func shortUrlToLogKey(shortUrl string) string {
return fmt.Sprintf("log::%s", strings.TrimRight(shortUrl, "/"))
}

func (ec *RedisCache) StoreLogState(log *CertificateLog) error {
func (ec *RedisCache) Migrate(logData *types.CTLogMetadata) error {
logUrlObj, err := url.Parse(logData.URL)
if err != nil {
return err
}

shortUrl := logUrlObj.Host + strings.TrimRight(logUrlObj.Path, "/")
newKey := shortUrlToLogKey(shortUrl)
_, err = ec.client.Get(newKey).Bytes()
if err != nil && err != redis.Nil {
return err
}
haveNew := err != redis.Nil

oldKey := newKey + "/"
oldData, err := ec.client.Get(oldKey).Bytes()
if err != nil && err != redis.Nil {
return err
}
haveOld := err != redis.Nil

// If we have both new and old data, then just delete old.
if haveOld && haveNew {
ec.client.Del(oldKey)
return nil
}

// If we have old data but not new, migrate.
if haveOld {
var log types.CTLogState
if err = json.Unmarshal(oldData, &log); err != nil {
return err
}
if err = ec.StoreLogState(&log); err != nil {
return err
}
ec.client.Del(oldKey)
return nil
}

// No data. Nothing to do.
return nil
}

func (ec *RedisCache) StoreLogState(log *types.CTLogState) error {
encoded, err := json.Marshal(log)
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions go/storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type RemoteCache interface {
ListRemove(key string, value string) error
TrySet(k string, v string, life time.Duration) (string, error)
KeysToChan(pattern string, c chan<- string) error
StoreLogState(aLogObj *CertificateLog) error
LoadLogState(aLogUrl string) (*CertificateLog, error)
LoadAllLogStates() ([]CertificateLog, error)
StoreLogState(aLogObj *types.CTLogState) error
LoadLogState(aLogUrl string) (*types.CTLogState, error)
LoadAllLogStates() ([]types.CTLogState, error)
Migrate(logData *types.CTLogMetadata) error
}

0 comments on commit 418a2e3

Please sign in to comment.