Search in sources :

Example 26 with Protocol

use of io.fabric8.gateway.handlers.detecting.Protocol in project fabric8 by jboss-fuse.

the class AmqpProtocol method experimentalSnoopConnectionParameters.

public void experimentalSnoopConnectionParameters(final NetSocket socket, final Buffer received, final Handler<ConnectionParameters> handler) {
    final AmqpProtocolDecoder h = new AmqpProtocolDecoder(this);
    final ConnectionParameters parameters = new ConnectionParameters();
    h.errorHandler(new Handler<String>() {

        @Override
        public void handle(String error) {
            LOG.info("STOMP protocol decoding error: " + error);
            socket.close();
        }
    });
    h.codecHandler(new Handler<AmqpEvent>() {

        EngineFactory engineFactory = new EngineFactoryImpl();

        Transport protonTransport = engineFactory.createTransport();

        Connection protonConnection = engineFactory.createConnection();

        Sasl sasl;

        @Override
        public void handle(AmqpEvent event) {
            switch(event.type) {
                case HEADER:
                    AmqpHeader header = (AmqpHeader) event.decodedFrame;
                    switch(header.getProtocolId()) {
                        case 0:
                            // amqpTransport.sendToAmqp(new AmqpHeader());
                            break;
                        // nothing to do..
                        case 3:
                            // Client will be using SASL for auth..
                            sasl = protonTransport.sasl();
                            // sasl.setMechanisms(new String[] { "ANONYMOUS", "PLAIN" });
                            sasl.server();
                            break;
                        default:
                    }
                    processEvent(event);
                    // Les send back the AMQP response headers so that the client
                    // can send us the SASL init or AMQP open frames.
                    Buffer buffer = toBuffer(protonTransport.getOutputBuffer());
                    protonTransport.outputConsumed();
                    socket.write(buffer);
                    break;
                default:
                    processEvent(event);
            }
        }

        private void processEvent(AmqpEvent event) {
            byte[] buffer = event.encodedFrame.getBytes();
            int offset = 0;
            int remaining = buffer.length;
            while (remaining > 0) {
                try {
                    int count = protonTransport.input(buffer, offset, remaining);
                    offset += count;
                    remaining -= count;
                } catch (Throwable e) {
                    LOG.info("Could not decode AMQP frame: " + e, e);
                    socket.close();
                    return;
                }
                if (sasl != null) {
                    // TODO: add timeout in case the client is waiting for SASL negotiation
                    if (sasl.getRemoteMechanisms().length > 0) {
                        parameters.protocolVirtualHost = getHostname(sasl);
                        if ("PLAIN".equals(sasl.getRemoteMechanisms()[0])) {
                            byte[] data = new byte[sasl.pending()];
                            sasl.recv(data, 0, data.length);
                            Buffer[] parts = split(new Buffer(data), (byte) 0);
                            if (parts.length > 0) {
                                parameters.protocolUser = parts[0].toString();
                            }
                            // We are done!
                            handler.handle(parameters);
                        }
                    }
                }
                if (protonConnection.getLocalState() == EndpointState.UNINITIALIZED && protonConnection.getRemoteState() != EndpointState.UNINITIALIZED) {
                    // If we get here them the connection was not using SASL.. we can get the hostname
                    // info from the open frame.
                    parameters.protocolVirtualHost = protonConnection.getRemoteHostname();
                    // We are done!
                    handler.handle(parameters);
                }
            }
        }
    });
    socket.dataHandler(h);
    h.handle(received);
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters) EngineFactoryImpl(org.apache.qpid.proton.engine.impl.EngineFactoryImpl)

Example 27 with Protocol

use of io.fabric8.gateway.handlers.detecting.Protocol in project strimzi by strimzi.

the class AbstractModel method createContainerPort.

protected ContainerPort createContainerPort(String name, int port, String protocol) {
    ContainerPort containerPort = new ContainerPortBuilder().withName(name).withProtocol(protocol).withContainerPort(port).build();
    log.trace("Created container port {}", containerPort);
    return containerPort;
}
Also used : ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort)

Example 28 with Protocol

use of io.fabric8.gateway.handlers.detecting.Protocol in project strimzi by strimzi.

the class AbstractModel method createServicePort.

protected ServicePort createServicePort(String name, int port, int targetPort, String protocol) {
    ServicePort servicePort = new ServicePortBuilder().withName(name).withProtocol(protocol).withPort(port).withNewTargetPort(targetPort).build();
    log.trace("Created service port {}", servicePort);
    return servicePort;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder)

Example 29 with Protocol

use of io.fabric8.gateway.handlers.detecting.Protocol in project fabric8-maven-plugin by fabric8io.

the class AbstractLiveEnricher method getExternalServiceURL.

/**
 * Returns the external access to the given service name
 *
 * @param serviceName name of the service
 * @param protocol URL protocol such as <code>http</code>
 */
