Skip to content

Instantly share code, notes, and snippets.

@scholzj
Last active Oct 31, 2018
Embed
What would you like to do?
SASL EXTERNAL in Go
package main
import (
"fmt"
"qpid.apache.org/electron"
"os"
"crypto/tls"
"io/ioutil"
"crypto/x509"
)
func main() {
// Prepare SSL and stuff around it
userKey, err := tls.LoadX509KeyPair("myKey.crt", "myKey.pem")
if err != nil {
fmt.Printf("Failed to load member certificate: %s", err)
os.Exit(1)
}
brokerCert, err := ioutil.ReadFile("broker.crt")
if err != nil {
fmt.Printf("Failed to load broker certificate: %s", err)
os.Exit(1)
}
brokerCertPool := x509.NewCertPool()
brokerCertPool.AppendCertsFromPEM(brokerCert)
tlsConfig := &tls.Config{
RootCAs: brokerCertPool,
InsecureSkipVerify: true,
GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &userKey, nil
},
}
tlsConn, err := tls.Dial("tcp", "localhost:5671", tlsConfig)
if err != nil {
fmt.Printf("Failed to open TLS connection: %s", err)
os.Exit(1)
}
container := electron.NewContainer(fmt.Sprintf("myContainer"))
conn, err := container.Connection(tlsConn, electron.SASLAllowedMechs("EXTERNAL"))
if err != nil {
fmt.Printf("Failed to open AMQP connection: %s", err)
os.Exit(1)
}
sess, err := conn.Session(electron.IncomingCapacity(1024000), electron.OutgoingWindow(1024))
if err != nil {
fmt.Printf("Something went wrong: %s", err)
os.Exit(1)
}
recv, err := sess.Receiver(electron.Source("myQueue"), electron.Capacity(100), electron.Prefetch(true))
if err != nil {
fmt.Printf("Something went wrong: %s", err)
os.Exit(1)
}
//msg, err := recv.ReceiveTimeout(time.Duration(10))
msg, err := recv.Receive()
if err == nil {
fmt.Printf("Message received: %s", msg.Message.Body())
msg.Accept()
} else if err == electron.Timeout {
fmt.Printf("No message received")
} else {
fmt.Printf("Something went wrong: %s", err)
os.Exit(1)
}
conn.Close(nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment