Skip to content

Commit

Permalink
feat: Add grpc over tls support (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
askolesov committed May 20, 2022
1 parent faa7a47 commit 323d835
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func serve() {
e.Use(middleware.Recover())

// Services
ledgerService := services.NewLedgerService(config.Ledger.Timeout)
ledgerService := services.NewLedgerService(config.Ledger.Timeout, config.Ledger.UseTls)

networks := strings.Split(config.Ledger.Networks, ";")
for _, network := range networks {
Expand Down
3 changes: 2 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ledger:
networks: "mainnet=grpc.seed1.ap.cheqd.net:9090;testnet=159.89.208.88:443"
networks: "mainnet=grpc.cheqd.net:443;testnet=grpc.cheqd.network:443"
useTls: true
timeout: "5s"

resolver:
Expand Down
14 changes: 13 additions & 1 deletion services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package services

import (
"context"
"crypto/tls"
"errors"
"fmt"
"time"

"google.golang.org/grpc/credentials"

cheqd "github.com/cheqd/cheqd-node/x/cheqd/types"
cheqdUtils "github.com/cheqd/cheqd-node/x/cheqd/utils"
"github.com/rs/zerolog/log"
Expand All @@ -21,11 +24,13 @@ type LedgerServiceI interface {
type LedgerService struct {
ledgers map[string]string // namespace -> url
connectionTimeout time.Duration
useTls bool
}

func NewLedgerService(connectionTimeout time.Duration) LedgerService {
func NewLedgerService(connectionTimeout time.Duration, useTls bool) LedgerService {
ls := LedgerService{
connectionTimeout: connectionTimeout,
useTls: useTls,
}
ls.ledgers = make(map[string]string)
return ls
Expand Down Expand Up @@ -83,6 +88,13 @@ func (ls LedgerService) openGRPCConnection(addr string) (conn *grpc.ClientConn,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}

if ls.useTls {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

ctx, cancel := context.WithTimeout(context.Background(), ls.connectionTimeout)
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion services/ledger_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestQueryDIDDoc(t *testing.T) {
timeout, err := time.ParseDuration("5s")
require.NoError(t, err)

ledgerService := NewLedgerService(timeout)
ledgerService := NewLedgerService(timeout, false)
didDoc, metadata, isFound, err := ledgerService.QueryDIDDoc("fake did")
require.EqualValues(t, subtest.expectedDID, didDoc)
require.EqualValues(t, subtest.expectedMetadata, metadata)
Expand Down
1 change: 1 addition & 0 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
type LedgerConfig struct {
Networks string
Timeout time.Duration
UseTls bool
}

type ResolverConfig struct {
Expand Down

0 comments on commit 323d835

Please sign in to comment.