2626import org .apache .iotdb .commons .audit .UserEntity ;
2727import org .apache .iotdb .commons .conf .CommonDescriptor ;
2828import org .apache .iotdb .commons .conf .IoTDBConstant ;
29- import org .apache .iotdb .commons .exception .IoTDBRuntimeException ;
3029import org .apache .iotdb .commons .service .JMXService ;
3130import org .apache .iotdb .commons .service .ServiceType ;
3231import org .apache .iotdb .commons .service .metric .MetricService ;
3332import org .apache .iotdb .commons .service .metric .enums .Metric ;
3433import org .apache .iotdb .commons .service .metric .enums .Tag ;
35- import org .apache .iotdb .commons .utils .AuthUtils ;
3634import org .apache .iotdb .commons .utils .CommonDateTimeUtils ;
3735import org .apache .iotdb .db .audit .DNAuditLogger ;
3836import org .apache .iotdb .db .auth .AuthorityChecker ;
3937import org .apache .iotdb .db .auth .LoginLockManager ;
40- import org .apache .iotdb .db .conf .IoTDBDescriptor ;
4138import org .apache .iotdb .db .protocol .basic .BasicOpenSessionResp ;
4239import org .apache .iotdb .db .protocol .thrift .OperationType ;
4340import org .apache .iotdb .db .queryengine .common .SessionInfo ;
44- import org .apache .iotdb .db .queryengine .plan .Coordinator ;
45- import org .apache .iotdb .db .queryengine .plan .analyze .ClusterPartitionFetcher ;
46- import org .apache .iotdb .db .queryengine .plan .analyze .schema .ClusterSchemaFetcher ;
47- import org .apache .iotdb .db .queryengine .plan .execution .ExecutionResult ;
48- import org .apache .iotdb .db .queryengine .plan .execution .IQueryExecution ;
49- import org .apache .iotdb .db .queryengine .plan .parser .StatementGenerator ;
50- import org .apache .iotdb .db .queryengine .plan .statement .Statement ;
5141import org .apache .iotdb .db .storageengine .dataregion .read .control .QueryResourceManager ;
5242import org .apache .iotdb .db .utils .DataNodeAuthUtils ;
5343import org .apache .iotdb .metrics .utils .MetricLevel ;
5646import org .apache .iotdb .rpc .TSStatusCode ;
5747import org .apache .iotdb .service .rpc .thrift .TSConnectionInfo ;
5848import org .apache .iotdb .service .rpc .thrift .TSConnectionInfoResp ;
59- import org .apache .iotdb .service .rpc .thrift .TSLastDataQueryReq ;
6049import org .apache .iotdb .service .rpc .thrift .TSProtocolVersion ;
6150
6251import org .apache .commons .lang3 .StringUtils ;
63- import org .apache .tsfile .read .common .block .TsBlock ;
6452import org .slf4j .Logger ;
6553import org .slf4j .LoggerFactory ;
6654
6755import java .time .Instant ;
6856import java .time .LocalDateTime ;
6957import java .time .ZoneId ;
7058import java .time .format .DateTimeFormatter ;
71- import java .util .Collections ;
7259import java .util .Comparator ;
7360import java .util .Map ;
74- import java .util .Optional ;
7561import java .util .Set ;
7662import java .util .TimeZone ;
7763import java .util .concurrent .ConcurrentHashMap ;
@@ -132,99 +118,6 @@ public BasicOpenSessionResp login(
132118 IClientSession .SqlDialect .TREE );
133119 }
134120
135- /**
136- * Check if the password for the give user has expired.
137- *
138- * @return the timestamp when the password will expire. Long.MAX if the password never expires.
139- * Null if the password history cannot be found.
140- */
141- public Long checkPasswordExpiration (String username , String password ) {
142- // check password expiration
143- long passwordExpirationDays =
144- CommonDescriptor .getInstance ().getConfig ().getPasswordExpirationDays ();
145- boolean mayBypassPasswordCheckInException =
146- CommonDescriptor .getInstance ().getConfig ().isMayBypassPasswordCheckInException ();
147-
148- TSLastDataQueryReq lastDataQueryReq = new TSLastDataQueryReq ();
149- lastDataQueryReq .setSessionId (0 );
150- lastDataQueryReq .setPaths (
151- Collections .singletonList (
152- DNAuditLogger .PREFIX_PASSWORD_HISTORY + ".`_" + username + "`.password" ));
153-
154- long queryId = -1 ;
155- try {
156- Statement statement = StatementGenerator .createStatement (lastDataQueryReq );
157- SessionInfo sessionInfo =
158- new SessionInfo (
159- 0 ,
160- new UserEntity (
161- AuthorityChecker .INTERNAL_AUDIT_USER_ID ,
162- AuthorityChecker .INTERNAL_AUDIT_USER ,
163- IoTDBDescriptor .getInstance ().getConfig ().getInternalAddress ()),
164- ZoneId .systemDefault ());
165-
166- queryId = requestQueryId ();
167- ExecutionResult result =
168- Coordinator .getInstance ()
169- .executeForTreeModel (
170- statement ,
171- queryId ,
172- sessionInfo ,
173- "" ,
174- ClusterPartitionFetcher .getInstance (),
175- ClusterSchemaFetcher .getInstance ());
176- if (result .status .getCode () != TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
177- LOGGER .warn ("Fail to check password expiration: {}" , result .status );
178- throw new IoTDBRuntimeException (
179- "Cannot query password history because: "
180- + result
181- + ", please log in later or disable password expiration." ,
182- result .status .getCode ());
183- }
184-
185- IQueryExecution queryExecution = Coordinator .getInstance ().getQueryExecution (queryId );
186- Optional <TsBlock > batchResult = queryExecution .getBatchResult ();
187- if (batchResult .isPresent ()) {
188- TsBlock tsBlock = batchResult .get ();
189- if (tsBlock .getPositionCount () <= 0 ) {
190- // no password history, may have upgraded from an older version
191- return null ;
192- }
193- long lastPasswordTime =
194- CommonDateTimeUtils .convertIoTDBTimeToMillis (tsBlock .getTimeByIndex (0 ));
195- // columns of last query: [timeseriesName, value, dataType]
196- String oldPassword = tsBlock .getColumn (1 ).getBinary (0 ).toString ();
197- if (oldPassword .equals (AuthUtils .encryptPassword (password ))) {
198- if (lastPasswordTime + passwordExpirationDays * 1000 * 86400 <= lastPasswordTime ) {
199- // overflow or passwordExpirationDays <= 0
200- return Long .MAX_VALUE ;
201- } else {
202- return lastPasswordTime + passwordExpirationDays * 1000 * 86400 ;
203- }
204- } else {
205- // 1. the password is incorrect, later logIn will fail
206- // 2. the password history does not record correctly, use the current time to create one
207- return null ;
208- }
209- } else {
210- return null ;
211- }
212- } catch (Throwable e ) {
213- LOGGER .error ("Fail to check password expiration" , e );
214- if (mayBypassPasswordCheckInException ) {
215- return Long .MAX_VALUE ;
216- } else {
217- throw new IoTDBRuntimeException (
218- "Internal server error " + ", please log in later or disable password expiration." ,
219- TSStatusCode .INTERNAL_SERVER_ERROR .getStatusCode ());
220- }
221- } finally {
222- if (queryId != -1 ) {
223- Coordinator .getInstance ().cleanupQueryExecution (queryId );
224- }
225- }
226- }
227-
228121 public BasicOpenSessionResp login (
229122 IClientSession session ,
230123 String username ,
@@ -235,7 +128,9 @@ public BasicOpenSessionResp login(
235128 IClientSession .SqlDialect sqlDialect ) {
236129 BasicOpenSessionResp openSessionResp = new BasicOpenSessionResp ();
237130
238- Long timeToExpire = checkPasswordExpiration (username , password );
131+ long userId = AuthorityChecker .getUserId (username ).orElse (-1L );
132+
133+ Long timeToExpire = DataNodeAuthUtils .checkPasswordExpiration (userId , password );
239134 if (timeToExpire != null && timeToExpire <= System .currentTimeMillis ()) {
240135 openSessionResp
241136 .sessionId (-1 )
@@ -244,7 +139,6 @@ public BasicOpenSessionResp login(
244139 return openSessionResp ;
245140 }
246141
247- long userId = AuthorityChecker .getUserId (username ).orElse (-1L );
248142 boolean enableLoginLock = userId != -1 ;
249143 LoginLockManager loginLockManager = LoginLockManager .getInstance ();
250144 if (enableLoginLock && loginLockManager .checkLock (userId , session .getClientAddress ())) {
@@ -281,7 +175,7 @@ public BasicOpenSessionResp login(
281175 username );
282176 long currentTime = CommonDateTimeUtils .currentTime ();
283177 TSStatus tsStatus =
284- DataNodeAuthUtils .recordPasswordHistory (username , password , password , currentTime );
178+ DataNodeAuthUtils .recordPasswordHistory (userId , password , password , currentTime );
285179 if (tsStatus .getCode () != TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
286180 openSessionResp
287181 .sessionId (-1 )
0 commit comments