Skip to content

Commit

Permalink
Merge pull request #728 from nats-io/examples-nkey-auth-2
Browse files Browse the repository at this point in the history
[ADDED] `--nkey` flag to enable nkey authentication using go examples
  • Loading branch information
kozlovic committed May 4, 2021
2 parents 6346f38 + 8dd5ec6 commit 6712740
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 13 deletions.
18 changes: 16 additions & 2 deletions examples/nats-bench/main.go
@@ -1,4 +1,4 @@
// Copyright 2015-2019 The NATS Authors
// Copyright 2015-2021 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -35,7 +35,7 @@ const (
)

func usage() {
log.Printf("Usage: nats-bench [-s server (%s)] [--tls] [-np NUM_PUBLISHERS] [-ns NUM_SUBSCRIBERS] [-n NUM_MSGS] [-ms MESSAGE_SIZE] [-csv csvfile] <subject>\n", nats.DefaultURL)
log.Printf("Usage: nats-bench [-s server (%s)] [--tls] [-np NUM_PUBLISHERS] [-ns NUM_SUBSCRIBERS] [-n NUM_MSGS] [-ms MESSAGE_SIZE] [-csv csvfile] [-creds file] [-nkey file] <subject>\n", nats.DefaultURL)
flag.PrintDefaults()
}

