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

Print security notice when index is added #616

Merged
merged 5 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 9 additions & 2 deletions cmd/krew/cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ var indexAddCmd = &cobra.Command{
if !indexoperations.IsValidIndexName(name) {
return errInvalidIndexName
}
return indexoperations.AddIndex(paths, name, args[1])
err := indexoperations.AddIndex(paths, name, args[1])
if err != nil {
return err
}
internal.PrintWarning(os.Stderr, `You have added a new index from %q
The plugins in this index are not audited for security by the Krew maintainers.
Install them at your own risk.`+"\n", args[1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
Install them at your own risk.`+"\n", args[1])
Install them at your own risk.
`, args[1])

We are already using multiline string litrals with odd indentation. Then let's not bother about prettifying the trailing newline.

return nil
},
}

Expand All @@ -86,7 +93,7 @@ var indexDeleteCmd = &cobra.Command{

It is only safe to remove indexes without installed plugins. Removing an index
while there are plugins installed will result in an error, unless the --force
option is used ( not recommended).`,
option is used (not recommended).`,

Args: cobra.ExactArgs(1),
RunE: indexDelete,
Expand Down
5 changes: 4 additions & 1 deletion cmd/krew/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/krew/internal/index/validation"
"sigs.k8s.io/krew/internal/installation"
"sigs.k8s.io/krew/internal/pathutil"
"sigs.k8s.io/krew/pkg/constants"
"sigs.k8s.io/krew/pkg/index"
)

Expand Down Expand Up @@ -173,7 +174,9 @@ Remarks:
output += fmt.Sprintf("Caveats:\n%s\n", indent(plugin.Spec.Caveats))
}
fmt.Fprintln(os.Stderr, indent(output))
internal.PrintSecurityNotice(plugin.Name)
if entry.indexName == constants.DefaultIndexName {
internal.PrintSecurityNotice(plugin.Name)
}
}
if len(failed) > 0 {
return errors.Wrapf(returnErr, "failed to install some plugins: %+v", failed)
Expand Down
5 changes: 4 additions & 1 deletion cmd/krew/cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/krew/internal/installation"
"sigs.k8s.io/krew/internal/installation/receipt"
"sigs.k8s.io/krew/internal/pathutil"
"sigs.k8s.io/krew/pkg/constants"
)

func init() {
Expand Down Expand Up @@ -104,7 +105,9 @@ kubectl krew upgrade foo bar"`,
return errors.Wrapf(err, "failed to upgrade plugin %q", pluginDisplayName)
}
fmt.Fprintf(os.Stderr, "Upgraded plugin: %s\n", pluginDisplayName)
internal.PrintSecurityNotice(plugin.Name)
if indexName == constants.DefaultIndexName {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestKrewInstallDoesntShowSecurityWarningForCustomIndex

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
Even if it looks redundant, it is an important documentation of our intended logic.

internal.PrintSecurityNotice(plugin.Name)
}
}
if nErrors > 0 {
fmt.Fprintf(os.Stderr, "WARNING: Some plugins failed to upgrade, check logs above.\n")
Expand Down
13 changes: 13 additions & 0 deletions integration_test/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ func TestKrewIndexAddUnsafe(t *testing.T) {
}
}

func TestKrewIndexAddShowsSecurityWarning(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()

test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex()
out := string(test.Krew("index", "add", "foo", test.TempDir().Path("index/"+constants.DefaultIndexName)).RunOrFailOutput())
if !strings.Contains(out, "WARNING") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also check for a more specific message? Just in case when add prints two warnings, we want to make sure the test asserts on the correct one.

t.Errorf("expected output to contain warning when adding custom index: %v", out)
}
}

func TestKrewIndexList(t *testing.T) {
skipShort(t)

Expand Down
13 changes: 13 additions & 0 deletions integration_test/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ func TestKrewInstall_CustomIndex(t *testing.T) {
test.AssertExecutableNotInPATH("kubectl-" + validPlugin2)
}

func TestKrewInstallNoSecurityWarningForCustomIndex(t *testing.T) {
skipShort(t)

test, cleanup := NewTest(t)
defer cleanup()

test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithDefaultIndex().WithCustomIndexFromDefault("foo")
out := string(test.Krew("install", "foo/"+validPlugin).RunOrFailOutput())
if strings.Contains(out, "Run them at your own risk") {
t.Errorf("expected install of custom plugin to not show security warning: %v", out)
}
}

func TestKrewInstall_Manifest(t *testing.T) {
skipShort(t)

Expand Down