Skip to content

Commit

Permalink
Change the license to use the new endpoint and format (#15091)
Browse files Browse the repository at this point in the history
* Change the license to use the new endpoint and format

1. Use the `_license` endpoint instead of using the `/_xpack`
2. Remove the `features` handling.
3. Add more golden files for the different returned format.
4. remove unused files and code
  • Loading branch information
ph authored Dec 17, 2019
1 parent 567f94a commit 556e8ff
Show file tree
Hide file tree
Showing 22 changed files with 225 additions and 1,015 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Remove version information from default ILM policy for improved upgrade experience on custom policies. {pull}14745[14745]
- Running `setup` cmd respects `setup.ilm.overwrite` setting for improved support of custom policies. {pull}14741[14741]
- Libbeat: Do not overwrite agent.*, ecs.version, and host.name. {pull}14407[14407]
- Libbeat: Cleanup the x-pack licenser code to use the new license endpoint and the new format. {pull}15091[15091]

*Auditbeat*

Expand Down
29 changes: 0 additions & 29 deletions x-pack/libbeat/licenser/callback_watcher.go

This file was deleted.

30 changes: 0 additions & 30 deletions x-pack/libbeat/licenser/callback_watcher_test.go

This file was deleted.

12 changes: 6 additions & 6 deletions x-pack/libbeat/licenser/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ func testCheckTrial(t *testing.T) {

t.Run("valid trial license", func(t *testing.T) {
l := License{
Mode: Trial,
Type: Trial,
TrialExpiry: expiryTime(time.Now().Add(1 * time.Hour)),
}
assert.True(t, CheckTrial(log, l))
})

t.Run("expired trial license", func(t *testing.T) {
l := License{
Mode: Trial,
Type: Trial,
TrialExpiry: expiryTime(time.Now().Add(-1 * time.Hour)),
}
assert.False(t, CheckTrial(log, l))
})

t.Run("other license", func(t *testing.T) {
l := License{Mode: Basic}
l := License{Type: Basic}
assert.False(t, CheckTrial(log, l))
})
}
Expand All @@ -51,19 +51,19 @@ func testCheckLicenseCover(t *testing.T) {
fn := CheckLicenseCover(license)

t.Run("active", func(t *testing.T) {
l := License{Mode: license, Status: Active}
l := License{Type: license, Status: Active}
assert.True(t, fn(log, l))
})

t.Run("inactive", func(t *testing.T) {
l := License{Mode: license, Status: Inactive}
l := License{Type: license, Status: Inactive}
assert.False(t, fn(log, l))
})
}
}

