|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "flag" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/Shopify/sarama" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + sarama.Logger = log.New(os.Stdout, "[Sarama] ", log.LstdFlags) |
| 17 | +} |
| 18 | + |
| 19 | +var ( |
| 20 | + brokers = flag.String("brokers", os.Getenv("KAFKA_PEERS"), "The Kafka brokers to connect to, as a comma separated list") |
| 21 | + userName = flag.String("username", "", "The SASL username") |
| 22 | + passwd = flag.String("passwd", "", "The SASL password") |
| 23 | + algorithm = flag.String("algorithm", "", "The SASL SCRAM SHA algorithm sha256 or sha512 as mechanism") |
| 24 | + topic = flag.String("topic", "default_topic", "The Kafka topic to use") |
| 25 | + certFile = flag.String("certificate", "", "The optional certificate file for client authentication") |
| 26 | + keyFile = flag.String("key", "", "The optional key file for client authentication") |
| 27 | + caFile = flag.String("ca", "", "The optional certificate authority file for TLS client authentication") |
| 28 | + verifySSL = flag.Bool("verify", false, "Optional verify ssl certificates chain") |
| 29 | + useTLS = flag.Bool("tls", false, "Use TLS to communicate with the cluster") |
| 30 | + |
| 31 | + logger = log.New(os.Stdout, "[Producer] ", log.LstdFlags) |
| 32 | +) |
| 33 | + |
| 34 | +func createTLSConfiguration() (t *tls.Config) { |
| 35 | + t = &tls.Config{ |
| 36 | + InsecureSkipVerify: *verifySSL, |
| 37 | + } |
| 38 | + if *certFile != "" && *keyFile != "" && *caFile != "" { |
| 39 | + cert, err := tls.LoadX509KeyPair(*certFile, *keyFile) |
| 40 | + if err != nil { |
| 41 | + log.Fatal(err) |
| 42 | + } |
| 43 | + |
| 44 | + caCert, err := ioutil.ReadFile(*caFile) |
| 45 | + if err != nil { |
| 46 | + log.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + caCertPool := x509.NewCertPool() |
| 50 | + caCertPool.AppendCertsFromPEM(caCert) |
| 51 | + |
| 52 | + t = &tls.Config{ |
| 53 | + Certificates: []tls.Certificate{cert}, |
| 54 | + RootCAs: caCertPool, |
| 55 | + InsecureSkipVerify: *verifySSL, |
| 56 | + } |
| 57 | + } |
| 58 | + return t |
| 59 | +} |
| 60 | + |
| 61 | +func main() { |
| 62 | + flag.Parse() |
| 63 | + |
| 64 | + if *brokers == "" { |
| 65 | + log.Fatalln("at least one brocker is required") |
| 66 | + } |
| 67 | + |
| 68 | + if *userName == "" { |
| 69 | + log.Fatalln("SASL username is required") |
| 70 | + } |
| 71 | + |
| 72 | + if *passwd == "" { |
| 73 | + log.Fatalln("SASL password is required") |
| 74 | + } |
| 75 | + |
| 76 | + conf := sarama.NewConfig() |
| 77 | + conf.Producer.Retry.Max = 1 |
| 78 | + conf.Producer.RequiredAcks = sarama.WaitForAll |
| 79 | + conf.Producer.Return.Successes = true |
| 80 | + conf.Metadata.Full = true |
| 81 | + conf.Version = sarama.V0_10_0_0 |
| 82 | + conf.ClientID = "sasl_scram_client" |
| 83 | + conf.Metadata.Full = true |
| 84 | + conf.Net.SASL.Enable = true |
| 85 | + conf.Net.SASL.User = *userName |
| 86 | + conf.Net.SASL.Password = *passwd |
| 87 | + conf.Net.SASL.Handshake = true |
| 88 | + if *algorithm == "sha512" { |
| 89 | + conf.Net.SASL.SCRAMClient = &XDGSCRAMClient{HashGeneratorFcn: SHA512} |
| 90 | + conf.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA512) |
| 91 | + } else if *algorithm == "sha256" { |
| 92 | + conf.Net.SASL.SCRAMClient = &XDGSCRAMClient{HashGeneratorFcn: SHA256} |
| 93 | + conf.Net.SASL.Mechanism = sarama.SASLMechanism(sarama.SASLTypeSCRAMSHA256) |
| 94 | + |
| 95 | + } else { |
| 96 | + log.Fatalf("invalid SHA algorithm \"%s\": can be either \"sha256\" or \"sha512\"", *algorithm) |
| 97 | + } |
| 98 | + |
| 99 | + if *useTLS { |
| 100 | + conf.Net.TLS.Enable = true |
| 101 | + conf.Net.TLS.Config = createTLSConfiguration() |
| 102 | + } |
| 103 | + |
| 104 | + syncProcuder, err := sarama.NewSyncProducer(strings.Split(*brokers, ","), conf) |
| 105 | + if err != nil { |
| 106 | + logger.Fatalln("failed to create producer: ", err) |
| 107 | + } |
| 108 | + partition, offset, err := syncProcuder.SendMessage(&sarama.ProducerMessage{ |
| 109 | + Topic: *topic, |
| 110 | + Value: sarama.StringEncoder("test_message"), |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + logger.Fatalln("failed to send message to ", *topic, err) |
| 114 | + } |
| 115 | + logger.Printf("wrote message at partition: %d, offset: %d", partition, offset) |
| 116 | + syncProcuder.Close() |
| 117 | + logger.Println("Bye now !") |
| 118 | +} |
0 commit comments