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

Added env based support for HTTPS connection #4706

Merged
merged 25 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0a19e54
Added env based support for HTTPS connection
Saranya-jena Jun 13, 2024
76345b2
Merge branch 'master' into encryption-fixes
Saranya-jena Jun 13, 2024
330ec52
resolved review comments
Saranya-jena Jun 21, 2024
336665c
Merge branch 'encryption-fixes' of https://github.com/Saranya-jena/li…
Saranya-jena Jun 21, 2024
149c387
Merge branch 'master' into encryption-fixes
namkyu1999 Jun 24, 2024
a9736c7
updated logic
Saranya-jena Jun 24, 2024
1d6eb99
Merge branch 'encryption-fixes' of https://github.com/Saranya-jena/li…
Saranya-jena Jun 24, 2024
710cb04
fixed importd
Saranya-jena Jun 24, 2024
f999601
added helper files
Saranya-jena Jun 24, 2024
13ec01c
resolved comments
Saranya-jena Jun 24, 2024
e9f6c01
resolved comments
Saranya-jena Jun 24, 2024
f32a433
Update push.yml
Saranya-jena Jul 2, 2024
12acd13
minor changes
Saranya-jena Jul 2, 2024
2979e72
Merge branch 'encryption-fixes' of https://github.com/Saranya-jena/li…
Saranya-jena Jul 2, 2024
708c787
minor changes
Saranya-jena Jul 2, 2024
187a959
minor changes
Saranya-jena Jul 2, 2024
9eadda5
minor changes
Saranya-jena Jul 2, 2024
70b280c
minor changes
Saranya-jena Jul 2, 2024
14796a4
Update push.yml
Saranya-jena Jul 2, 2024
53228c0
updated branch
Saranya-jena Jul 3, 2024
cc8f811
Merge branch 'encryption-fixes' of https://github.com/Saranya-jena/li…
Saranya-jena Jul 3, 2024
0cedfc6
updated branch
Saranya-jena Jul 8, 2024
f479270
updated oush.yam
Saranya-jena Jul 8, 2024
2240e1e
Merge branch 'master' of https://github.com/litmuschaos/litmus into e…
Saranya-jena Jul 8, 2024
427c364
updated manifest
Saranya-jena Jul 8, 2024
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
65 changes: 61 additions & 4 deletions chaoscenter/authentication/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"flag"
"fmt"
"google.golang.org/grpc/credentials"
"net"
"runtime"
"strconv"
"time"

grpcHandler "github.com/litmuschaos/litmus/chaoscenter/authentication/api/handlers/grpc"
Expand Down Expand Up @@ -114,7 +116,21 @@ func main() {

validatedAdminSetup(applicationService)

go runGrpcServer(applicationService)
enableHTTPSConnection, err := strconv.ParseBool(utils.EnableHTTPSConnection)
if err != nil {
log.Errorf("unable to parse boolean value %v", err)
}

if enableHTTPSConnection {
if utils.CustomTlsCert != "" && utils.TlSKey != "" {
go runGrpcServerWithTLS(applicationService)
} else {
log.Fatalf("Failure to start chaoscenter authentication REST server due to empty TLS cert file path and TLS key path")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Fatalf("Failure to start chaoscenter authentication REST server due to empty TLS cert file path and TLS key path")
log.Fatalf("Failure to start chaoscenter authentication GRPC server due to empty TLS cert file path and TLS key path")

}
} else {
go runGrpcServer(applicationService)
}

runRestServer(applicationService)
}

Expand Down Expand Up @@ -172,10 +188,27 @@ 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)
}

if enableHTTPSConnection {
if utils.CustomTlsCert != "" && utils.TlSKey != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can rename it wrt paths

log.Infof("Listening and serving HTTPS on %s", utils.Port)
err := app.RunTLS(utils.Port, utils.CustomTlsCert, utils.TlSKey)
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")
}
} else {
log.Infof("Listening and serving HTTP on %s", utils.Port)
err := app.Run(utils.Port)
if err != nil {
log.Fatalf("Failure to start litmus-portal authentication REST server due to %v", err)
}
}
}

Expand All @@ -195,3 +228,27 @@ 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.GrpcPort)
if err != nil {
log.Fatalf("Failure to start litmus-portal authentication server due to %s", err)
}

// Load TLS credentials
creds, err := credentials.NewServerTLSFromFile(utils.CustomTlsCert, utils.TlSKey)
if err != nil {
log.Fatalf("failed to create credentials: %v", err)
}

