import sources
This commit is contained in:
27
pkg/client/auth.go
Normal file
27
pkg/client/auth.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type AuthCredential struct {
|
||||
Payload map[string]string
|
||||
}
|
||||
|
||||
func NewAuthCredential(username, password string) *AuthCredential {
|
||||
payload := make(map[string]string)
|
||||
payload["username"] = username
|
||||
payload["password"] = password
|
||||
return &AuthCredential{
|
||||
Payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func (cred *AuthCredential) GetRequestMetadata(ctx context.Context, data ...string) (map[string]string, error) {
|
||||
var err error
|
||||
return cred.Payload, err
|
||||
}
|
||||
|
||||
func (cred *AuthCredential) RequireTransportSecurity() bool {
|
||||
return false
|
||||
}
|
||||
58
pkg/client/control.go
Normal file
58
pkg/client/control.go
Normal file
@@ -0,0 +1,58 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user