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 panic when os.Stat returns an error besides ErrNotExists #2162

Merged
merged 6 commits into from Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/cosign/cli/generate/generate_key_pair.go
Expand Up @@ -85,7 +85,12 @@ func GenerateKeyPairCmd(ctx context.Context, kmsVal string, args []string) error
return err
}

if cosign.FileExists("cosign.key") {
fileExists, err := cosign.FileExists("cosign.key")
if err != nil {
return err
cpanato marked this conversation as resolved.
Show resolved Hide resolved
}

if fileExists {
var overwrite string
fmt.Fprint(os.Stderr, "File cosign.key already exists. Overwrite (y/n)? ")
fmt.Scanf("%s", &overwrite)
Expand Down
7 changes: 6 additions & 1 deletion cmd/cosign/cli/importkeypair/import_key_pair.go
Expand Up @@ -36,7 +36,12 @@ func ImportKeyPairCmd(ctx context.Context, keyVal string, args []string) error {
return err
}

if cosign.FileExists("import-cosign.key") {
fileExists, err := cosign.FileExists("import-cosign.key\000x")
cpanato marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
cpanato marked this conversation as resolved.
Show resolved Hide resolved
}

if fileExists {
var overwrite string
fmt.Fprint(os.Stderr, "File import-cosign.key already exists. Overwrite (y/n)? ")
fmt.Scanf("%s", &overwrite)
Expand Down
9 changes: 6 additions & 3 deletions pkg/cosign/common.go
Expand Up @@ -27,12 +27,15 @@ import (
)

// TODO(jason): Move this to an internal package.
func FileExists(filename string) bool {
func FileExists(filename string) (bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

What do other semver folks think about this change? It's technically breaking, but I can't find any usage of it on GitHub.

Copy link
Contributor Author

@dsa0x dsa0x Aug 15, 2022

Choose a reason for hiding this comment

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

Good catch. Also, to Jason's (Todo) point, I think this should be in an internal package, as this behaviour doesn't need to be public to users. It's trivial to implement if they need to. That would prevent us from running into issues with breaking semver.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, if we're going to change the interface we might as well move it internal at the same time to prevent future issues. WDYT about making the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I can make a commit to that effect. Thanks

Copy link
Member

Choose a reason for hiding this comment

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

+1

info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
return false, nil
}
return !info.IsDir()
if err != nil {
return false, err
}
return !info.IsDir(), nil
}

// ConfirmPrompt prompts the user for confirmation for an action. Supports skipping
Expand Down
51 changes: 51 additions & 0 deletions pkg/cosign/common_test.go
@@ -0,0 +1,51 @@
//
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cosign

import (
"os"
"testing"
)

func Test_FileExists(t *testing.T) {
tmpFile, err := os.CreateTemp(os.TempDir(), "cosign_test.txt")
if err != nil {
t.Fatal(err)
}

tests := []struct {
name string
path string
exists bool
wantErr bool
}{
{"file exists", tmpFile.Name(), true, false},
{"file does not exist", "testt.txt", false, false},
{"other error e.g cannot access file", "\000x", false, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FileExists(tt.path)
if (err != nil) != tt.wantErr {
t.Errorf("FileExists() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.exists {
t.Errorf("FileExists() = %v, want %v", got, tt.exists)
}
})
}
}