|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.gradle.precommit; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.attribute.PosixFileAttributeView; |
| 25 | +import java.nio.file.attribute.PosixFilePermission; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import org.apache.tools.ant.taskdefs.condition.Os; |
| 31 | +import org.gradle.api.DefaultTask; |
| 32 | +import org.gradle.api.GradleException; |
| 33 | +import org.gradle.api.file.FileCollection; |
| 34 | +import org.gradle.api.file.FileTree; |
| 35 | +import org.gradle.api.plugins.JavaPluginConvention; |
| 36 | +import org.gradle.api.tasks.InputFiles; |
| 37 | +import org.gradle.api.tasks.OutputFile; |
| 38 | +import org.gradle.api.tasks.SkipWhenEmpty; |
| 39 | +import org.gradle.api.tasks.SourceSetContainer; |
| 40 | +import org.gradle.api.tasks.StopExecutionException; |
| 41 | +import org.gradle.api.tasks.TaskAction; |
| 42 | +import org.gradle.api.tasks.util.PatternFilterable; |
| 43 | +import org.gradle.api.tasks.util.PatternSet; |
| 44 | + |
| 45 | +/** |
| 46 | + * Checks source files for correct file permissions. |
| 47 | + */ |
| 48 | +public class FilePermissionsTask extends DefaultTask { |
| 49 | + |
| 50 | + /** |
| 51 | + * A pattern set of which files should be checked. |
| 52 | + */ |
| 53 | + private final PatternFilterable filesFilter = new PatternSet() |
| 54 | + // we always include all source files, and exclude what should not be checked |
| 55 | + .include("**") |
| 56 | + // exclude sh files that might have the executable bit set |
| 57 | + .exclude("**/*.sh"); |
| 58 | + |
| 59 | + private File outputMarker = new File(getProject().getBuildDir(), "markers/filePermissions"); |
| 60 | + |
| 61 | + public FilePermissionsTask() { |
| 62 | + setDescription("Checks java source files for correct file permissions"); |
| 63 | + } |
| 64 | + |
| 65 | + private static boolean isExecutableFile(File file) { |
| 66 | + try { |
| 67 | + Set<PosixFilePermission> permissions = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class) |
| 68 | + .readAttributes() |
| 69 | + .permissions(); |
| 70 | + return permissions.contains(PosixFilePermission.OTHERS_EXECUTE) |
| 71 | + || permissions.contains(PosixFilePermission.OWNER_EXECUTE) |
| 72 | + || permissions.contains(PosixFilePermission.GROUP_EXECUTE); |
| 73 | + } catch (IOException e) { |
| 74 | + throw new IllegalStateException("unable to read the file " + file + " attributes", e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns the files this task will check |
| 80 | + */ |
| 81 | + @InputFiles |
| 82 | + @SkipWhenEmpty |
| 83 | + public FileCollection getFiles() { |
| 84 | + SourceSetContainer sourceSets = getProject().getConvention().getPlugin(JavaPluginConvention.class).getSourceSets(); |
| 85 | + return sourceSets.stream() |
| 86 | + .map(sourceSet -> sourceSet.getAllSource().matching(filesFilter)) |
| 87 | + .reduce(FileTree::plus) |
| 88 | + .orElse(getProject().files().getAsFileTree()); |
| 89 | + } |
| 90 | + |
| 91 | + @TaskAction |
| 92 | + public void checkInvalidPermissions() throws IOException { |
| 93 | + if (Os.isFamily(Os.FAMILY_WINDOWS)) { |
| 94 | + throw new StopExecutionException(); |
| 95 | + } |
| 96 | + List<String> failures = getFiles().getFiles().stream() |
| 97 | + .filter(FilePermissionsTask::isExecutableFile) |
| 98 | + .map(file -> "Source file is executable: " + file) |
| 99 | + .collect(Collectors.toList()); |
| 100 | + |
| 101 | + if (!failures.isEmpty()) { |
| 102 | + throw new GradleException("Found invalid file permissions:\n" + String.join("\n", failures)); |
| 103 | + } |
| 104 | + |
| 105 | + outputMarker.getParentFile().mkdirs(); |
| 106 | + Files.write(outputMarker.toPath(), "done".getBytes("UTF-8")); |
| 107 | + } |
| 108 | + |
| 109 | + @OutputFile |
| 110 | + public File getOutputMarker() { |
| 111 | + return outputMarker; |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments