-
Notifications
You must be signed in to change notification settings - Fork 699
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
Added env based support for HTTPS connection #4706
Changes from 8 commits
0a19e54
76345b2
330ec52
336665c
149c387
a9736c7
1d6eb99
710cb04
f999601
13ec01c
e9f6c01
f32a433
12acd13
2979e72
708c787
187a959
9eadda5
70b280c
14796a4
53228c0
cc8f811
0cedfc6
f479270
2240e1e
427c364
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,9 +4,13 @@ import ( | |||||
"flag" | ||||||
"fmt" | ||||||
"net" | ||||||
"net/http" | ||||||
"runtime" | ||||||
"strconv" | ||||||
"time" | ||||||
|
||||||
"google.golang.org/grpc/credentials" | ||||||
|
||||||
grpcHandler "github.com/litmuschaos/litmus/chaoscenter/authentication/api/handlers/grpc" | ||||||
grpcPresenter "github.com/litmuschaos/litmus/chaoscenter/authentication/api/presenter/protos" | ||||||
"github.com/litmuschaos/litmus/chaoscenter/authentication/api/routes" | ||||||
|
@@ -114,7 +118,20 @@ func main() { | |||||
|
||||||
validatedAdminSetup(applicationService) | ||||||
|
||||||
enableHTTPSConnection, err := strconv.ParseBool(utils.EnableHTTPSConnection) | ||||||
if err != nil { | ||||||
log.Errorf("unable to parse boolean value %v", err) | ||||||
} | ||||||
|
||||||
go runGrpcServer(applicationService) | ||||||
if enableHTTPSConnection { | ||||||
if utils.CustomTlsCertPath != "" && utils.TlSKeyPath != "" { | ||||||
go runGrpcServerWithTLS(applicationService) | ||||||
} else { | ||||||
log.Fatalf("Failure to start chaoscenter authentication GRPC server due to empty TLS cert file path and TLS key path") | ||||||
} | ||||||
} | ||||||
|
||||||
runRestServer(applicationService) | ||||||
} | ||||||
|
||||||
|
@@ -172,10 +189,35 @@ func runRestServer(applicationService services.ApplicationService) { | |||||
routes.ProjectRouter(app, applicationService) | ||||||
routes.CapabilitiesRouter(app) | ||||||
|
||||||
log.Infof("Listening and serving HTTP on %s", utils.Port) | ||||||
err := app.Run(utils.Port) | ||||||
enableHTTPSConnection, err := strconv.ParseBool(utils.EnableHTTPSConnection) | ||||||
if err != nil { | ||||||
log.Fatalf("Failure to start litmus-portal authentication REST server due to %v", err) | ||||||
log.Errorf("unable to parse boolean value %v", err) | ||||||
} | ||||||
|
||||||
log.Infof("Listening and serving HTTP on %s", utils.Port) | ||||||
go func() { | ||||||
err = app.Run(utils.Port) | ||||||
if err != nil { | ||||||
log.Fatalf("Failure to start litmus-portal authentication REST server due to %v", err) | ||||||
} | ||||||
}() | ||||||
if enableHTTPSConnection { | ||||||
if utils.CustomTlsCertPath != "" && utils.TlSKeyPath != "" { | ||||||
conf := utils.GetTlsConfig() | ||||||
|
||||||
server := http.Server{ | ||||||
Addr: utils.PortHttps, | ||||||
Handler: app, | ||||||
TLSConfig: conf, | ||||||
} | ||||||
log.Infof("Listening and serving HTTPS on %s", utils.Port) | ||||||
err = server.ListenAndServeTLS("", "") | ||||||
if err != nil { | ||||||
log.Fatalf("Failure to start litmus-portal authentication REST server due to %v", err) | ||||||
} | ||||||
} else { | ||||||
log.Fatalf("Failure to start chaoscenter authentication REST server due to empty TLS cert file path and TLS key path") | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -195,3 +237,31 @@ func runGrpcServer(applicationService services.ApplicationService) { | |||||
log.Fatalf("Failure to start litmus-portal authentication GRPC server due to %v", err) | ||||||
} | ||||||
} | ||||||
|
||||||
func runGrpcServerWithTLS(applicationService services.ApplicationService) { | ||||||
|
||||||
// Starting gRPC server | ||||||
lis, err := net.Listen("tcp", utils.GrpcPortHttps) | ||||||
if err != nil { | ||||||
log.Fatalf("Failure to start litmus-portal authentication server due to %s", err) | ||||||
} | ||||||
|
||||||
// configuration of the certificate what we want | ||||||
conf := utils.GetTlsConfig() | ||||||
|
||||||
// create tls credentials | ||||||
tlsCredentials := credentials.NewTLS(conf) | ||||||
|
||||||
// create grpc server with tls credential | ||||||
grpcServer := grpc.NewServer(grpc.Creds(tlsCredentials)) | ||||||
|
||||||
grpcApplicationServer := grpcHandler.ServerGrpc{ApplicationService: applicationService} | ||||||
|
||||||
grpcPresenter.RegisterAuthRpcServiceServer(grpcServer, &grpcApplicationServer) | ||||||
|
||||||
log.Infof("Listening and serving gRPC on %s with TLS", utils.GrpcPort) | ||||||
err = grpcServer.Serve(lis) | ||||||
if err != nil { | ||||||
log.Fatalf("Failure to start litmus-portal authentication GRPC server due to %v", err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package authorization | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/grpc" | ||
|
||
|
@@ -20,6 +21,7 @@ func ValidateRole(ctx context.Context, projectID string, | |
requiredRoles, | ||
invitation) | ||
if err != nil { | ||
fmt.Println("errrrrrrrrrrrr ", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed or print it with logrus and make it |
||
return errors.New("permission_denied") | ||
} | ||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package main | |
import ( | ||
"strconv" | ||
|
||
"google.golang.org/grpc/credentials" | ||
|
||
"github.com/gin-contrib/cors" | ||
"github.com/gin-gonic/gin" | ||
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/api/middleware" | ||
|
@@ -106,7 +108,20 @@ func main() { | |
if err := validateVersion(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
enableHTTPSConnection, err := strconv.ParseBool(utils.Config.EnableHTTPSConnection) | ||
if err != nil { | ||
logrus.Errorf("unable to parse boolean value %v", err) | ||
} | ||
|
||
go startGRPCServer(utils.Config.RpcPort, mongodbOperator) // start GRPC serve | ||
if enableHTTPSConnection { | ||
if utils.Config.ServerTlsCertPath != "" && utils.Config.ServerTlsKeyPath != "" { | ||
go startGRPCServerWithTLS("8001", mongodbOperator) // start GRPC serve | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Port is hardcoded |
||
} else { | ||
log.Fatalf("Failure to start chaoscenter authentication REST server due to empty TLS cert file path and TLS key path") | ||
} | ||
} | ||
|
||
srv := handler.New(generated.NewExecutableSchema(graph.NewConfig(mongodbOperator))) | ||
srv.AddTransport(transport.POST{}) | ||
|
@@ -148,8 +163,28 @@ func main() { | |
projectEventChannel := make(chan string) | ||
go projects.ProjectEvents(projectEventChannel, mongodb.MgoClient, mongodbOperator) | ||
|
||
log.Infof("chaos manager running at http://localhost:%s", utils.Config.HttpPort) | ||
log.Fatal(http.ListenAndServe(":"+utils.Config.HttpPort, router)) | ||
log.Infof("graphql server running at http://localhost:%s", utils.Config.HttpPort) | ||
go func() { | ||
err := http.ListenAndServe(":"+utils.Config.HttpPort, router) | ||
if err != nil { | ||
logrus.Fatal(err) | ||
} | ||
}() | ||
if enableHTTPSConnection { | ||
// configuration of the certificate what we want | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need log here as well same as above
|
||
conf := utils.GetTlsConfig(utils.Config.ServerTlsCertPath, utils.Config.ServerTlsKeyPath, true) | ||
|
||
server := http.Server{ | ||
Addr: ":" + utils.Config.HttpsPort, | ||
Handler: router, | ||
TLSConfig: conf, | ||
} | ||
if utils.Config.ServerTlsCertPath != "" && utils.Config.ServerTlsKeyPath != "" { | ||
log.Fatal(server.ListenAndServeTLS("", "")) | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
// startGRPCServer initializes, registers services to and starts the gRPC server for RPC calls | ||
|
@@ -168,3 +203,28 @@ func startGRPCServer(port string, mongodbOperator mongodb.MongoOperator) { | |
log.Infof("GRPC server listening on %v", lis.Addr()) | ||
log.Fatal(grpcServer.Serve(lis)) | ||
} | ||
|
||
// startGRPCServerWithTLS initializes, registers services to and starts the gRPC server for RPC calls | ||
func startGRPCServerWithTLS(port string, mongodbOperator mongodb.MongoOperator) { | ||
|
||
lis, err := net.Listen("tcp", ":"+utils.Config.RpcPortHttps) | ||
if err != nil { | ||
log.Fatal("failed to listen: %w", err) | ||
} | ||
|
||
// configuration of the certificate what we want | ||
conf := utils.GetTlsConfig(utils.Config.ServerTlsCertPath, utils.Config.ServerTlsKeyPath, true) | ||
|
||
// create tls credentials | ||
tlsCredentials := credentials.NewTLS(conf) | ||
|
||
// create grpc server with tls credential | ||
grpcServer := grpc.NewServer(grpc.Creds(tlsCredentials)) | ||
|
||
// Register services | ||
|
||
pb.RegisterProjectServer(grpcServer, &projects.ProjectServer{Operator: mongodbOperator}) | ||
|
||
log.Infof("GRPC server listening on %v", lis.Addr()) | ||
log.Fatal(grpcServer.Serve(lis)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func GetTlsConfig(certPath string, keyPath string, isServerConfig bool) *tls.Config { | ||
|
||
// read ca's cert, verify to client's certificate | ||
caPem, err := os.ReadFile(Config.CaCertPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// create cert pool and append ca's cert | ||
certPool := x509.NewCertPool() | ||
if !certPool.AppendCertsFromPEM(caPem) { | ||
log.Fatal(err) | ||
} | ||
|
||
// read server cert & key | ||
cert, err := tls.LoadX509KeyPair(certPath, keyPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
config := &tls.Config{ | ||
Certificates: []tls.Certificate{cert}, | ||
RootCAs: certPool, | ||
} | ||
|
||
if isServerConfig { | ||
// configuration of the certificate what we want to | ||
conf := &tls.Config{ | ||
Certificates: []tls.Certificate{cert}, | ||
ClientAuth: tls.RequireAndVerifyClientCert, | ||
ClientCAs: certPool, | ||
} | ||
return conf | ||
} | ||
|
||
return config | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.