Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor cmds functions #40

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
gnoios "github.com/openconfig/gnoi/os"
"github.com/openconfig/gnoi/system"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

var DefaultTargetTimeout = 10 * time.Second
Expand Down Expand Up @@ -85,6 +86,18 @@ func (t *Target) CreateGrpcClient(ctx context.Context, opts ...grpc.DialOption)

func (t *Target) Conn() grpc.ClientConnInterface { return t.client }

func (t *Target) AppendMetadata(ctx context.Context) context.Context {
kv := make([]string, 0, 4)
if t.Config.Username != nil {
kv = append(kv, "username", *t.Config.Username)
}
if t.Config.Password != nil {
kv = append(kv, "password", *t.Config.Password)
}

return metadata.AppendToOutgoingContext(ctx, kv...)
}

func (t *Target) CertClient() cert.CertificateManagementClient {
return cert.NewCertificateManagementClient(t.client)
}
Expand Down
13 changes: 6 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"context"
"fmt"
"os"
"sync"
Expand All @@ -20,8 +19,6 @@ import (
)

type App struct {
ctx context.Context
Cfn context.CancelFunc
RootCmd *cobra.Command

wg *sync.WaitGroup
Expand All @@ -33,12 +30,14 @@ type App struct {
pm *sync.Mutex
}

type TargetResponse interface {
Target() string
Response() any
}

func New() *App {
ctx, cancel := context.WithCancel(context.Background())
logger := log.New()
a := &App{
ctx: ctx,
Cfn: cancel,
RootCmd: new(cobra.Command),
wg: new(sync.WaitGroup),
Config: config.New(),
Expand Down Expand Up @@ -103,7 +102,7 @@ func (a *App) createBaseDialOpts() []grpc.DialOption {
return opts
}

func (a *App) printMsg(targetName string, m proto.Message) {
func (a *App) printProtoMsg(targetName string, m proto.Message) {
if !a.Config.PrintProto {
return
}
Expand Down
110 changes: 69 additions & 41 deletions app/certCanGenerateCSR.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import (
"github.com/karimra/gnoic/api"
gcert "github.com/karimra/gnoic/api/cert"
"github.com/olekukonko/tablewriter"
"github.com/openconfig/gnoi/cert"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc/metadata"
)

type certCGCSRResponse struct {
TargetError
can bool
rsp *cert.CanGenerateCSRResponse
}

func (r *certCGCSRResponse) Target() string {
return r.TargetName
}

func (r *certCGCSRResponse) Response() any {
return r.rsp
}

func (a *App) InitCertCanGenerateCSRFlags(cmd *cobra.Command) {
Expand All @@ -42,38 +50,13 @@ func (a *App) RunECertCanGenerateCSR(cmd *cobra.Command, args []string) error {

a.wg.Add(numTargets)
for _, t := range targets {
go func(t *api.Target) {
defer a.wg.Done()
ctx, cancel := context.WithCancel(a.ctx)
defer cancel()
ctx = metadata.AppendToOutgoingContext(ctx, "username", *t.Config.Username, "password", *t.Config.Password)

err = t.CreateGrpcClient(ctx, a.createBaseDialOpts()...)
if err != nil {
responseChan <- &certCGCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Address,
Err: err,
},
}
return
}
defer t.Close()
can, err := a.CertCanGenerateCSR(ctx, t)
responseChan <- &certCGCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Address,
Err: err,
},
can: can,
}
}(t)
go a.certCanGenerateCSRRequest(cmd.Context(), t, responseChan)
}
a.wg.Wait()
close(responseChan)

errs := make([]error, 0, numTargets)
result := make([]*certCGCSRResponse, 0, numTargets)
result := make([]TargetResponse, 0, numTargets)
for rsp := range responseChan {
if rsp.Err != nil {
wErr := fmt.Errorf("%q Cert CanGenerateCSR failed: %v", rsp.TargetName, rsp.Err)
Expand All @@ -82,45 +65,90 @@ func (a *App) RunECertCanGenerateCSR(cmd *cobra.Command, args []string) error {
continue
}
result = append(result, rsp)
a.printProtoMsg(rsp.TargetName, rsp.rsp)
}
fmt.Print(certCGCSRTable(result))
a.printCMDOutput(result, a.certCGCSRTable)
return a.handleErrs(errs)
}

func (a *App) CertCanGenerateCSR(ctx context.Context, t *api.Target) (bool, error) {
func (a *App) certCanGenerateCSRRequest(ctx context.Context, t *api.Target, rspCh chan<- *certCGCSRResponse) {
defer a.wg.Done()
ctx = t.AppendMetadata(ctx)
ctx, cancel := context.WithCancel(ctx)
defer cancel()

err := t.CreateGrpcClient(ctx, a.createBaseDialOpts()...)
if err != nil {
rspCh <- &certCGCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Address,
Err: err,
},
}
return
}
defer t.Close()
rspCh <- a.certCanGenerateCSR(ctx, t)
}

func (a *App) certCanGenerateCSR(ctx context.Context, t *api.Target) *certCGCSRResponse {
req, err := gcert.NewCertCanGenerateCSRRequest(
gcert.CertificateType(a.Config.CertCanGenerateCSRCertificateType),
gcert.KeyType(a.Config.CertCanGenerateCSRKeyType),
gcert.KeySize(a.Config.CertCanGenerateCSRKeySize),
)
if err != nil {
return false, err
return &certCGCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Name,
Err: err,
},
}
}

a.printMsg(t.Config.Name, req)
a.printProtoMsg(t.Config.Name, req)
certClient := t.CertClient()
resp, err := certClient.CanGenerateCSR(ctx, req)
if err != nil {
return false, err
return &certCGCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Name,
Err: err,
},
}
}