Expand All @@ -55,6 +55,7 @@ func main() {
var msgSize = flag.Int("ms", DefaultMessageSize, "Size of the message.")
var csvFile = flag.String("csv", "", "Save bench data to csv file")
var userCreds = flag.String("creds", "", "User Credentials File")
var nkeyFile = flag.String("nkey", "", "NKey Seed File")
var showHelp = flag.Bool("h", false, "Show help message")

log.SetFlags(0)
Expand All @@ -77,11 +78,24 @@ func main() {
// Connect Options.
opts := []nats.Option{nats.Name("NATS Benchmark")}

if *userCreds != "" && *nkeyFile != "" {
log.Fatal("specify -seed or -creds")
}

// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
}

// Use Nkey authentication.
if *nkeyFile != "" {
opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
if err != nil {
log.Fatal(err)
}
opts = append(opts, opt)
}

// Use TLS specified
if *tls {
opts = append(opts, nats.Secure(nil))
Expand Down
16 changes: 15 additions & 1 deletion examples/nats-echo/main.go
@@ -1,4 +1,4 @@
// Copyright 2018-2019 The NATS Authors
// Copyright 2018-2021 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -50,6 +50,7 @@ func printMsg(m *nats.Msg, i int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var userCreds = flag.String("creds", "", "User Credentials File")
var nkeyFile = flag.String("nkey", "", "NKey Seed File")
var showTime = flag.Bool("t", false, "Display timestamps")
var showHelp = flag.Bool("h", false, "Show help message")
var geoloc = flag.Bool("geo", false, "Display geo location of echo service")
Expand All @@ -76,11 +77,24 @@ func main() {
opts := []nats.Option{nats.Name("NATS Echo Service")}
opts = setupConnOptions(opts)

if *userCreds != "" && *nkeyFile != "" {
log.Fatal("specify -seed or -creds")
}

// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
}

// Use Nkey authentication.
if *nkeyFile != "" {
opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
if err != nil {
log.Fatal(err)
}
opts = append(opts, opt)
}

// Connect to NATS
nc, err := nats.Connect(*urls, opts...)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions examples/nats-pub/main.go
@@ -1,4 +1,4 @@
// Copyright 2012-2020 The NATS Authors
// Copyright 2012-2021 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -26,7 +26,7 @@ import (
// nats-pub -s demo.nats.io:4443 <subject> <msg> (TLS version)

func usage() {
log.Printf("Usage: nats-pub [-s server] [-creds file] [-tlscert file] [-tlskey file] [-tlscacert file] <subject> <msg>\n")
log.Printf("Usage: nats-pub [-s server] [-creds file] [-nkey file] [-tlscert file] [-tlskey file] [-tlscacert file] <subject> <msg>\n")
flag.PrintDefaults()
}

Expand All @@ -38,6 +38,7 @@ func showUsageAndExit(exitcode int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var userCreds = flag.String("creds", "", "User Credentials File")
var nkeyFile = flag.String("nkey", "", "NKey Seed File")
var tlsClientCert = flag.String("tlscert", "", "TLS client certificate file")
var tlsClientKey = flag.String("tlskey", "", "Private key file for client certificate")
var tlsCACert = flag.String("tlscacert", "", "CA certificate to verify peer against")
Expand All @@ -60,6 +61,10 @@ func main() {
// Connect Options.
opts := []nats.Option{nats.Name("NATS Sample Publisher")}

if *userCreds != "" && *nkeyFile != "" {
log.Fatal("specify -seed or -creds")
}

// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
Expand All @@ -75,6 +80,15 @@ func main() {
opts = append(opts, nats.RootCAs(*tlsCACert))
}

// Use Nkey authentication.
if *nkeyFile != "" {
opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
if err != nil {
log.Fatal(err)
}
opts = append(opts, opt)
}

// Connect to NATS
nc, err := nats.Connect(*urls, opts...)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions examples/nats-qsub/main.go
@@ -1,4 +1,4 @@
// Copyright 2012-2019 The NATS Authors
// Copyright 2012-2021 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -28,7 +28,7 @@ import (
// nats-qsub -s demo.nats.io:4443 <subject> <queue> (TLS version)

func usage() {
log.Printf("Usage: nats-qsub [-s server] [-creds file] [-t] <subject> <queue>\n")
log.Printf("Usage: nats-qsub [-s server] [-creds file] [-nkey file] [-t] <subject> <queue>\n")
flag.PrintDefaults()
}

Expand All @@ -44,6 +44,7 @@ func printMsg(m *nats.Msg, i int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var userCreds = flag.String("creds", "", "User Credentials File")
var nkeyFile = flag.String("nkey", "", "NKey Seed File")
var showTime = flag.Bool("t", false, "Display timestamps")
var showHelp = flag.Bool("h", false, "Show help message")

Expand All @@ -64,11 +65,24 @@ func main() {
opts := []nats.Option{nats.Name("NATS Sample Queue Subscriber")}
opts = setupConnOptions(opts)

if *userCreds != "" && *nkeyFile != "" {
log.Fatal("specify -seed or -creds")
}

// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
}

// Use Nkey authentication.
if *nkeyFile != "" {
opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
if err != nil {
log.Fatal(err)
}
opts = append(opts, opt)
}

// Connect to NATS
nc, err := nats.Connect(*urls, opts...)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions examples/nats-req/main.go
@@ -1,4 +1,4 @@
// Copyright 2012-2019 The NATS Authors
// Copyright 2012-2021 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -27,7 +27,7 @@ import (
// nats-req -s demo.nats.io:4443 <subject> <msg> (TLS version)

func usage() {
log.Printf("Usage: nats-req [-s server] [-creds file] <subject> <msg>\n")
log.Printf("Usage: nats-req [-s server] [-creds file] [-nkey file] <subject> <msg>\n")
flag.PrintDefaults()
}

Expand All @@ -39,6 +39,7 @@ func showUsageAndExit(exitcode int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var userCreds = flag.String("creds", "", "User Credentials File")
var nkeyFile = flag.String("nkey", "", "NKey Seed File")
var showHelp = flag.Bool("h", false, "Show help message")

log.SetFlags(0)
Expand All @@ -57,11 +58,24 @@ func main() {
// Connect Options.
opts := []nats.Option{nats.Name("NATS Sample Requestor")}

if *userCreds != "" && *nkeyFile != "" {
log.Fatal("specify -seed or -creds")
}

// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
}

// Use Nkey authentication.
if *nkeyFile != "" {
opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
if err != nil {
log.Fatal(err)
}
opts = append(opts, opt)
}

// Connect to NATS
nc, err := nats.Connect(*urls, opts...)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions examples/nats-rply/main.go
@@ -1,4 +1,4 @@
// Copyright 2012-2019 The NATS Authors
// Copyright 2012-2021 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -28,7 +28,7 @@ import (
// nats-rply -s demo.nats.io:4443 <subject> <response> (TLS version)

func usage() {
log.Printf("Usage: nats-rply [-s server] [-creds file] [-t] [-q queue] <subject> <response>\n")
log.Printf("Usage: nats-rply [-s server] [-creds file] [-nkey file] [-t] [-q queue] <subject> <response>\n")
flag.PrintDefaults()
}

Expand All @@ -44,6 +44,7 @@ func printMsg(m *nats.Msg, i int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var userCreds = flag.String("creds", "", "User Credentials File")
var nkeyFile = flag.String("nkey", "", "NKey Seed File")
var showTime = flag.Bool("t", false, "Display timestamps")
var queueName = flag.String("q", "NATS-RPLY-22", "Queue Group Name")
var showHelp = flag.Bool("h", false, "Show help message")
Expand All @@ -65,11 +66,24 @@ func main() {
opts := []nats.Option{nats.Name("NATS Sample Responder")}
opts = setupConnOptions(opts)

if *userCreds != "" && *nkeyFile != "" {
log.Fatal("specify -seed or -creds")
}

// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
}

// Use Nkey authentication.
if *nkeyFile != "" {
opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
if err != nil {
log.Fatal(err)
}
opts = append(opts, opt)
}

// Connect to NATS
nc, err := nats.Connect(*urls, opts...)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions examples/nats-sub/main.go
@@ -1,4 +1,4 @@
// Copyright 2012-2020 The NATS Authors
// Copyright 2012-2021 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -28,7 +28,7 @@ import (
// nats-sub -s demo.nats.io:4443 <subject> (TLS version)

func usage() {
log.Printf("Usage: nats-sub [-s server] [-creds file] [-tlscert file] [-tlskey file] [-tlscacert file] [-t] <subject>\n")
log.Printf("Usage: nats-sub [-s server] [-creds file] [-nkey file] [-tlscert file] [-tlskey file] [-tlscacert file] [-t] <subject>\n")
flag.PrintDefaults()
}

Expand All @@ -44,6 +44,7 @@ func printMsg(m *nats.Msg, i int) {
func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var userCreds = flag.String("creds", "", "User Credentials File")
var nkeyFile = flag.String("nkey", "", "NKey Seed File")
var tlsClientCert = flag.String("tlscert", "", "TLS client certificate file")
var tlsClientKey = flag.String("tlskey", "", "Private key file for client certificate")
var tlsCACert = flag.String("tlscacert", "", "CA certificate to verify peer against")
Expand All @@ -67,6 +68,10 @@ func main() {
opts := []nats.Option{nats.Name("NATS Sample Subscriber")}
opts = setupConnOptions(opts)

if *userCreds != "" && *nkeyFile != "" {
log.Fatal("specify -seed or -creds")
}

// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
Expand All @@ -82,6 +87,15 @@ func main() {
opts = append(opts, nats.RootCAs(*tlsCACert))
}

// Use Nkey authentication.
if *nkeyFile != "" {
opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
if err != nil {
log.Fatal(err)
}
opts = append(opts, opt)
}

// Connect to NATS
nc, err := nats.Connect(*urls, opts...)
if err != nil {
Expand Down

0 comments on commit 6712740

Please sign in to comment.