Skip to content

Commit f8ad340

Browse files
authored
Avoid throwing AuthException to the state machine (#16677)
1 parent fe37a4b commit f8ad340

File tree

2 files changed

+70
-20
lines changed

2 files changed

+70
-20
lines changed

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/auth/AuthorInfo.java

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,34 +150,74 @@ public TSStatus authorNonQuery(AuthorRelationalPlan authorPlan) {
150150
return authorPlanExecutor.executeRelationalAuthorNonQuery(authorPlan);
151151
}
152152

153-
public PermissionInfoResp executeListUsers(final AuthorPlan plan) throws AuthException {
154-
return authorPlanExecutor.executeListUsers(plan);
153+
public PermissionInfoResp executeListUsers(final AuthorPlan plan) {
154+
try {
155+
return authorPlanExecutor.executeListUsers(plan);
156+
} catch (AuthException e) {
157+
PermissionInfoResp resp = new PermissionInfoResp();
158+
resp.setStatus(new TSStatus(e.getCode().getStatusCode()).setMessage(e.getMessage()));
159+
return resp;
160+
}
155161
}
156162

157-
public PermissionInfoResp executeListRoles(final AuthorPlan plan) throws AuthException {
158-
return authorPlanExecutor.executeListRoles(plan);
163+
public PermissionInfoResp executeListRoles(final AuthorPlan plan) {
164+
try {
165+
return authorPlanExecutor.executeListRoles(plan);
166+
} catch (AuthException e) {
167+
PermissionInfoResp resp = new PermissionInfoResp();
168+
resp.setStatus(new TSStatus(e.getCode().getStatusCode()).setMessage(e.getMessage()));
169+
return resp;
170+
}
159171
}
160172

161-
public PermissionInfoResp executeListRolePrivileges(final AuthorPlan plan) throws AuthException {
162-
return authorPlanExecutor.executeListRolePrivileges(plan);
173+
public PermissionInfoResp executeListRolePrivileges(final AuthorPlan plan) {
174+
try {
175+
return authorPlanExecutor.executeListRolePrivileges(plan);
176+
} catch (AuthException e) {
177+
PermissionInfoResp resp = new PermissionInfoResp();
178+
resp.setStatus(new TSStatus(e.getCode().getStatusCode()).setMessage(e.getMessage()));
179+
return resp;
180+
}
163181
}
164182

165-
public PermissionInfoResp executeListUserPrivileges(final AuthorPlan plan) throws AuthException {
166-
return authorPlanExecutor.executeListUserPrivileges(plan);
183+
public PermissionInfoResp executeListUserPrivileges(final AuthorPlan plan) {
184+
try {
185+
return authorPlanExecutor.executeListUserPrivileges(plan);
186+
} catch (AuthException e) {
187+
PermissionInfoResp resp = new PermissionInfoResp();
188+
resp.setStatus(new TSStatus(e.getCode().getStatusCode()).setMessage(e.getMessage()));
189+
return resp;
190+
}
167191
}
168192

169-
public TAuthizedPatternTreeResp generateAuthorizedPTree(String username, int permission)
170-
throws AuthException {
171-
return authorPlanExecutor.generateAuthorizedPTree(username, permission);
193+
public TAuthizedPatternTreeResp generateAuthorizedPTree(String username, int permission) {
194+
try {
195+
return authorPlanExecutor.generateAuthorizedPTree(username, permission);
196+
} catch (AuthException e) {
197+
TAuthizedPatternTreeResp resp = new TAuthizedPatternTreeResp();
198+
resp.setStatus(new TSStatus(e.getCode().getStatusCode()).setMessage(e.getMessage()));
199+
return resp;
200+
}
172201
}
173202

174-
public TPermissionInfoResp checkRoleOfUser(String username, String roleName)
175-
throws AuthException {
176-
return authorPlanExecutor.checkRoleOfUser(username, roleName);
203+
public TPermissionInfoResp checkRoleOfUser(String username, String roleName) {
204+
try {
205+
return authorPlanExecutor.checkRoleOfUser(username, roleName);
206+
} catch (AuthException e) {
207+
TPermissionInfoResp resp = new TPermissionInfoResp();
208+
resp.setStatus(new TSStatus(e.getCode().getStatusCode()).setMessage(e.getMessage()));
209+
return resp;
210+
}
177211
}
178212

179-
public TPermissionInfoResp getUser(String username) throws AuthException {
180-
return authorPlanExecutor.getUser(username);
213+
public TPermissionInfoResp getUser(String username) {
214+
try {
215+
return authorPlanExecutor.getUser(username);
216+
} catch (AuthException e) {
217+
TPermissionInfoResp resp = new TPermissionInfoResp();
218+
resp.setStatus(new TSStatus(e.getCode().getStatusCode()).setMessage(e.getMessage()));
219+
return resp;
220+
}
181221
}
182222

183223
public String getUserName(long userId) throws AuthException {
@@ -199,9 +239,14 @@ public void processLoadSnapshot(File snapshotDir) throws TException, IOException
199239
*
200240
* @param username The username of the user that needs to be cached
201241
*/
202-
public TPermissionInfoResp getUserPermissionInfo(String username, ModelType type)
203-
throws AuthException {
204-
return authorPlanExecutor.getUserPermissionInfo(username, type);
242+
public TPermissionInfoResp getUserPermissionInfo(String username, ModelType type) {
243+
try {
244+
return authorPlanExecutor.getUserPermissionInfo(username, type);
245+
} catch (AuthException e) {
246+
TPermissionInfoResp resp = new TPermissionInfoResp();
247+
resp.setStatus(new TSStatus(e.getCode().getStatusCode()).setMessage(e.getMessage()));
248+
return resp;
249+
}
205250
}
206251

207252
public TSStatus enableSeparationOfAdminPowers(

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/auth/AuthorPlanExecutor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,11 @@ public TPermissionInfoResp getUser(String username) throws AuthException {
715715

716716
@Override
717717
public String getUserName(long userId) throws AuthException {
718-
return authorizer.getUser(userId).getName();
718+
User user = authorizer.getUser(userId);
719+
if (user == null) {
720+
throw new AuthException(
721+
TSStatusCode.USER_NOT_EXIST, String.format("No such user id: " + userId));
722+
}
723+
return user.getName();
719724
}
720725
}

0 commit comments

Comments
 (0)