Search in sources :

Example 6 with MessageLite

use of com.google.protobuf.MessageLite in project MSEC by Tencent.

the class ResponseEncoder method encodeProtobufBody.

protected int encodeProtobufBody(RpcResponse rpcResponse, ChannelBufferOutputStream stream) throws IOException {
    if (rpcResponse.getResultObj() == null) {
        return 0;
    }
    MessageLite message = (MessageLite) rpcResponse.getResultObj();
    stream.write(message.toByteArray());
    return message.getSerializedSize();
}
Also used : MessageLite(com.google.protobuf.MessageLite)

Example 7 with MessageLite

use of com.google.protobuf.MessageLite in project MSEC by Tencent.

the class RequestDecoder method deserializeProtobufPackage.

private RpcRequest deserializeProtobufPackage(byte[] headBytes, byte[] bodyBytes) {
    Head.CRpcHead pbHead = null;
    RpcRequest rpcRequest = new RpcRequest();
    try {
        pbHead = Head.CRpcHead.parseFrom(headBytes);
        rpcRequest.setSeq(pbHead.getSequence());
    } catch (InvalidProtocolBufferException e) {
        log.error("Parse protobuf head failed.");
        rpcRequest.setException(new IllegalArgumentException("Parse protobuf head failed."));
        return rpcRequest;
    }
    String serviceMethodName = pbHead.getMethodName().toStringUtf8();
    int pos = serviceMethodName.lastIndexOf('.');
    if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
        log.error("Invalid serviceMethodName (" + serviceMethodName + "). Must be in format like *.*");
        rpcRequest.setException(new IllegalArgumentException("Invalid serviceMethodName (" + serviceMethodName + "). Must be in format like *.*"));
        return rpcRequest;
    }
    String serviceName = serviceMethodName.substring(0, pos);
    String methodName = serviceMethodName.substring(pos + 1);
    rpcRequest.setServiceName(serviceName);
    rpcRequest.setMethodName(methodName);
    rpcRequest.setFlowid(pbHead.getFlowId());
    if (pbHead.getCaller() != null && !pbHead.getCaller().isEmpty()) {
        rpcRequest.setFromModule(pbHead.getCaller().toStringUtf8());
    } else {
        rpcRequest.setFromModule("UnknownModule");
    }
    // If flowid == 0, we must generate one
    if (rpcRequest.getFlowid() == 0) {
        rpcRequest.setFlowid(rpcRequest.getSeq() ^ NettyCodecUtils.generateColorId(serviceMethodName));
    }
    AccessMonitor.add("frm.rpc request incoming: " + methodName);
    ServiceFactory.ServiceMethodEntry serviceMethodEntry = ServiceFactory.getServiceMethodEntry(serviceName, methodName);
    if (serviceMethodEntry == null) {
        log.error("No service method registered: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName());
        rpcRequest.setException(new IllegalArgumentException("No service method registered: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName()));
        return rpcRequest;
    }
    MessageLite param = null;
    try {
        param = (MessageLite) serviceMethodEntry.getParamTypeParser().parseFrom(bodyBytes);
    } catch (InvalidProtocolBufferException ex) {
        log.error("Parse protobuf body failed.");
        rpcRequest.setException(new IllegalArgumentException("RParse protobuf body failed." + ex.getMessage()));
        return rpcRequest;
    }
    rpcRequest.setParameter(param);
    log.info("RPC Request received. ServiceMethodName: " + rpcRequest.getServiceName() + "/" + rpcRequest.getMethodName() + "\tSeq: " + rpcRequest.getSeq() + "\tParameter: " + rpcRequest.getParameter());
    return rpcRequest;
}
Also used : Head(srpc.Head) ServiceFactory(org.msec.rpc.ServiceFactory) RpcRequest(org.msec.rpc.RpcRequest) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) MessageLite(com.google.protobuf.MessageLite)

Example 8 with MessageLite

use of com.google.protobuf.MessageLite in project grpc-java by grpc.

the class AbstractInteropTest method checkMetrics.

private static void checkMetrics(MetricsRecord record, boolean server, Collection<? extends MessageLite> requests, Collection<? extends MessageLite> responses) {
    int uncompressedRequestsSize = 0;
    for (MessageLite request : requests) {
        uncompressedRequestsSize += request.getSerializedSize();
    }
    int uncompressedResponsesSize = 0;
    for (MessageLite response : responses) {
        uncompressedResponsesSize += response.getSerializedSize();
    }
    if (server) {
        assertEquals(uncompressedRequestsSize, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES));
        assertEquals(uncompressedResponsesSize, record.getMetricAsLongOrFail(RpcConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES));
        // It's impossible to get the expected wire sizes because it may be compressed, so we just
        // check if they are recorded.
        assertNotNull(record.getMetric(RpcConstants.RPC_SERVER_REQUEST_BYTES));
        assertNotNull(record.getMetric(RpcConstants.RPC_SERVER_RESPONSE_BYTES));
    } else {
        assertEquals(uncompressedRequestsSize, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES));
        assertEquals(uncompressedResponsesSize, record.getMetricAsLongOrFail(RpcConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES));
        assertNotNull(record.getMetric(RpcConstants.RPC_CLIENT_SERVER_ELAPSED_TIME));
        // It's impossible to get the expected wire sizes because it may be compressed, so we just
        // check if they are recorded.
        assertNotNull(record.getMetric(RpcConstants.RPC_CLIENT_REQUEST_BYTES));
        assertNotNull(record.getMetric(RpcConstants.RPC_CLIENT_RESPONSE_BYTES));
    }
}
Also used : MessageLite(com.google.protobuf.MessageLite)

