| ... | ... |
@@ -161,6 +161,9 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
| 161 | 161 |
if _, err = os.Stat(filename); os.IsNotExist(err) {
|
| 162 | 162 |
return fmt.Errorf("no Dockerfile found in %s", cmd.Arg(0))
|
| 163 | 163 |
} |
| 164 |
+ if err = utils.ValidateContextDirectory(root); err != nil {
|
|
| 165 |
+ return fmt.Errorf("Error checking context is accessible: '%s'. Please check permissions and try again.", err)
|
|
| 166 |
+ } |
|
| 164 | 167 |
context, err = archive.Tar(root, archive.Uncompressed) |
| 165 | 168 |
} |
| 166 | 169 |
var body io.Reader |
| ... | ... |
@@ -2,8 +2,10 @@ package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"fmt" |
| 5 |
+ "os" |
|
| 5 | 6 |
"os/exec" |
| 6 | 7 |
"path/filepath" |
| 8 |
+ "strings" |
|
| 7 | 9 |
"testing" |
| 8 | 10 |
) |
| 9 | 11 |
|
| ... | ... |
@@ -119,6 +121,92 @@ func TestAddWholeDirToRoot(t *testing.T) {
|
| 119 | 119 |
logDone("build - add whole directory to root")
|
| 120 | 120 |
} |
| 121 | 121 |
|
| 122 |
+// Issue #5270 - ensure we throw a better error than "unexpected EOF" |
|
| 123 |
+// when we can't access files in the context. |
|
| 124 |
+func TestBuildWithInaccessibleFilesInContext(t *testing.T) {
|
|
| 125 |
+ buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildWithInaccessibleFilesInContext") |
|
| 126 |
+ addUserCmd := exec.Command("adduser", "unprivilegeduser")
|
|
| 127 |
+ out, _, err := runCommandWithOutput(addUserCmd) |
|
| 128 |
+ errorOut(err, t, fmt.Sprintf("failed to add user: %v %v", out, err))
|
|
| 129 |
+ |
|
| 130 |
+ {
|
|
| 131 |
+ // This is used to ensure we detect inaccessible files early during build in the cli client |
|
| 132 |
+ pathToInaccessibleFileBuildDirectory := filepath.Join(buildDirectory, "inaccessiblefile") |
|
| 133 |
+ pathToFileWithoutReadAccess := filepath.Join(pathToInaccessibleFileBuildDirectory, "fileWithoutReadAccess") |
|
| 134 |
+ |
|
| 135 |
+ err = os.Chown(pathToFileWithoutReadAccess, 0, 0) |
|
| 136 |
+ errorOut(err, t, fmt.Sprintf("failed to chown file to root: %s", err))
|
|
| 137 |
+ err = os.Chmod(pathToFileWithoutReadAccess, 0700) |
|
| 138 |
+ errorOut(err, t, fmt.Sprintf("failed to chmod file to 700: %s", err))
|
|
| 139 |
+ |
|
| 140 |
+ buildCommandStatement := fmt.Sprintf("%s build -t inaccessiblefiles .", dockerBinary)
|
|
| 141 |
+ buildCmd := exec.Command("su", "unprivilegeduser", "-c", buildCommandStatement)
|
|
| 142 |
+ buildCmd.Dir = pathToInaccessibleFileBuildDirectory |
|
| 143 |
+ out, exitCode, err := runCommandWithOutput(buildCmd) |
|
| 144 |
+ if err == nil || exitCode == 0 {
|
|
| 145 |
+ t.Fatalf("build should have failed: %s %s", err, out)
|
|
| 146 |
+ } |
|
| 147 |
+ |
|
| 148 |
+ // check if we've detected the failure before we started building |
|
| 149 |
+ if !strings.Contains(out, "no permission to read from ") {
|
|
| 150 |
+ t.Fatalf("output should've contained the string: no permission to read from ")
|
|
| 151 |
+ } |
|
| 152 |
+ |
|
| 153 |
+ if !strings.Contains(out, "Error checking context is accessible") {
|
|
| 154 |
+ t.Fatalf("output should've contained the string: Error checking context is accessible")
|
|
| 155 |
+ } |
|
| 156 |
+ } |
|
| 157 |
+ {
|
|
| 158 |
+ // This is used to ensure we detect inaccessible directories early during build in the cli client |
|
| 159 |
+ pathToInaccessibleDirectoryBuildDirectory := filepath.Join(buildDirectory, "inaccessibledirectory") |
|
| 160 |
+ pathToDirectoryWithoutReadAccess := filepath.Join(pathToInaccessibleDirectoryBuildDirectory, "directoryWeCantStat") |
|
| 161 |
+ pathToFileInDirectoryWithoutReadAccess := filepath.Join(pathToDirectoryWithoutReadAccess, "bar") |
|
| 162 |
+ |
|
| 163 |
+ err = os.Chown(pathToDirectoryWithoutReadAccess, 0, 0) |
|
| 164 |
+ errorOut(err, t, fmt.Sprintf("failed to chown directory to root: %s", err))
|
|
| 165 |
+ err = os.Chmod(pathToDirectoryWithoutReadAccess, 0444) |
|
| 166 |
+ errorOut(err, t, fmt.Sprintf("failed to chmod directory to 755: %s", err))
|
|
| 167 |
+ err = os.Chmod(pathToFileInDirectoryWithoutReadAccess, 0700) |
|
| 168 |
+ errorOut(err, t, fmt.Sprintf("failed to chmod file to 444: %s", err))
|
|
| 169 |
+ |
|
| 170 |
+ buildCommandStatement := fmt.Sprintf("%s build -t inaccessiblefiles .", dockerBinary)
|
|
| 171 |
+ buildCmd := exec.Command("su", "unprivilegeduser", "-c", buildCommandStatement)
|
|
| 172 |
+ buildCmd.Dir = pathToInaccessibleDirectoryBuildDirectory |
|
| 173 |
+ out, exitCode, err := runCommandWithOutput(buildCmd) |
|
| 174 |
+ if err == nil || exitCode == 0 {
|
|
| 175 |
+ t.Fatalf("build should have failed: %s %s", err, out)
|
|
| 176 |
+ } |
|
| 177 |
+ |
|
| 178 |
+ // check if we've detected the failure before we started building |
|
| 179 |
+ if !strings.Contains(out, "can't stat") {
|
|
| 180 |
+ t.Fatalf("output should've contained the string: can't access %s", out)
|
|
| 181 |
+ } |
|
| 182 |
+ |
|
| 183 |
+ if !strings.Contains(out, "Error checking context is accessible") {
|
|
| 184 |
+ t.Fatalf("output should've contained the string: Error checking context is accessible")
|
|
| 185 |
+ } |
|
| 186 |
+ |
|
| 187 |
+ } |
|
| 188 |
+ {
|
|
| 189 |
+ // This is used to ensure we don't follow links when checking if everything in the context is accessible |
|
| 190 |
+ // This test doesn't require that we run commands as an unprivileged user |
|
| 191 |
+ pathToDirectoryWhichContainsLinks := filepath.Join(buildDirectory, "linksdirectory") |
|
| 192 |
+ |
|
| 193 |
+ buildCmd := exec.Command(dockerBinary, "build", "-t", "testlinksok", ".") |
|
| 194 |
+ buildCmd.Dir = pathToDirectoryWhichContainsLinks |
|
| 195 |
+ out, exitCode, err := runCommandWithOutput(buildCmd) |
|
| 196 |
+ if err != nil || exitCode != 0 {
|
|
| 197 |
+ t.Fatalf("build should have worked: %s %s", err, out)
|
|
| 198 |
+ } |
|
| 199 |
+ |
|
| 200 |
+ deleteImages("testlinksok")
|
|
| 201 |
+ |
|
| 202 |
+ } |
|
| 203 |
+ deleteImages("inaccessiblefiles")
|
|
| 204 |
+ logDone("build - ADD from context with inaccessible files must fail")
|
|
| 205 |
+ logDone("build - ADD from context with accessible links must work")
|
|
| 206 |
+} |
|
| 207 |
+ |
|
| 122 | 208 |
// TODO: TestCaching |
| 123 | 209 |
|
| 124 | 210 |
// TODO: TestADDCacheInvalidation |
| ... | ... |
@@ -1,7 +1,9 @@ |
| 1 | 1 |
package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "fmt" |
|
| 4 | 5 |
"os" |
| 6 |
+ "os/exec" |
|
| 5 | 7 |
) |
| 6 | 8 |
|
| 7 | 9 |
// the docker binary to use |
| ... | ... |
@@ -18,6 +20,15 @@ var workingDirectory string |
| 18 | 18 |
func init() {
|
| 19 | 19 |
if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
|
| 20 | 20 |
dockerBinary = dockerBin |
| 21 |
+ } else {
|
|
| 22 |
+ whichCmd := exec.Command("which", "docker")
|
|
| 23 |
+ out, _, err := runCommandWithOutput(whichCmd) |
|
| 24 |
+ if err == nil {
|
|
| 25 |
+ dockerBinary = stripTrailingCharacters(out) |
|
| 26 |
+ } else {
|
|
| 27 |
+ fmt.Printf("ERROR: couldn't resolve full path to the Docker binary")
|
|
| 28 |
+ os.Exit(1) |
|
| 29 |
+ } |
|
| 21 | 30 |
} |
| 22 | 31 |
if registryImage := os.Getenv("REGISTRY_IMAGE"); registryImage != "" {
|
| 23 | 32 |
registryImageName = registryImage |
| ... | ... |
@@ -1051,3 +1051,40 @@ func TreeSize(dir string) (size int64, err error) {
|
| 1051 | 1051 |
}) |
| 1052 | 1052 |
return |
| 1053 | 1053 |
} |
| 1054 |
+ |
|
| 1055 |
+// ValidateContextDirectory checks if all the contents of the directory |
|
| 1056 |
+// can be read and returns an error if some files can't be read |
|
| 1057 |
+// symlinks which point to non-existing files don't trigger an error |
|
| 1058 |
+func ValidateContextDirectory(srcPath string) error {
|
|
| 1059 |
+ var finalError error |
|
| 1060 |
+ |
|
| 1061 |
+ filepath.Walk(filepath.Join(srcPath, "."), func(filePath string, f os.FileInfo, err error) error {
|
|
| 1062 |
+ // skip this directory/file if it's not in the path, it won't get added to the context |
|
| 1063 |
+ _, err = filepath.Rel(srcPath, filePath) |
|
| 1064 |
+ if err != nil && os.IsPermission(err) {
|
|
| 1065 |
+ return nil |
|
| 1066 |
+ } |
|
| 1067 |
+ |
|
| 1068 |
+ if _, err := os.Stat(filePath); err != nil && os.IsPermission(err) {
|
|
| 1069 |
+ finalError = fmt.Errorf("can't stat '%s'", filePath)
|
|
| 1070 |
+ return err |
|
| 1071 |
+ } |
|
| 1072 |
+ // skip checking if symlinks point to non-existing files, such symlinks can be useful |
|
| 1073 |
+ lstat, _ := os.Lstat(filePath) |
|
| 1074 |
+ if lstat.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
| 1075 |
+ return err |
|
| 1076 |
+ } |
|
| 1077 |
+ |
|
| 1078 |
+ if !f.IsDir() {
|
|
| 1079 |
+ currentFile, err := os.Open(filePath) |
|
| 1080 |
+ if err != nil && os.IsPermission(err) {
|
|
| 1081 |
+ finalError = fmt.Errorf("no permission to read from '%s'", filePath)
|
|
| 1082 |
+ return err |
|
| 1083 |
+ } else {
|
|
| 1084 |
+ currentFile.Close() |
|
| 1085 |
+ } |
|
| 1086 |
+ } |
|
| 1087 |
+ return nil |
|
| 1088 |
+ }) |
|
| 1089 |
+ return finalError |
|
| 1090 |
+} |