-
Notifications
You must be signed in to change notification settings - Fork 126
Add setting to configure a directory with geoip databases #1211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Directory containing GeoIP databases for stacks managed by elastic-agent. | ||
# stack.geoip_dir: "/path/to/geoip_dir/" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# An expected setting. | ||
stack.geoip_dir: "/home/foo/Documents/ingest-geoip" | ||
|
||
# An empty string, should exist, but return empty. | ||
other.empty: "" | ||
|
||
# A nested value, should work as "other.nested". | ||
other: | ||
nested: "foo" | ||
|
||
# A number. Will be parsed as string. | ||
other.number: 42 | ||
|
||
# A float. Will be parsed as string. | ||
other.float: 0.12345 | ||
|
||
# A bool. Will be parsed as string. | ||
other.bool: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 profile | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/elastic/go-ucfg/yaml" | ||
|
||
"github.com/elastic/elastic-package/internal/common" | ||
) | ||
|
||
type config struct { | ||
settings common.MapStr | ||
} | ||
|
||
func loadProfileConfig(path string) (config, error) { | ||
cfg, err := yaml.NewConfigWithFile(path) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return config{}, nil | ||
} | ||
if err != nil { | ||
return config{}, fmt.Errorf("can't load profile configuration (%s): %w", path, err) | ||
} | ||
|
||
settings := make(common.MapStr) | ||
err = cfg.Unpack(settings) | ||
if err != nil { | ||
return config{}, fmt.Errorf("can't unpack configuration: %w", err) | ||
} | ||
|
||
return config{settings: settings}, nil | ||
} | ||
|
||
func (c *config) get(name string) (string, bool) { | ||
raw, err := c.settings.GetValue(name) | ||
if err != nil { | ||
return "", false | ||
} | ||
switch v := raw.(type) { | ||
case string: | ||
return v, true | ||
default: | ||
return fmt.Sprintf("%v", v), true | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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 profile | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadProfileConfig(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
expected string | ||
found bool | ||
}{ | ||
{ | ||
name: "stack.geoip_dir", | ||
expected: "/home/foo/Documents/ingest-geoip", | ||
found: true, | ||
}, | ||
{ | ||
name: "other.empty", | ||
expected: "", | ||
found: true, | ||
}, | ||
{ | ||
name: "other.nested", | ||
expected: "foo", | ||
found: true, | ||
}, | ||
{ | ||
name: "other.number", | ||
expected: "42", | ||
found: true, | ||
}, | ||
{ | ||
name: "other.float", | ||
expected: "0.12345", | ||
found: true, | ||
}, | ||
{ | ||
name: "other.bool", | ||
expected: "false", | ||
found: true, | ||
}, | ||
{ | ||
name: "not.present", | ||
found: false, | ||
}, | ||
} | ||
|
||
config, err := loadProfileConfig("_testdata/config.yml") | ||
require.NoError(t, err) | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
value, found := config.get(c.name) | ||
if assert.Equal(t, c.found, found) { | ||
assert.Equal(t, c.expected, value) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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 stack | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/elastic/elastic-package/internal/profile" | ||
) | ||
|
||
func TestApplyResourcesWithCustomGeoipDir(t *testing.T) { | ||
const expectedGeoipPath = "/some/path/ingest-geoip" | ||
const profileName = "custom_geoip" | ||
|
||
elasticPackagePath := t.TempDir() | ||
profilesPath := filepath.Join(elasticPackagePath, "profiles") | ||
|
||
os.Setenv("ELASTIC_PACKAGE_DATA_HOME", elasticPackagePath) | ||
|
||
// Create profile. | ||
err := profile.CreateProfile(profile.Options{ | ||
// PackagePath is actually the profiles path, what is a bit counterintuitive. | ||
PackagePath: profilesPath, | ||
Name: profileName, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Write configuration to the profile. | ||
configPath := filepath.Join(profilesPath, profileName, profile.PackageProfileConfigFile) | ||
config := fmt.Sprintf("stack.geoip_dir: %q", expectedGeoipPath) | ||
err = os.WriteFile(configPath, []byte(config), 0644) | ||
require.NoError(t, err) | ||
|
||
p, err := profile.LoadProfile(profileName) | ||
require.NoError(t, err) | ||
t.Logf("Profile name: %s, path: %s", p.ProfileName, p.ProfilePath) | ||
|
||
// Smoke test to check that we are actually loading the profile we want and it has the setting. | ||
v := p.Config("stack.geoip_dir", "") | ||
require.Equal(t, expectedGeoipPath, v) | ||
|
||
// Now, apply resources and check that the variable has been used. | ||
err = applyResources(p, "8.6.1") | ||
require.NoError(t, err) | ||
|
||
d, err := os.ReadFile(p.Path(profileStackPath, SnapshotFile)) | ||
require.NoError(t, err) | ||
|
||
var composeFile struct { | ||
Services struct { | ||
Elasticsearch struct { | ||
Volumes []string `yaml:"volumes"` | ||
} `yaml:"elasticsearch"` | ||
} `yaml:"services"` | ||
} | ||
err = yaml.Unmarshal(d, &composeFile) | ||
require.NoError(t, err) | ||
|
||
volumes := composeFile.Services.Elasticsearch.Volumes | ||
expectedVolume := fmt.Sprintf("%s:/usr/share/elasticsearch/config/ingest-geoip", expectedGeoipPath) | ||
assert.Contains(t, volumes, expectedVolume) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.