Skip to content
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

fix: do not overwrite local plugin manifest if fetched manifest is invalid #1000

Merged
merged 2 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/cmd/plugin/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ func (uc *UpgradeCmd) runUpgradeCmd(cmd *cobra.Command, args []string) error {
})

// Refresh the plugin info before proceeding
plugins.RefreshPluginManifest(cmd.Context(), uc.cfg, uc.fs, uc.apiBaseURL)
if err := plugins.RefreshPluginManifest(cmd.Context(), uc.cfg, uc.fs, uc.apiBaseURL); err != nil {
log.Debug(err)
fmt.Println("Unable to refresh plugin manifest, continuing with cached manifest...")
}

plugin, err := plugins.LookUpPlugin(cmd.Context(), uc.cfg, uc.fs, args[0])

Expand Down
16 changes: 16 additions & 0 deletions pkg/plugins/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func RefreshPluginManifest(ctx context.Context, config config.IConfig, fs afero.
return err
}

if err := validatePluginManifest(body); err != nil {
return err
}

configPath := config.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME"))
pluginManifestPath := filepath.Join(configPath, "plugins.toml")

Expand All @@ -131,6 +135,18 @@ func RefreshPluginManifest(ctx context.Context, config config.IConfig, fs afero.
return nil
}

func validatePluginManifest(body []byte) error {
var manifestBody PluginList

if err := toml.Unmarshal(body, &manifestBody); err != nil {
return fmt.Errorf("Received an invalid plugin manifest. Error: %s", err)
}
if len(manifestBody.Plugins) == 0 {
return fmt.Errorf("Received an empty plugin manifest")
}
return nil
}

// AddEntryToPluginManifest update plugins.toml with a new release version
func AddEntryToPluginManifest(ctx context.Context, config config.IConfig, fs afero.Fs, entry Plugin) error {
color := ansi.Color(os.Stdout)
Expand Down
17 changes: 17 additions & 0 deletions pkg/plugins/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ func TestRefreshPluginManifest(t *testing.T) {
require.Equal(t, updatedManifestContent, pluginManifestContent)
}

func TestRefreshPluginManifestFailsInvalidManifest(t *testing.T) {
fs := setUpFS()
config := &TestConfig{}
config.InitConfig()
emptyManifestContent := []byte{}
testServers := setUpServers(t, emptyManifestContent)
defer func() { testServers.CloseAll() }()

err := RefreshPluginManifest(context.Background(), config, fs, testServers.StripeServer.URL)
require.NotNil(t, err)
require.ErrorContains(t, err, "Received an empty plugin manifest")
// We expect the /plugins.toml file in the test fs has NOT been updated
pluginManifestContent, err := afero.ReadFile(fs, "/plugins.toml")
require.Nil(t, err)
require.NotEqual(t, emptyManifestContent, pluginManifestContent)
}

func TestIsPluginCommand(t *testing.T) {
pluginCmd := &cobra.Command{
Annotations: map[string]string{"scope": "plugin"},
Expand Down