func testValidate(t *testing.T) {
l := License{Mode: Basic, Status: Active}
l := License{Type: Basic, Status: Active}
t.Run("when one of the check is valid", func(t *testing.T) {
valid := Validate(logp.NewLogger(""), l, CheckLicenseCover(Platinum), CheckLicenseCover(Basic))
assert.True(t, valid)
Expand Down
24 changes: 2 additions & 22 deletions x-pack/libbeat/licenser/elastic_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (

"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs/elasticsearch"
)

const xPackURL = "/_xpack"
const xPackURL = "/_license"

// params defaults query parameters to send to the '_xpack' endpoint by default we only need
// machine parseable data.
Expand Down Expand Up @@ -129,8 +128,7 @@ func (f *ElasticFetcher) Fetch() (*License, error) {

// Xpack Response, temporary struct to merge the features into the license struct.
type xpackResponse struct {
License License `json:"license"`
Features features `json:"features"`
License License `json:"license"`
}

func (f *ElasticFetcher) parseJSON(b []byte) (*License, error) {
Expand All @@ -141,7 +139,6 @@ func (f *ElasticFetcher) parseJSON(b []byte) (*License, error) {
}

license := info.License
license.Features = info.Features

return &license, nil
}
Expand Down Expand Up @@ -193,20 +190,3 @@ func newESClientMux(clients []elasticsearch.Client) *esClientMux {

return &esClientMux{idx: idx, clients: tmp}
}

// Create takes a raw configuration and will create a a license manager based on the elasticsearch
// output configuration, if no output is found we return an error.
func Create(cfg *common.ConfigNamespace, refreshDelay, graceDelay time.Duration) (*Manager, error) {
if !cfg.IsSet() || cfg.Name() != "elasticsearch" {
return nil, ErrNoElasticsearchConfig
}

clients, err := elasticsearch.NewElasticsearchClients(cfg.Config())
if err != nil {
return nil, err
}
clientsMux := newESClientMux(clients)

manager := New(clientsMux, refreshDelay, graceDelay)
return manager, nil
}
8 changes: 0 additions & 8 deletions x-pack/libbeat/licenser/elastic_fetcher_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,4 @@ func TestElasticsearch(t *testing.T) {
assert.Equal(t, Active, license.Status)

assert.NotEmpty(t, license.UUID)

assert.NotNil(t, license.Features.Graph)
assert.NotNil(t, license.Features.Logstash)
assert.NotNil(t, license.Features.ML)
assert.NotNil(t, license.Features.Monitoring)
assert.NotNil(t, license.Features.Rollup)
assert.NotNil(t, license.Features.Security)
assert.NotNil(t, license.Features.Watcher)
}
24 changes: 1 addition & 23 deletions x-pack/libbeat/licenser/elastic_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

func newServerClientPair(t *testing.T, handler http.HandlerFunc) (*httptest.Server, *elasticsearch.Client) {
mux := http.NewServeMux()
mux.Handle("/_xpack/", http.HandlerFunc(handler))
mux.Handle("/_license/", http.HandlerFunc(handler))

server := httptest.NewServer(mux)

Expand Down Expand Up @@ -135,29 +135,7 @@ func TestParseJSON(t *testing.T) {
assert.True(t, len(license.UUID) > 0)

assert.NotNil(t, license.Type)
assert.NotNil(t, license.Mode)
assert.NotNil(t, license.Status)

assert.False(t, license.Features.Graph.Available)
assert.True(t, license.Features.Graph.Enabled)

assert.False(t, license.Features.Logstash.Available)
assert.True(t, license.Features.Logstash.Enabled)

assert.False(t, license.Features.ML.Available)
assert.True(t, license.Features.ML.Enabled)

assert.True(t, license.Features.Monitoring.Available)
assert.True(t, license.Features.Monitoring.Enabled)

assert.True(t, license.Features.Rollup.Available)
assert.True(t, license.Features.Rollup.Enabled)

assert.False(t, license.Features.Security.Available)
assert.True(t, license.Features.Security.Enabled)

assert.False(t, license.Features.Watcher.Available)
assert.True(t, license.Features.Watcher.Enabled)
})

return nil
Expand Down
61 changes: 20 additions & 41 deletions x-pack/libbeat/licenser/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,37 @@ import (
//
// The x-pack endpoint returns the following JSON response.
//
// "license": {
// "uid": "936183d8-f48c-4a3f-959a-a52aa2563279",
// "type": "platinum",
// "mode": "platinum",
// "status": "active"
// },
//
//{
// "license" : {
// "status" : "active",
// "uid" : "cbff45e7-c553-41f7-ae4f-9205eabd80xx",
// "type" : "trial",
// "issue_date" : "2018-10-20T22:05:12.332Z",
// "issue_date_in_millis" : 1540073112332,
// "expiry_date" : "2018-11-19T22:05:12.332Z",
// "expiry_date_in_millis" : 1542665112332,
// "max_nodes" : 1000,
// "issued_to" : "test",
// "issuer" : "elasticsearch",
// "start_date_in_millis" : -1
// }
// }
// Definition:
// type is the installed license.
// mode is the license in operation. (effective license)
// status is the type installed is active or not.
type License struct {
UUID string `json:"uid"`
Type LicenseType `json:"type"`
Mode LicenseType `json:"mode"`
Status State `json:"status"`
Features features `json:"features"`
TrialExpiry expiryTime `json:"expiry_date_in_millis,omitempty"`
}

// Features defines the list of features exposed by the elasticsearch cluster.
type features struct {
Graph graph `json:"graph"`
Logstash logstash `json:"logstash"`
ML ml `json:"ml"`
Monitoring monitoring `json:"monitoring"`
Rollup rollup `json:"rollup"`
Security security `json:"security"`
Watcher watcher `json:"watcher"`
}

type expiryTime time.Time

// Base define the field common for every feature.
type Base struct {
Enabled bool `json:"enabled"`
Available bool `json:"available"`
}

// Defines all the available features
type graph struct{ *Base }
type logstash struct{ *Base }
type ml struct{ *Base }
type monitoring struct{ *Base }
type rollup struct{ *Base }
type security struct{ *Base }
type watcher struct{ *Base }

// Get return the current license
// Get returns the license type.
func (l *License) Get() LicenseType {
return l.Mode
return l.Type
}

// Cover returns true if the provided license is included in the range of license.
Expand All @@ -72,15 +52,15 @@ func (l *License) Get() LicenseType {
// gold -> match gold and platinum
// platinum -> match platinum only
func (l *License) Cover(license LicenseType) bool {
if l.Mode >= license {
if l.Type >= license {
return true
}
return false
}

// Is returns true if the provided license is an exact match.
func (l *License) Is(license LicenseType) bool {
return l.Mode == license
return l.Type == license
}

// IsActive returns true if the current license from the server is active.
Expand All @@ -90,7 +70,7 @@ func (l *License) IsActive() bool {

// IsTrial returns true if the remote cluster is in trial mode.
func (l *License) IsTrial() bool {
return l.Mode == Trial
return l.Type == Trial
}

// IsTrialExpired returns false if the we are not in trial mode and when we are in trial mode
Expand All @@ -112,6 +92,5 @@ func (l *License) IsTrialExpired() bool {
func (l *License) EqualTo(other *License) bool {
return l.UUID == other.UUID &&
l.Type == other.Type &&
l.Mode == other.Mode &&
l.Status == other.Status
}
16 changes: 8 additions & 8 deletions x-pack/libbeat/licenser/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestLicenseGet(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
l := License{Mode: test.t}
l := License{Type: test.t}
assert.Equal(t, test.t, l.Get())
})
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestLicenseIs(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
l := License{Mode: test.t}
l := License{Type: test.t}
assert.Equal(t, test.expected, l.Cover(test.query))
})
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestLicenseIsStrict(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
l := License{Mode: test.t}
l := License{Type: test.t}
assert.Equal(t, test.expected, l.Is(test.query))
})
}
Expand Down Expand Up @@ -149,12 +149,12 @@ func TestIsTrial(t *testing.T) {
}{
{
name: "is a trial license",
l: License{Mode: Trial},
l: License{Type: Trial},
expected: true,
},
{
name: "is not a trial license",
l: License{Mode: Basic},
l: License{Type: Basic},
expected: false,
},
}
Expand All @@ -174,17 +174,17 @@ func TestIsTrialExpired(t *testing.T) {
}{
{
name: "trial is expired",
l: License{Mode: Trial, TrialExpiry: expiryTime(time.Now().Add(-2 * time.Hour))},
l: License{Type: Trial, TrialExpiry: expiryTime(time.Now().Add(-2 * time.Hour))},
expected: true,
},
{
name: "trial is not expired",
l: License{Mode: Trial, TrialExpiry: expiryTime(time.Now().Add(2 * time.Minute))},
l: License{Type: Trial, TrialExpiry: expiryTime(time.Now().Add(2 * time.Minute))},
expected: false,
},
{
name: "license is not on trial",
l: License{Mode: Basic, TrialExpiry: expiryTime(time.Now().Add(2 * time.Minute))},
l: License{Type: Basic, TrialExpiry: expiryTime(time.Now().Add(2 * time.Minute))},
expected: false,
},
}
Expand Down
Loading

0 comments on commit 556e8ff

Please sign in to comment.