Skip to content

Commit c732967

Browse files
committed
cmd/scion-pki: improve CLI UX
The `scion-pki` CLI commands now support reading from stdin when the file path is set to `-`, where appropriate. This allows the user to pipe data directly into the command without the need to create temporary files. Furthermore, the `scion-pki trc extract certificates` command has two additional filtering options: `--type` and `--subject.isd-as`, allowing the user to filter the extracted certificates.
1 parent ad39a82 commit c732967

File tree

17 files changed

+149
-30
lines changed

17 files changed

+149
-30
lines changed

doc/command/scion-pki/scion-pki_trc_extract_certificates.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ Options
2929

3030
::
3131

32-
-h, --help help for certificates
33-
-o, --out string Output file (required)
32+
-h, --help help for certificates
33+
-o, --out string Output file (optional)
34+
--subject.isd-as strings Filter certificates by ISD-AS of the subject (e.g., 1-ff00:0:110)
35+
--type strings Filter certificates by type (any|cp-as|cp-ca|cp-root|regular-voting|sensitive-voting)
3436

3537
SEE ALSO
3638
~~~~~~~~

private/app/helper.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package app
1616

1717
import (
1818
"context"
19+
"io"
1920
"os"
2021
"os/signal"
2122
"syscall"
@@ -106,3 +107,12 @@ func (c *Cleanup) Do() error {
106107
}
107108
return errs.ToError()
108109
}
110+
111+
// ReadFileOrStdin reads the content of a file or stdin if the path is "-".
112+
// It returns the content as a byte slice.
113+
func ReadFileOrStdin(path string) ([]byte, error) {
114+
if path == "-" {
115+
return io.ReadAll(os.Stdin)
116+
}
117+
return os.ReadFile(path)
118+
}

scion-pki/certs/fingerprint.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,29 @@ package certs
1616

1717
import (
1818
"crypto/sha256"
19+
"crypto/x509"
1920
"fmt"
2021

2122
"github.com/spf13/cobra"
2223

2324
"github.com/scionproto/scion/pkg/private/serrors"
2425
"github.com/scionproto/scion/pkg/scrypto/cppki"
26+
"github.com/scionproto/scion/private/app"
2527
"github.com/scionproto/scion/private/app/command"
2628
"github.com/scionproto/scion/scion-pki/encoding"
2729
)
2830

31+
func ReadPEMCerts(file string) ([]*x509.Certificate, error) {
32+
raw, err := app.ReadFileOrStdin(file)
33+
if err != nil {
34+
return nil, err
35+
}
36+
if len(raw) == 0 {
37+
return nil, fmt.Errorf("file %q is empty", file)
38+
}
39+
return cppki.ParsePEMCerts(raw)
40+
}
41+
2942
func newFingerprintCmd(pather command.Pather) *cobra.Command {
3043
var flags struct {
3144
format string
@@ -53,7 +66,7 @@ If the flag \--format is set to "emoji", the format of the output is a string of
5366
}
5467
cmd.SilenceUsage = true
5568

56-
chain, err := cppki.ReadPEMCerts(args[0])
69+
chain, err := ReadPEMCerts(args[0])
5770
if err != nil {
5871
return serrors.Wrap("loading certificate chain", err)
5972
}

scion-pki/certs/inspect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
"encoding/pem"
2020
"fmt"
2121
"io"
22-
"os"
2322

2423
"github.com/spf13/cobra"
2524

2625
"github.com/scionproto/scion/pkg/private/serrors"
2726
"github.com/scionproto/scion/pkg/scrypto/cppki"
27+
"github.com/scionproto/scion/private/app"
2828
"github.com/scionproto/scion/private/app/command"
2929
)
3030

