Skip to content

Commit

Permalink
Merge pull request #533 from anandrgitnirman/dyna_pricing
Browse files Browse the repository at this point in the history
Support Secure connections from Daemon
  • Loading branch information
anandrgitnirman authored Feb 16, 2021
2 parents b604cd9 + 263c3e2 commit f110ca9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
11 changes: 11 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ func TestValidateEndpoints(t *testing.T) {
assert.Equal(t, nil, err)
err = ValidateEndpoints("1.2.3.4:8080", "")
assert.Equal(t, "passthrough_endpoint is the endpoint of your AI service in the daemon config and needs to be a valid url.", err.Error())
err = ValidateEndpoints("0.0.0.0:7000", "http://localhost:8080")
assert.Equal(t, nil, err)
err = ValidateEndpoints("0.0.0.0:7000", "http://localhost:8080")
assert.Equal(t, nil, err)
err = ValidateEndpoints("0.0.0.0:7000", "localhost:8080")
assert.Equal(t, "passthrough_endpoint is the endpoint of your AI service in the daemon config and needs to be a valid url.", err.Error())
err = ValidateEndpoints("0.0.0.0:7000", "http://somedomain")
assert.Equal(t, nil, err)
err = ValidateEndpoints("0.0.0.0:7000", "https://somedomain:8093")
assert.Nil(t, err)

}

func TestAllowedUserChecks(t *testing.T) {
Expand Down
16 changes: 13 additions & 3 deletions handler/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"github.com/singnet/snet-daemon/blockchain"
"google.golang.org/grpc/credentials"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -53,10 +54,19 @@ func NewGrpcHandler(serviceMetadata *blockchain.ServiceMetadata) grpc.StreamHand
if err != nil {
log.WithError(err).Panic("error parsing passthrough endpoint")
}
var conn *grpc.ClientConn
if strings.Compare(passthroughURL.Scheme, "https") == 0 {
conn, err = grpc.Dial(passthroughURL.Host,
grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), h.options)
if err != nil {
log.WithError(err).Panic("error dialing service")
}
} else {
conn, err = grpc.Dial(passthroughURL.Host, grpc.WithInsecure(), h.options)

conn, err := grpc.Dial(passthroughURL.Host, grpc.WithInsecure(), h.options)
if err != nil {
log.WithError(err).Panic("error dialing service")
if err != nil {
log.WithError(err).Panic("error dialing service")
}
}
h.grpcConn = conn
return h.grpcToGRPC
Expand Down
15 changes: 15 additions & 0 deletions handler/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package handler
import (
"context"
"net"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -73,3 +74,17 @@ func (suite *GrpcTestSuite) TestReturnCustomErrorCodeViaGrpc() {

assert.Equal(suite.T(), err, expectedErr)
}

func (suite *GrpcTestSuite) TestPassThroughEndPoint() {
passthroughURL, err := url.Parse("http://localhost:8080")
assert.Equal(suite.T(), passthroughURL.Scheme, "http")
assert.Nil(suite.T(), err)
passthroughURL, err = url.Parse("https://localhost:8080")
assert.Equal(suite.T(), passthroughURL.Scheme, "https")
passthroughURL, err = url.Parse("localhost:8080")
assert.NotEqual(suite.T(), passthroughURL.Scheme, "https")
passthroughURL, err = url.Parse("0.0.0.0:7000")
assert.NotNil(suite.T(), err)
passthroughURL, err = url.Parse("http://somedomain")
assert.Equal(suite.T(), passthroughURL.Scheme, "http")
}

0 comments on commit f110ca9

Please sign in to comment.