52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"time"
|
|
|
|
"certmanager/pkg/cmctl"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
)
|
|
|
|
const (
|
|
DefaultWrpcPort int = 20121
|
|
DefaultGrpcPort int = 20122
|
|
)
|
|
|
|
type Access struct {
|
|
Hostname string
|
|
Port int
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
func NewClient(access *Access) (cmctl.ControlClient, error) {
|
|
var err error
|
|
var cli cmctl.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{
|
|
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 = cmctl.NewControlClient(conn)
|
|
return cli, err
|
|
}
|