diff --git a/actions/admin.go b/actions/admin.go index 2ce8fe2..6499398 100644 --- a/actions/admin.go +++ b/actions/admin.go @@ -87,3 +87,71 @@ func ActionAdminUploadBinary(c *cli.Context) error { } return nil } + +func ActionAdminCertList(c *cli.Context) error { + cfg, err := getConfig(c) + if err != nil { + return err + } + + addr := cfg.Client.DefaultServer + if c.IsSet("addr") { + addr = c.String("addr") + } + + clientCreds, err := cfg.Client.Creds() + if err != nil { + return err + } + conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) + + if err != nil { + return err + } + defer conn.Close() + + client := pb.NewCertificateServiceClient(conn) + resp, err := client.ListCertificates(c.Context, &pb.Empty{}) + if err != nil { + return cli.Exit(fmt.Sprintf("unable to list certificates: %s", err), 1) + } + + for _, info := range resp.Certificates { + fmt.Printf("%s - %s", info.Serial, info.OwnerUsername) + } + return nil +} + +func ActionAdminCertRevoke(c *cli.Context) error { + if c.Args().Len() < 1 { + return cli.Exit("need at least 1 argument", 1) + } + cfg, err := getConfig(c) + if err != nil { + return err + } + + addr := cfg.Client.DefaultServer + if c.IsSet("addr") { + addr = c.String("addr") + } + + clientCreds, err := cfg.Client.Creds() + if err != nil { + return err + } + conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) + + if err != nil { + return err + } + defer conn.Close() + + client := pb.NewCertificateServiceClient(conn) + for _, serial := range c.Args().Slice() { + if _, err := client.RevokeCertificate(c.Context, &pb.RevokeCertificateRequest{Serial: serial}); err != nil { + fmt.Printf("Revoked %s\n", serial) + } + } + return nil +} diff --git a/actions/client.go b/actions/client.go index a9f02b4..7b811b3 100644 --- a/actions/client.go +++ b/actions/client.go @@ -361,7 +361,7 @@ func ActionClientLogin(c *cli.Context) error { return nil } -func ActionClientChangePassword(c *cli.Context) error { +func ActionClientPassword(c *cli.Context) error { cfg, err := getConfig(c) if err != nil { return err @@ -405,74 +405,6 @@ func ActionClientChangePassword(c *cli.Context) error { return nil } -func ActionClientCertList(c *cli.Context) error { - cfg, err := getConfig(c) - if err != nil { - return err - } - - addr := cfg.Client.DefaultServer - if c.IsSet("addr") { - addr = c.String("addr") - } - - clientCreds, err := cfg.Client.Creds() - if err != nil { - return err - } - conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) - - if err != nil { - return err - } - defer conn.Close() - - client := pb.NewCertificateServiceClient(conn) - resp, err := client.ListCertificates(c.Context, &pb.Empty{}) - if err != nil { - return cli.Exit(fmt.Sprintf("unable to list certificates: %s", err), 1) - } - - for _, info := range resp.Certificates { - fmt.Printf("%s - %s", info.Serial, info.OwnerUsername) - } - return nil -} - -func ActionClientCertRevoke(c *cli.Context) error { - if c.Args().Len() < 1 { - return cli.Exit("need at least 1 argument", 1) - } - cfg, err := getConfig(c) - if err != nil { - return err - } - - addr := cfg.Client.DefaultServer - if c.IsSet("addr") { - addr = c.String("addr") - } - - clientCreds, err := cfg.Client.Creds() - if err != nil { - return err - } - conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) - - if err != nil { - return err - } - defer conn.Close() - - client := pb.NewCertificateServiceClient(conn) - for _, serial := range c.Args().Slice() { - if _, err := client.RevokeCertificate(c.Context, &pb.RevokeCertificateRequest{Serial: serial}); err != nil { - fmt.Printf("Revoked %s\n", serial) - } - } - return nil -} - func ActionClientUpdate(c *cli.Context) error { cfg, err := getConfig(c) if err != nil { diff --git a/actions/misc.go b/actions/misc.go index 11ddb86..b64da2e 100644 --- a/actions/misc.go +++ b/actions/misc.go @@ -8,9 +8,6 @@ import ( "github.com/urfave/cli/v2" ) -// TODO: This should probably be in some more sensible package -const Version = "v0.1.1" - func ActionGencerts(c *cli.Context) error { outDir := "." if c.IsSet("out-dir") { @@ -23,11 +20,6 @@ func ActionGencerts(c *cli.Context) error { return certs.GenAllCerts(outDir, hostname) } -func ActionInitConfig(c *cli.Context) error { - defaultCfg := config.FromDefault() - return defaultCfg.ToDefaultFile() -} - func getConfig(c *cli.Context) (*config.Config, error) { if c.IsSet("config") { cfgPath := c.String("config") diff --git a/main.go b/main.go index 8eb03ad..7b554b2 100644 --- a/main.go +++ b/main.go @@ -117,15 +117,9 @@ func main() { }, }, { - Name: "change-password", + Name: "password", Usage: "Change password", - Action: actions.ActionClientChangePassword, - }, - { - // TODO: Remove - Name: "config-init", - Usage: "Initialize default config", - Action: actions.ActionInitConfig, + Action: actions.ActionClientPassword, }, { Name: "update", @@ -138,23 +132,6 @@ func main() { }, }, }, - { - Name: "cert", - Usage: "Certificate-related commands", - Subcommands: []*cli.Command{ - { - Name: "list", - Usage: "List certificates", - Action: actions.ActionClientCertList, - }, - { - Name: "revoke", - Usage: "Revoke certificate(s)", - ArgsUsage: "SERIAL [SERIAL...]", - Action: actions.ActionClientCertRevoke, - }, - }, - }, }, }, { @@ -167,6 +144,23 @@ func main() { ArgsUsage: "FILENAME [FILENAME...]", Action: actions.ActionAdminUploadBinary, }, + { + Name: "cert", + Usage: "Certificate-related commands", + Subcommands: []*cli.Command{ + { + Name: "list", + Usage: "List certificates", + Action: actions.ActionAdminCertList, + }, + { + Name: "revoke", + Usage: "Revoke certificate(s)", + ArgsUsage: "SERIAL [SERIAL...]", + Action: actions.ActionAdminCertRevoke, + }, + }, + }, }, }, {