|
23 | 23 | import org.apache.hadoop.fs.FileSystem; |
24 | 24 | import org.apache.hadoop.fs.FsShell; |
25 | 25 | import org.apache.hadoop.fs.Path; |
| 26 | +import org.apache.hadoop.fs.RawLocalFileSystem; |
26 | 27 | import org.apache.hadoop.fs.permission.AclStatus; |
27 | 28 | import org.apache.hadoop.fs.permission.FsAction; |
28 | 29 | import org.apache.hadoop.fs.permission.FsPermission; |
|
31 | 32 | import org.apache.hadoop.security.UserGroupInformation; |
32 | 33 | import org.junit.Test; |
33 | 34 | import org.junit.experimental.categories.Category; |
34 | | -import org.mockito.Mockito; |
35 | 35 |
|
36 | | -import javax.security.auth.login.LoginException; |
| 36 | +import java.io.FileNotFoundException; |
37 | 37 | import java.io.IOException; |
38 | 38 | import java.util.ArrayList; |
39 | 39 | import java.util.List; |
@@ -65,143 +65,143 @@ private Path createFile(FileSystem fs, FsPermission perms) throws IOException { |
65 | 65 | return p; |
66 | 66 | } |
67 | 67 |
|
68 | | - private Configuration makeConf() { |
69 | | - // Make sure that the user doesn't happen to be in the super group |
70 | | - Configuration conf = new Configuration(); |
71 | | - conf.set("dfs.permissions.supergroup", "ubermensch"); |
72 | | - return conf; |
73 | | - } |
74 | | - |
75 | | - private UserGroupInformation ugiInvalidUserValidGroups() throws LoginException, IOException { |
76 | | - UserGroupInformation ugi = Mockito.mock(UserGroupInformation.class); |
77 | | - Mockito.when(ugi.getShortUserName()).thenReturn("nosuchuser"); |
78 | | - Mockito.when(ugi.getGroupNames()).thenReturn(SecurityUtils.getUGI().getGroupNames()); |
79 | | - return ugi; |
| 68 | + private UserGroupInformation ugiInvalidUserValidGroups() throws IOException { |
| 69 | + return UserGroupInformation.createUserForTesting("nosuchuser", SecurityUtils.getUGI().getGroupNames()); |
80 | 70 | } |
81 | 71 |
|
82 | 72 | private UserGroupInformation ugiInvalidUserInvalidGroups() { |
83 | | - UserGroupInformation ugi = Mockito.mock(UserGroupInformation.class); |
84 | | - Mockito.when(ugi.getShortUserName()).thenReturn("nosuchuser"); |
85 | | - Mockito.when(ugi.getGroupNames()).thenReturn(new String[]{"nosuchgroup"}); |
86 | | - return ugi; |
| 73 | + return UserGroupInformation.createUserForTesting("nosuchuser", new String[]{"nosuchgroup"}); |
87 | 74 | } |
88 | 75 |
|
89 | 76 | @Test |
90 | | - public void userReadWriteExecute() throws IOException, LoginException { |
91 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 77 | + public void userReadWriteExecute() throws IOException { |
| 78 | + FileSystem fs = FileSystem.get(new Configuration()); |
92 | 79 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.NONE)); |
93 | 80 | UserGroupInformation ugi = SecurityUtils.getUGI(); |
94 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
95 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
96 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 81 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
| 82 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
| 83 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
97 | 84 | } |
98 | 85 |
|
99 | 86 | @Test(expected = AccessControlException.class) |
100 | | - public void userNoRead() throws IOException, LoginException { |
101 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 87 | + public void userNoRead() throws IOException { |
| 88 | + FileSystem fs = FileSystem.get(new Configuration()); |
102 | 89 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.ALL, FsAction.ALL)); |
103 | 90 | UserGroupInformation ugi = SecurityUtils.getUGI(); |
104 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
| 91 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
105 | 92 | } |
106 | 93 |
|
107 | 94 | @Test(expected = AccessControlException.class) |
108 | | - public void userNoWrite() throws IOException, LoginException { |
109 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 95 | + public void userNoWrite() throws IOException { |
| 96 | + FileSystem fs = FileSystem.get(new Configuration()); |
110 | 97 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.ALL, FsAction.ALL)); |
111 | 98 | UserGroupInformation ugi = SecurityUtils.getUGI(); |
112 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
| 99 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
113 | 100 | } |
114 | 101 |
|
115 | 102 | @Test(expected = AccessControlException.class) |
116 | | - public void userNoExecute() throws IOException, LoginException { |
117 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 103 | + public void userNoExecute() throws IOException { |
| 104 | + FileSystem fs = FileSystem.get(new Configuration()); |
118 | 105 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.ALL, FsAction.ALL)); |
119 | 106 | UserGroupInformation ugi = SecurityUtils.getUGI(); |
120 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 107 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
121 | 108 | } |
122 | 109 |
|
123 | 110 | @Test |
124 | | - public void groupReadWriteExecute() throws IOException, LoginException { |
125 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 111 | + public void groupReadWriteExecute() throws IOException { |
| 112 | + FileSystem fs = FileSystem.get(new Configuration()); |
126 | 113 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.ALL, FsAction.NONE)); |
127 | 114 | UserGroupInformation ugi = ugiInvalidUserValidGroups(); |
128 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
129 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
130 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 115 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
| 116 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
| 117 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
131 | 118 | } |
132 | 119 |
|
133 | 120 | @Test(expected = AccessControlException.class) |
134 | | - public void groupNoRead() throws IOException, LoginException { |
135 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 121 | + public void groupNoRead() throws IOException { |
| 122 | + FileSystem fs = FileSystem.get(new Configuration()); |
136 | 123 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.ALL)); |
137 | 124 | UserGroupInformation ugi = ugiInvalidUserValidGroups(); |
138 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
| 125 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
139 | 126 | } |
140 | 127 |
|
141 | 128 | @Test(expected = AccessControlException.class) |
142 | | - public void groupNoWrite() throws IOException, LoginException { |
143 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 129 | + public void groupNoWrite() throws IOException { |
| 130 | + FileSystem fs = FileSystem.get(new Configuration()); |
144 | 131 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.ALL)); |
145 | 132 | UserGroupInformation ugi = ugiInvalidUserValidGroups(); |
146 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
| 133 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
147 | 134 | } |
148 | 135 |
|
149 | 136 | @Test(expected = AccessControlException.class) |
150 | | - public void groupNoExecute() throws IOException, LoginException { |
151 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 137 | + public void groupNoExecute() throws IOException { |
| 138 | + FileSystem fs = FileSystem.get(new Configuration()); |
152 | 139 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.ALL)); |
153 | 140 | UserGroupInformation ugi = ugiInvalidUserValidGroups(); |
154 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 141 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
155 | 142 | } |
156 | 143 |
|
157 | 144 | @Test |
158 | | - public void otherReadWriteExecute() throws IOException, LoginException { |
159 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 145 | + public void otherReadWriteExecute() throws IOException { |
| 146 | + FileSystem fs = FileSystem.get(new Configuration()); |
160 | 147 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.NONE, FsAction.ALL)); |
161 | 148 | UserGroupInformation ugi = ugiInvalidUserInvalidGroups(); |
162 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
163 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
164 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 149 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
| 150 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
| 151 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
165 | 152 | } |
166 | 153 |
|
167 | 154 | @Test(expected = AccessControlException.class) |
168 | | - public void otherNoRead() throws IOException, LoginException { |
169 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 155 | + public void otherNoRead() throws IOException { |
| 156 | + FileSystem fs = FileSystem.get(new Configuration()); |
170 | 157 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.NONE)); |
171 | 158 | UserGroupInformation ugi = ugiInvalidUserInvalidGroups(); |
172 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
| 159 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
173 | 160 | } |
174 | 161 |
|
175 | 162 | @Test(expected = AccessControlException.class) |
176 | | - public void otherNoWrite() throws IOException, LoginException { |
177 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 163 | + public void otherNoWrite() throws IOException { |
| 164 | + FileSystem fs = FileSystem.get(new Configuration()); |
178 | 165 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.NONE)); |
179 | 166 | UserGroupInformation ugi = ugiInvalidUserInvalidGroups(); |
180 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
| 167 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
181 | 168 | } |
182 | 169 |
|
183 | 170 | @Test(expected = AccessControlException.class) |
184 | | - public void otherNoExecute() throws IOException, LoginException { |
185 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 171 | + public void otherNoExecute() throws IOException { |
| 172 | + FileSystem fs = FileSystem.get(new Configuration()); |
186 | 173 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.NONE)); |
187 | 174 | UserGroupInformation ugi = ugiInvalidUserInvalidGroups(); |
188 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 175 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
189 | 176 | } |
190 | 177 |
|
191 | | - @Test |
192 | | - public void rootReadWriteExecute() throws IOException, LoginException { |
193 | | - UserGroupInformation ugi = SecurityUtils.getUGI(); |
| 178 | + @Test (expected = FileNotFoundException.class) |
| 179 | + public void nonExistentFile() throws IOException { |
194 | 180 | FileSystem fs = FileSystem.get(new Configuration()); |
195 | | - String old = fs.getConf().get("dfs.permissions.supergroup"); |
196 | | - try { |
197 | | - fs.getConf().set("dfs.permissions.supergroup", ugi.getPrimaryGroupName()); |
198 | | - Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.NONE, FsAction.NONE)); |
199 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
200 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
201 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
202 | | - } finally { |
203 | | - fs.getConf().set("dfs.permissions.supergroup", old); |
204 | | - } |
| 181 | + Path p = new Path("/tmp/nosuchfile"); |
| 182 | + UserGroupInformation ugi = SecurityUtils.getUGI(); |
| 183 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
| 184 | + } |
| 185 | + |
| 186 | + @Test(expected = AccessControlException.class) |
| 187 | + public void accessPerssionFromCustomFilesystem() throws IOException { |
| 188 | + FileSystem fs = new RawLocalFileSystem() { |
| 189 | + @Override |
| 190 | + public void access(Path path, FsAction mode) throws AccessControlException, IOException { |
| 191 | + if (path.toString().contains("noaccess")) { |
| 192 | + throw new AccessControlException("no access"); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public Configuration getConf() { |
| 198 | + return new Configuration(); |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + UserGroupInformation ugi = SecurityUtils.getUGI(); |
| 203 | + Path p = new Path("/tmp/noaccess"); |
| 204 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
205 | 205 | } |
206 | 206 |
|
207 | 207 | /** |
|
0 commit comments