Skip to content

Commit

Permalink
Merge pull request #941 from nats-io/david-gurley-natscredentials
Browse files Browse the repository at this point in the history
[ADDED] Credentials option to connect to external NATS Server
  • Loading branch information
kozlovic committed Sep 11, 2019
2 parents a4c3624 + 86de3a0 commit 12f7398
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions server/conf.go
Expand Up @@ -95,6 +95,11 @@ func ProcessConfigFile(configFile string, opts *Options) error {
return err
}
opts.NATSServerURL = v.(string)
case "credentials":
if err := checkType(k, reflect.String, v); err != nil {
return err
}
opts.NATSCredentials = v.(string)
case "secure":
if err := checkType(k, reflect.Bool, v); err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions server/conf_test.go
Expand Up @@ -75,6 +75,9 @@ func TestParseConfig(t *testing.T) {
if opts.ClientCA != "/path/to/client/ca_file" {
t.Fatalf("Expected ClientCA to be %q, got %q", "/path/to/client/ca_file", opts.ClientCA)
}
if opts.NATSCredentials != "credentials.creds" {
t.Fatalf("Expected Credentials to be %q, got %q", "credentials.creds", opts.NATSCredentials)
}
if !opts.FileStoreOpts.CompactEnabled {
t.Fatalf("Expected CompactEnabled to be true, got false")
}
Expand Down Expand Up @@ -477,6 +480,7 @@ func TestParseWrongTypes(t *testing.T) {
expectFailureFor(t, "encrypt: 123", wrongTypeErr)
expectFailureFor(t, "encryption_cipher: 123", wrongTypeErr)
expectFailureFor(t, "encryption_key: 123", wrongTypeErr)
expectFailureFor(t, "credentials: 123", wrongTypeErr)
}

func expectFailureFor(t *testing.T, content, errorMatch string) {
Expand Down
6 changes: 5 additions & 1 deletion server/server.go
Expand Up @@ -1279,6 +1279,7 @@ type Options struct {
IOBatchSize int // Maximum number of messages collected from clients before starting their processing.
IOSleepTime int64 // Duration (in micro-seconds) the server waits for more message to fill up a batch.
NATSServerURL string // URL for external NATS Server to connect to. If empty, NATS Server is embedded.
NATSCredentials string // Credentials file for connecting to external NATS Server.
ClientHBInterval time.Duration // Interval at which server sends heartbeat to a client.
ClientHBTimeout time.Duration // How long server waits for a heartbeat response.
ClientHBFailCount int // Number of failed heartbeats before server closes client connection.
Expand Down Expand Up @@ -1315,7 +1316,6 @@ var defaultOptions = Options{
FileStoreOpts: stores.DefaultFileStoreOptions,
IOBatchSize: DefaultIOBatchSize,
IOSleepTime: DefaultIOSleepTime,
NATSServerURL: "",
ClientHBInterval: DefaultHeartBeatInterval,
ClientHBTimeout: DefaultClientHBTimeout,
ClientHBFailCount: DefaultMaxFailedHeartBeats,
Expand Down Expand Up @@ -1428,6 +1428,10 @@ func (s *StanServer) createNatsClientConn(name string) (*nats.Conn, error) {
var err error
ncOpts := nats.DefaultOptions

if s.opts.NATSCredentials != "" {
nats.UserCredentials(s.opts.NATSCredentials)(&ncOpts)
}

for _, o := range s.opts.NATSClientOpts {
o(&ncOpts)
}
Expand Down
1 change: 1 addition & 0 deletions test/configs/test_parse.conf
Expand Up @@ -16,6 +16,7 @@ streaming: {
encrypt: true
encryption_cipher: "AES"
encryption_key: "key"
credentials: "credentials.creds"

store_limits: {
max_channels: 11
Expand Down

0 comments on commit 12f7398

Please sign in to comment.