Skip to content

Commit bfa61d9

Browse files
makkeshiddeco
authored andcommitted
Apply default permission mode to all files/dirs in an artifact archive
Files: 0644 Directories: 0755 closes #1019 Signed-off-by: Max Jonas Werner <[email protected]>
1 parent e24ce86 commit bfa61d9

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

controllers/storage.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ import (
4949

5050
const GarbageCountLimit = 1000
5151

52+
const (
53+
// defaultFileMode is the permission mode applied to all files inside of an artifact archive.
54+
defaultFileMode int64 = 0o644
55+
// defaultDirMode is the permission mode applied to all directories inside of an artifact archive.
56+
defaultDirMode int64 = 0o755
57+
)
58+
5259
// Storage manages artifacts
5360
type Storage struct {
5461
// BasePath is the local directory path where the source artifacts are stored.
@@ -409,6 +416,10 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
409416
header.ModTime = time.Time{}
410417
header.AccessTime = time.Time{}
411418
header.ChangeTime = time.Time{}
419+
header.Mode = defaultFileMode
420+
if fi.Mode().IsDir() {
421+
header.Mode = defaultDirMode
422+
}
412423

413424
if err := tw.WriteHeader(header); err != nil {
414425
return err

controllers/storage_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ func TestStorageConstructor(t *testing.T) {
6060

6161
// walks a tar.gz and looks for paths with the basename. It does not match
6262
// symlinks properly at this time because that's painful.
63-
func walkTar(tarFile string, match string, dir bool) (int64, bool, error) {
63+
func walkTar(tarFile string, match string, dir bool) (int64, int64, bool, error) {
6464
f, err := os.Open(tarFile)
6565
if err != nil {
66-
return 0, false, fmt.Errorf("could not open file: %w", err)
66+
return 0, 0, false, fmt.Errorf("could not open file: %w", err)
6767
}
6868
defer f.Close()
6969

7070
gzr, err := gzip.NewReader(f)
7171
if err != nil {
72-
return 0, false, fmt.Errorf("could not unzip file: %w", err)
72+
return 0, 0, false, fmt.Errorf("could not unzip file: %w", err)
7373
}
7474
defer gzr.Close()
7575

@@ -79,24 +79,24 @@ func walkTar(tarFile string, match string, dir bool) (int64, bool, error) {
7979
if err == io.EOF {
8080
break
8181
} else if err != nil {
82-
return 0, false, fmt.Errorf("corrupt tarball reading header: %w", err)
82+
return 0, 0, false, fmt.Errorf("corrupt tarball reading header: %w", err)
8383
}
8484

8585
switch header.Typeflag {
8686
case tar.TypeDir:
8787
if header.Name == match && dir {
88-
return 0, true, nil
88+
return 0, header.Mode, true, nil
8989
}
9090
case tar.TypeReg:
9191
if header.Name == match {
92-
return header.Size, true, nil
92+
return header.Size, header.Mode, true, nil
9393
}
9494
default:
9595
// skip
9696
}
9797
}
9898

99-
return 0, false, nil
99+
return 0, 0, false, nil
100100
}
101101

102102
func TestStorage_Archive(t *testing.T) {
@@ -134,7 +134,7 @@ func TestStorage_Archive(t *testing.T) {
134134
if !mustExist {
135135
name = name[1:]
136136
}
137-
s, exist, err := walkTar(storage.LocalPath(artifact), name, false)
137+
s, m, exist, err := walkTar(storage.LocalPath(artifact), name, false)
138138
if err != nil {
139139
t.Fatalf("failed reading tarball: %v", err)
140140
}
@@ -148,13 +148,16 @@ func TestStorage_Archive(t *testing.T) {
148148
t.Errorf("tarball contained excluded file %q", name)
149149
}
150150
}
151+
if exist && m != defaultFileMode {
152+
t.Fatalf("%q mode %v != %v", name, m, defaultFileMode)
153+
}
151154
}
152155
for _, name := range dirs {
153156
mustExist := !(name[0:1] == "!")
154157
if !mustExist {
155158
name = name[1:]
156159
}
157-
_, exist, err := walkTar(storage.LocalPath(artifact), name, true)
160+
_, m, exist, err := walkTar(storage.LocalPath(artifact), name, true)
158161
if err != nil {
159162
t.Fatalf("failed reading tarball: %v", err)
160163
}
@@ -165,6 +168,10 @@ func TestStorage_Archive(t *testing.T) {
165168
t.Errorf("tarball contained excluded file %q", name)
166169
}
167170
}
171+
if exist && m != defaultDirMode {
172+
t.Fatalf("%q mode %v != %v", name, m, defaultDirMode)
173+
}
174+
168175
}
169176
}
170177

0 commit comments

Comments
 (0)