Skip to content

Commit 6eafd50

Browse files
committed
debian index file is now deleted after use
updated caching directories to ensure that there cannot be clashes between different repositories fixed a potential file descriptor leak when updating CA certificates
1 parent 9de7a3a commit 6eafd50

9 files changed

Lines changed: 307 additions & 301 deletions

File tree

pkg/ca-certificates/update-ca-certificates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func readBlockedCerts(ctx context.Context, path string, rootfs memfs.FullFS) ([]
8484
log.Error(err, "failed to open configuration file")
8585
return nil, err
8686
}
87+
defer f.Close()
8788

8889
var blocked []string
8990

pkg/ca-certificates/update-ca-certificates_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package ca_certificates
22

33
import (
44
"context"
5+
"testing"
6+
57
"github.com/Snakdy/container-build-engine/pkg/files"
68
"github.com/Snakdy/container-build-engine/pkg/vfs"
79
"github.com/go-logr/logr"
810
"github.com/go-logr/logr/testr"
911
"github.com/stretchr/testify/assert"
1012
"github.com/stretchr/testify/require"
11-
"testing"
1213
)
1314

1415
func TestUpdateCertificates(t *testing.T) {

pkg/debian/index.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func downloadIndex(ctx context.Context, repository, release, component, arch, fi
5656
return nil, err
5757
}
5858
defer f.Close()
59+
defer func() {
60+
_ = os.Remove(f.Name())
61+
}()
5962
resp, err := http.Get(target)
6063
if err != nil {
6164
return nil, err

pkg/downloader/downloader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ func (d *Downloader) Download(ctx context.Context, src string) (string, error) {
3030

3131
// download the file to a predictable location so that
3232
// we can avoid repeated downloads
33-
dst := filepath.Join(d.cacheDir, uri.Hostname(), filepath.Base(uri.Path))
33+
cachePath := HashString(uri.Hostname() + "/" + uri.Path)
34+
dst := filepath.Join(d.cacheDir, cachePath, filepath.Base(uri.Path))
3435
// create the parent directory so that we don't
3536
// have any name clashes between different repositories
3637
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {

pkg/downloader/hash.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package downloader
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
)
7+
8+
// HashString generates a 12-character SHA256 hash
9+
// from a given string.
10+
// It should not be used for cryptographic operations.
11+
func HashString(s string) string {
12+
h := sha256.Sum256([]byte(s))
13+
return hex.EncodeToString(h[:])[:12]
14+
}

tests/fixtures/alpine_318_full-lock.json

Lines changed: 0 additions & 293 deletions
This file was deleted.

0 commit comments

Comments
 (0)