a.printMsg(t.Config.Name, resp)
a.printProtoMsg(t.Config.Name, resp)
a.Logger.Infof("%q key-type=%s, cert-type=%s, key-size=%d: can_generate: %v",
t.Config.Address,
a.Config.CertCanGenerateCSRKeyType,
a.Config.CertCanGenerateCSRCertificateType,
a.Config.CertCanGenerateCSRKeySize,
resp.GetCanGenerate())
return resp.GetCanGenerate(), nil
return &certCGCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Name,
Err: err,
},
rsp: resp,
}
}

func certCGCSRTable(rsps []*certCGCSRResponse) string {
func (a *App) certCGCSRTable(rsps []TargetResponse) string {
tabData := make([][]string, 0, len(rsps))
sort.Slice(rsps, func(i, j int) bool {
return rsps[i].Target() < rsps[j].Target()
})
for _, rsp := range rsps {
tabData = append(tabData, []string{
rsp.TargetName,
fmt.Sprintf("%t", rsp.can),
})
switch r := rsp.Response().(type) {
case *cert.CanGenerateCSRResponse:
tabData = append(tabData, []string{
rsp.Target(),
fmt.Sprintf("%t", r.GetCanGenerate()),
})
default:
a.Logger.Printf("%s: unexpected message type: %T", rsp.Target(), rsp.Response())
}
}
sort.Slice(tabData, func(i, j int) bool {
return tabData[i][0] < tabData[j][0]
Expand Down
59 changes: 30 additions & 29 deletions app/certGenerateCSR.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/openconfig/gnoi/cert"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc/metadata"
)

type certGenCSRResponse struct {
Expand All @@ -22,7 +21,6 @@ type certGenCSRResponse struct {
func (a *App) InitCertGenerateCSRFlags(cmd *cobra.Command) {
cmd.ResetFlags()
//
//
cmd.Flags().StringVar(&a.Config.CertGenerateCSRCertificateID, "id", "", "Certificate ID")
cmd.Flags().StringVar(&a.Config.CertGenerateCSRKeyType, "key-type", "KT_RSA", "Key Type")
cmd.Flags().StringVar(&a.Config.CertGenerateCSRCertificateType, "cert-type", "CT_X509", "Certificate Type")
Expand Down Expand Up @@ -51,31 +49,7 @@ func (a *App) RunEGenerateCSR(cmd *cobra.Command, args []string) error {
responseChan := make(chan *certGenCSRResponse, numTargets)
a.wg.Add(numTargets)
for _, t := range targets {
go func(t *api.Target) {
defer a.wg.Done()
ctx, cancel := context.WithCancel(a.ctx)
defer cancel()
ctx = metadata.AppendToOutgoingContext(ctx, "username", *t.Config.Username, "password", *t.Config.Password)
err = t.CreateGrpcClient(ctx, a.createBaseDialOpts()...)
if err != nil {
responseChan <- &certGenCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Address,
Err: err,
},
}
return
}
defer t.Close()
rsp, err := a.CertGenerateCSR(ctx, t)
responseChan <- &certGenCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Address,
Err: err,
},
rsp: rsp,
}
}(t)
go a.certGenerateCSRRequest(cmd.Context(), t, responseChan)
}
a.wg.Wait()
close(responseChan)
Expand Down Expand Up @@ -121,13 +95,13 @@ func (a *App) CertGenerateCSR(ctx context.Context, t *api.Target) (*cert.Generat
if err != nil {
return nil, err
}
a.printMsg(t.Config.Name, req)
a.printProtoMsg(t.Config.Name, req)
certClient := t.CertClient()
resp, err := certClient.GenerateCSR(ctx, req)
if err != nil {
return nil, err
}
a.printMsg(t.Config.Name, resp)
a.printProtoMsg(t.Config.Name, resp)
return resp, nil
}

Expand All @@ -154,3 +128,30 @@ func (a *App) saveCSR(rsp *certGenCSRResponse) error {
}
return nil
}

func (a *App) certGenerateCSRRequest(ctx context.Context, t *api.Target, rspCh chan<- *certGenCSRResponse) {
defer a.wg.Done()
ctx = t.AppendMetadata(ctx)
ctx, cancel := context.WithCancel(ctx)
defer cancel()

err := t.CreateGrpcClient(ctx, a.createBaseDialOpts()...)
if err != nil {
rspCh <- &certGenCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Address,
Err: err,
},
}
return
}
defer t.Close()
rsp, err := a.CertGenerateCSR(ctx, t)
rspCh <- &certGenCSRResponse{
TargetError: TargetError{
TargetName: t.Config.Address,
Err: err,
},
rsp: rsp,
}
}
Loading