restic/internal/restic/json.go
Michael Eischer 5b85fc0361 repository: restrict SaveUnpacked and RemoveUnpacked
Those methods now only allow modifying snapshots. Internal data types
used by the repository are now read-only. The repository-internal code
can bypass the restrictions by wrapping the repository in an
`unpackedRepository` type.

The restriction itself is implemented by using a new datatype
WriteableFileType in the SaveUnpacked and RemoveUnpacked methods. This
statically ensures that code cannot bypass the access restrictions.

The test changes are somewhat noisy as some of them modify repository
internals and therefore require some way to bypass the access
restrictions. This works by capturing an `unpackedRepository` or
`Backend` when creating the Repository using a test helper function.
2024-12-01 13:00:29 +01:00

32 lines
914 B
Go

package restic
import (
"context"
"encoding/json"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
)
// LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on
// the item.
func LoadJSONUnpacked(ctx context.Context, repo LoaderUnpacked, t FileType, id ID, item interface{}) (err error) {
buf, err := repo.LoadUnpacked(ctx, t, id)
if err != nil {
return err
}
return json.Unmarshal(buf, item)
}
// SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the
// backend as type t, without a pack. It returns the storage hash.
func SaveJSONUnpacked[FT FileTypes](ctx context.Context, repo SaverUnpacked[FT], t FT, item interface{}) (ID, error) {
debug.Log("save new blob %v", t)
plaintext, err := json.Marshal(item)
if err != nil {
return ID{}, errors.Wrap(err, "json.Marshal")
}
return repo.SaveUnpacked(ctx, t, plaintext)
}