-
Notifications
You must be signed in to change notification settings - Fork 0
integrity: implement integrity storage #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bigbes
wants to merge
1
commit into
master
Choose a base branch
from
bigbes/TNTP-4191-implement-storage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ linters: | |
| varnamelen: | ||
| ignore-names: | ||
| - tt | ||
| - ok | ||
| ignore-decls: | ||
| - mc *minimock.Controller | ||
| - t T | ||
|
|
||
This file contains hidden or 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 hidden or 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,194 @@ | ||
| // Package integrity provides integrity-protected storage operations. | ||
| // It includes generators, validators, and builders for creating typed storage | ||
| // with hash and signature verification. | ||
| package integrity | ||
|
|
||
| import ( | ||
| "maps" | ||
| "slices" | ||
|
|
||
| "github.com/tarantool/go-storage" | ||
| "github.com/tarantool/go-storage/crypto" | ||
| "github.com/tarantool/go-storage/hasher" | ||
| "github.com/tarantool/go-storage/marshaller" | ||
| "github.com/tarantool/go-storage/namer" | ||
| ) | ||
|
|
||
| type NamerConstructor func(prefix string, hashNames []string, sigNames []string) namer.Namer | ||
|
|
||
| // TypedBuilder builds typed storage instances with integrity protection. | ||
| type TypedBuilder[T any] struct { | ||
| storage storage.Storage | ||
| hashers []hasher.Hasher | ||
| signers []crypto.Signer | ||
| verifiers []crypto.Verifier | ||
| marshaller marshaller.TypedYamlMarshaller[T] | ||
|
|
||
| prefix string | ||
| namerFunc NamerConstructor | ||
| } | ||
|
|
||
| // NewTypedBuilder creates a new TypedBuilder for the given storage instance. | ||
| func NewTypedBuilder[T any](storageInstance storage.Storage) TypedBuilder[T] { | ||
| return TypedBuilder[T]{ | ||
| storage: storageInstance, | ||
| hashers: []hasher.Hasher{}, | ||
| signers: []crypto.Signer{}, | ||
| verifiers: []crypto.Verifier{}, | ||
| marshaller: marshaller.NewTypedYamlMarshaller[T](), | ||
|
|
||
| prefix: "/", | ||
| namerFunc: nil, | ||
| } | ||
| } | ||
|
|
||
| func (s TypedBuilder[T]) copy() TypedBuilder[T] { | ||
| return TypedBuilder[T]{ | ||
| storage: s.storage, | ||
| hashers: slices.Clone(s.hashers), | ||
| signers: slices.Clone(s.signers), | ||
| verifiers: slices.Clone(s.verifiers), | ||
| marshaller: s.marshaller, | ||
|
|
||
| prefix: s.prefix, | ||
| namerFunc: s.namerFunc, | ||
| } | ||
| } | ||
|
|
||
| // WithHasher adds a hasher to the builder. | ||
| func (s TypedBuilder[T]) WithHasher(h hasher.Hasher) TypedBuilder[T] { | ||
| out := s.copy() | ||
|
|
||
| out.hashers = append(out.hashers, h) | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| // WithSignerVerifier adds a signer/verifier to the builder. | ||
| func (s TypedBuilder[T]) WithSignerVerifier(sv crypto.SignerVerifier) TypedBuilder[T] { | ||
| out := s.copy() | ||
|
|
||
| out.signers = append(out.signers, sv) | ||
| out.verifiers = append(out.verifiers, sv) | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| // WithSigner adds a signer to the builder. | ||
| func (s TypedBuilder[T]) WithSigner(signer crypto.Signer) TypedBuilder[T] { | ||
| out := s.copy() | ||
|
|
||
| out.signers = append(out.signers, signer) | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| // WithVerifier adds a verifier to the builder. | ||
| func (s TypedBuilder[T]) WithVerifier(verifier crypto.Verifier) TypedBuilder[T] { | ||
| out := s.copy() | ||
|
|
||
| out.verifiers = append(out.verifiers, verifier) | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| // WithMarshaller sets the marshaller for the builder. | ||
| func (s TypedBuilder[T]) WithMarshaller(marshaller marshaller.TypedYamlMarshaller[T]) TypedBuilder[T] { | ||
| out := s.copy() | ||
|
|
||
| out.marshaller = marshaller | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| // WithPrefix sets the key prefix for the builder. | ||
| func (s TypedBuilder[T]) WithPrefix(prefix string) TypedBuilder[T] { | ||
| out := s.copy() | ||
|
|
||
| out.prefix = prefix | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| // WithNamer sets the namer for the builder using a constructor function. | ||
| // The constructor function will be called during Build() with the current prefix. | ||
| func (s TypedBuilder[T]) WithNamer(namerFunc NamerConstructor) TypedBuilder[T] { | ||
| out := s.copy() | ||
|
|
||
| out.namerFunc = namerFunc | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| func (s TypedBuilder[T]) getHasherNames() []string { | ||
| names := make([]string, 0, len(s.hashers)) | ||
| for _, hasherInstance := range s.hashers { | ||
| names = append(names, hasherInstance.Name()) | ||
| } | ||
|
|
||
| return names | ||
| } | ||
|
|
||
| func (s TypedBuilder[T]) getSignerNames() []string { | ||
| names := make([]string, 0, len(s.signers)) | ||
| for _, signerInstance := range s.signers { | ||
| names = append(names, signerInstance.Name()) | ||
| } | ||
|
|
||
| return names | ||
| } | ||
|
|
||
| func (s TypedBuilder[T]) getVerifierNames() []string { | ||
| names := make([]string, 0, len(s.verifiers)) | ||
| for _, verifierInstance := range s.verifiers { | ||
| names = append(names, verifierInstance.Name()) | ||
| } | ||
|
|
||
| return names | ||
| } | ||
|
|
||
| func (s TypedBuilder[T]) getSignerVerifierNames() []string { | ||
| names := map[string]struct{}{} | ||
|
|
||
| for _, name := range s.getSignerNames() { | ||
| names[name] = struct{}{} | ||
| } | ||
|
|
||
| for _, name := range s.getVerifierNames() { | ||
| names[name] = struct{}{} | ||
| } | ||
|
|
||
| return slices.Collect(maps.Keys(names)) | ||
| } | ||
|
|
||
| // Build creates a new Typed storage instance with the configured options. | ||
| func (s TypedBuilder[T]) Build() *Typed[T] { | ||
| if s.namerFunc == nil { | ||
| s.namerFunc = namer.NewDefaultNamer | ||
| } | ||
|
|
||
| hasherNames := s.getHasherNames() | ||
|
|
||
| gen := NewGenerator( | ||
| s.namerFunc(s.prefix, hasherNames, s.getSignerNames()), | ||
| s.marshaller, | ||
| s.hashers, | ||
| s.signers, | ||
| ) | ||
|
|
||
| val := NewValidator( | ||
| s.namerFunc(s.prefix, hasherNames, s.getVerifierNames()), | ||
| s.marshaller, | ||
| s.hashers, | ||
| s.verifiers, | ||
| ) | ||
|
|
||
| namerInstance := s.namerFunc(s.prefix, hasherNames, s.getSignerVerifierNames()) | ||
|
|
||
| return &Typed[T]{ | ||
| base: s.storage, | ||
| gen: gen, | ||
| val: val, | ||
| namer: namerInstance, | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // Package integrity provides integrity storage. | ||
| package integrity //nolint:testpackage | ||
|
|
||
| import ( | ||
|
|
||
This file contains hidden or 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,12 @@ | ||
| package integrity | ||
|
|
||
| import ( | ||
| "github.com/tarantool/go-option" | ||
| ) | ||
|
|
||
| // ValidatedResult represents a validated named value. | ||
| type ValidatedResult[T any] struct { | ||
| Name string | ||
| Value option.Generic[T] | ||
| Error error | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No tests for the new type.