359 lines
9.0 KiB
Go
359 lines
9.0 KiB
Go
/*
|
|
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
cmapi "certmanager/api/certmanagercontrol"
|
|
"certmanager/pkg/client"
|
|
|
|
"sigs.k8s.io/yaml"
|
|
)
|
|
|
|
const (
|
|
defaultHostname = "localhost"
|
|
rcFilename = ".certmanager.yaml"
|
|
defaultPort int = client.DefaultGrpcPort
|
|
|
|
getStatusCmd = "getStatus"
|
|
helpCmd = "help"
|
|
|
|
createIssuerPairCmd = "createIssuerPair"
|
|
importIssuerPairCmd = "importIssuerPair"
|
|
revokeIssuerPairCmd = "revokeIssuerPair"
|
|
unrevokeIssuerPairCmd = "unrevokeIssuerPair"
|
|
listIssuerPairsCmd = "listIssuerPairs"
|
|
getIssuerCertificateCmd = "getIssuerCertificate"
|
|
createServicePairCmd = "createServicePair"
|
|
revokeServicePairCmd = "revokeServicePair"
|
|
listServicePairsCmd = "listServicePairs"
|
|
getServicePairCmd = "getServicePair"
|
|
)
|
|
|
|
func main() {
|
|
var err error
|
|
util := NewUtil()
|
|
err = util.Exec()
|
|
if err != nil {
|
|
os.Exit(1)
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
type Util struct {
|
|
subCmd string
|
|
cmdTimeout int64
|
|
access client.Access
|
|
cont *cmapi.ControlClient
|
|
|
|
getStatusParams cmapi.GetStatusParams
|
|
createIssuerPairParams cmapi.CreateIssuerPairParams
|
|
importIssuerPairParams cmapi.ImportIssuerPairParams
|
|
revokeIssuerPairParams cmapi.RevokeIssuerPairParams
|
|
unrevokeIssuerPairParams cmapi.UnrevokeIssuerPairParams
|
|
listIssuerPairsParams cmapi.ListIssuerPairsParams
|
|
getIssuerCertificateParams cmapi.GetIssuerCertificateParams
|
|
createServicePairParams cmapi.CreateServicePairParams
|
|
revokeServicePairParams cmapi.RevokeServicePairParams
|
|
listServicePairsParams cmapi.ListServicePairsParams
|
|
getServicePairParams cmapi.GetServicePairParams
|
|
}
|
|
|
|
func NewUtil() *Util {
|
|
var util Util
|
|
util.cmdTimeout = 120
|
|
util.access = client.Access{
|
|
Hostname: defaultHostname,
|
|
Port: defaultPort,
|
|
Username: "certmanager",
|
|
Password: "certmanager",
|
|
}
|
|
return &util
|
|
}
|
|
|
|
func (util *Util) GetOpt() error {
|
|
var err error
|
|
|
|
homeDir := os.Getenv("HOME")
|
|
if homeDir == "" {
|
|
currUsr, err := user.Current()
|
|
if err == nil {
|
|
homeDir = currUsr.HomeDir
|
|
}
|
|
}
|
|
if homeDir != "" {
|
|
confPath := filepath.Join(homeDir, rcFilename)
|
|
confData, err := os.ReadFile(confPath)
|
|
if err == nil && len(confData) > 0 {
|
|
yaml.Unmarshal(confData, &util.access)
|
|
}
|
|
}
|
|
exeName := filepath.Base(os.Args[0])
|
|
|
|
flag.Int64Var(&util.cmdTimeout, "timeout", util.cmdTimeout, "command execution timeout")
|
|
flag.StringVar(&util.access.Hostname, "host", util.access.Hostname, "service hostname")
|
|
flag.IntVar(&util.access.Port, "port", util.access.Port, "service port")
|
|
flag.StringVar(&util.access.Username, "user", util.access.Username, "access login")
|
|
flag.StringVar(&util.access.Password, "pass", util.access.Password, "access password")
|
|
|
|
help := func() {
|
|
fmt.Println("")
|
|
fmt.Printf("Usage: %s [option] command [command option]\n", exeName)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Command list: help, %s\n", getStatusCmd)
|
|
fmt.Printf("Command list: %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s\n",
|
|
createIssuerPairCmd,
|
|
importIssuerPairCmd,
|
|
revokeIssuerPairCmd,
|
|
unrevokeIssuerPairCmd,
|
|
listIssuerPairsCmd,
|
|
getIssuerCertificateCmd,
|
|
createServicePairCmd,
|
|
revokeServicePairCmd,
|
|
listServicePairsCmd,
|
|
getServicePairCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Global options:\n")
|
|
flag.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flag.Usage = help
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
var subCmd string
|
|
var subArgs []string
|
|
if len(args) > 0 {
|
|
subCmd = args[0]
|
|
subArgs = args[1:]
|
|
}
|
|
|
|
switch subCmd {
|
|
case helpCmd:
|
|
help()
|
|
return errors.New("Unknown command")
|
|
case getStatusCmd:
|
|
flagSet := flag.NewFlagSet(getStatusCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case createIssuerPairCmd:
|
|
flagSet := flag.NewFlagSet(createIssuerPairCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case importIssuerPairCmd:
|
|
flagSet := flag.NewFlagSet(importIssuerPairCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case revokeIssuerPairCmd:
|
|
flagSet := flag.NewFlagSet(revokeIssuerPairCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case unrevokeIssuerPairCmd:
|
|
flagSet := flag.NewFlagSet(unrevokeIssuerPairCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case listIssuerPairsCmd:
|
|
flagSet := flag.NewFlagSet(listIssuerPairsCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case getIssuerCertificateCmd:
|
|
flagSet := flag.NewFlagSet(getIssuerCertificateCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case createServicePairCmd:
|
|
flagSet := flag.NewFlagSet(createServicePairCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case revokeServicePairCmd:
|
|
flagSet := flag.NewFlagSet(revokeServicePairCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case listServicePairsCmd:
|
|
flagSet := flag.NewFlagSet(listServicePairsCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
case getServicePairCmd:
|
|
flagSet := flag.NewFlagSet(getServicePairCmd, flag.ExitOnError)
|
|
|
|
flagSet.Usage = func() {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
|
fmt.Printf("\n")
|
|
fmt.Printf("The command options: none\n")
|
|
flagSet.PrintDefaults()
|
|
fmt.Printf("\n")
|
|
}
|
|
flagSet.Parse(subArgs)
|
|
util.subCmd = subCmd
|
|
|
|
default:
|
|
help()
|
|
return errors.New("Unknown command")
|
|
}
|
|
return err
|
|
}
|
|
|
|
type Response struct {
|
|
Error bool `json:"error" yaml:"error"`
|
|
Message string `json:"message,omitempty" yaml:"message,omitempty"`
|
|
Result any `json:"result,omitempty" yaml:"result,omitempty"`
|
|
}
|
|
|
|
func (util *Util) Exec() error {
|
|
var err error
|
|
err = util.GetOpt()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var timeout = time.Duration(util.cmdTimeout) * time.Second
|
|
ctx, close := context.WithTimeout(context.Background(), timeout)
|
|
defer close()
|
|
|
|
var res any
|
|
switch util.subCmd {
|
|
case getStatusCmd:
|
|
res, err = util.GetStatus(ctx)
|
|
default:
|
|
err = errors.New("Unknown cli command")
|
|
}
|
|
|
|
var resp Response
|
|
if err != nil {
|
|
resp.Error = true
|
|
resp.Message = fmt.Sprintf("%v", err)
|
|
} else {
|
|
resp.Result = res
|
|
}
|
|
respBytes, _ := yaml.Marshal(resp)
|
|
fmt.Println(string(respBytes))
|
|
|
|
return err
|
|
}
|
|
|
|
func (util *Util) GetStatus(ctx context.Context) (*cmapi.GetStatusResult, error) {
|
|
var err error
|
|
res := &cmapi.GetStatusResult{}
|
|
cont, err := client.NewClient(&util.access)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
params := util.getStatusParams
|
|
res, err = cont.GetStatus(ctx, ¶ms)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
return res, err
|
|
}
|