Skip to content

Commit ad53170

Browse files
Add signature to UpdateContent (#301)
1 parent b33ab76 commit ad53170

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

client/clientimpl_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,7 @@ type packageTestCase struct {
12721272
available *protobufs.PackagesAvailable
12731273
expectedStatus *protobufs.PackageStatuses
12741274
expectedFileContent map[string][]byte
1275+
expectedSignature map[string][]byte
12751276
expectedError string
12761277
}
12771278

@@ -1393,6 +1394,10 @@ func verifyUpdatePackages(t *testing.T, testCase packageTestCase) {
13931394
for pkgName, receivedContent := range localPackageState.GetContent() {
13941395
expectedContent := testCase.expectedFileContent[pkgName]
13951396
assert.EqualValues(t, expectedContent, receivedContent)
1397+
1398+
actualSignature := localPackageState.GetSignature()[pkgName]
1399+
expectedSignature := testCase.expectedSignature[pkgName]
1400+
assert.EqualValues(t, expectedSignature, actualSignature)
13961401
}
13971402
}
13981403

@@ -1467,6 +1472,7 @@ func createPackageTestCase(name string, downloadSrv *httptest.Server) packageTes
14671472
File: &protobufs.DownloadableFile{
14681473
DownloadUrl: downloadSrv.URL + packageFileURL,
14691474
ContentHash: []byte{4, 5},
1475+
Signature: []byte{6, 7},
14701476
},
14711477
Hash: []byte{1, 2, 3},
14721478
},
@@ -1492,6 +1498,10 @@ func createPackageTestCase(name string, downloadSrv *httptest.Server) packageTes
14921498
expectedFileContent: map[string][]byte{
14931499
"package1": packageFileContent,
14941500
},
1501+
1502+
expectedSignature: map[string][]byte{
1503+
"package1": {6, 7},
1504+
},
14951505
}
14961506
}
14971507

client/internal/inmempackagestore.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type InMemPackagesStore struct {
1313
allPackagesHash []byte
1414
pkgState map[string]types.PackageState
1515
fileContents map[string][]byte
16+
fileSignatures map[string][]byte
1617
fileHashes map[string][]byte
1718
lastReportedStatuses *protobufs.PackageStatuses
1819

@@ -23,9 +24,10 @@ var _ types.PackagesStateProvider = (*InMemPackagesStore)(nil)
2324

2425
func NewInMemPackagesStore() *InMemPackagesStore {
2526
return &InMemPackagesStore{
26-
fileContents: map[string][]byte{},
27-
fileHashes: map[string][]byte{},
28-
pkgState: map[string]types.PackageState{},
27+
fileContents: map[string][]byte{},
28+
fileSignatures: map[string][]byte{},
29+
fileHashes: map[string][]byte{},
30+
pkgState: map[string]types.PackageState{},
2931
}
3032
}
3133

@@ -63,12 +65,13 @@ func (l *InMemPackagesStore) FileContentHash(packageName string) ([]byte, error)
6365
return l.fileHashes[packageName], nil
6466
}
6567

66-
func (l *InMemPackagesStore) UpdateContent(_ context.Context, packageName string, data io.Reader, contentHash []byte) error {
68+
func (l *InMemPackagesStore) UpdateContent(_ context.Context, packageName string, data io.Reader, contentHash, signature []byte) error {
6769
b, err := io.ReadAll(data)
6870
if err != nil {
6971
return err
7072
}
7173
l.fileContents[packageName] = b
74+
l.fileSignatures[packageName] = signature
7275
l.fileHashes[packageName] = contentHash
7376
return nil
7477
}
@@ -92,6 +95,10 @@ func (l *InMemPackagesStore) GetContent() map[string][]byte {
9295
return l.fileContents
9396
}
9497

98+
func (l *InMemPackagesStore) GetSignature() map[string][]byte {
99+
return l.fileSignatures
100+
}
101+
95102
func (l *InMemPackagesStore) LastReportedStatuses() (*protobufs.PackageStatuses, error) {
96103
return l.lastReportedStatuses, nil
97104
}

client/internal/packagessyncer.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,7 @@ func (s *packagesSyncer) downloadFile(ctx context.Context, pkgName string, file
290290
return fmt.Errorf("cannot download file from %s, HTTP response=%v", file.DownloadUrl, resp.StatusCode)
291291
}
292292

293-
// TODO: either add a callback to verify file.Signature or pass the Signature
294-
// as a parameter to UpdateContent.
295-
296-
err = s.localState.UpdateContent(ctx, pkgName, resp.Body, file.ContentHash)
293+
err = s.localState.UpdateContent(ctx, pkgName, resp.Body, file.ContentHash, file.Signature)
297294
if err != nil {
298295
return fmt.Errorf("failed to install/update the package %s downloaded from %s: %v", pkgName, file.DownloadUrl, err)
299296
}

client/types/packagessyncer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type PackagesStateProvider interface {
7777
// an error.
7878
// Content hash must be updated if the data is updated without failure.
7979
// The function must cancel and return an error if the context is cancelled.
80-
UpdateContent(ctx context.Context, packageName string, data io.Reader, contentHash []byte) error
80+
UpdateContent(ctx context.Context, packageName string, data io.Reader, contentHash, signature []byte) error
8181

8282
// DeletePackage deletes the package from the Agent's local storage.
8383
DeletePackage(packageName string) error

0 commit comments

Comments
 (0)