-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GODT-2385): Fallback for store read operations
Allow the user to specify a fallback reader when reading store files from disk. This is a stop-gap feature until we have a proper migration feature in Gluon to allow users of the library to read old cache files from before the changes made in 90330fd.
- Loading branch information
1 parent
1c65442
commit 2f3a201
Showing
8 changed files
with
289 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package store | ||
|
||
import ( | ||
"crypto/cipher" | ||
"io" | ||
) | ||
|
||
// Fallback provides an interface to supply an alternative way to read a store file should the main route fail. | ||
// This is mainly intended to allow users of the library to read old store formats they may have kept on disk. | ||
// This is a stop-gap until a complete data migration cycle can be implemented in gluon. | ||
type Fallback interface { | ||
Read(gcm cipher.AEAD, reader io.Reader) ([]byte, error) | ||
|
||
Write(gcm cipher.AEAD, filepath string, data []byte) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package fallback_v0 | ||
|
||
type Compressor interface { | ||
Compress([]byte) ([]byte, error) | ||
Decompress([]byte) ([]byte, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package fallback_v0 | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
) | ||
|
||
type GZipCompressor struct{} | ||
|
||
func (GZipCompressor) Compress(dec []byte) ([]byte, error) { | ||
buf := new(bytes.Buffer) | ||
|
||
zw := gzip.NewWriter(buf) | ||
|
||
if _, err := zw.Write(dec); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := zw.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
func (GZipCompressor) Decompress(cmp []byte) ([]byte, error) { | ||
zr, err := gzip.NewReader(bytes.NewReader(cmp)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
if _, err := buf.ReadFrom(zr); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := zr.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package fallback_v0 | ||
|
||
import ( | ||
"bytes" | ||
"compress/zlib" | ||
) | ||
|
||
type ZLibCompressor struct{} | ||
|
||
func (ZLibCompressor) Compress(dec []byte) ([]byte, error) { | ||
buf := new(bytes.Buffer) | ||
|
||
zw := zlib.NewWriter(buf) | ||
|
||
if _, err := zw.Write(dec); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := zw.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
func (ZLibCompressor) Decompress(cmp []byte) ([]byte, error) { | ||
zr, err := zlib.NewReader(bytes.NewReader(cmp)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
if _, err := buf.ReadFrom(zr); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := zr.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package fallback_v0 | ||
|
||
import ( | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"io" | ||
"os" | ||
|
||
"github.com/ProtonMail/gluon/store" | ||
) | ||
|
||
type onDiskStoreV0 struct { | ||
compressor Compressor | ||
} | ||
|
||
func NewOnDiskStoreV0() store.Fallback { | ||
return &onDiskStoreV0{compressor: nil} | ||
} | ||
|
||
func NewOnDiskStoreV0WithCompressor(c Compressor) store.Fallback { | ||
return &onDiskStoreV0{compressor: c} | ||
} | ||
|
||
func (d *onDiskStoreV0) Read(gcm cipher.AEAD, reader io.Reader) ([]byte, error) { | ||
enc, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
b, err := gcm.Open(nil, enc[:gcm.NonceSize()], enc[gcm.NonceSize():], nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if d.compressor != nil { | ||
dec, err := d.compressor.Decompress(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
b = dec | ||
} | ||
|
||
return b, nil | ||
} | ||
|
||
func (d *onDiskStoreV0) Write(gcm cipher.AEAD, filepath string, data []byte) error { | ||
nonce := make([]byte, gcm.NonceSize()) | ||
|
||
if _, err := rand.Read(nonce); err != nil { | ||
return err | ||
} | ||
|
||
if d.compressor != nil { | ||
enc, err := d.compressor.Compress(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data = enc | ||
} | ||
|
||
return os.WriteFile( | ||
filepath, | ||
gcm.Seal(nonce, nonce, data, nil), | ||
0o600, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters