Search in sources :

Example 1 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class SimpleAdminConnector method doServerAdmin.

// ==================== helper method ====================
private String doServerAdmin(String action) {
    try {
        writeWithHeader(Packet.newBuilder().setType(PacketType.SERVER).setBody(ServerAdmin.newBuilder().setAction(action).build().toByteString()).build().toByteArray());
        Packet p = Packet.parseFrom(readNextPacket());
        Ack ack = Ack.parseFrom(p.getBody());
        if (ack.getCode() > 0) {
            throw new ServiceException("failed to subscribe with reason: " + ack.getMessage());
        }
        return ack.getMessage();
    } catch (IOException e) {
        throw new ServiceException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.AdminPacket.Packet) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) Ack(com.alibaba.otter.canal.protocol.AdminPacket.Ack) IOException(java.io.IOException)

Example 2 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class SimpleAdminConnector method connect.

@Override
public void connect() throws ServiceException {
    try {
        if (connected) {
            return;
        }
        channel = SocketChannel.open();
        channel.socket().setSoTimeout(soTimeout);
        channel.connect(address);
        readableChannel = Channels.newChannel(channel.socket().getInputStream());
        writableChannel = Channels.newChannel(channel.socket().getOutputStream());
        Packet p = Packet.parseFrom(readNextPacket());
        if (p.getVersion() != 1) {
            throw new CanalClientException("unsupported version at this client.");
        }
        if (p.getType() != PacketType.HANDSHAKE) {
            throw new CanalClientException("expect handshake but found other type.");
        }
        Handshake handshake = Handshake.parseFrom(p.getBody());
        // seed for auth
        ByteString seed = handshake.getSeeds();
        String newPasswd = passwd;
        if (passwd != null) {
            // encode passwd
            newPasswd = SecurityUtil.byte2HexStr(SecurityUtil.scramble411(passwd.getBytes(), seed.toByteArray()));
        }
        ClientAuth ca = ClientAuth.newBuilder().setUsername(user != null ? user : "").setPassword(ByteString.copyFromUtf8(newPasswd != null ? newPasswd : "")).setNetReadTimeout(idleTimeout).setNetWriteTimeout(idleTimeout).build();
        writeWithHeader(Packet.newBuilder().setType(PacketType.CLIENTAUTHENTICATION).setBody(ca.toByteString()).build().toByteArray());
        // 
        Packet ack = Packet.parseFrom(readNextPacket());
        if (ack.getType() != PacketType.ACK) {
            throw new CanalClientException("unexpected packet type when ack is expected");
        }
        Ack ackBody = Ack.parseFrom(ack.getBody());
        if (ackBody.getCode() > 0) {
            throw new ServiceException("something goes wrong when doing authentication: " + ackBody.getMessage());
        }
        connected = true;
    } catch (IOException | NoSuchAlgorithmException e) {
        throw new ServiceException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.AdminPacket.Packet) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ByteString(com.google.protobuf.ByteString) Ack(com.alibaba.otter.canal.protocol.AdminPacket.Ack) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientAuth(com.alibaba.otter.canal.protocol.AdminPacket.ClientAuth) Handshake(com.alibaba.otter.canal.protocol.AdminPacket.Handshake)

Example 3 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class SimpleAdminConnector method doLogAdmin.

private String doLogAdmin(String type, String action, String destination, String file, int count) {
    try {
        writeWithHeader(Packet.newBuilder().setType(PacketType.LOG).setBody(LogAdmin.newBuilder().setType(type).setAction(action).setDestination(destination == null ? "" : destination).setFile(file == null ? "" : file).setCount(count).build().toByteString()).build().toByteArray());
        Packet p = Packet.parseFrom(readNextPacket());
        Ack ack = Ack.parseFrom(p.getBody());
        if (ack.getCode() > 0) {
            throw new ServiceException("failed to subscribe with reason: " + ack.getMessage());
        }
        return ack.getMessage();
    } catch (IOException e) {
        throw new ServiceException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.AdminPacket.Packet) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) Ack(com.alibaba.otter.canal.protocol.AdminPacket.Ack) IOException(java.io.IOException)

Example 4 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class NodeServerServiceImpl method delete.

public void delete(Long id) {
    NodeServer nodeServer = NodeServer.find.byId(id);
    if (nodeServer != null) {
        // 判断是否存在实例
        int cnt = CanalInstanceConfig.find.query().where().eq("serverId", nodeServer.getId()).findCount();
        if (cnt > 0) {
            throw new ServiceException("当前Server下存在Instance配置, 无法删除");
        }
        // 同时删除配置
        CanalConfig canalConfig = CanalConfig.find.query().where().eq("serverId", id).findOne();
        if (canalConfig != null) {
            canalConfig.delete();
        }
        nodeServer.delete();
    }
}
Also used : CanalConfig(com.alibaba.otter.canal.admin.model.CanalConfig) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) NodeServer(com.alibaba.otter.canal.admin.model.NodeServer)

Example 5 with ServiceException

use of com.alibaba.otter.canal.admin.common.exception.ServiceException in project canal by alibaba.

the class UserServiceImpl method find4Login.

public User find4Login(String username, String password) {
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
        return null;
    }
    User user = User.find.query().where().eq("username", username).findOne();
    if (user == null) {
        throw new ServiceException("user:" + username + " auth failed!");
    }
    try {
        byte[] pass = SecurityUtil.scramble411(password.getBytes(), seeds);
        if (!SecurityUtil.scrambleServerAuth(pass, SecurityUtil.hexStr2Bytes(user.getPassword()), seeds)) {
            throw new ServiceException("user:" + user.getName() + " passwd incorrect!");
        }
    } catch (NoSuchAlgorithmException e) {
        throw new ServiceException("user:" + user.getName() + " auth failed!");
    }
    user.setPassword("");
    return user;
}
Also used : User(com.alibaba.otter.canal.admin.model.User) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)14 NodeServer (com.alibaba.otter.canal.admin.model.NodeServer)5 CanalConfig (com.alibaba.otter.canal.admin.model.CanalConfig)4 Ack (com.alibaba.otter.canal.protocol.AdminPacket.Ack)4 Packet (com.alibaba.otter.canal.protocol.AdminPacket.Packet)4 IOException (java.io.IOException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 CanalCluster (com.alibaba.otter.canal.admin.model.CanalCluster)2 User (com.alibaba.otter.canal.admin.model.User)2 BaseModel (com.alibaba.otter.canal.admin.model.BaseModel)1 CanalInstanceConfig (com.alibaba.otter.canal.admin.model.CanalInstanceConfig)1 ClientAuth (com.alibaba.otter.canal.protocol.AdminPacket.ClientAuth)1 Handshake (com.alibaba.otter.canal.protocol.AdminPacket.Handshake)1 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)1 ByteString (com.google.protobuf.ByteString)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1