Skip to content

Commit e0ea94a

Browse files
committed
Merge branch 'master' into context
2 parents d1f415e + 8da6a4d commit e0ea94a

File tree

6 files changed

+52
-42
lines changed

6 files changed

+52
-42
lines changed

azure-pipelines.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ stages:
2121
- task: GoTool@0
2222
displayName: "Install Go"
2323
inputs:
24-
version: '1.23.3'
24+
version: '1.24.0'
2525

2626
- pwsh: |
2727
choco install -y make

pkg/cwhub/item.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@ type Item struct {
113113
Dependencies
114114
}
115115

116-
// InstallPath returns the location of the symlink to the item in the hub, or the path of the item itself if it's local
117-
// (eg. /etc/crowdsec/collections/xyz.yaml).
118-
// Raises an error if the path goes outside of the install dir.
116+
// InstallPath returns the path to use for the install symlink.
117+
// Returns an error if an item is already installed or if the path goes outside of the install dir.
119118
func (i *Item) InstallPath() (string, error) {
119+
if i.State.Installed {
120+
return "", fmt.Errorf("%s is already installed at %s", i.FQName(), i.State.LocalPath)
121+
}
122+
120123
p := i.Type
121124
if i.Stage != "" {
122125
p = filepath.Join(p, i.Stage)
@@ -205,13 +208,7 @@ func (i *Item) CurrentDependencies() Dependencies {
205208
return i.Dependencies
206209
}
207210

208-
contentPath, err := i.InstallPath()
209-
if err != nil {
210-
i.hub.logger.Warningf("can't access dependencies for %s, using index", i.FQName())
211-
return i.Dependencies
212-
}
213-
214-
currentContent, err := os.ReadFile(contentPath)
211+
currentContent, err := os.ReadFile(i.State.LocalPath)
215212
if errors.Is(err, fs.ErrNotExist) {
216213
return i.Dependencies
217214
}

pkg/cwhub/sync.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111

1212
"github.com/Masterminds/semver/v3"
13-
"github.com/sirupsen/logrus"
1413
"gopkg.in/yaml.v3"
1514

1615
"github.com/crowdsecurity/go-cs-lib/downloader"
@@ -86,7 +85,7 @@ type itemSpec struct {
8685
local bool // is this a spec for a local item?
8786
}
8887

89-
func newHubItemSpec(path string, subs []string, logger *logrus.Logger) (*itemSpec, error) {
88+
func newHubItemSpec(path string, subs []string) (*itemSpec, error) {
9089
// .../hub/parsers/s00-raw/crowdsecurity/skip-pretag.yaml
9190
// .../hub/scenarios/crowdsecurity/ssh_bf.yaml
9291
// .../hub/profiles/crowdsecurity/linux.yaml
@@ -126,9 +125,7 @@ func newHubItemSpec(path string, subs []string, logger *logrus.Logger) (*itemSpe
126125
return &spec, nil
127126
}
128127

129-
func newInstallItemSpec(path string, subs []string, logger *logrus.Logger) (*itemSpec, error) {
130-
logger.Tracef("%s in install dir", path)
131-
128+
func newInstallItemSpec(path string, subs []string) (*itemSpec, error) {
132129
// .../config/parser/stage/file.yaml
133130
// .../config/postoverflow/stage/file.yaml
134131
// .../config/scenarios/scenar.yaml
@@ -161,19 +158,19 @@ func newInstallItemSpec(path string, subs []string, logger *logrus.Logger) (*ite
161158
return &spec, nil
162159
}
163160

164-
func newItemSpec(path, hubDir, installDir string, logger *logrus.Logger) (*itemSpec, error) {
161+
func newItemSpec(path, hubDir, installDir string) (*itemSpec, error) {
165162
var (
166163
spec *itemSpec
167164
err error
168165
)
169166

170167
if subs := relativePathComponents(path, hubDir); len(subs) > 0 {
171-
spec, err = newHubItemSpec(path, subs, logger)
168+
spec, err = newHubItemSpec(path, subs)
172169
if err != nil {
173170
return nil, err
174171
}
175172
} else if subs := relativePathComponents(path, installDir); len(subs) > 0 {
176-
spec, err = newInstallItemSpec(path, subs, logger)
173+
spec, err = newInstallItemSpec(path, subs)
177174
if err != nil {
178175
return nil, err
179176
}
@@ -306,7 +303,7 @@ func (h *Hub) itemVisit(path string, f os.DirEntry, err error) (*itemSpec, error
306303
return nil, ErrSkipPath
307304
}
308305

309-
spec, err := newItemSpec(path, h.local.HubDir, h.local.InstallDir, h.logger)
306+
spec, err := newItemSpec(path, h.local.HubDir, h.local.InstallDir)
310307
if err != nil {
311308
h.logger.Warningf("Ignoring file %s: %s", path, err)
312309
return nil, ErrSkipPath
@@ -323,10 +320,6 @@ func updateNonLocalItem(h *Hub, path string, spec *itemSpec, symlinkTarget strin
323320
}
324321

325322
for _, item := range h.GetItemMap(spec.ftype) {
326-
if spec.fname != item.FileName {
327-
continue
328-
}
329-
330323
if item.Stage != spec.stage {
331324
continue
332325
}
@@ -405,6 +398,8 @@ func (h *Hub) addItemFromSpec(spec *itemSpec) error {
405398
if err != nil {
406399
return err
407400
}
401+
402+
item.State.LocalPath = spec.path
408403
}
409404

410405
if item == nil {
@@ -653,7 +648,6 @@ func (i *Item) setVersionState(path string, inhub bool) error {
653648
i.hub.logger.Tracef("got tainted match for %s: %s", i.Name, path)
654649

655650
if !inhub {
656-
i.State.LocalPath = path
657651
i.State.Installed = true
658652
}
659653

@@ -669,7 +663,6 @@ func (i *Item) setVersionState(path string, inhub bool) error {
669663

670664
if !inhub {
671665
i.hub.logger.Tracef("found exact match for %s, version is %s, latest is %s", i.Name, i.State.LocalVersion, i.Version)
672-
i.State.LocalPath = path
673666
i.State.Tainted = false
674667
// if we're walking the hub, present file doesn't means installed file
675668
i.State.Installed = true

pkg/hubops/datarefresh.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ import (
1111
// XXX: TODO: temporary for hubtests, but will have to go.
1212
// DownloadDataIfNeeded downloads the data set for the item.
1313
func DownloadDataIfNeeded(ctx context.Context, hub *cwhub.Hub, item *cwhub.Item, force bool) (bool, error) {
14-
itemFilePath, err := item.InstallPath()
14+
itemFile, err := os.Open(item.State.LocalPath)
1515
if err != nil {
16-
return false, err
17-
}
18-
19-
itemFile, err := os.Open(itemFilePath)
20-
if err != nil {
21-
return false, fmt.Errorf("while opening %s: %w", itemFilePath, err)
16+
return false, fmt.Errorf("while opening %s: %w", item.State.LocalPath, err)
2217
}
2318

2419
defer itemFile.Close()
2520

2621
needReload, err := downloadDataSet(ctx, hub.GetDataDir(), force, itemFile)
2722
if err != nil {
28-
return needReload, fmt.Errorf("while downloading data for %s: %w", itemFilePath, err)
23+
return needReload, fmt.Errorf("while downloading data for %s: %w", item.State.LocalPath, err)
2924
}
3025

3126
return needReload, nil

pkg/hubops/disable.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ import (
1010

1111
// RemoveInstallLink removes the item's symlink between the installation directory and the local hub.
1212
func RemoveInstallLink(i *cwhub.Item) error {
13-
syml, err := i.InstallPath()
14-
if err != nil {
15-
return err
16-
}
17-
18-
stat, err := os.Lstat(syml)
13+
stat, err := os.Lstat(i.State.LocalPath)
1914
if err != nil {
2015
return err
2116
}
@@ -25,7 +20,7 @@ func RemoveInstallLink(i *cwhub.Item) error {
2520
return fmt.Errorf("%s isn't managed by hub", i.Name)
2621
}
2722

28-
hubpath, err := os.Readlink(syml)
23+
hubpath, err := os.Readlink(i.State.LocalPath)
2924
if err != nil {
3025
return fmt.Errorf("while reading symlink: %w", err)
3126
}
@@ -39,7 +34,7 @@ func RemoveInstallLink(i *cwhub.Item) error {
3934
return fmt.Errorf("%s isn't managed by hub", i.Name)
4035
}
4136

42-
if err := os.Remove(syml); err != nil {
37+
if err := os.Remove(i.State.LocalPath); err != nil {
4338
return fmt.Errorf("while removing symlink: %w", err)
4439
}
4540

test/bats/20_hub.bats

+30
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,36 @@ teardown() {
104104
assert_stderr --partial "crowdsecurity/sshd is tainted by missing parsers:crowdsecurity/sshd-logs"
105105
}
106106

107+
@test "an install symlink can have a different name than the items it points to" {
108+
rune -0 cscli scenarios install crowdsecurity/ssh-bf
109+
rune -0 cscli scenarios inspect crowdsecurity/ssh-bf -o json
110+
rune -0 jq -r '.local_path' <(output)
111+
rune -0 mv "$output" "$CONFIG_DIR/scenarios/newname.yaml"
112+
rune -0 cscli hub list -o json
113+
rune -0 jq -r '.scenarios.[].name' <(output)
114+
assert_output 'crowdsecurity/ssh-bf'
115+
116+
rune -0 cscli scenarios inspect crowdsecurity/ssh-bf -o json
117+
rune -0 jq -r '.installed' <(output)
118+
assert_output true
119+
120+
rune -0 cscli scenarios remove crowdsecurity/ssh-bf
121+
assert_output - <<-EOT
122+
Action plan:
123+
❌ disable
124+
scenarios: crowdsecurity/ssh-bf
125+
126+
disabling scenarios:crowdsecurity/ssh-bf
127+
128+
$RELOAD_MESSAGE
129+
EOT
130+
refute_stderr
131+
132+
rune -0 cscli scenarios inspect crowdsecurity/ssh-bf -o json
133+
rune -0 jq -r '.installed' <(output)
134+
assert_output false
135+
}
136+
107137
@test "cscli hub update" {
108138
rm -f "$INDEX_PATH"
109139
rune -0 cscli hub update

0 commit comments

Comments
 (0)