-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added
notation certificate
command for trust store (#405)
This PR is the implementation of `notation certificate` command regarding the use of trust store. Based on community meeting, this command only effects on User level. Signed-off-by: Patrick Zheng <[email protected]>
- Loading branch information
1 parent
8d1d4dc
commit f0e77eb
Showing
28 changed files
with
968 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cert | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/notaryproject/notation/cmd/notation/internal/truststore" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type certAddOpts struct { | ||
storeType string | ||
namedStore string | ||
path []string | ||
} | ||
|
||
func certAddCommand(opts *certAddOpts) *cobra.Command { | ||
if opts == nil { | ||
opts = &certAddOpts{} | ||
} | ||
command := &cobra.Command{ | ||
Use: "add --type <type> --store <name> [flags] <cert_path>...", | ||
Short: "Add certificates to the trust store.", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return errors.New("missing certificate path") | ||
} | ||
opts.path = args | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return addCerts(opts) | ||
}, | ||
} | ||
command.Flags().StringVarP(&opts.storeType, "type", "t", "", "specify trust store type, options: ca, signingAuthority") | ||
command.Flags().StringVarP(&opts.namedStore, "store", "s", "", "specify named store") | ||
return command | ||
} | ||
|
||
func addCerts(opts *certAddOpts) error { | ||
storeType := opts.storeType | ||
if storeType == "" { | ||
return errors.New("store type cannot be empty") | ||
} | ||
if !truststore.IsValidStoreType(storeType) { | ||
return fmt.Errorf("unsupported store type: %s", storeType) | ||
} | ||
namedStore := opts.namedStore | ||
if !truststore.IsValidFileName(namedStore) { | ||
return errors.New("named store name needs to follow [a-zA-Z0-9_.-]+ format") | ||
} | ||
var success []string | ||
var failure []string | ||
var errorSlice []error | ||
for _, p := range opts.path { | ||
err := truststore.AddCert(p, storeType, namedStore, false) | ||
if err != nil { | ||
failure = append(failure, p) | ||
errorSlice = append(errorSlice, err) | ||
} else { | ||
success = append(success, p) | ||
} | ||
} | ||
|
||
//write out | ||
if len(success) != 0 { | ||
fmt.Printf("Successfully added following certificates to named store %s of type %s:\n", namedStore, storeType) | ||
for _, p := range success { | ||
fmt.Println(p) | ||
} | ||
} | ||
if len(failure) != 0 { | ||
errStr := fmt.Sprintf("Failed to add following certificates to named store %s of type %s:\n", namedStore, storeType) | ||
|
||
for ind := range failure { | ||
errStr = errStr + fmt.Sprintf("%s, with error %q\n", failure[ind], errorSlice[ind]) | ||
} | ||
return errors.New(errStr) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cert | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestCertAddCommand(t *testing.T) { | ||
opts := &certAddOpts{} | ||
cmd := certAddCommand(opts) | ||
expected := &certAddOpts{ | ||
storeType: "ca", | ||
namedStore: "test", | ||
path: []string{"path"}, | ||
} | ||
if err := cmd.ParseFlags([]string{ | ||
"path", | ||
"-t", "ca", | ||
"-s", "test"}); err != nil { | ||
t.Fatalf("Parse Flag failed: %v", err) | ||
} | ||
if err := cmd.Args(cmd, cmd.Flags().Args()); err != nil { | ||
t.Fatalf("Parse Args failed: %v", err) | ||
} | ||
if !reflect.DeepEqual(*expected, *opts) { | ||
t.Fatalf("Expect cert add opts: %v, got: %v", expected, opts) | ||
} | ||
} | ||
|
||
func TestCertAddCommand_MissingArgs(t *testing.T) { | ||
cmd := certAddCommand(nil) | ||
if err := cmd.ParseFlags([]string{}); err != nil { | ||
t.Fatalf("Parse Flag failed: %v", err) | ||
} | ||
if err := cmd.Args(cmd, cmd.Flags().Args()); err == nil { | ||
t.Fatal("Parse Args expected error, but ok") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cert | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func Cmd() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "certificate", | ||
Aliases: []string{"cert"}, | ||
Short: "Manage certificates in trust store", | ||
Long: "Manage certificates in trust store for signature verification.", | ||
} | ||
|
||
command.AddCommand( | ||
certAddCommand(nil), | ||
certListCommand(nil), | ||
certShowCommand(nil), | ||
certDeleteCommand(nil), | ||
certGenerateTestCommand(nil), | ||
) | ||
|
||
return command | ||
} |
Oops, something went wrong.