new version provision
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
|
||||
*
|
||||
* 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 org.maxkey.persistence.mapper;
|
||||
|
||||
import org.apache.mybatis.jpa.persistence.IJpaBaseMapper;
|
||||
import org.maxkey.entity.Connectors;
|
||||
|
||||
/**
|
||||
* @author Crystal.sea
|
||||
*
|
||||
*/
|
||||
|
||||
public interface ConnectorsMapper extends IJpaBaseMapper<Connectors> {
|
||||
|
||||
}
|
||||
@@ -71,7 +71,7 @@ public class AccountsService extends JpaBaseService<Accounts>{
|
||||
|
||||
public boolean insert(Accounts account) {
|
||||
if (super.insert(account)) {
|
||||
if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
|
||||
if(mqPersistService.getApplicationConfig().isProvisionSupport()) {
|
||||
UserInfo loadUserInfo = userInfoService.findUserRelated(account.getUserId());
|
||||
account.setUserInfo(loadUserInfo);
|
||||
OrganizationsCast cast = new OrganizationsCast();
|
||||
@@ -91,7 +91,7 @@ public class AccountsService extends JpaBaseService<Accounts>{
|
||||
|
||||
public boolean update(Accounts account) {
|
||||
if (super.update(account)) {
|
||||
if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
|
||||
if(mqPersistService.getApplicationConfig().isProvisionSupport()) {
|
||||
UserInfo loadUserInfo = userInfoService.findUserRelated(account.getUserId());
|
||||
account.setUserInfo(loadUserInfo);
|
||||
OrganizationsCast cast = new OrganizationsCast();
|
||||
@@ -116,7 +116,7 @@ public class AccountsService extends JpaBaseService<Accounts>{
|
||||
Accounts account = this.get(id);
|
||||
if (super.remove(id)) {
|
||||
UserInfo loadUserInfo = null;
|
||||
if(mqPersistService.getApplicationConfig().isMessageQueueSupport()) {
|
||||
if(mqPersistService.getApplicationConfig().isProvisionSupport()) {
|
||||
loadUserInfo = userInfoService.findUserRelated(account.getUserId());
|
||||
account.setUserInfo(loadUserInfo);
|
||||
mqPersistService.send(
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
|
||||
*
|
||||
* 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 org.maxkey.persistence.service;
|
||||
|
||||
import org.apache.mybatis.jpa.persistence.JpaBaseService;
|
||||
import org.maxkey.entity.Connectors;
|
||||
import org.maxkey.persistence.mapper.ConnectorsMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class ConnectorsService extends JpaBaseService<Connectors>{
|
||||
final static Logger _logger = LoggerFactory.getLogger(ConnectorsService.class);
|
||||
|
||||
public ConnectorsService() {
|
||||
super(ConnectorsMapper.class);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.connsec.db.service.BaseService#getMapper()
|
||||
*/
|
||||
@Override
|
||||
public ConnectorsMapper getMapper() {
|
||||
return (ConnectorsMapper)super.getMapper();
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,7 @@ public class UserInfoService extends JpaBaseService<UserInfo> {
|
||||
public boolean insert(UserInfo userInfo) {
|
||||
this.passwordEncoder(userInfo);
|
||||
if (super.insert(userInfo)) {
|
||||
if(messageQueueService.getApplicationConfig().isMessageQueueSupport()) {
|
||||
if(messageQueueService.getApplicationConfig().isProvisionSupport()) {
|
||||
UserInfo loadUserInfo = findUserRelated(userInfo.getId());
|
||||
messageQueueService.send(
|
||||
ProvisionTopic.USERINFO_TOPIC,
|
||||
@@ -92,7 +92,7 @@ public class UserInfoService extends JpaBaseService<UserInfo> {
|
||||
public boolean update(UserInfo userInfo) {
|
||||
ChangePassword changePassword = this.passwordEncoder(userInfo);
|
||||
if (super.update(userInfo)) {
|
||||
if(messageQueueService.getApplicationConfig().isMessageQueueSupport()) {
|
||||
if(messageQueueService.getApplicationConfig().isProvisionSupport()) {
|
||||
UserInfo loadUserInfo = findUserRelated(userInfo.getId());
|
||||
accountUpdate(loadUserInfo);
|
||||
messageQueueService.send(
|
||||
@@ -110,7 +110,7 @@ public class UserInfoService extends JpaBaseService<UserInfo> {
|
||||
|
||||
public boolean delete(UserInfo userInfo) {
|
||||
UserInfo loadUserInfo = null;
|
||||
if(messageQueueService.getApplicationConfig().isMessageQueueSupport()) {
|
||||
if(messageQueueService.getApplicationConfig().isProvisionSupport()) {
|
||||
loadUserInfo = findUserRelated(userInfo.getId());
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ public class ProvisionMessage {
|
||||
String sendTime;
|
||||
String content;
|
||||
int connected;
|
||||
int instId;
|
||||
|
||||
Object sourceObject;
|
||||
|
||||
@@ -60,7 +61,7 @@ public class ProvisionMessage {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Object getContent() {
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@@ -84,6 +85,14 @@ public class ProvisionMessage {
|
||||
this.sourceObject = sourceObject;
|
||||
}
|
||||
|
||||
public int getInstId() {
|
||||
return instId;
|
||||
}
|
||||
|
||||
public void setInstId(int instId) {
|
||||
this.instId = instId;
|
||||
}
|
||||
|
||||
public ProvisionMessage() {
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public class ProvisionService {
|
||||
*/
|
||||
public void send(String topic,Object content,String actionType) {
|
||||
//maxkey.server.message.queue , if not none
|
||||
if(applicationConfig.isMessageQueueSupport()) {
|
||||
if(applicationConfig.isProvisionSupport()) {
|
||||
ProvisionMessage message =
|
||||
new ProvisionMessage(
|
||||
UUID.randomUUID().toString(), //message id as uuid
|
||||
@@ -58,7 +58,7 @@ public class ProvisionService {
|
||||
);
|
||||
//sand msg to provision topic
|
||||
Thread thread = null;
|
||||
if(applicationConfig.getMessageQueue().equalsIgnoreCase("provision")) {
|
||||
if(applicationConfig.isProvisionSupport()) {
|
||||
_logger.trace("message...");
|
||||
thread = new ProvisioningThread(jdbcTemplate,message);
|
||||
thread.start();
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
package org.maxkey.provision.thread;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.mybatis.jpa.query.Query;
|
||||
import org.maxkey.crypto.password.PasswordReciprocal;
|
||||
import org.maxkey.entity.ChangePassword;
|
||||
import org.maxkey.entity.Connectors;
|
||||
import org.maxkey.entity.Message;
|
||||
import org.maxkey.entity.Organizations;
|
||||
import org.maxkey.entity.UserInfo;
|
||||
import org.maxkey.persistence.service.ConnectorsService;
|
||||
import org.maxkey.provision.ProvisionAction;
|
||||
import org.maxkey.provision.ProvisionMessage;
|
||||
import org.maxkey.provision.ProvisionTopic;
|
||||
import org.maxkey.util.AuthorizationHeaderUtils;
|
||||
import org.maxkey.util.DateUtils;
|
||||
import org.maxkey.util.JsonUtils;
|
||||
import org.maxkey.util.ObjectTransformer;
|
||||
import org.maxkey.web.HttpRequestAdapter;
|
||||
import org.maxkey.web.WebContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
public class ProvisioningRunner {
|
||||
private static final Logger _logger = LoggerFactory.getLogger(ProvisioningRunner.class);
|
||||
|
||||
static final String PROVISION_SELECT_STATEMENT = "select * from mxk_history_provisions where connected = 0 order by sendtime asc limit 500";
|
||||
|
||||
static final String PROVISION_UPDATE_STATEMENT = "update mxk_history_provisions set connected = connected + 1 where id = ?";
|
||||
|
||||
static final String PROVISION_LOG_INSERT_STATEMENT = "insert into mxk_history_connector(id,conname,topic,actiontype,sourceid,sourcename,synctime,result,instid) values (? , ? , ? , ? , ? , ? , ? , ? , ? )";
|
||||
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
ConnectorsService connectorsService;
|
||||
|
||||
public ProvisioningRunner(ConnectorsService connectorsService,JdbcTemplate jdbcTemplate) {
|
||||
this.connectorsService = connectorsService;
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
|
||||
public void provisions() {
|
||||
List<Connectors> listConnectors = connectorsService.query(new Query().eq("status", 1).eq("justintime", 1));
|
||||
List<ProvisionMessage> listProvisionMessage = jdbcTemplate.query(PROVISION_SELECT_STATEMENT, new ProvisionMessageRowMapper());
|
||||
for(ProvisionMessage msg : listProvisionMessage) {
|
||||
for(Connectors connector: listConnectors) {
|
||||
provision(msg,connector);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void provision(ProvisionMessage provisionMessage,Connectors connector) {
|
||||
if(Integer.parseInt(connector.getInstId()) == provisionMessage.getInstId()) {
|
||||
String url = connector.getProviderUrl();
|
||||
if(!url.endsWith("/")) {
|
||||
url = url + "/";
|
||||
}
|
||||
String resultMessage = "";
|
||||
String objectId = "";
|
||||
String objectName = "";
|
||||
if(provisionMessage.getTopic().equalsIgnoreCase(ProvisionTopic.USERINFO_TOPIC)) {
|
||||
UserInfo user = (UserInfo)ObjectTransformer.deserialize(provisionMessage.getContent());
|
||||
user.setPassword(null);
|
||||
user.setDecipherable(null);
|
||||
objectId = user.getId();
|
||||
objectName = user.getDisplayName()+"("+user.getUsername()+")";
|
||||
resultMessage = provisionUser(user,url,provisionMessage.getActionType(),connector);
|
||||
provisionLog( connector.getConnName(),
|
||||
"Users",
|
||||
provisionMessage.getActionType(),
|
||||
objectId,
|
||||
objectName,
|
||||
resultMessage,
|
||||
provisionMessage.getInstId()
|
||||
);
|
||||
}else if(provisionMessage.getTopic().equalsIgnoreCase(ProvisionTopic.PASSWORD_TOPIC)) {
|
||||
ChangePassword changePassword = (ChangePassword)ObjectTransformer.deserialize(provisionMessage.getContent());
|
||||
objectId = changePassword.getUserId();
|
||||
objectName = changePassword.getDisplayName()+"("+changePassword.getUsername()+")";
|
||||
resultMessage = provisionChangePassword(changePassword,url,provisionMessage.getActionType(),connector);
|
||||
provisionLog( connector.getConnName(),
|
||||
"Password",
|
||||
provisionMessage.getActionType(),
|
||||
objectId,
|
||||
objectName,
|
||||
resultMessage,
|
||||
provisionMessage.getInstId()
|
||||
);
|
||||
}else if(provisionMessage.getTopic().equalsIgnoreCase(ProvisionTopic.ORG_TOPIC)) {
|
||||
Organizations organization = (Organizations)ObjectTransformer.deserialize(provisionMessage.getContent());
|
||||
objectId = organization.getId();
|
||||
objectName = organization.getOrgName();
|
||||
resultMessage = provisionOrganization(organization,url,provisionMessage.getActionType(),connector);
|
||||
provisionLog( connector.getConnName(),
|
||||
"Organizations",
|
||||
provisionMessage.getActionType(),
|
||||
objectId,
|
||||
objectName,
|
||||
resultMessage,
|
||||
provisionMessage.getInstId()
|
||||
);
|
||||
}
|
||||
|
||||
jdbcTemplate.update(PROVISION_UPDATE_STATEMENT,provisionMessage.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public void provisionLog(String conName,String topic,String actionType,String sourceId,String sourceName,String resultMessage,int instid) {
|
||||
Message<?> resultMsg = JsonUtils.json2Object(resultMessage, Message.class);
|
||||
String result = "success";
|
||||
if(resultMsg == null || resultMsg.getCode() != 0) {
|
||||
result = "fail";
|
||||
}
|
||||
|
||||
jdbcTemplate.update(PROVISION_LOG_INSERT_STATEMENT,
|
||||
WebContext.genId(),
|
||||
conName,
|
||||
topic,
|
||||
actionType.replace("_ACTION", "").toLowerCase(),
|
||||
sourceId,
|
||||
sourceName,
|
||||
DateUtils.getCurrentDateTimeAsString(),
|
||||
result,
|
||||
instid
|
||||
);
|
||||
}
|
||||
|
||||
public String getActionType(String actionType) {
|
||||
if(actionType.equalsIgnoreCase(ProvisionAction.CREATE_ACTION)) {
|
||||
return "create";
|
||||
}else if(actionType.equalsIgnoreCase(ProvisionAction.UPDATE_ACTION)) {
|
||||
return "update";
|
||||
}else if(actionType.equalsIgnoreCase(ProvisionAction.DELETE_ACTION)) {
|
||||
return "delete";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
String provisionUser(UserInfo user,String baseUrl,String actionType,Connectors connector){
|
||||
baseUrl = baseUrl + "Users/" + getActionType(actionType);
|
||||
_logger.debug("URL {} ", baseUrl);
|
||||
HashMap<String,String> authorizationMap = AuthorizationHeaderUtils.authorization(
|
||||
AuthorizationHeaderUtils.createBasic(
|
||||
connector.getPrincipal(),
|
||||
PasswordReciprocal.getInstance().decoder(connector.getCredentials()))
|
||||
);
|
||||
|
||||
return new HttpRequestAdapter().postJson( baseUrl,
|
||||
JsonUtils.gson2Json(user),
|
||||
authorizationMap
|
||||
);
|
||||
}
|
||||
|
||||
String provisionOrganization(Organizations organizations,String baseUrl,String actionType,Connectors connector){
|
||||
baseUrl = baseUrl + "Organizations/"+ getActionType(actionType);
|
||||
_logger.debug("URL {} ", baseUrl);
|
||||
HashMap<String,String> authorizationMap = AuthorizationHeaderUtils.authorization(
|
||||
AuthorizationHeaderUtils.createBasic(
|
||||
connector.getPrincipal(),
|
||||
PasswordReciprocal.getInstance().decoder(connector.getCredentials()))
|
||||
);
|
||||
|
||||
return new HttpRequestAdapter().postJson( baseUrl,
|
||||
JsonUtils.gson2Json(organizations),
|
||||
authorizationMap
|
||||
);
|
||||
}
|
||||
|
||||
String provisionChangePassword(ChangePassword changePassword,String baseUrl,String actionType,Connectors connector){
|
||||
baseUrl = baseUrl + "Users/changePassword";
|
||||
_logger.debug("URL {} ", baseUrl);
|
||||
HashMap<String,String> authorizationMap = AuthorizationHeaderUtils.authorization(
|
||||
AuthorizationHeaderUtils.createBasic(
|
||||
connector.getPrincipal(),
|
||||
PasswordReciprocal.getInstance().decoder(connector.getCredentials()))
|
||||
);
|
||||
|
||||
return new HttpRequestAdapter().postJson( baseUrl,
|
||||
JsonUtils.gson2Json(changePassword),
|
||||
authorizationMap
|
||||
);
|
||||
}
|
||||
|
||||
public class ProvisionMessageRowMapper implements RowMapper<ProvisionMessage> {
|
||||
@Override
|
||||
public ProvisionMessage mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
ProvisionMessage msg = new ProvisionMessage();
|
||||
msg.setId(rs.getString("id"));
|
||||
msg.setActionType(rs.getString("actiontype"));
|
||||
msg.setTopic(rs.getString("topic"));
|
||||
msg.setContent(rs.getString("content"));
|
||||
msg.setConnected(rs.getInt("connected"));
|
||||
msg.setInstId(rs.getInt("instid"));
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.maxkey.provision.thread;
|
||||
|
||||
public class ProvisioningRunnerThread extends Thread{
|
||||
|
||||
ProvisioningRunner runner;
|
||||
|
||||
public ProvisioningRunnerThread(ProvisioningRunner runner) {
|
||||
super();
|
||||
this.runner = runner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(true) {
|
||||
try {
|
||||
Thread.sleep(60 * 1000);
|
||||
runner.provisions();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import java.sql.Types;
|
||||
|
||||
import org.maxkey.pretty.impl.JsonPretty;
|
||||
import org.maxkey.provision.ProvisionMessage;
|
||||
import org.maxkey.util.JsonUtils;
|
||||
import org.maxkey.util.ObjectTransformer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -33,7 +34,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
public class ProvisioningThread extends Thread{
|
||||
private static final Logger _logger = LoggerFactory.getLogger(ProvisioningThread.class);
|
||||
|
||||
static final String PROVISION_INSERT_STATEMENT = "insert into mxk_history_provisions(`id`,`topic`,`actiontype`,`content`,`sendtime`,`connected`) values (? , ? , ? , ? , ? , ? )";
|
||||
static final String PROVISION_INSERT_STATEMENT = "insert into mxk_history_provisions(id,topic,actiontype,content,sendtime,connected,instid) values (? , ? , ? , ? , ? , ? , ? )";
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
@@ -49,15 +50,31 @@ public class ProvisioningThread extends Thread{
|
||||
public void run() {
|
||||
_logger.debug("send message \n{}" ,new JsonPretty().jacksonFormat(msg.getSourceObject()));
|
||||
msg.setContent(ObjectTransformer.serialize((Serializable)msg.getSourceObject()));
|
||||
Inst inst = JsonUtils.gson2Object(JsonUtils.gson2Json(msg.getSourceObject()), Inst.class);
|
||||
jdbcTemplate.update(PROVISION_INSERT_STATEMENT,
|
||||
new Object[] {
|
||||
msg.getId(), msg.getTopic(), msg.getActionType(), msg.getContent(),
|
||||
msg.getSendTime(),msg.getConnected()
|
||||
msg.getSendTime(),msg.getConnected(),inst.getInstId()
|
||||
},
|
||||
new int[] {
|
||||
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
|
||||
Types.TINYINT
|
||||
Types.TINYINT,Types.TINYINT
|
||||
});
|
||||
_logger.debug("send to Message Queue finished .");
|
||||
}
|
||||
|
||||
class Inst{
|
||||
|
||||
int instId;
|
||||
|
||||
public int getInstId() {
|
||||
return instId;
|
||||
}
|
||||
|
||||
public void setInstId(int instId) {
|
||||
this.instId = instId;
|
||||
}
|
||||
|
||||
public Inst() {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.maxkey.persistence.mapper.ConnectorsMapper">
|
||||
|
||||
<sql id="where_statement">
|
||||
<if test="id != null and id != ''">
|
||||
and id = #{id}
|
||||
</if>
|
||||
<if test="connName != null and connName != ''">
|
||||
and connname like '%${connName}%'
|
||||
</if>
|
||||
</sql>
|
||||
|
||||
<select id="queryPageResults" parameterType="Connectors" resultType="Connectors">
|
||||
select
|
||||
*
|
||||
from
|
||||
mxk_connectors
|
||||
where
|
||||
instid = #{instId}
|
||||
<include refid="where_statement"/>
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
@@ -11,10 +11,6 @@
|
||||
and conName = #{conName}
|
||||
</if>
|
||||
|
||||
<if test="conType != null and conType != ''">
|
||||
and conType = #{conType}
|
||||
</if>
|
||||
|
||||
<if test="sourceId != null and sourceId != ''">
|
||||
and sourceId = #{sourceId}
|
||||
</if>
|
||||
|
||||
Reference in New Issue
Block a user