Skip to content

Commit

Permalink
Fix config token config detection (#2495)
Browse files Browse the repository at this point in the history
* Fix config token config detection

* Add unit tests
  • Loading branch information
michel-laterman authored Apr 13, 2023
1 parent 1d0c181 commit e8fe231
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/pkg/agent/configuration/fleet_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func ElasticsearchFromConnStr(conn string, serviceToken, serviceTokenPath string
VerificationMode: tlscommon.VerifyNone,
}
}
if serviceToken == "" {
if serviceToken == "" && serviceTokenPath == "" {
return Elasticsearch{}, errors.New("invalid connection string: must include a service token")
}
cfg.ServiceToken = serviceToken
Expand Down
69 changes: 69 additions & 0 deletions internal/pkg/agent/configuration/fleet_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package configuration

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
)

func TestElasticsearchFromConnStr(t *testing.T) {
testcases := []struct {
name string
conn string
token string
path string
insecure bool
es Elasticsearch
err error
}{{
name: "ok",
conn: "https://localhost:9200",
token: "my-token",
path: "",
insecure: false,
es: Elasticsearch{
Protocol: "https",
Hosts: []string{"localhost:9200"},
ServiceToken: "my-token",
},
err: nil,
}, {
name: "ok with path",
conn: "https://localhost:9200",
token: "",
path: "/path/to/token",
insecure: false,
es: Elasticsearch{
Protocol: "https",
Hosts: []string{"localhost:9200"},
ServiceTokenPath: "/path/to/token",
},
err: nil,
}, {
name: "no token or path",
conn: "https://localhost:9200",
token: "",
path: "",
insecure: false,
es: Elasticsearch{},
err: errors.New("invalid connection string: must include a service token"),
}}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
es, err := ElasticsearchFromConnStr(tc.conn, tc.token, tc.path, tc.insecure)
if tc.err != nil {
assert.EqualError(t, err, tc.err.Error())
} else {
assert.NoError(t, err)
}
assert.Equal(t, tc.es, es)
})
}
}

0 comments on commit e8fe231

Please sign in to comment.