Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/jetstream-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func run() error {
cert := flag.String("tlscert", "", "NATS TLS public certificate")
key := flag.String("tlskey", "", "NATS TLS private key")
ca := flag.String("tlsca", "", "NATS TLS certificate authority chain")
tokenFile := flag.String("token-file", "", "File to read an auth_token from")
tlsfirst := flag.Bool("tlsfirst", false, "If enabled, forces explicit TLS without waiting for Server INFO")
server := flag.String("s", "", "NATS Server URL")
crdConnect := flag.Bool("crd-connect", false, "If true, then NATS connections will be made from CRD config, not global config. Ignored if running with control loop, CRD options will always override global config")
Expand Down Expand Up @@ -111,6 +112,7 @@ func run() error {
Certificate: *cert,
Key: *key,
TLSFirst: *tlsfirst,
TokenFile: *tokenFile,
}

if *ca != "" {
Expand Down
26 changes: 24 additions & 2 deletions internal/controller/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"k8s.io/klog/v2"
)

type NatsConfig struct {
Expand All @@ -22,6 +23,7 @@ type NatsConfig struct {
Credentials string `json:"credential,omitempty"`
NKey string `json:"nkey,omitempty"`
Token string `json:"token,omitempty"`
TokenFile string `json:"token_file,omitempty"`
User string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
JsDomain string `json:"js_domain,omitempty"`
Expand Down Expand Up @@ -84,6 +86,14 @@ func (o *NatsConfig) Hash() (string, error) {
b = append(b, fb...)
}

if o.TokenFile != "" {
fb, err := os.ReadFile(o.TokenFile)
if err != nil {
return "", fmt.Errorf("error opening token file %s: %v", o.TokenFile, err)
}
b = append(b, fb...)
}

hash := sha256.New()
hash.Write(b)
return fmt.Sprintf("%x", hash.Sum(nil)), nil
Expand Down Expand Up @@ -125,6 +135,8 @@ func (o *NatsConfig) Overlay(overlay *NatsConfig) {
o.Credentials = overlay.Credentials
} else if overlay.NKey != "" {
o.NKey = overlay.NKey
} else if overlay.TokenFile != "" {
o.TokenFile = overlay.TokenFile
} else if overlay.Token != "" {
o.Token = overlay.Token
} else if overlay.User != "" && overlay.Password != "" {
Expand All @@ -134,7 +146,7 @@ func (o *NatsConfig) Overlay(overlay *NatsConfig) {
}

func (o *NatsConfig) HasAuth() bool {
return o.Credentials != "" || o.NKey != "" || o.Token != "" || (o.User != "" && o.Password != "")
return o.Credentials != "" || o.NKey != "" || o.Token != "" || (o.User != "" && o.Password != "" || o.TokenFile != "")
}

func (o *NatsConfig) UnsetAuth() {
Expand All @@ -143,6 +155,7 @@ func (o *NatsConfig) UnsetAuth() {
o.User = ""
o.Password = ""
o.Token = ""
o.TokenFile = ""
}

// buildOptions creates options from the config to be used in nats.Connect.
Expand Down Expand Up @@ -181,14 +194,23 @@ func (o *NatsConfig) buildOptions() ([]nats.Option, error) {
opts = append(opts, opt)
}

if o.Token != "" {
if o.TokenFile != "" {
klog.Infof("reading token from: %v", o.TokenFile)
token, err := os.ReadFile(o.TokenFile)
if err != nil {
return nil, fmt.Errorf("read token file %s: %w", o.TokenFile, err)
}
opts = append(opts, nats.Token(string(token)))
} else if o.Token != "" {
opts = append(opts, nats.Token(o.Token))
}

if o.User != "" && o.Password != "" {
opts = append(opts, nats.UserInfo(o.User, o.Password))
}

klog.Infof("NATS client options: %v", opts)

return opts, nil
}

Expand Down