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

feat(cfssljson): Add -output argument to save files in another directory #1278

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ filenames in the following way:
* if __bundle__ is specified, __basename-bundle.pem__ will be produced.
* if __ocspResponse__ is specified, __basename-response.der__ will be produced.

If you want to save the files in another directory, you can pass the
folder path using the `-output` argument.

Instead of saving to a file, you can pass `-stdout` to output the encoded
contents to standard output.

Expand Down
24 changes: 23 additions & 1 deletion cmd/cfssljson/cfssljson.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/cloudflare/cfssl/cli/version"
)
Expand All @@ -28,6 +29,16 @@ func writeFile(filespec, contents string, perms os.FileMode) {
}
}

// isDirectory returns true if the given path leads to a directory.
func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, fmt.Errorf("failed to stat the directory (--output=%s): %v\n", path, err)
}

return fileInfo.IsDir(), nil
}

// ResponseMessage represents the format of a CFSSL output for an error or message
type ResponseMessage struct {
Code int `json:"int"`
Expand All @@ -54,13 +65,23 @@ func main() {
inFile := flag.String("f", "-", "JSON input")
output := flag.Bool("stdout", false, "output the response instead of saving to a file")
printVersion := flag.Bool("version", false, "print version and exit")
outputDirectory := flag.String("output", "./", "place output files in a specific directory. Must be a folder")
flag.Parse()

if *printVersion {
fmt.Printf("%s", version.FormatVersion())
return
}

if ok, err := isDirectory(*outputDirectory); !ok {
if err != nil {
fmt.Fprintf(os.Stderr, "failed to ensure directory: %v", err)
} else {
fmt.Fprintf(os.Stderr, "output argument is not a folder (path=%s)", *outputDirectory)
}
os.Exit(1)
}

var baseName string
if flag.NArg() == 0 {
baseName = "cert"
Expand Down Expand Up @@ -205,7 +226,8 @@ func main() {
}
fmt.Fprintf(os.Stdout, "%s\n", e.Contents)
} else {
writeFile(e.Filename, e.Contents, e.Perms)
p := filepath.Join(*outputDirectory, e.Filename)
writeFile(p, e.Contents, e.Perms)
}
}
}
35 changes: 35 additions & 0 deletions cmd/cfssljson/cfssljson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,38 @@ func TestReadFile(t *testing.T) {
t.Fatal("File not read correctly")
}
}

func TestIsDirectory(t *testing.T) {
t.Run("OK", func(t *testing.T) {
ok, err := isDirectory("testdata")
if err != nil {
t.Fatal(err)
}

if ok == false {
t.Fatal("should be a directory")
}
})

t.Run("NOK - Not a directory", func(t *testing.T) {
ok, err := isDirectory("cfssljson.go")
if err != nil {
t.Fatal(err)
}

if ok == true {
t.Fatal("should not be a directory")
}
})

t.Run("NOK - File not present", func(t *testing.T) {
ok, err := isDirectory("notpresent")
if err == nil {
t.Fatal(err)
}

if ok == true {
t.Fatal("should not exist")
}
})
}