-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add setting to configure a directory with geoip databases
- Loading branch information
Showing
10 changed files
with
272 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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