Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:SP-1653-self-signed-certificates-grpc-gateway #79

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions pkg/grpc/gateway/gateway.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: MIT

Check failure on line 1 in pkg/grpc/gateway/gateway.go

View workflow job for this annotation

GitHub Actions / build

: # github.com/scanoss/go-grpc-helper/pkg/grpc/gateway [github.com/scanoss/go-grpc-helper/pkg/grpc/gateway.test]
/*
* Copyright (c) 2023, SCANOSS
*
Expand All @@ -24,6 +24,7 @@
package gateway

import (
"crypto/tls"
"fmt"
"net/http"
"strings"
Expand All @@ -39,15 +40,18 @@
)

// SetupGateway configures.
func SetupGateway(grpcPort, httpPort, tlsCertFile string, allowedIPs, deniedIPs []string,
blockByDefault, trustProxy, startTLS bool) (*http.Server, *runtime.ServeMux, string, []grpc.DialOption, error) {
func SetupGateway(grpcPort, httpPort, tlsCertFile, tlsKeyFile string, allowedIPs, deniedIPs []string,
blockByDefault, trustProxy, startTLS bool, insecureSkipVerify bool) (*http.Server, *runtime.ServeMux, string, []grpc.DialOption, error) {
httpPort = utils.SetupPort(httpPort)
mux := runtime.NewServeMux()
srv := &http.Server{
Addr: httpPort,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
Handler: mux,
TLSConfig: &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
},
}
if len(allowedIPs) > 0 || len(deniedIPs) > 0 { // Configure the list of allowed/denied IPs to connect
zlog.S.Debugf("Filtering requests by allowed: %v, denied: %v, block-by-default: %v, trust-proxy: %v",
Expand All @@ -58,13 +62,29 @@
srv.Handler = handler // assign the filtered handler
}
var opts []grpc.DialOption

if startTLS {
creds, err := credentials.NewClientTLSFromFile(tlsCertFile, "")
var cred credentials.TransportCredentials
var err error
cred, err = credentials.NewClientTLSFromFile(tlsCertFile, "")
if err != nil {
zlog.S.Errorf("Problem loading TLS file: %s - %v", tlsCertFile, err)
return nil, nil, "", nil, fmt.Errorf("failed to load TLS credentials from file")
}
opts = []grpc.DialOption{grpc.WithTransportCredentials(creds)}

if insecureSkipVerify == true {
cert, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile)
if err != nil {
return nil, nil, "", nil, fmt.Errorf("failed to load TLS certificate and key: %v", err)
}
// Create custom TLS config that skips hostname validation
config := &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{cert},
}
cred = credentials.NewTLS(config)
}
opts = []grpc.DialOption{grpc.WithTransportCredentials(cred)}
} else {
opts = []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
}
Expand Down
Loading