예제 #1
0
        private void ValidateConnection(string correlationId, ConnectionParams connection)
        {
            if (connection == null)
            {
                throw new ConfigException(correlationId, "NO_CONNECTION", "NATS connection is not set");
            }

            var uri = connection.Uri;

            if (!string.IsNullOrEmpty(uri))
            {
                return;
            }

            var protocol = connection.GetProtocolWithDefault("nats");

            if (string.IsNullOrEmpty(protocol))
            {
                throw new ConfigException(correlationId, "NO_PROTOCOL", "Connection protocol is not set");
            }
            if (protocol != "nats")
            {
                throw new ConfigException(correlationId, "UNSUPPORTED_PROTOCOL", "The protocol " + protocol + " is not supported");
            }

            var host = connection.Host;

            if (string.IsNullOrEmpty(host))
            {
                throw new ConfigException(correlationId, "NO_HOST", "Connection host is not set");
            }

            var port = connection.GetAsIntegerWithDefault("port", 1883);

            if (port == 0)
            {
                throw new ConfigException(correlationId, "NO_PORT", "Connection port is not set");
            }

            return;
        }
        private void ValidateConnection(string correlationId, ConnectionParams connection, CredentialParams credential)
        {
            if (connection == null)
            {
                throw new ConfigException(correlationId, "NO_CONNECTION", "HTTP connection is not set");
            }

            var uri = connection.Uri;

            if (!string.IsNullOrEmpty(uri))
            {
                return;
            }

            var protocol = connection.GetProtocolWithDefault("http");

            if ("http" != protocol && "https" != protocol)
            {
                throw new ConfigException(
                          correlationId, "WRONG_PROTOCOL", "Protocol is not supported by REST connection")
                      .WithDetails("protocol", protocol);
            }

            var host = connection.Host;

            if (host == null)
            {
                throw new ConfigException(correlationId, "NO_HOST", "Connection host is not set");
            }

            var port = connection.Port;

            if (port == 0)
            {
                throw new ConfigException(correlationId, "NO_PORT", "Connection port is not set");
            }

            // Check HTTPS credentials
            if (protocol == "https")
            {
                // Check for credential
                if (credential == null)
                {
                    throw new ConfigException(
                              correlationId, "NO_CREDENTIAL", "SSL certificates are not configured for HTTPS protocol");
                }

                // Sometimes when we use https we are on an internal network and do not want to have to deal with security.
                // When we need a https connection and we don't want to pass credentials, set the value 'no_credentials_needed' in the config yml credentials section
                if (credential.GetAsNullableString("internal_network") == null)
                {
                    if (credential.GetAsNullableString("ssl_password") == null)
                    {
                        throw new ConfigException(
                                  correlationId, "NO_SSL_PASSWORD", "SSL password is not configured in credentials");
                    }

                    if (credential.GetAsNullableString("ssl_pfx_file") == null)
                    {
                        throw new ConfigException(
                                  correlationId, "NO_SSL_PFX_FILE", "SSL pfx file is not configured in credentials");
                    }
                }
            }
        }