Skip to content

Commit

Permalink
TPE-547: Create Doomsday CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkey007 committed Oct 13, 2022
1 parent 7e2075d commit 4f82bc7
Show file tree
Hide file tree
Showing 12 changed files with 491 additions and 11 deletions.
11 changes: 2 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
doomsday-*

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
*.out
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
BUILD_TARGET ?= main.go
APP_NAME := doomsday
OUTPUT_NAME ?= $(APP_NAME)
SHELL := $(shell which bash)
VERSION := $(shell git describe --tags --abbrev=0)
LDFLAGS := -X "main.appVersion=$(VERSION)"
BUILD := go build -v -ldflags='$(LDFLAGS)' -o $(OUTPUT_NAME) $(BUILD_TARGET)

.PHONY: build server darwin darwin-amd64 darwin-arm64 linux linux-amd64 all clean
.DEFAULT: build

build: server

server:
@echo $(VERSION)
GOOS=$(GOOS) GOARCH=$(GOARCH) VERSION=$(VERSION) $(BUILD)

darwin: darwin-amd64 darwin-arm64

darwin-amd64:
GOOS=darwin GOARCH=amd64 OUTPUT_NAME=$(APP_NAME)-darwin-amd64 $(MAKE) server

darwin-arm64:
GOOS=darwin GOARCH=arm64 OUTPUT_NAME=$(APP_NAME)-darwin-arm64 $(MAKE) server

linux: linux-amd64

linux-amd64:
GOOS=linux GOARCH=amd64 OUTPUT_NAME=$(APP_NAME)-linux-amd64 $(MAKE) server

all: darwin linux

clean:
rm -f $(APP_NAME) $(APP_NAME)-darwin-amd64 $(APP_NAME)-darwin-arm64 $(APP_NAME)-linux-amd64
90 changes: 88 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
# doomsday-cli
Doomsday CLI
# Doomsday CLI

