Selaa lähdekoodia

fix extracting over symlinks

Alex Suraci 7 vuotta sitten
vanhempi
commit
0b49915c54
6 muutettua tiedostoa jossa 49 lisäystä ja 8 poistoa
  1. 16 3
      cmd/in/unpack.go
  2. 3 2
      go.mod
  3. 5 0
      go.sum
  4. 19 1
      in_test.go
  5. 3 0
      testdata/removed-hardlinks/Dockerfile
  6. 3 2
      testdata/symlinks/Dockerfile

+ 16 - 3
cmd/in/unpack.go

@@ -127,15 +127,28 @@ func extractLayer(dest string, layer v1.Layer, bar *mpb.Bar, chown bool) error {
 		}
 
 		if hdr.Typeflag == tar.TypeSymlink {
-			log.Warnf("symlinking to %s", hdr.Linkname)
+			log.Debugf("symlinking to %s", hdr.Linkname)
 		}
 
 		if hdr.Typeflag == tar.TypeLink {
-			log.Warnf("hardlinking to %s", hdr.Linkname)
+			log.Debugf("hardlinking to %s", hdr.Linkname)
+		}
+
+		if fi, err := os.Lstat(path); err == nil {
+			if fi.IsDir() && hdr.Name == "." {
+				continue
+			}
+
+			if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
+				log.Debugf("removing existing path")
+				if err := os.RemoveAll(path); err != nil {
+					return err
+				}
+			}
 		}
 
 		if err := tarfs.ExtractEntry(hdr, dest, tr, chown); err != nil {
-			log.Infof("extracting")
+			log.Debugf("extracting")
 			return err
 		}
 	}

+ 3 - 2
go.mod

@@ -11,6 +11,7 @@ require (
 	github.com/google/go-containerregistry v0.0.0-20180801194910-5f7b0e489541
 	github.com/hpcloud/tail v1.0.0 // indirect
 	github.com/kr/pretty v0.1.0 // indirect
+	github.com/kr/pty v1.1.2 // indirect
 	github.com/mattn/go-colorable v0.0.9 // indirect
 	github.com/mattn/go-isatty v0.0.3 // indirect
 	github.com/onsi/ginkgo v1.6.0
@@ -19,10 +20,10 @@ require (
 	github.com/sirupsen/logrus v1.0.6
 	github.com/stretchr/testify v1.2.2 // indirect
 	github.com/vbauerster/mpb v3.3.1+incompatible
-	golang.org/x/crypto v0.0.0-20180802221240-56440b844dfe // indirect
+	golang.org/x/crypto v0.0.0-20180807104621-f027049dab0a // indirect
 	golang.org/x/net v0.0.0-20180801234040-f4c29de78a2a // indirect
 	golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
-	golang.org/x/sys v0.0.0-20180802203216-0ffbfd41fbef // indirect
+	golang.org/x/sys v0.0.0-20180806192500-2be389f392cd // indirect
 	golang.org/x/text v0.3.0 // indirect
 	gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
 	gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect

+ 5 - 0
go.sum

@@ -19,6 +19,7 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
 github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/pty v1.1.2/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
@@ -39,12 +40,16 @@ github.com/vbauerster/mpb v3.3.1+incompatible h1:895YxWn6TBP+leAp2kml6H+Rccyo6kX
 github.com/vbauerster/mpb v3.3.1+incompatible/go.mod h1:zAHG26FUhVKETRu+MWqYXcI70POlC6N8up9p1dID7SU=
 golang.org/x/crypto v0.0.0-20180802221240-56440b844dfe h1:APBCFlxGVQi3YDSHtTbNXRZhDEuz9rrnVPXZA4YbUx8=
 golang.org/x/crypto v0.0.0-20180802221240-56440b844dfe/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
+golang.org/x/crypto v0.0.0-20180807104621-f027049dab0a h1:PulT0Y50PcfTWomfsD39bSQyVrjjWdIuJKfyR4nOCJw=
+golang.org/x/crypto v0.0.0-20180807104621-f027049dab0a/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/net v0.0.0-20180801234040-f4c29de78a2a h1:8fCF9zjAir2SP3N+axz9xs+0r4V8dqPzqsWO10t8zoo=
 golang.org/x/net v0.0.0-20180801234040-f4c29de78a2a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sys v0.0.0-20180802203216-0ffbfd41fbef h1:ESfhYoBNk2UQGmavscFPKfwmc4ZTB2+UdQYsVw6Bq9M=
 golang.org/x/sys v0.0.0-20180802203216-0ffbfd41fbef/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20180806192500-2be389f392cd h1:KFYUs6SCkSktZ+xJWb5YbuSCJLLphbTsg0kvyirtlQ8=
+golang.org/x/sys v0.0.0-20180806192500-2be389f392cd/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=

+ 19 - 1
in_test.go

@@ -153,7 +153,7 @@ var _ = Describe("In", func() {
 
 	Describe("a hardlink that is later removed", func() {
 		BeforeEach(func() {
-			req.Source.Repository = "concourse/test-image-symlinks"
+			req.Source.Repository = "concourse/test-image-removed-hardlinks"
 			req.Version.Digest = latestDigest(req.Source.Repository)
 		})
 
@@ -167,4 +167,22 @@ var _ = Describe("In", func() {
 			Expect(stat.Mode() & os.ModeSymlink).To(BeZero())
 		})
 	})
+
+	Describe("layers that replace symlinks with regular files", func() {
+		BeforeEach(func() {
+			req.Source.Repository = "concourse/test-image-symlinks"
+			req.Version.Digest = latestDigest(req.Source.Repository)
+		})
+
+		It("removes the symlink and writes to a new file rather than trying to open and write to it (thereby overwriting its target)", func() {
+			Expect(cat(rootfsPath("a"))).To(Equal("symlinked\n"))
+			Expect(cat(rootfsPath("b"))).To(Equal("replaced\n"))
+		})
+	})
 })
+
+func cat(path string) string {
+	bytes, err := ioutil.ReadFile(path)
+	Expect(err).ToNot(HaveOccurred())
+	return string(bytes)
+}

+ 3 - 0
testdata/removed-hardlinks/Dockerfile

@@ -0,0 +1,3 @@
+FROM alpine
+RUN apk --no-cache add git
+RUN rm /usr/bin/git

+ 3 - 2
testdata/symlinks/Dockerfile

@@ -1,3 +1,4 @@
 FROM alpine
-RUN apk --no-cache add git
-RUN rm /usr/bin/git
+RUN echo symlinked > a
+RUN ln -s a b
+RUN rm b && echo replaced > b