Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.

Commit

Permalink
Added nkey seed file helper and updated examples
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Nov 2, 2018
1 parent 507e5b3 commit 9da7a9a
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 23 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -115,6 +115,24 @@ c.Subscribe("help", func(subj, reply string, msg string) {
c.Close();
```

## Nkey Authentication (server versions >= 2.0.0)
To authenticate with nkeys, the nkey seed should be in a read only file, e.g. seed.txt
```bash
> cat seed.txt
# This is my seed nkey!
SUAGMJH5XLGZKQQWAWKRZJIGMOU4HPFUYLXJMXOO5NLFEO2OOQJ5LPRDPM
```

This is a helper function which will load and decode and do the proper signing for the server nonce.
It will clear memory in between invocations.
You can choose to use the low level option and provide the public key and a signature callback on your own.

```go
opt, err := nats.NkeyOptionFromSeed("seed.txt")
nc, err := nats.Connect(serverUrl, opt)

```

## TLS

```go
Expand Down
2 changes: 2 additions & 0 deletions examples/nats-bench.go
Expand Up @@ -11,6 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// +build ignore

package main

import (
Expand Down
89 changes: 89 additions & 0 deletions examples/nats-echo.go
@@ -0,0 +1,89 @@
// Copyright 2018 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build ignore

package main

import (
"flag"
"log"
"runtime"

"github.com/nats-io/go-nats"
)

// NOTE: Can test with demo servers.
// nats-echo -s demo.nats.io <subject>
// nats-echo -s demo.nats.io:4443 <subject> (TLS version)

func usage() {
log.Fatalf("Usage: nats-echo [-s server] [-t] [-nkey seedfile] <subject>")
}

func printMsg(m *nats.Msg, i int) {
log.Printf("[#%d] Echoing %q", i, m.Data)
}

func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var nkeyFile = flag.String("nkey", "", "Use the nkey seed file for authentication")
var showTime = flag.Bool("t", false, "Display timestamps")

log.SetFlags(0)
flag.Usage = usage
flag.Parse()

args := flag.Args()
if len(args) != 1 {
usage()
}

// general options.
opts := []nats.Option{nats.Name("NATS Echo Service")}

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

nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}

subj, i := args[0], 0

nc.Subscribe(subj, func(msg *nats.Msg) {
i++
printMsg(msg, i)
// Just echo back what they sent us.
nc.Publish(msg.Reply, msg.Data)
})
nc.Flush()

if err := nc.LastError(); err != nil {
log.Fatal(err)
}

log.Printf("Echo Service listening on [%s]\n", subj)
if *showTime {
log.SetFlags(log.LstdFlags)
}

runtime.Goexit()
}
24 changes: 20 additions & 4 deletions examples/nats-pub.go
Expand Up @@ -22,24 +22,40 @@ import (
"github.com/nats-io/go-nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-pub -s tls://demo.nats.io:4443 foo hello
// NOTE: Can test with demo servers.
// nats-pub -s demo.nats.io <subject> <msg>
// nats-pub -s demo.nats.io:4443 <subject> <msg> (TLS version)

func usage() {
log.Fatalf("Usage: nats-pub [-s server (%s)] <subject> <msg> \n", nats.DefaultURL)
log.Fatalf("Usage: nats-pub [-s server] <subject> <msg>")
}

func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var nkeyFile = flag.String("nkey", "", "Use the nkey seed file for authentication")

log.SetFlags(0)
flag.Usage = usage
flag.Parse()

args := flag.Args()
if len(args) < 2 {
if len(args) != 2 {
usage()
}

nc, err := nats.Connect(*urls)
// general options.
opts := []nats.Option{nats.Name("NATS Sample Publisher")}

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

nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatal(err)
}
Expand Down
24 changes: 20 additions & 4 deletions examples/nats-qsub.go
Expand Up @@ -24,9 +24,12 @@ import (
"github.com/nats-io/go-nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-qsub -s tls://demo.nats.io:4443 foo
// NOTE: Can test with demo servers.
// nats-qsub -s demo.nats.io <subject> <queue>
// nats-qsub -s demo.nats.io:4443 <subject> <queue> (TLS version)

func usage() {
log.Fatalf("Usage: nats-qsub [-s server] [-t] <subject> <queue-group>\n")
log.Fatalf("Usage: nats-qsub [-s server] [-t] <subject> <queue>")
}

func printMsg(m *nats.Msg, i int) {
Expand All @@ -35,18 +38,31 @@ func printMsg(m *nats.Msg, i int) {

func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var nkeyFile = flag.String("nkey", "", "Use the nkey seed file for authentication")
var showTime = flag.Bool("t", false, "Display timestamps")

log.SetFlags(0)
flag.Usage = usage
flag.Parse()

args := flag.Args()
if len(args) < 2 {
if len(args) != 2 {
usage()
}

nc, err := nats.Connect(*urls)
// general options.
opts := []nats.Option{nats.Name("NATS Sample Queue Subscriber")}

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

nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
Expand Down
24 changes: 20 additions & 4 deletions examples/nats-req.go
Expand Up @@ -23,13 +23,17 @@ import (
"github.com/nats-io/go-nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-req -s tls://demo.nats.io:4443 foo hello
// NOTE: Can test with demo servers.
// nats-req -s demo.nats.io <subject> <msg>
// nats-req -s demo.nats.io:4443 <subject> <msg> (TLS version)

func usage() {
log.Fatalf("Usage: nats-req [-s server (%s)] <subject> <msg> \n", nats.DefaultURL)
log.Fatalf("Usage: nats-req [-s server] <subject> <msg>")
}

func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var nkeyFile = flag.String("nkey", "", "Use the nkey seed file for authentication")

log.SetFlags(0)
flag.Usage = usage
Expand All @@ -40,14 +44,26 @@ func main() {
usage()
}

nc, err := nats.Connect(*urls)
// general options.
opts := []nats.Option{nats.Name("NATS Sample Requestor")}

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

nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
defer nc.Close()
subj, payload := args[0], []byte(args[1])

msg, err := nc.Request(subj, []byte(payload), 500*time.Millisecond)
msg, err := nc.Request(subj, []byte(payload), time.Second)
if err != nil {
if nc.LastError() != nil {
log.Fatalf("Error in Request: %v\n", nc.LastError())
Expand Down
22 changes: 19 additions & 3 deletions examples/nats-rply.go
Expand Up @@ -23,9 +23,12 @@ import (
"github.com/nats-io/go-nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-rply -s tls://demo.nats.io:4443 foo hello
// NOTE: Can test with demo servers.
// nats-rply -s demo.nats.io <subject> <response>
// nats-rply -s demo.nats.io:4443 <subject> <response> (TLS version)

func usage() {
log.Fatalf("Usage: nats-rply [-s server][-t] <subject> <response>\n")
log.Fatalf("Usage: nats-rply [-s server] [-t] <subject> <response>")
}

func printMsg(m *nats.Msg, i int) {
Expand All @@ -34,6 +37,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 nkeyFile = flag.String("nkey", "", "Use the nkey seed file for authentication")
var showTime = flag.Bool("t", false, "Display timestamps")

log.SetFlags(0)
Expand All @@ -45,7 +49,19 @@ func main() {
usage()
}

nc, err := nats.Connect(*urls)
// general options.
opts := []nats.Option{nats.Name("NATS Sample Responder")}

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

nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
Expand Down
24 changes: 20 additions & 4 deletions examples/nats-sub.go
Expand Up @@ -23,9 +23,12 @@ import (
"github.com/nats-io/go-nats"
)

// NOTE: Use tls scheme for TLS, e.g. nats-sub -s tls://demo.nats.io:4443 foo
// NOTE: Can test with demo servers.
// nats-sub -s demo.nats.io <subject>
// nats-sub -s demo.nats.io:4443 <subject> (TLS version)

func usage() {
log.Fatalf("Usage: nats-sub [-s server] [-t] <subject> \n")
log.Fatalf("Usage: nats-sub [-s server] [-t] <subject>")
}

func printMsg(m *nats.Msg, i int) {
Expand All @@ -34,18 +37,31 @@ func printMsg(m *nats.Msg, i int) {

func main() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var nkeyFile = flag.String("nkey", "", "Use the nkey seed file for authentication")
var showTime = flag.Bool("t", false, "Display timestamps")

log.SetFlags(0)
flag.Usage = usage
flag.Parse()

args := flag.Args()
if len(args) < 1 {
if len(args) != 1 {
usage()
}

nc, err := nats.Connect(*urls)
// general options.
opts := []nats.Option{nats.Name("NATS Sample Subscriber")}

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

nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}
Expand Down

0 comments on commit 9da7a9a

Please sign in to comment.