Example 9 with MessageLite

use of com.google.protobuf.MessageLite in project tink by google.

the class Ed25519PublicKeyManagerTest method testModifiedSignature.

@Test
public void testModifiedSignature() throws Exception {
    Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
    KeyTemplate template = SignatureKeyTemplates.ED25519;
    MessageLite key = manager.newKey(template);
    Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
    PublicKeySign signer = manager.getPrimitive(key);
    byte[] message = Random.randBytes(20);
    byte[] signature = signer.sign(message);
    Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
    PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
    try {
        verifier.verify(signature, message);
    } catch (GeneralSecurityException e) {
        fail("Did not expect GeneralSecurityException: " + e);
    }
    // Flip bits in message.
    for (int i = 0; i < message.length; i++) {
        byte[] copy = Arrays.copyOf(message, message.length);
        copy[i] = (byte) (copy[i] ^ 0xff);
        try {
            verifier.verify(signature, copy);
            fail("Expected GeneralSecurityException");
        } catch (GeneralSecurityException e) {
            assertExceptionContains(e, "Signature check failed.");
        }
    }
    // Flip bits in signature.
    // Flip the last byte.
    byte[] copySig = Arrays.copyOf(signature, signature.length);
    copySig[copySig.length - 1] = (byte) (copySig[copySig.length - 1] ^ 0xff);
    try {
        verifier.verify(copySig, message);
        fail("Expected GeneralSecurityException");
    } catch (GeneralSecurityException e) {
        assertExceptionContains(e, "Signature check failed.");
    }
    // Flip other bytes.
    for (int i = 0; i < signature.length - 1; i++) {
        byte[] copy = Arrays.copyOf(signature, signature.length);
        copy[i] = (byte) (copy[i] ^ 0xff);
        try {
            verifier.verify(copy, message);
            fail("Expected GeneralSecurityException");
        } catch (GeneralSecurityException e) {
            assertExceptionContains(e, "Signature check failed.");
        }
    }
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) MessageLite(com.google.protobuf.MessageLite) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Example 10 with MessageLite

use of com.google.protobuf.MessageLite in project MSEC by Tencent.

the class ServiceFactory method callMethod.

public static MessageLite callMethod(String moduleName, String serviceMethodName, MessageLite request, MessageLite responseInstance, int timeoutMillis) throws Exception {
    MessageLite ret = null;
    NettyClient client = null;
    // System.out.println("GetClient begin: " + System.currentTimeMillis());
    try {
        client = ClientManager.getClient(moduleName, true);
    } catch (Exception e) {
        log.error("get route for [" + moduleName + "] failed.", e);
        return ret;
    }
    // System.out.println("AfterConnect: " + System.currentTimeMillis());
    int pos = serviceMethodName.lastIndexOf('.');
    if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
        throw new Exception("Invalid serviceMethodName. Must be in format like *.*");
    }
    String serviceName = serviceMethodName.substring(0, pos);
    String methodName = serviceMethodName.substring(pos + 1);
    RpcRequest rpcRequest = new RpcRequest();
    rpcRequest.setSeq(generateSequence());
    rpcRequest.setServiceName(serviceName);
    rpcRequest.setMethodName(methodName);
    rpcRequest.setParameter(request);
    RpcContext context = (RpcContext) RequestProcessor.getThreadContext("session");
    if (context != null) {
        rpcRequest.setFlowid(context.getRequest().getFlowid());
    }
    RpcResponse rpcResponse = client.sendRequestAndWaitResponse(rpcRequest, timeoutMillis);
    if (rpcResponse != null) {
        if (rpcResponse.getErrno() == 0) {
            ret = responseInstance.getParserForType().parseFrom((byte[]) rpcResponse.getResultObj());
            return ret;
        } else if (rpcResponse.getError() != null) {
            throw rpcResponse.getError();
        } else {
            throw new Exception("Rpc failed: errno = " + rpcResponse.getErrno());
        }
    }
    return ret;
}
Also used : NettyClient(org.msec.net.NettyClient) MessageLite(com.google.protobuf.MessageLite) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

MessageLite (com.google.protobuf.MessageLite)17 PublicKeySign (com.google.crypto.tink.PublicKeySign)2 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)2 Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)2 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Test (org.junit.Test)2 TypicalData (protos.TypicalData)2 Ed25519Sign (com.google.crypto.tink.subtle.Ed25519Sign)1 Ed25519Verify (com.google.crypto.tink.subtle.Ed25519Verify)1 Descriptors (com.google.protobuf.Descriptors)1 ExtensionRegistryLite (com.google.protobuf.ExtensionRegistryLite)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Parser (com.google.protobuf.Parser)1 Deadline (io.grpc.Deadline)1 ForwardingClientCallListener (io.grpc.ForwardingClientCallListener)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1