From 99e4d6051607f62660ecc1dadd3a2e59c8fb8dda Mon Sep 17 00:00:00 2001 From: Gibheer Date: Wed, 7 Jun 2017 22:35:12 +0200 Subject: [PATCH] add list option and some smaller adjustments This adjusts the output of the initial help and adds the list option. --- cmd/pkiadm/main.go | 92 +++++++++++++++++++++++++++++++++++++------ cmd/pkiadmd/server.go | 25 ++++++++++++ transport.go | 16 ++++++++ 3 files changed, 122 insertions(+), 11 deletions(-) diff --git a/cmd/pkiadm/main.go b/cmd/pkiadm/main.go index 9395836..0b6ea8f 100644 --- a/cmd/pkiadm/main.go +++ b/cmd/pkiadm/main.go @@ -6,6 +6,7 @@ import ( "text/tabwriter" "github.com/gibheer/pkiadm" + flag "github.com/spf13/pflag" ) func main() { @@ -30,6 +31,18 @@ func main() { cmd := os.Args[1] args := os.Args[2:] switch cmd { + case `list`: + err = list(args, client) + case `create-serial`: + err = createSerial(args, client) + case `delete-serial`: + err = deleteSerial(args, client) + case `list-serial`: + err = listSerial(args, client) + case `set-serial`: + err = setSerial(args, client) + case `show-serial`: + err = showSerial(args, client) case `create-subj`: err = createSubject(args, client) case `delete-subj`: @@ -80,6 +93,16 @@ func main() { err = setCSR(args, client) case `show-csr`: err = showCSR(args, client) + case `create-cert`: + err = createCertificate(args, client) + case `delete-cert`: + err = deleteCertificate(args, client) + case `list-cert`: + err = listCertificate(args, client) + case `set-cert`: + err = setCertificate(args, client) + case `show-cert`: + err = showCertificate(args, client) default: fmt.Printf("unknown subcommand '%s'\n", cmd) printCommands() @@ -95,16 +118,63 @@ func printCommands() { fmt.Println(`Usage: pkiadm [options] where subcommand is one of:`) out := tabwriter.NewWriter(os.Stdout, 0, 4, 1, ' ', 0) - fmt.Fprintf(out, " %s\t%s\n", "def-list", "list all registered definitions") - fmt.Fprintf(out, " %s\t%s\n", "create-file", "create a new file export") - fmt.Fprintf(out, " %s\t%s\n", "list-files", "list all file exports") - fmt.Fprintf(out, " %s\t%s\n", "delete-file", "delete a file export from the database and os") - fmt.Fprintf(out, " %s\t%s\n", "create-private-key", "create a new private key") - fmt.Fprintf(out, " %s\t%s\n", "list-private-keys", "list all private keys") - fmt.Fprintf(out, " %s\t%s\n", "get-private-key", "get information on a specific private key") - fmt.Fprintf(out, " %s\t%s\n", "delete-private-key", "delete a specific private key") - fmt.Fprintf(out, " %s\t%s\n", "create-public-key", "create a new public key") - fmt.Fprintf(out, " %s\t%s\n", "list-public-keys", "list all public keys") - fmt.Fprintf(out, " %s\t%s\n", "delete-public-key", "delete a specific public key") + fmt.Fprintf(out, " %s\t%s\n", "create-cert", "create a new certificate") + fmt.Fprintf(out, " %s\t%s\n", "create-csr", "create a new certificate sign request") + fmt.Fprintf(out, " %s\t%s\n", "create-location", "create a new file export") + fmt.Fprintf(out, " %s\t%s\n", "create-private", "create a new private key") + fmt.Fprintf(out, " %s\t%s\n", "create-public", "create a new public key") + fmt.Fprintf(out, " %s\t%s\n", "create-serial", "") + fmt.Fprintf(out, " %s\t%s\n", "create-subj", "") + + fmt.Fprintf(out, " %s\t%s\n", "delete-cert", "") + fmt.Fprintf(out, " %s\t%s\n", "delete-csr", "") + fmt.Fprintf(out, " %s\t%s\n", "delete-location", "") + fmt.Fprintf(out, " %s\t%s\n", "delete-private", "") + fmt.Fprintf(out, " %s\t%s\n", "delete-public", "") + fmt.Fprintf(out, " %s\t%s\n", "delete-serial", "") + fmt.Fprintf(out, " %s\t%s\n", "delete-subj", "") + + fmt.Fprintf(out, " %s\t%s\n", "list", "") + fmt.Fprintf(out, " %s\t%s\n", "list-cert", "list all available certificates") + fmt.Fprintf(out, " %s\t%s\n", "list-csr", "list all available certificate sign requests") + fmt.Fprintf(out, " %s\t%s\n", "list-location", "list all file exports") + fmt.Fprintf(out, " %s\t%s\n", "list-private", "list all private keys") + fmt.Fprintf(out, " %s\t%s\n", "list-public", "list all public keys") + fmt.Fprintf(out, " %s\t%s\n", "list-serial", "") + fmt.Fprintf(out, " %s\t%s\n", "list-subj", "") + + fmt.Fprintf(out, " %s\t%s\n", "set-cert", "change attributes of a certificate") + fmt.Fprintf(out, " %s\t%s\n", "set-csr", "change attributes of a certificate sign request") + fmt.Fprintf(out, " %s\t%s\n", "set-location", "change attributes of a location") + fmt.Fprintf(out, " %s\t%s\n", "set-private", "change attributes of a private key") + fmt.Fprintf(out, " %s\t%s\n", "set-public", "change attributes of a public key") + fmt.Fprintf(out, " %s\t%s\n", "set-serial", "") + fmt.Fprintf(out, " %s\t%s\n", "set-subj", "") + + fmt.Fprintf(out, " %s\t%s\n", "show-cert", "") + fmt.Fprintf(out, " %s\t%s\n", "show-csr", "") + fmt.Fprintf(out, " %s\t%s\n", "show-location", "") + fmt.Fprintf(out, " %s\t%s\n", "show-private", "") + fmt.Fprintf(out, " %s\t%s\n", "show-public", "") + fmt.Fprintf(out, " %s\t%s\n", "show-serial", "") + fmt.Fprintf(out, " %s\t%s\n", "show-subj", "") + out.Flush() } + +func list(args []string, c *pkiadm.Client) error { + fs := flag.NewFlagSet("pkiadm list", flag.ExitOnError) + fs.Parse(args) + + resources, err := c.List() + if err != nil { + return err + } + out := tabwriter.NewWriter(os.Stdout, 0, 4, 1, ' ', 0) + fmt.Fprintf(out, "%s\t%s\t\n", "type", "id") + for _, res := range resources { + fmt.Fprintf(out, "%s\t%s\t\n", res.Type, res.ID) + } + out.Flush() + return nil +} diff --git a/cmd/pkiadmd/server.go b/cmd/pkiadmd/server.go index 1c1f25f..b766031 100644 --- a/cmd/pkiadmd/server.go +++ b/cmd/pkiadmd/server.go @@ -31,3 +31,28 @@ func (s *Server) store(res *pkiadm.Result) error { } return nil } + +func (s *Server) List(filter pkiadm.Filter, result *pkiadm.ResultResource) error { + for _, res := range s.storage.PrivateKeys { + result.Resources = append(result.Resources, res.Name()) + } + for _, res := range s.storage.PublicKeys { + result.Resources = append(result.Resources, res.Name()) + } + for _, res := range s.storage.Locations { + result.Resources = append(result.Resources, res.Name()) + } + for _, res := range s.storage.Certificates { + result.Resources = append(result.Resources, res.Name()) + } + for _, res := range s.storage.CSRs { + result.Resources = append(result.Resources, res.Name()) + } + for _, res := range s.storage.Serials { + result.Resources = append(result.Resources, res.Name()) + } + for _, res := range s.storage.Subjects { + result.Resources = append(result.Resources, res.Name()) + } + return nil +} diff --git a/transport.go b/transport.go index 9cdfbab..a40203a 100644 --- a/transport.go +++ b/transport.go @@ -49,3 +49,19 @@ type ResourceType uint func (r ResourceName) String() string { return r.Type.String() + "/" + r.ID } type Filter struct{} + +type ResultResource struct { + Result Result + Resources []ResourceName +} + +func (c *Client) List() ([]ResourceName, error) { + result := ResultResource{} + if err := c.query("List", Filter{}, &result); err != nil { + return []ResourceName{}, err + } + if result.Result.HasError { + return []ResourceName{}, result.Result.Error + } + return result.Resources, nil +}