Skip to content

Commit

Permalink
grpc-client: distinguish no tls or tls: insecure skip verify
Browse files Browse the repository at this point in the history
  • Loading branch information
sfwn committed Dec 13, 2023
1 parent 3a92b81 commit fb45e3c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
11 changes: 11 additions & 0 deletions providers/grpcclient/examples/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
examples:

# insecure_skip_verify: true
grpc-client:
addr: ai-proxy-grpc.erda.cloud:443
tls:
insecure_skip_verify: true

# notls (default)
#grpc-client:
# addr: localhost:8082
55 changes: 55 additions & 0 deletions providers/grpcclient/examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"fmt"
"os"

"github.com/erda-project/erda-infra/base/servicehub"
"github.com/erda-project/erda-infra/providers/grpcclient"
_ "github.com/erda-project/erda-infra/providers/grpcclient"
)

type provider struct {
GRPCClient grpcclient.Interface `optional:"false"`
}

func (p *provider) Run(ctx context.Context) error {
fmt.Println(p.GRPCClient.Get().Target())
fmt.Println(p.GRPCClient.Get().GetState())
return nil
}

func (p *provider) Init(ctx servicehub.Context) error {
return nil
}

func init() {
servicehub.Register("examples", &servicehub.Spec{
Services: []string{"hello"},
Dependencies: []string{"grpc-client"},
Description: "hello for example",
Creator: func() servicehub.Provider {
return &provider{}
},
})
}

func main() {
hub := servicehub.New()
hub.Run("examples", "", os.Args...)
}
19 changes: 16 additions & 3 deletions providers/grpcclient/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ package grpcclient

import (
"context"
"crypto/tls"
"fmt"
"reflect"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-infra/base/servicehub"
grpccontext "github.com/erda-project/erda-infra/pkg/trace/inject/context/grpc"
transgrpc "github.com/erda-project/erda-infra/pkg/transport/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

// Interface .
Expand All @@ -44,6 +46,7 @@ type config struct {
TLS struct {
ServerNameOverride string `file:"cert_file" desc:"the server name used to verify the hostname returned by the TLS handshake"`
CAFile string `file:"ca_file" desc:"the file containing the CA root cert file"`
InsecureSkipVerify bool `file:"insecure_skip_verify" desc:"skip verify"`
} `file:"tls"`
Singleton bool `file:"singleton" default:"true" desc:"one client instance"`
Block bool `file:"block" default:"true" desc:"block until the connection is up"`
Expand All @@ -66,7 +69,17 @@ func (p *provider) Init(ctx servicehub.Context) error {
}
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
// distinguish `no tls` or `tls: insecure skip verify`
notls := true // default no tls, compatible with old config
if p.Cfg.TLS.InsecureSkipVerify {
notls = false
}
if notls {
opts = append(opts, grpc.WithInsecure())
} else {
insecureSkipVerifyTLS := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})
opts = append(opts, grpc.WithTransportCredentials(insecureSkipVerifyTLS))
}
}
if p.Cfg.TraceEnable {
opts = append(opts,
Expand Down

0 comments on commit fb45e3c

Please sign in to comment.