@@ -46,7 +46,7 @@ request (CSR) in human readable format.`,
4646
Args: cobra.ExactArgs(1),
4747
RunE: func(cmd *cobra.Command, args []string) error {
4848
cmd.SilenceUsage = true
49-
raw, err := os.ReadFile(args[0])
49+
raw, err := app.ReadFileOrStdin(args[0])
5050
if err != nil {
5151
return serrors.Wrap("loading file", err)
5252
}

scion-pki/certs/sign.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/scionproto/scion/pkg/private/serrors"
2727
"github.com/scionproto/scion/pkg/scrypto/cppki"
28+
"github.com/scionproto/scion/private/app"
2829
"github.com/scionproto/scion/private/app/command"
2930
"github.com/scionproto/scion/private/app/flag"
3031
scionpki "github.com/scionproto/scion/scion-pki"
@@ -96,7 +97,7 @@ and not to \--not-before.
9697

9798
cmd.SilenceUsage = true
9899

99-
csrRaw, err := os.ReadFile(args[0])
100+
csrRaw, err := app.ReadFileOrStdin(args[0])
100101
if err != nil {
101102
return serrors.Wrap("loading CSR", err)
102103
}

scion-pki/certs/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ period. This can be enabled by specifying the \--check-time flag.
6565
cmd.SilenceUsage = true
6666

6767
filename := args[0]
68-
certs, err := cppki.ReadPEMCerts(filename)
68+
certs, err := ReadPEMCerts(filename)
6969
if err != nil {
7070
return err
7171
}

scion-pki/certs/verify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ the expected ISD-AS value.
6161
Args: cobra.ExactArgs(1),
6262
RunE: func(cmd *cobra.Command, args []string) error {
6363
cmd.SilenceUsage = true
64-
chain, err := cppki.ReadPEMCerts(args[0])
64+
chain, err := ReadPEMCerts(args[0])
6565
if err != nil {
6666
return serrors.Wrap("reading chain", err, "file", args[0])
6767
}
@@ -148,7 +148,7 @@ The CA certificate must be a PEM encoded.
148148
Args: cobra.ExactArgs(1),
149149
RunE: func(cmd *cobra.Command, args []string) error {
150150
cmd.SilenceUsage = true
151-
certs, err := cppki.ReadPEMCerts(args[0])
151+
certs, err := ReadPEMCerts(args[0])
152152
if err != nil {
153153
return serrors.Wrap("reading certificate", err, "file", args[0])
154154
}

scion-pki/key/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ go_library(
1818
"//pkg/private/serrors:go_default_library",
1919
"//pkg/scrypto:go_default_library",
2020
"//pkg/scrypto/cppki:go_default_library",
21+
"//private/app:go_default_library",
2122
"//private/app/command:go_default_library",
2223
"//scion-pki:go_default_library",
2324
"//scion-pki/encoding:go_default_library",

scion-pki/key/fingerprint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
"crypto/x509"
2121
"encoding/pem"
2222
"fmt"
23-
"os"
2423

2524
"github.com/spf13/cobra"
2625

2726
"github.com/scionproto/scion/pkg/private/serrors"
2827
"github.com/scionproto/scion/pkg/scrypto/cppki"
28+
"github.com/scionproto/scion/private/app"
2929
"github.com/scionproto/scion/private/app/command"
3030
"github.com/scionproto/scion/scion-pki/encoding"
3131
)
@@ -102,7 +102,7 @@ The subject key ID is written to standard out.
102102

103103
// loadPublicKey loads the public key from file and distinguishes what type of key it is.
104104
func loadPublicKey(filename string) (crypto.PublicKey, error) {
105-
raw, err := os.ReadFile(filename)
105+
raw, err := app.ReadFileOrStdin(filename)
106106
if err != nil {
107107
return nil, serrors.Wrap("reading input file", err)
108108
}

scion-pki/key/public.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
"crypto/x509"
2020
"encoding/pem"
2121
"fmt"
22-
"os"
2322
"path/filepath"
2423

2524
"github.com/spf13/cobra"
2625

2726
"github.com/scionproto/scion/pkg/private/serrors"
27+
"github.com/scionproto/scion/private/app"
2828
"github.com/scionproto/scion/private/app/command"
2929
scionpki "github.com/scionproto/scion/scion-pki"
3030
"github.com/scionproto/scion/scion-pki/file"
@@ -98,7 +98,7 @@ By default, the public key is written to standard out.
9898
// LoadPrivate key loads a private key from file.
9999
func LoadPrivateKey(kms, name string) (crypto.Signer, error) {
100100
if kms == "" {
101-
raw, err := os.ReadFile(name)
101+
raw, err := app.ReadFileOrStdin(name)
102102
if err != nil {
103103
return nil, serrors.Wrap("reading private key", err)
104104
}

0 commit comments

Comments
 (0)