Files
certmanager/pkg/client/control.go
Олег Бородин e9d4d1ef07 import sources
2024-07-30 09:49:53 +02:00

59 lines
1.3 KiB
Go

package client
import (
"context"
"crypto/tls"
"fmt"
"time"
"certmanager/api/certmanagercontrol"
//"certmanager/internal/config"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const (
DefaultWrpcPort int = 20107
DefaultGrpcPort int = 20108
)
type Access struct {
Hostname string
Port int
Username string
Password string
}
func NewClient(access *Access) (certmanagercontrol.ControlClient, error) {
var err error
var cli certmanagercontrol.ControlClient
if access.Port == 0 {
access.Port = DefaultGrpcPort
}
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
const dialTimeout time.Duration = 5 * time.Second
const idleTimeout time.Duration = 10 * 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 cli, fmt.Errorf("Dial error: %v", err)
}
cli = certmanagercontrol.NewControlClient(conn)
return cli, err
}