[Doomsday](https://github.com/doomsday-project/doomsday) is a server which can be configured to track certificates from different storage backends (Vault, Credhub, Pivotal Ops Manager, or actual websites) and provide a tidy view into when certificates will expire. Doomsday CLI provides the option to get all required information about certificates expiration in the JSON format and apply filters to get more detailed results.

## Installation

Installation process is very simple.
* Check available [release versions](https://github.com/starkandwayne/doomsday-cli/releases)
* Download binary for your operating system and architecture and save it as `doomsday` file
* Open your terminal console e.g. iTerm and open directory where the binary was downloaded
* Add execution permission to `doomsday` file and move it to `/usr/local/bin/` directory with the following command
```
chmod +x ./doomsday && mv ./doomsday /usr/local/bin/
```

## Using the CLI

### Command line global options
In the Doomsday CLI, command line global options are parameters you can use to override the default server configuration and user authentication settings, or environment variable settings.

You can use the following command line options to override the default configuration settings.
```
--server, -s <string>
Doomsday server URL
--username, -u <string>
Username for server authentication
--password, -p <string>
Password for server authentication
```

### Environment variables
The following examples show how you can configure environment variables for the doomsday CLI instead specyfing command line option flags.

```
$ export DOOMSDAY_SERVER=https://127.0.0.1:443
$ export DOOMSDAY_USER=doomsday
$ export DOOMSDAY_PASSWORD=password
```

### Getting help
You can get help with any command when using the Doomsday CLI. To do so, simply type `help` at the end of a command name.

For example, the following command displays help for the general Doomsday CLI options and the available top-level commands.
```
$ doomsday help
```
The following command displays the available certificates specific commands.
```
$ doomsday certificates help
```

### Command structure
The CLI uses a multipart structure on the command line that must be specified in this order:
1. The base call to the `doomsday` program.
2. The top-level command, which typically corresponds to a Doomsday server API endpoint supported by the CLI.
3. The subcommand that specifies which operation to perform.
4. General CLI options or parameters required by the operation.
```
$ doomsday <command> <subcommand> [options and parameters]
```
Parameters can take different types of input values, such as numbers or strings.

### Command examples
Here are some of the example commands used to interact with Doomsday server.
* Get all certificates and their corresponding data
```
$ doomsday certificates
```
* Get all `expired` certificates and their corresponding data
```
$ doomsday certificates expired
```
* Get all certificates that will expire in the next 30 days
```
$ doomsday certificates willexpire --days 30
```
* Refresh certificates cache
```
$ doomsday refresh
```
* Get Doomsday server version and enabled authentication method
```
$ doomsday info
```
### Output
All commands will return output in the JSON format.
24 changes: 24 additions & 0 deletions cli/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package doomsday

import (
"context"
"fmt"

"github.com/urfave/cli/v2"
)

func refreshCache(ctx context.Context, c *cli.Context) error {
client, err := newClient(ctx, c)
if err != nil {
return err
}

err = client.RefreshCache()
if err != nil {
return err
}

fmt.Print("Certificates cache has been refreshed")

return nil
}
87 changes: 87 additions & 0 deletions cli/certificates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package doomsday

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/doomsday-project/doomsday/client/doomsday"
"github.com/urfave/cli/v2"
)

func getCertificates(ctx context.Context, c *cli.Context) (doomsday.CacheItems, error) {
var certificates doomsday.CacheItems

client, err := newClient(ctx, c)
if err != nil {
return certificates, err
}

certificates, err = client.GetCache()
if err != nil {
return certificates, err
}

return certificates, nil
}

func getAllCertificates(ctx context.Context, c *cli.Context) error {
certificates, err := getCertificates(ctx, c)
if err != nil {
return err
}

allCertificates, err := json.Marshal(certificates)

fmt.Print(string(allCertificates))

return nil
}

func getExpiredCertificates(ctx context.Context, c *cli.Context) error {
certificates, err := getCertificates(ctx, c)
if err != nil {
return err
}

var within time.Duration = 0

if err != nil {
return err
}

filter := doomsday.CacheItemFilter{Within: &within}

certificates = certificates.Filter(filter)

expiredCertificates, err := json.Marshal(certificates)

fmt.Print(string(expiredCertificates))

return nil
}

func getCertificatesThatWillExpire(ctx context.Context, c *cli.Context) error {
certificates, err := getCertificates(ctx, c)
if err != nil {
return err
}

var beyond time.Duration = 1000000000 * 0
var within time.Duration = 1000000000 * 3600 * 24 * time.Duration(c.Int64("days"))

if err != nil {
return err
}

filter := doomsday.CacheItemFilter{Within: &within, Beyond: &beyond}

certificates = certificates.Filter(filter)

expiredCertificates, err := json.Marshal(certificates)

fmt.Print(string(expiredCertificates))

return nil
}
95 changes: 95 additions & 0 deletions cli/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package doomsday

import (
"context"

"github.com/urfave/cli/v2"
)

func GetCommands(ctx context.Context) []*cli.Command {
var commands []*cli.Command

// Certificates defines the certificates command
var Certificates = &cli.Command{
Name: "certificates",
Usage: "Get certificates",
Description: "Use 'doomsday certificates ...' to get certifictes from Doomsday.",
Action: func(c *cli.Context) error {
err := getAllCertificates(ctx, c)
if err != nil {
return err
}
return nil
},
Subcommands: []*cli.Command{
{
Name: "expired",
Aliases: []string{"e"},
Usage: "Get expired certificates",
Description: "Get expired certificates",
Action: func(c *cli.Context) error {
err := getExpiredCertificates(ctx, c)
if err != nil {
return err
}
return nil
},
},
{
Name: "willexpire",
Aliases: []string{"we"},
Usage: "Get certificates that will expire",
Description: "Get certificates that will expire in given number of days",
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "days",
Aliases: []string{"d"},
Value: 30,
Usage: "Number of days for which certificates will expire. Defaults to 30 days if not provided.",
},
},
Action: func(c *cli.Context) error {
err := getCertificatesThatWillExpire(ctx, c)
if err != nil {
return err
}
return nil
},
},
},
}

// Refresh defines the refresh command
var Refresh = &cli.Command{
Name: "refresh",
Usage: "Refresh certificates cache",
Description: "Use 'doomsday refresh' to refresh certifictes cache.",
Action: func(c *cli.Context) error {
err := refreshCache(ctx, c)
if err != nil {
return err
}
return nil
},
}

// Info defines the info command
var Info = &cli.Command{
Name: "info",
Usage: "Doomsday information",
Description: "Use 'doomsday info' to get information about Doomsday version and authentication method.",
Action: func(c *cli.Context) error {
err := getInfo(ctx, c)
if err != nil {
return err
}
return nil
},
}

commands = append(commands, Certificates)
commands = append(commands, Refresh)
commands = append(commands, Info)

return commands
}
27 changes: 27 additions & 0 deletions cli/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package doomsday

import (
"context"
"encoding/json"
"fmt"

"github.com/urfave/cli/v2"
)

func getInfo(ctx context.Context, c *cli.Context) error {
client, err := newClient(ctx, c)
if err != nil {
return err
}

resp, err := client.Info()
if err != nil {
return err
}

info, err := json.Marshal(resp)

fmt.Print(string(info))

return nil
}
Loading

0 comments on commit 4f82bc7

Please sign in to comment.