protected String getExternalServiceURL(String serviceName, String protocol) {
    if (!isOnline()) {
        getLog().info("Not looking for service " + serviceName + " as we are in offline mode");
        return null;
    } else {
        try {
            KubernetesClient kubernetes = getKubernetes();
            String ns = kubernetes.getNamespace();
            if (Strings.isNullOrBlank(ns)) {
                ns = getNamespace();
            }
            Service service = kubernetes.services().inNamespace(ns).withName(serviceName).get();
            return service != null ? KubernetesHelper.getServiceURL(kubernetes, serviceName, ns, protocol, true) : null;
        } catch (Throwable e) {
            Throwable cause = e;
            boolean notFound = false;
            boolean connectError = false;
            Stack<Throwable> stack = unfoldExceptions(e);
            while (!stack.isEmpty()) {
                Throwable t = stack.pop();
                if (t instanceof ConnectException || "No route to host".equals(t.getMessage())) {
                    getLog().warn("Cannot connect to Kubernetes to find URL for service %s : %s", serviceName, cause.getMessage());
                    return null;
                } else if (t instanceof IllegalArgumentException || t.getMessage() != null && t.getMessage().matches("^No.*found.*$")) {
                    getLog().warn("%s", cause.getMessage());
                    return null;
                }
                ;
            }
            getLog().warn("Cannot find URL for service %s : %s", serviceName, cause.getMessage());
            return null;
        }
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Service(io.fabric8.kubernetes.api.model.Service) Stack(java.util.Stack) ConnectException(java.net.ConnectException)

Example 30 with Protocol

use of io.fabric8.gateway.handlers.detecting.Protocol in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method ensureExternalGitSecretsAreSetupFor.

protected void ensureExternalGitSecretsAreSetupFor(KubernetesClient kubernetes, String namespace, String gitRemoteURL) throws MojoExecutionException {
    String secretNamespace = getSecretNamespace();
    ensureNamespaceExists(kubernetes, secretNamespace);
    ConfigMap configMap = getSecretGitConfigMap(kubernetes, namespace, secretNamespace);
    String host = GitUtils.getGitHostName(gitRemoteURL);
    if (host == null) {
        host = "default";
    }
    String protocol = GitUtils.getGitProtocol(gitRemoteURL);
    boolean isSsh = Objects.equal("ssh", protocol);
    String currentSecretName = configMap.getData().get(host);
    if (currentSecretName == null) {
        currentSecretName = createGitSecretName(namespace, host);
    }
    Secret secret = findOrCreateGitSecret(kubernetes, currentSecretName, host);
    if (isSsh) {
        // lets see if we need to import ssh keys
        Map<String, String> secretData = secret.getData();
        if (secretData == null) {
            secretData = new HashMap<>();
        }
        if (!secretData.containsKey(PROPERTY_PRIVATE_KEY) || !secretData.containsKey(PROPERTY_PUBLIC_KEY)) {
            String answer = null;
            try {
                answer = prompter.prompt("Would you like to import your local SSH public/private key pair from your ~/.ssh folder? (Y/n)");
            } catch (PrompterException e) {
                log.warn("Failed to get prompt: %s", e);
            }
            if (answer != null && answer.trim().isEmpty() || answer.trim().toUpperCase().startsWith("Y")) {
                chooseSshKeyPairs(secretData, host);
                secret.setData(secretData);
            }
        }
    } else {
        // if empty or retrying lets re-enter the user/pwd
        getGogsSecretField(kubernetes, secret, host, "username");
        getGogsSecretField(kubernetes, secret, host, "password");
    }
    createOrUpdateSecret(kubernetes, secret);
    updateSecretGitConfigMap(kubernetes, secretNamespace, configMap, host, currentSecretName);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Aggregations

ArrayList (java.util.ArrayList)9 Protocol (io.fabric8.annotations.Protocol)5 ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)5 PortName (io.fabric8.annotations.PortName)4 ServiceName (io.fabric8.annotations.ServiceName)4 AmqpProtocol (io.fabric8.gateway.handlers.detecting.protocol.amqp.AmqpProtocol)4 HttpProtocol (io.fabric8.gateway.handlers.detecting.protocol.http.HttpProtocol)4 MqttProtocol (io.fabric8.gateway.handlers.detecting.protocol.mqtt.MqttProtocol)4 OpenwireProtocol (io.fabric8.gateway.handlers.detecting.protocol.openwire.OpenwireProtocol)4 SslConfig (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslConfig)4 SslProtocol (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslProtocol)4 StompProtocol (io.fabric8.gateway.handlers.detecting.protocol.stomp.StompProtocol)4 LoadBalancer (io.fabric8.gateway.loadbalancer.LoadBalancer)4 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)4 File (java.io.File)4 IOException (java.io.IOException)4 URI (java.net.URI)4 List (java.util.List)4 External (io.fabric8.annotations.External)3 URISyntaxException (java.net.URISyntaxException)3