Skip to content

Commit

Permalink
Fix license nil pointer scenario (#12958)
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator authored Jul 18, 2019
1 parent 8f8d0b3 commit 4f74aa9
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions metricbeat/module/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ func GetLicense(http *helper.HTTP, resetURI string) (*License, error) {
// First, check the cache
license := licenseCache.get()

// License found in cache, return it
if license != nil {
return license, nil
}

// License not found in cache, fetch it from Elasticsearch
info, err := GetInfo(http, resetURI)
if err != nil {
return nil, err
Expand All @@ -275,24 +281,22 @@ func GetLicense(http *helper.HTTP, resetURI string) (*License, error) {
licensePath = "_license"
}

// Not cached, fetch license from Elasticsearch
if license == nil {
content, err := fetchPath(http, resetURI, licensePath, "")
if err != nil {
return nil, err
}

var data licenseWrapper
err = json.Unmarshal(content, &data)
if err != nil {
return nil, err
}
content, err := fetchPath(http, resetURI, licensePath, "")
if err != nil {
return nil, err
}

// Cache license for a minute
licenseCache.set(&data.License, time.Minute)
var data licenseWrapper
err = json.Unmarshal(content, &data)
if err != nil {
return nil, err
}

return licenseCache.get(), nil
// Cache license for a minute
license = &data.License
licenseCache.set(license, time.Minute)

return license, nil
}

// GetClusterState returns cluster state information.
Expand Down

0 comments on commit 4f74aa9

Please sign in to comment.