|
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; |
37 | 36 | import java.io.IOException; |
38 | 37 | import java.util.ArrayList; |
39 | 38 | import java.util.List; |
@@ -65,143 +64,136 @@ private Path createFile(FileSystem fs, FsPermission perms) throws IOException { |
65 | 64 | return p; |
66 | 65 | } |
67 | 66 |
|
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; |
| 67 | + private UserGroupInformation ugiInvalidUserValidGroups() throws IOException { |
| 68 | + return UserGroupInformation.createUserForTesting("nosuchuser", SecurityUtils.getUGI().getGroupNames()); |
80 | 69 | } |
81 | 70 |
|
82 | 71 | 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; |
| 72 | + return UserGroupInformation.createUserForTesting("nosuchuser", new String[]{"nosuchgroup"}); |
87 | 73 | } |
88 | 74 |
|
89 | 75 | @Test |
90 | | - public void userReadWriteExecute() throws IOException, LoginException { |
91 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 76 | + public void userReadWriteExecute() throws IOException { |
| 77 | + FileSystem fs = FileSystem.get(new Configuration()); |
92 | 78 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.NONE)); |
93 | 79 | 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); |
| 80 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
| 81 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
| 82 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
97 | 83 | } |
98 | 84 |
|
99 | 85 | @Test(expected = AccessControlException.class) |
100 | | - public void userNoRead() throws IOException, LoginException { |
101 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 86 | + public void userNoRead() throws IOException { |
| 87 | + FileSystem fs = FileSystem.get(new Configuration()); |
102 | 88 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.ALL, FsAction.ALL)); |
103 | 89 | UserGroupInformation ugi = SecurityUtils.getUGI(); |
104 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
| 90 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
105 | 91 | } |
106 | 92 |
|
107 | 93 | @Test(expected = AccessControlException.class) |
108 | | - public void userNoWrite() throws IOException, LoginException { |
109 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 94 | + public void userNoWrite() throws IOException { |
| 95 | + FileSystem fs = FileSystem.get(new Configuration()); |
110 | 96 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.ALL, FsAction.ALL)); |
111 | 97 | UserGroupInformation ugi = SecurityUtils.getUGI(); |
112 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
| 98 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
113 | 99 | } |
114 | 100 |
|
115 | 101 | @Test(expected = AccessControlException.class) |
116 | | - public void userNoExecute() throws IOException, LoginException { |
117 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 102 | + public void userNoExecute() throws IOException { |
| 103 | + FileSystem fs = FileSystem.get(new Configuration()); |
118 | 104 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.ALL, FsAction.ALL)); |
119 | 105 | UserGroupInformation ugi = SecurityUtils.getUGI(); |
120 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 106 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
121 | 107 | } |
122 | 108 |
|
123 | 109 | @Test |
124 | | - public void groupReadWriteExecute() throws IOException, LoginException { |
125 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 110 | + public void groupReadWriteExecute() throws IOException { |
| 111 | + FileSystem fs = FileSystem.get(new Configuration()); |
126 | 112 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.ALL, FsAction.NONE)); |
127 | 113 | 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); |
| 114 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
| 115 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
| 116 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
131 | 117 | } |
132 | 118 |
|
133 | 119 | @Test(expected = AccessControlException.class) |
134 | | - public void groupNoRead() throws IOException, LoginException { |
135 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 120 | + public void groupNoRead() throws IOException { |
| 121 | + FileSystem fs = FileSystem.get(new Configuration()); |
136 | 122 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.ALL)); |
137 | 123 | UserGroupInformation ugi = ugiInvalidUserValidGroups(); |
138 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
| 124 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
139 | 125 | } |
140 | 126 |
|
141 | 127 | @Test(expected = AccessControlException.class) |
142 | | - public void groupNoWrite() throws IOException, LoginException { |
143 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 128 | + public void groupNoWrite() throws IOException { |
| 129 | + FileSystem fs = FileSystem.get(new Configuration()); |
144 | 130 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.ALL)); |
145 | 131 | UserGroupInformation ugi = ugiInvalidUserValidGroups(); |
146 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
| 132 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
147 | 133 | } |
148 | 134 |
|
149 | 135 | @Test(expected = AccessControlException.class) |
150 | | - public void groupNoExecute() throws IOException, LoginException { |
151 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 136 | + public void groupNoExecute() throws IOException { |
| 137 | + FileSystem fs = FileSystem.get(new Configuration()); |
152 | 138 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.ALL)); |
153 | 139 | UserGroupInformation ugi = ugiInvalidUserValidGroups(); |
154 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 140 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
155 | 141 | } |
156 | 142 |
|
157 | 143 | @Test |
158 | | - public void otherReadWriteExecute() throws IOException, LoginException { |
159 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 144 | + public void otherReadWriteExecute() throws IOException { |
| 145 | + FileSystem fs = FileSystem.get(new Configuration()); |
160 | 146 | Path p = createFile(fs, new FsPermission(FsAction.NONE, FsAction.NONE, FsAction.ALL)); |
161 | 147 | 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); |
| 148 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
| 149 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
| 150 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
165 | 151 | } |
166 | 152 |
|
167 | 153 | @Test(expected = AccessControlException.class) |
168 | | - public void otherNoRead() throws IOException, LoginException { |
169 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 154 | + public void otherNoRead() throws IOException { |
| 155 | + FileSystem fs = FileSystem.get(new Configuration()); |
170 | 156 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.NONE)); |
171 | 157 | UserGroupInformation ugi = ugiInvalidUserInvalidGroups(); |
172 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.READ, ugi); |
| 158 | + HdfsUtils.checkFileAccess(fs, p, FsAction.READ, ugi); |
173 | 159 | } |
174 | 160 |
|
175 | 161 | @Test(expected = AccessControlException.class) |
176 | | - public void otherNoWrite() throws IOException, LoginException { |
177 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 162 | + public void otherNoWrite() throws IOException { |
| 163 | + FileSystem fs = FileSystem.get(new Configuration()); |
178 | 164 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.NONE)); |
179 | 165 | UserGroupInformation ugi = ugiInvalidUserInvalidGroups(); |
180 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.WRITE, ugi); |
| 166 | + HdfsUtils.checkFileAccess(fs, p, FsAction.WRITE, ugi); |
181 | 167 | } |
182 | 168 |
|
183 | 169 | @Test(expected = AccessControlException.class) |
184 | | - public void otherNoExecute() throws IOException, LoginException { |
185 | | - FileSystem fs = FileSystem.get(makeConf()); |
| 170 | + public void otherNoExecute() throws IOException { |
| 171 | + FileSystem fs = FileSystem.get(new Configuration()); |
186 | 172 | Path p = createFile(fs, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.NONE)); |
187 | 173 | UserGroupInformation ugi = ugiInvalidUserInvalidGroups(); |
188 | | - HdfsUtils.checkFileAccess(fs, fs.getFileStatus(p), FsAction.EXECUTE, ugi); |
| 174 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
189 | 175 | } |
190 | 176 |
|
191 | | - @Test |
192 | | - public void rootReadWriteExecute() throws IOException, LoginException { |
| 177 | + |
| 178 | + @Test(expected = AccessControlException.class) |
| 179 | + public void accessPerssionFromCustomFilesystem() throws IOException { |
| 180 | + FileSystem fs = new RawLocalFileSystem() { |
| 181 | + @Override |
| 182 | + public void access(Path path, FsAction mode) throws AccessControlException, IOException { |
| 183 | + if (path.toString().contains("noaccess")) { |
| 184 | + throw new AccessControlException("no access"); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public Configuration getConf() { |
| 190 | + return new Configuration(); |
| 191 | + } |
| 192 | + }; |
| 193 | + |
193 | 194 | UserGroupInformation ugi = SecurityUtils.getUGI(); |
194 | | - 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 | | - } |
| 195 | + Path p = new Path("/tmp/noaccess"); |
| 196 | + HdfsUtils.checkFileAccess(fs, p, FsAction.EXECUTE, ugi); |
205 | 197 | } |
206 | 198 |
|
207 | 199 | /** |
|
0 commit comments