grpcApplicationServer := grpcHandler.ServerGrpc{ApplicationService: applicationService}
grpcServer := grpc.NewServer(grpc.Creds(creds))
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.Fatalf("Failure to start litmus-portal authentication GRPC server due to %v", err)
log.Fatalf("Failure to start chaos-center authentication GRPC server due to %v", err)

}
}
3 changes: 3 additions & 0 deletions chaoscenter/authentication/pkg/utils/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var (
DexClientID = os.Getenv("DEX_OAUTH_CLIENT_ID")
DexClientSecret = os.Getenv("DEX_OAUTH_CLIENT_SECRET")
DexOIDCIssuer = os.Getenv("OIDC_ISSUER")
EnableHTTPSConnection = os.Getenv("ENABLE_HTTPS_CONNECTION")
CustomTlsCert = os.Getenv("CUSTOM_TLS_CERT")
TlSKey = os.Getenv("TLS_KEY")
DBName = "auth"
Port = ":3000"
GrpcPort = ":3030"
Expand Down
30 changes: 28 additions & 2 deletions chaoscenter/graphql/server/pkg/grpc/auth_grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package grpc
import (
"context"
"errors"
"google.golang.org/grpc/credentials"
"strconv"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/protos"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
Expand All @@ -13,10 +15,34 @@ import (

// GetAuthGRPCSvcClient returns an RPC client for Authentication service
func GetAuthGRPCSvcClient(conn *grpc.ClientConn) (protos.AuthRpcServiceClient, *grpc.ClientConn) {
conn, err := grpc.Dial(utils.Config.LitmusAuthGrpcEndpoint+utils.Config.LitmusAuthGrpcPort, grpc.WithBlock(), grpc.WithInsecure())

enableHTTPSConnection, err := strconv.ParseBool(utils.Config.EnableHTTPSConnection)
if err != nil {
logrus.Fatalf("did not connect: %s", err)
logrus.Errorf("unable to parse boolean value %v", err)
}

if enableHTTPSConnection {
if utils.Config.CustomTlsCert != "" {
// Create tls based credential.
creds, err := credentials.NewClientTLSFromFile(utils.Config.CustomTlsCert, "")
if err != nil {
logrus.Fatalf("failed to load credentials: %v", err)
}
// Set up a connection to the server.
conn, err = grpc.NewClient(utils.Config.LitmusAuthGrpcEndpoint+utils.Config.LitmusAuthGrpcPort, grpc.WithTransportCredentials(creds))
if err != nil {
logrus.Fatalf("did not connect: %v", err)
}
} else {
logrus.Fatalf("Failure to start chaoscenter authentication REST server due to empty TLS cert file path and TLS key path")
}
} else {
conn, err = grpc.Dial(utils.Config.LitmusAuthGrpcEndpoint+utils.Config.LitmusAuthGrpcPort, grpc.WithBlock(), grpc.WithInsecure())
if err != nil {
logrus.Fatalf("did not connect: %s", err)
}
}

return protos.NewAuthRpcServiceClient(conn), conn
}

Expand Down
51 changes: 48 additions & 3 deletions chaoscenter/graphql/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"google.golang.org/grpc/credentials"
"strconv"

"github.com/gin-contrib/cors"
Expand Down Expand Up @@ -106,7 +107,21 @@ func main() {
if err := validateVersion(); err != nil {
log.Fatal(err)
}
go startGRPCServer(utils.Config.RpcPort, mongodbOperator) // start GRPC serve

enableHTTPSConnection, err := strconv.ParseBool(utils.Config.EnableHTTPSConnection)
if err != nil {
logrus.Errorf("unable to parse boolean value %v", err)
}

if enableHTTPSConnection {
if utils.Config.CustomTlsCert != "" && utils.Config.TlSKey != "" {
go startGRPCServer(utils.Config.RpcPort, mongodbOperator) // start GRPC serve
} else {
log.Fatalf("Failure to start chaoscenter authentication REST server due to empty TLS cert file path and TLS key path")
}
} else {
go startGRPCServerWithTLS(utils.Config.RpcPort, mongodbOperator) // start GRPC serve
}

srv := handler.New(generated.NewExecutableSchema(graph.NewConfig(mongodbOperator)))
srv.AddTransport(transport.POST{})
Expand Down Expand Up @@ -148,8 +163,15 @@ 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)
if enableHTTPSConnection {
if utils.Config.CustomTlsCert != "" && utils.Config.TlSKey != "" {
log.Fatal(http.ListenAndServeTLS(":"+utils.Config.HttpPort, utils.Config.CustomTlsCert, utils.Config.TlSKey, router))
}
} else {
log.Fatal(http.ListenAndServe(":"+utils.Config.HttpPort, router))
}

}

// startGRPCServer initializes, registers services to and starts the gRPC server for RPC calls
Expand All @@ -168,3 +190,26 @@ 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", ":"+port)
if err != nil {
log.Fatal("failed to listen: %w", err)
}

// Load TLS credentials
creds, err := credentials.NewServerTLSFromFile(utils.Config.CustomTlsCert, utils.Config.TlSKey)
if err != nil {
log.Fatalf("failed to create credentials: %v", err)
}

grpcServer := grpc.NewServer(grpc.Creds(creds))

// Register services

pb.RegisterProjectServer(grpcServer, &projects.ProjectServer{Operator: mongodbOperator})

log.Infof("GRPC server listening on %v", lis.Addr())
log.Fatal(grpcServer.Serve(lis))
}
3 changes: 3 additions & 0 deletions chaoscenter/graphql/server/utils/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type Configuration struct {
CustomChaosHubPath string `split_words:"true" default:"/tmp/"`
DefaultChaosHubPath string `split_words:"true" default:"/tmp/default/"`
EnableGQLIntrospection string `split_words:"true" default:"false"`
EnableHTTPSConnection string `required:"true" split_words:"true" default:"false"`
CustomTlsCert string `split_words:"true"`
TlSKey string `split_words:"true"`
}

var Config Configuration
Loading