Optimize code according to lint

Signed-off-by: spike <hello@spike.wiki>
This commit is contained in:
spike 2022-11-20 15:33:53 +08:00
parent 6937857bf8
commit 55a06c40c4
4 changed files with 12 additions and 14 deletions

View File

@ -16,7 +16,6 @@ package mqtt
import (
"context"
"crypto/tls"
"encoding/pem"
"errors"
"fmt"
"net/url"
@ -64,13 +63,6 @@ func NewMQTTPubSub(logger logger.Logger) pubsub.PubSub {
}
}
// isValidPEM validates the provided input has PEM formatted block.
func isValidPEM(val string) bool {
block, _ := pem.Decode([]byte(val))
return block != nil
}
// Init parses metadata and creates a new Pub Sub client.
func (m *mqttPubSub) Init(metadata pubsub.Metadata) error {
mqttMeta, err := parseMQTTMetaData(metadata, m.logger)

View File

@ -38,7 +38,7 @@ func newRabbitMQTest(broker *rabbitMQInMemoryBroker) pubsub.PubSub {
return &rabbitMQ{
declaredExchanges: make(map[string]bool),
logger: logger.NewLogger("test"),
connectionDial: func(schema, uri string, tlsCfg *tls.Config) (rabbitMQConnectionBroker, rabbitMQChannelBroker, error) {
connectionDial: func(protocol, uri string, tlsCfg *tls.Config) (rabbitMQConnectionBroker, rabbitMQChannelBroker, error) {
broker.connectCount++
return broker, broker, nil

View File

@ -8,6 +8,7 @@ import (
"fmt"
)
// TLSProperties is a struct that contains the TLS properties.
type TLSProperties struct {
CACert string
ClientCert string
@ -15,9 +16,12 @@ type TLSProperties struct {
}
const (
CACert = "caCert"
// CACert is the metadata key name for the CA certificate.
CACert = "caCert"
// ClientCert is the metadata key name for the client certificate.
ClientCert = "clientCert"
ClientKey = "clientKey"
// ClientKey is the metadata key name for the client key.
ClientKey = "clientKey"
)
// TLS takes a metadata object and returns the TLSProperties configured.
@ -46,13 +50,14 @@ func TLS(metadata map[string]string) (TLSProperties, error) {
return cfg, nil
}
// ConvertTLSPropertiesToTLSConfig converts the TLSProperties to a tls.Config.
func ConvertTLSPropertiesToTLSConfig(properties TLSProperties) (*tls.Config, error) {
tlsConfig := new(tls.Config)
if properties.ClientCert != "" && properties.ClientKey != "" {
cert, err := tls.X509KeyPair([]byte(properties.ClientCert), []byte(properties.ClientKey))
if err != nil {
return &tls.Config{}, fmt.Errorf("unable to load client certificate and key pair. Err: %v", err)
return tlsConfig, fmt.Errorf("unable to load client certificate and key pair. Err: %v", err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
@ -60,7 +65,7 @@ func ConvertTLSPropertiesToTLSConfig(properties TLSProperties) (*tls.Config, err
if properties.CACert != "" {
tlsConfig.RootCAs = x509.NewCertPool()
if ok := tlsConfig.RootCAs.AppendCertsFromPEM([]byte(properties.CACert)); !ok {
return &tls.Config{}, fmt.Errorf("unable to load CA certificate")
return tlsConfig, fmt.Errorf("unable to load CA certificate")
}
}

View File

@ -3,8 +3,9 @@ package pubsub
import (
"crypto/x509"
"encoding/pem"
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConvertTLSPropertiesToTLSConfig(t *testing.T) {