2022-04-19 10:07:35 +00:00
|
|
|
package cwhub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
2022-04-19 10:07:35 +00:00
|
|
|
)
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// Download index, install collection. Add scenario to collection (hub-side), update index, upgrade collection.
|
|
|
|
// We expect the new scenario to be installed.
|
|
|
|
func TestUpgradeItemNewScenarioInCollection(t *testing.T) {
|
|
|
|
hub := envSetup(t)
|
2023-12-01 08:36:38 +00:00
|
|
|
item := hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection")
|
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
// fresh install of collection
|
2023-12-01 08:36:38 +00:00
|
|
|
require.False(t, item.State.Downloaded)
|
|
|
|
require.False(t, item.State.Installed)
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
require.NoError(t, item.Install(false, false))
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
require.True(t, item.State.Downloaded)
|
|
|
|
require.True(t, item.State.Installed)
|
|
|
|
require.True(t, item.State.UpToDate)
|
|
|
|
require.False(t, item.State.Tainted)
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-07-06 08:14:45 +00:00
|
|
|
// This is the scenario that gets added in next version of collection
|
2023-12-01 08:36:38 +00:00
|
|
|
require.Nil(t, hub.GetItem(SCENARIOS, "crowdsecurity/barfoo_scenario"))
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
assertCollectionDepsInstalled(t, hub, "crowdsecurity/test_collection")
|
2022-04-19 10:07:35 +00:00
|
|
|
|
|
|
|
// collection receives an update. It now adds new scenario "crowdsecurity/barfoo_scenario"
|
|
|
|
pushUpdateToCollectionInHub()
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
remote := &RemoteHubCfg{
|
|
|
|
URLTemplate: mockURLTemplate,
|
|
|
|
Branch: "master",
|
|
|
|
IndexPath: ".index.json",
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
2023-10-03 09:20:56 +00:00
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := NewHub(hub.local, remote, true, nil)
|
2023-11-24 14:57:32 +00:00
|
|
|
require.NoError(t, err, "failed to download index: %s", err)
|
|
|
|
|
|
|
|
hub = getHubOrFail(t, hub.local, remote)
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
item = hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection")
|
2023-12-01 08:36:38 +00:00
|
|
|
|
|
|
|
require.True(t, item.State.Downloaded)
|
|
|
|
require.True(t, item.State.Installed)
|
|
|
|
require.False(t, item.State.UpToDate)
|
|
|
|
require.False(t, item.State.Tainted)
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
didUpdate, err := item.Upgrade(false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, didUpdate)
|
|
|
|
assertCollectionDepsInstalled(t, hub, "crowdsecurity/test_collection")
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
require.True(t, hub.GetItem(SCENARIOS, "crowdsecurity/barfoo_scenario").State.Downloaded)
|
|
|
|
require.True(t, hub.GetItem(SCENARIOS, "crowdsecurity/barfoo_scenario").State.Installed)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Install a collection, disable a scenario.
|
|
|
|
// Upgrade should install should not enable/download the disabled scenario.
|
2023-11-24 14:57:32 +00:00
|
|
|
func TestUpgradeItemInDisabledScenarioShouldNotBeInstalled(t *testing.T) {
|
|
|
|
hub := envSetup(t)
|
2023-12-01 08:36:38 +00:00
|
|
|
item := hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection")
|
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
// fresh install of collection
|
2023-12-01 08:36:38 +00:00
|
|
|
require.False(t, item.State.Downloaded)
|
|
|
|
require.False(t, item.State.Installed)
|
|
|
|
require.False(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
require.NoError(t, item.Install(false, false))
|
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
require.True(t, item.State.Downloaded)
|
|
|
|
require.True(t, item.State.Installed)
|
|
|
|
require.True(t, item.State.UpToDate)
|
|
|
|
require.False(t, item.State.Tainted)
|
|
|
|
require.True(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
2023-11-24 14:57:32 +00:00
|
|
|
assertCollectionDepsInstalled(t, hub, "crowdsecurity/test_collection")
|
|
|
|
|
|
|
|
item = hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario")
|
|
|
|
didRemove, err := item.Remove(false, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, didRemove)
|
|
|
|
|
|
|
|
remote := &RemoteHubCfg{
|
|
|
|
URLTemplate: mockURLTemplate,
|
|
|
|
Branch: "master",
|
|
|
|
IndexPath: ".index.json",
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
hub = getHubOrFail(t, hub.local, remote)
|
|
|
|
// scenario referenced by collection was deleted hence, collection should be tainted
|
2023-12-01 08:36:38 +00:00
|
|
|
require.False(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
|
|
|
|
|
|
|
require.True(t, hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection").State.Tainted)
|
|
|
|
require.True(t, hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection").State.Downloaded)
|
|
|
|
require.True(t, hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection").State.Installed)
|
|
|
|
require.True(t, hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection").State.UpToDate)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err = NewHub(hub.local, remote, true, nil)
|
2023-11-24 14:57:32 +00:00
|
|
|
require.NoError(t, err, "failed to download index: %s", err)
|
|
|
|
|
|
|
|
item = hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection")
|
|
|
|
didUpdate, err := item.Upgrade(false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, didUpdate)
|
|
|
|
|
|
|
|
hub = getHubOrFail(t, hub.local, remote)
|
2023-12-01 08:36:38 +00:00
|
|
|
require.False(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// getHubOrFail refreshes the hub state (load index, sync) and returns the singleton, or fails the test.
|
|
|
|
func getHubOrFail(t *testing.T, local *csconfig.LocalHubCfg, remote *RemoteHubCfg) *Hub {
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := NewHub(local, remote, false, nil)
|
2023-11-24 14:57:32 +00:00
|
|
|
require.NoError(t, err, "failed to load hub index")
|
|
|
|
|
|
|
|
return hub
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Install a collection. Disable a referenced scenario. Publish new version of collection with new scenario
|
|
|
|
// Upgrade should not enable/download the disabled scenario.
|
|
|
|
// Upgrade should install and enable the newly added scenario.
|
2023-11-24 14:57:32 +00:00
|
|
|
func TestUpgradeItemNewScenarioIsInstalledWhenReferencedScenarioIsDisabled(t *testing.T) {
|
|
|
|
hub := envSetup(t)
|
2023-12-01 08:36:38 +00:00
|
|
|
item := hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection")
|
|
|
|
|
2022-04-19 10:07:35 +00:00
|
|
|
// fresh install of collection
|
2023-12-01 08:36:38 +00:00
|
|
|
require.False(t, item.State.Downloaded)
|
|
|
|
require.False(t, item.State.Installed)
|
|
|
|
require.False(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
require.NoError(t, item.Install(false, false))
|
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
require.True(t, item.State.Downloaded)
|
|
|
|
require.True(t, item.State.Installed)
|
|
|
|
require.True(t, item.State.UpToDate)
|
|
|
|
require.False(t, item.State.Tainted)
|
|
|
|
require.True(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
2023-11-24 14:57:32 +00:00
|
|
|
assertCollectionDepsInstalled(t, hub, "crowdsecurity/test_collection")
|
|
|
|
|
|
|
|
item = hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario")
|
|
|
|
didRemove, err := item.Remove(false, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, didRemove)
|
|
|
|
|
|
|
|
remote := &RemoteHubCfg{
|
|
|
|
URLTemplate: mockURLTemplate,
|
|
|
|
Branch: "master",
|
|
|
|
IndexPath: ".index.json",
|
|
|
|
}
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
hub = getHubOrFail(t, hub.local, remote)
|
2022-04-19 10:07:35 +00:00
|
|
|
// scenario referenced by collection was deleted hence, collection should be tainted
|
2023-12-01 08:36:38 +00:00
|
|
|
require.False(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
|
|
|
require.True(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Downloaded) // this fails
|
|
|
|
require.True(t, hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection").State.Tainted)
|
|
|
|
require.True(t, hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection").State.Downloaded)
|
|
|
|
require.True(t, hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection").State.Installed)
|
|
|
|
require.True(t, hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection").State.UpToDate)
|
2022-04-19 10:07:35 +00:00
|
|
|
|
|
|
|
// collection receives an update. It now adds new scenario "crowdsecurity/barfoo_scenario"
|
|
|
|
// we now attempt to upgrade the collection, however it shouldn't install the foobar_scenario
|
2023-07-06 08:14:45 +00:00
|
|
|
// we just removed. Nor should it install the newly added scenario
|
2022-04-19 10:07:35 +00:00
|
|
|
pushUpdateToCollectionInHub()
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err = NewHub(hub.local, remote, true, nil)
|
2023-11-24 14:57:32 +00:00
|
|
|
require.NoError(t, err, "failed to download index: %s", err)
|
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
require.False(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
2023-11-24 14:57:32 +00:00
|
|
|
hub = getHubOrFail(t, hub.local, remote)
|
2023-10-03 09:20:56 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
item = hub.GetItem(COLLECTIONS, "crowdsecurity/test_collection")
|
|
|
|
didUpdate, err := item.Upgrade(false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, didUpdate)
|
2022-04-19 10:07:35 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
hub = getHubOrFail(t, hub.local, remote)
|
2023-12-01 08:36:38 +00:00
|
|
|
require.False(t, hub.GetItem(SCENARIOS, "crowdsecurity/foobar_scenario").State.Installed)
|
|
|
|
require.True(t, hub.GetItem(SCENARIOS, "crowdsecurity/barfoo_scenario").State.Installed)
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
func assertCollectionDepsInstalled(t *testing.T, hub *Hub, collection string) {
|
2022-04-19 10:07:35 +00:00
|
|
|
t.Helper()
|
2023-10-03 09:20:56 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
c := hub.GetItem(COLLECTIONS, collection)
|
2023-12-15 14:27:22 +00:00
|
|
|
require.Empty(t, c.checkSubItemVersions())
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func pushUpdateToCollectionInHub() {
|
2023-10-04 10:54:21 +00:00
|
|
|
responseByPath["/master/.index.json"] = fileToStringX("./testdata/index2.json")
|
|
|
|
responseByPath["/master/collections/crowdsecurity/test_collection.yaml"] = fileToStringX("./testdata/collection_v2.yaml")
|
2022-04-19 10:07:35 +00:00
|
|
|
}
|