Skip to content

Commit

Permalink
assign user to locked
Browse files Browse the repository at this point in the history
  • Loading branch information
krihy committed Oct 10, 2023
1 parent 03b2ef5 commit 1ba5ad6
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.oceanbase.tools.dbbrowser.model;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* @author yaobin
* @date 2023-09-27
* @since 4.2.3
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class DBUserDetailIdentity extends DBObjectIdentity {

private DBUserLockStatusType userStatus;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.oceanbase.tools.dbbrowser.model;

/**
* @author yaobin
* @date 2023-09-27
* @since 4.2.3
*/
public enum DBUserLockStatusType {

LOCKED,

UNLOCKED;

public static DBUserLockStatusType from(int lockedStatus) {
if (lockedStatus != 1 && lockedStatus != 0) {
throw new IllegalArgumentException("invalid Statue");
}
return lockedStatus == 1 ? LOCKED : UNLOCKED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.oceanbase.tools.dbbrowser.model.DBTableSubpartitionDefinition;
import com.oceanbase.tools.dbbrowser.model.DBTrigger;
import com.oceanbase.tools.dbbrowser.model.DBType;
import com.oceanbase.tools.dbbrowser.model.DBUserDetailIdentity;
import com.oceanbase.tools.dbbrowser.model.DBVariable;
import com.oceanbase.tools.dbbrowser.model.DBView;

Expand Down Expand Up @@ -63,6 +64,13 @@ public interface DBSchemaAccessor {
*/
List<DBObjectIdentity> listUsers();

/**
* list all user details
*
* @return user detail list
*/
List<DBUserDetailIdentity> listUsersDetail();

/**
* Show all table names list in the specified schema
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import com.oceanbase.tools.dbbrowser.model.DBTableSubpartitionDefinition;
import com.oceanbase.tools.dbbrowser.model.DBTrigger;
import com.oceanbase.tools.dbbrowser.model.DBType;
import com.oceanbase.tools.dbbrowser.model.DBUserDetailIdentity;
import com.oceanbase.tools.dbbrowser.model.DBVariable;
import com.oceanbase.tools.dbbrowser.model.DBView;
import com.oceanbase.tools.dbbrowser.model.MySQLConstants;
Expand Down Expand Up @@ -1221,4 +1222,11 @@ public DBSequence getSequence(String schemaName, String sequenceName) {
public DBSynonym getSynonym(String schemaName, String synonymName, DBSynonymType synonymType) {
throw new UnsupportedOperationException("Not supported yet");
}


@Override
public List<DBUserDetailIdentity> listUsersDetail() {
throw new UnsupportedOperationException("Not supported yet");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.oceanbase.tools.dbbrowser.model.DBObjectWarningDescriptor;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn;
import com.oceanbase.tools.dbbrowser.model.DBTableIndex;
import com.oceanbase.tools.dbbrowser.model.DBUserDetailIdentity;
import com.oceanbase.tools.dbbrowser.model.DBUserLockStatusType;
import com.oceanbase.tools.dbbrowser.parser.SqlParser;
import com.oceanbase.tools.dbbrowser.parser.result.ParseSqlResult;
import com.oceanbase.tools.dbbrowser.schema.DBSchemaAccessorSqlMappers;
Expand Down Expand Up @@ -94,6 +96,19 @@ public List<DBDatabase> listDatabases() {
});
}

@Override
public List<DBUserDetailIdentity> listUsersDetail() {
MySQLSqlBuilder sb = new MySQLSqlBuilder();
sb.append("SELECT user_name,is_locked FROM oceanbase.__all_user");
return jdbcOperations.query(sb.toString(), (rs, rowNum) -> {
DBUserDetailIdentity dbUser = new DBUserDetailIdentity();
dbUser.setName(rs.getString(1));
dbUser.setType(DBObjectType.USER);
dbUser.setUserStatus(DBUserLockStatusType.from(rs.getInt(2)));
return dbUser;
});
}

@Override
public List<DBObjectIdentity> listTables(String schemaName, String tableNameLike) {
List<DBObjectIdentity> results = super.listTables(schemaName, tableNameLike);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import com.oceanbase.tools.dbbrowser.model.DBTrigger;
import com.oceanbase.tools.dbbrowser.model.DBType;
import com.oceanbase.tools.dbbrowser.model.DBTypeCode;
import com.oceanbase.tools.dbbrowser.model.DBUserDetailIdentity;
import com.oceanbase.tools.dbbrowser.model.DBUserLockStatusType;
import com.oceanbase.tools.dbbrowser.model.DBView;
import com.oceanbase.tools.dbbrowser.model.OracleConstants;
import com.oceanbase.tools.dbbrowser.model.PLConstants;
Expand Down Expand Up @@ -134,6 +136,20 @@ public List<DBDatabase> listDatabases() {
return databases;
}


@Override
public List<DBUserDetailIdentity> listUsersDetail() {
SqlBuilder sb = new OracleSqlBuilder();
sb.append("SELECT user_name,is_locked FROM SYS.ALL_VIRTUAL_USER_REAL_AGENT");
return jdbcOperations.query(sb.toString(), (rs, rowNum) -> {
DBUserDetailIdentity dbUser = new DBUserDetailIdentity();
dbUser.setName(rs.getString(1));
dbUser.setType(DBObjectType.USER);
dbUser.setUserStatus(DBUserLockStatusType.from(rs.getInt(2)));
return dbUser;
});
}

@Override
public List<DBTableIndex> listTableIndexes(String schemaName, String tableName) {
List<DBTableIndex> indexList = super.listTableIndexes(schemaName, tableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.oceanbase.tools.dbbrowser.model.DBTableSubpartitionDefinition;
import com.oceanbase.tools.dbbrowser.model.DBTrigger;
import com.oceanbase.tools.dbbrowser.model.DBType;
import com.oceanbase.tools.dbbrowser.model.DBUserDetailIdentity;
import com.oceanbase.tools.dbbrowser.model.DBVariable;
import com.oceanbase.tools.dbbrowser.model.DBView;
import com.oceanbase.tools.dbbrowser.model.OracleConstants;
Expand Down Expand Up @@ -928,4 +929,9 @@ public DBSynonym getSynonym(String schemaName, String synonymName, DBSynonymType
throw new UnsupportedOperationException("Not supported yet");
}

@Override
public List<DBUserDetailIdentity> listUsersDetail() {
throw new UnsupportedOperationException("Not supported yet");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.junit.After;
import org.junit.Assert;
Expand All @@ -43,6 +44,8 @@
import com.oceanbase.tools.dbbrowser.model.DBTableIndex;
import com.oceanbase.tools.dbbrowser.model.DBTablePartition;
import com.oceanbase.tools.dbbrowser.model.DBTablePartitionType;
import com.oceanbase.tools.dbbrowser.model.DBUserDetailIdentity;
import com.oceanbase.tools.dbbrowser.model.DBUserLockStatusType;
import com.oceanbase.tools.dbbrowser.model.DBVariable;
import com.oceanbase.tools.dbbrowser.model.DBView;
import com.oceanbase.tools.dbbrowser.util.DBSchemaAccessorUtil;
Expand Down Expand Up @@ -101,6 +104,18 @@ public void listUsers_Success() {
Assert.assertNotNull(dbUsers.get(0).getName());
}

@Test
public void listUsersDetail_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getOBMySQLDataSource()).createOBMysql();
List<DBUserDetailIdentity> dbUsers = accessor.listUsersDetail();
Assert.assertFalse(dbUsers.isEmpty());
Assert.assertSame(DBObjectType.USER, dbUsers.get(0).getType());
Assert.assertNotNull(dbUsers.get(0).getName());
Optional<DBUserDetailIdentity> dbUser = dbUsers.stream().filter(a -> a.getName().equals("root")).findFirst();
Assert.assertTrue(dbUser.isPresent());
Assert.assertSame(DBUserLockStatusType.UNLOCKED, dbUser.get().getUserStatus());
}

@Test
public void listBasicSchemaColumns_TestAllColumnDataTypes_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getOBMySQLDataSource()).createOBMysql();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -53,6 +54,8 @@
import com.oceanbase.tools.dbbrowser.model.DBTrigger;
import com.oceanbase.tools.dbbrowser.model.DBType;
import com.oceanbase.tools.dbbrowser.model.DBTypeCode;
import com.oceanbase.tools.dbbrowser.model.DBUserDetailIdentity;
import com.oceanbase.tools.dbbrowser.model.DBUserLockStatusType;
import com.oceanbase.tools.dbbrowser.model.DBVariable;
import com.oceanbase.tools.dbbrowser.model.DBView;
import com.oceanbase.tools.dbbrowser.schema.OBMySQLSchemaAccessorTest.DataType;
Expand Down Expand Up @@ -122,6 +125,19 @@ public void listUsers_Success() {
Assert.assertNotNull(dbUsers.get(0).getName());
}


@Test
public void listUsersDetail_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getOBOracleDataSource()).createOBOracle();
List<DBUserDetailIdentity> dbUsers = accessor.listUsersDetail();
Assert.assertFalse(dbUsers.isEmpty());
Assert.assertSame(DBObjectType.USER, dbUsers.get(0).getType());
Assert.assertNotNull(dbUsers.get(0).getName());
Optional<DBUserDetailIdentity> dbUser = dbUsers.stream().filter(a -> a.getName().equals("SYS")).findFirst();
Assert.assertTrue(dbUser.isPresent());
Assert.assertSame(DBUserLockStatusType.UNLOCKED, dbUser.get().getUserStatus());
}

@Test
public void listDatabases_Success() {
DBSchemaAccessor accessor = new DBSchemaAccessors(getOBOracleDataSource()).createOBOracle();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2023 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.oceanbase.odc.service.onlineschemachange;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.oceanbase.odc.TestConnectionUtil;
import com.oceanbase.odc.core.session.ConnectionSession;
import com.oceanbase.odc.core.shared.constant.ConnectType;
import com.oceanbase.odc.service.onlineschemachange.monitor.DBUserLogStatusMonitorFactory;
import com.oceanbase.odc.service.onlineschemachange.monitor.DBUserMonitor;

/**
* @author yaobin
* @date 2023-10-10
* @since 4.2.3
*/
public class DBUserLogStatusMonitorTest {

private static ConnectionSession obMySqlConnSession;
private static ConnectionSession obOracleConnSession;

@BeforeClass
public static void startUp() {
obMySqlConnSession = TestConnectionUtil.getTestConnectionSession(ConnectType.OB_MYSQL);
obOracleConnSession = TestConnectionUtil.getTestConnectionSession(ConnectType.OB_ORACLE);
}

@Test
public void test_monitor_ob_mysql_successful() throws InterruptedException {
List<String> toMonitorUsers = new ArrayList<>();
toMonitorUsers.add("root");
doMonitor(toMonitorUsers, obMySqlConnSession);
}

@Test
public void test_monitor_ob_oracle_successful() throws InterruptedException {
List<String> toMonitorUsers = new ArrayList<>();
toMonitorUsers.add("SYS");
doMonitor(toMonitorUsers, obOracleConnSession);
}

private static void doMonitor(List<String> toMonitorUsers, ConnectionSession connectionSession)
throws InterruptedException {
Integer period = 200;
Integer timeout = 5000;
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
DBUserLogStatusMonitorFactory monitorFactory = new DBUserLogStatusMonitorFactory(null);
DBUserMonitor dbUserMonitor = monitorFactory.generateDBUserMonitor(connectionSession,
toMonitorUsers, period, timeout, timeUnit);

ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
executorService.execute(dbUserMonitor);
Assert.assertFalse(dbUserMonitor.isDone());
Thread.sleep(5000 - 1);
dbUserMonitor.stop();
Assert.assertTrue(dbUserMonitor.isDone());
} finally {
executorService.shutdownNow();
}
}
}
Loading

0 comments on commit 1ba5ad6

Please sign in to comment.