working changes

This commit is contained in:
Олег Бородин
2024-07-30 16:39:01 +02:00
parent e9d4d1ef07
commit f605a01b22
10 changed files with 720 additions and 29 deletions

159
pkg/client/certman.go Normal file
View File

@@ -0,0 +1,159 @@
package client
import (
"context"
"time"
cmapi "certmanager/api/certmanagercontrol"
"certmanager/pkg/auxgrpc"
)
func (cont *Control) CreateIssuerPair(ctx context.Context, param *cmapi.CreateIssuerPairParams) (*cmapi.CreateIssuerPairResult, error) {
var err error
res := &cmapi.CreateIssuerPairResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.CreateIssuerPair(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) ImportIssuerPair(ctx context.Context, param *cmapi.ImportIssuerPairParams) (*cmapi.ImportIssuerPairResult, error) {
var err error
res := &cmapi.ImportIssuerPairResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.ImportIssuerPair(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) RevokeIssuerPair(ctx context.Context, param *cmapi.RevokeIssuerPairParams) (*cmapi.RevokeIssuerPairResult, error) {
var err error
res := &cmapi.RevokeIssuerPairResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.RevokeIssuerPair(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) UnrevokeIssuerPair(ctx context.Context, param *cmapi.UnrevokeIssuerPairParams) (*cmapi.UnrevokeIssuerPairResult, error) {
var err error
res := &cmapi.UnrevokeIssuerPairResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.UnrevokeIssuerPair(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) ListIssuerPairs(ctx context.Context, param *cmapi.ListIssuerPairsParams) (*cmapi.ListIssuerPairsResult, error) {
var err error
res := &cmapi.ListIssuerPairsResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.ListIssuerPairs(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) GetIssuerCertificate(ctx context.Context, param *cmapi.GetIssuerCertificateParams) (*cmapi.GetIssuerCertificateResult, error) {
var err error
res := &cmapi.GetIssuerCertificateResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.GetIssuerCertificate(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) CreateServicePair(ctx context.Context, param *cmapi.CreateServicePairParams) (*cmapi.CreateServicePairResult, error) {
var err error
res := &cmapi.CreateServicePairResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.CreateServicePair(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) RevokeServicePair(ctx context.Context, param *cmapi.RevokeServicePairParams) (*cmapi.RevokeServicePairResult, error) {
var err error
res := &cmapi.RevokeServicePairResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.RevokeServicePair(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) ListServicePairs(ctx context.Context, param *cmapi.ListServicePairsParams) (*cmapi.ListServicePairsResult, error) {
var err error
res := &cmapi.ListServicePairsResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.ListServicePairs(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}
func (cont *Control) GetServicePair(ctx context.Context, param *cmapi.GetServicePairParams) (*cmapi.GetServicePairResult, error) {
var err error
res := &cmapi.GetServicePairResult{}
const timeout time.Duration = 50 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
res, err = cont.client.GetServicePair(ctx, param)
err = auxgrpc.FmtError(err)
if err != nil {
return res, err
}
return res, err
}

View File

@@ -6,16 +6,15 @@ import (
"fmt"
"time"
"certmanager/api/certmanagercontrol"
//"certmanager/internal/config"
cmapi "certmanager/api/certmanagercontrol"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const (
DefaultWrpcPort int = 20107
DefaultGrpcPort int = 20108
DefaultWrpcPort int = 20311
DefaultGrpcPort int = 20312
)
type Access struct {
@@ -25,20 +24,59 @@ type Access struct {
Password string
}
func NewClient(access *Access) (certmanagercontrol.ControlClient, error) {
var err error
var cli certmanagercontrol.ControlClient
if access.Port == 0 {
access.Port = DefaultGrpcPort
}
type Control struct {
conn *grpc.ClientConn
client cmapi.ControlClient
}
func NewControl(access *Access) (*Control, error) {
var err error
cont := &Control{}
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
const dialTimeout time.Duration = 5 * time.Second
const idleTimeout time.Duration = 10 * time.Second
const dialTimeout time.Duration = 1 * time.Second
const idleTimeout time.Duration = 5 * time.Second
authCred := NewAuthCredential(access.Username, access.Password)
dialOpts := []grpc.DialOption{
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
grpc.WithPerRPCCredentials(authCred),
grpc.WithBlock(),
grpc.WithIdleTimeout(idleTimeout),
}
address := fmt.Sprintf("%s:%d", access.Hostname, access.Port)
ctx, _ := context.WithTimeout(context.Background(), dialTimeout)
conn, err := grpc.DialContext(ctx, address, dialOpts...)
if err != nil {
return cont, fmt.Errorf("Dial error: %v", err)
}
cont.conn = conn
cont.client = cmapi.NewControlClient(conn)
if cont.client == nil {
return cont, fmt.Errorf("Nil control client")
}
return cont, err
}
func (cont *Control) Close() {
if cont.client != nil {
cont.conn.Close()
}
}
func NewClient(access *Access) (cmapi.ControlClient, error) {
var err error
var cli cmapi.ControlClient
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
const dialTimeout time.Duration = 1 * time.Second
const idleTimeout time.Duration = 5 * time.Second
authCred := NewAuthCredential(access.Username, access.Password)
dialOpts := []grpc.DialOption{
@@ -53,6 +91,6 @@ func NewClient(access *Access) (certmanagercontrol.ControlClient, error) {
if err != nil {
return cli, fmt.Errorf("Dial error: %v", err)
}
cli = certmanagercontrol.NewControlClient(conn)
cli = cmapi.NewControlClient(conn)
return cli, err
}