Skip to content

Commit a57e2cc

Browse files
Merge pull request #20858 from smarterclayton/extract_permissions
Strip permissions when extracting a release, and create the directory
2 parents b875c81 + a1a8224 commit a57e2cc

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

pkg/oc/cli/admin/release/extract.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package release
22

33
import (
44
"fmt"
5+
"os"
56
"time"
67

78
"github.com/spf13/cobra"
@@ -62,6 +63,9 @@ func (o *ExtractOptions) Run() error {
6263
}
6364

6465
dir := o.Directory
66+
if err := os.MkdirAll(dir, 0755); err != nil {
67+
return err
68+
}
6569

6670
src := o.From
6771
ref, err := imagereference.Parse(src)
@@ -70,6 +74,7 @@ func (o *ExtractOptions) Run() error {
7074
}
7175
opts := extract.NewOptions(genericclioptions.IOStreams{Out: o.Out, ErrOut: o.ErrOut})
7276
opts.OnlyFiles = true
77+
opts.RemovePermissions = true
7378
opts.Mappings = []extract.Mapping{
7479
{
7580
ImageRef: ref,
@@ -80,9 +85,9 @@ func (o *ExtractOptions) Run() error {
8085
}
8186
opts.ImageMetadataCallback = func(m *extract.Mapping, dgst digest.Digest, config *docker10.DockerImageConfig) {
8287
if len(ref.ID) > 0 {
83-
fmt.Fprintf(o.Out, "Extracted release payload from %s created at %s\n", ref, config.Created.Format(time.RFC3339))
88+
fmt.Fprintf(o.Out, "Extracted release payload created at %s\n", config.Created.Format(time.RFC3339))
8489
} else {
85-
fmt.Fprintf(o.Out, "Extracted release payload from %s (digest %s) created at %s\n", ref, dgst, config.Created.Format(time.RFC3339))
90+
fmt.Fprintf(o.Out, "Extracted release payload from digest %s created at %s\n", dgst, config.Created.Format(time.RFC3339))
8691
}
8792
}
8893
if err := opts.Run(); err != nil {

pkg/oc/cli/image/extract/extract.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ type Options struct {
9292

9393
Paths []string
9494

95-
OnlyFiles bool
95+
OnlyFiles bool
96+
RemovePermissions bool
9697

9798
FilterOptions imagemanifest.FilterOptions
9899

@@ -313,6 +314,9 @@ func (o *Options) Run() error {
313314
return fmt.Errorf("unable to filter layers for %s: %v", from, err)
314315
}
315316
}
317+
if o.RemovePermissions {
318+
alter = append(alter, removePermissions{})
319+
}
316320

317321
for i := range filteredLayers {
318322
layer := &filteredLayers[i]
@@ -417,6 +421,18 @@ func (a alterations) Alter(hdr *tar.Header) (bool, error) {
417421
return true, nil
418422
}
419423

424+
type removePermissions struct{}
425+
426+
func (_ removePermissions) Alter(hdr *tar.Header) (bool, error) {
427+
switch hdr.Typeflag {
428+
case tar.TypeReg, tar.TypeRegA:
429+
hdr.Mode = int64(os.FileMode(0640))
430+
default:
431+
hdr.Mode = int64(os.FileMode(0755))
432+
}
433+
return true, nil
434+
}
435+
420436
type copyFromDirectory struct {
421437
From string
422438
}

0 commit comments

Comments
 (0)