Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Skip auth when options empty
Browse files Browse the repository at this point in the history
  • Loading branch information
BirknerAlex authored and msniveau committed Feb 7, 2023
1 parent 39141c8 commit 03188be
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions pkg/gpcloud/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"crypto/tls"
"fmt"
"log"

"buf.build/gen/go/gportal/gportal-cloud/grpc/go/gpcloud/api/auth/v1/authv1grpc"
"buf.build/gen/go/gportal/gportal-cloud/grpc/go/gpcloud/api/cloud/v1/cloudv1grpc"
Expand All @@ -21,8 +20,6 @@ type Client struct {
grpcClient *grpc.ClientConn
}

type EndpointOverrideOption string

// CloudClient Returns the CloudServiceClient
func (c *Client) CloudClient() cloudv1grpc.CloudServiceClient {
return cloudv1grpc.NewCloudServiceClient(c.grpcClient)
Expand All @@ -49,38 +46,29 @@ func (c *Client) PaymentClient() paymentv1grpc.PaymentServiceClient {
}

// NewClient Returns a new GRPC client
func NewClient(extraOptions ...interface{}) (*Client, error) {
func NewClient(authOptions AuthOptions, options ...grpc.DialOption) (*Client, error) {
cl := &Client{}

var options []grpc.DialOption
// Certificate pinning
options = append(options, grpc.WithTransportCredentials(credentials.NewTLS(getTLSOptions())))

// User Agent
options = append(options, grpc.WithUserAgent(fmt.Sprintf("GPCloud Golang Client [%s]", Version)))

endpoint := DefaultEndpoint
authenticationDefined := false
for _, option := range extraOptions {
if opt, ok := option.(grpc.DialOption); ok {
options = append(options, opt)
continue
}
if opt, ok := option.(EndpointOverrideOption); ok {
endpoint = string(opt)
continue
}
if opt, ok := option.(AuthProviderOption); ok && !authenticationDefined {
log.Printf("Using auth provider: %T", opt)
options = append(options, grpc.WithPerRPCCredentials(&AuthOption{
Provider: &opt,
}))
authenticationDefined = true
continue
// Default auth loader
if authOptions.ClientID != "" &&
authOptions.ClientSecret != "" &&
authOptions.Username != "" &&
authOptions.Password != "" {
auth, err := NewAuth(&authOptions)
if err != nil {
return nil, err
}
// Access Token
options = append(options, grpc.WithPerRPCCredentials(auth))
}

clientConn, err := grpc.Dial(endpoint, options...)
clientConn, err := grpc.Dial(DefaultEndpoint, options...)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 03188be

Please sign in to comment.