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 8 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
76 changes: 73 additions & 3 deletions chaoscenter/authentication/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
}
}
}

Expand All @@ -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
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
// configuration of the certificate what we want
// configuring TLS config based on provided certificates & keys

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)
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)

}
}
40 changes: 40 additions & 0 deletions chaoscenter/authentication/pkg/utils/configs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package utils

import (
"crypto/tls"
"crypto/x509"
"os"
"strconv"

log "github.com/sirupsen/logrus"
)

var (
Expand All @@ -21,9 +25,15 @@ 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")
CustomTlsCertPath = os.Getenv("CUSTOM_TLS_CERT_PATH")
TlSKeyPath = os.Getenv("TLS_KEY_PATH")
CaCertPath = os.Getenv("CA_CERT_PATH")
DBName = "auth"
Port = ":3000"
PortHttps = ":3001"
GrpcPort = ":3030"
GrpcPortHttps = ":3031"
UserCollection = "users"
ProjectCollection = "project"
RevokedTokenCollection = "revoked-token"
Expand All @@ -33,6 +43,7 @@ var (
PasswordEncryptionCost = 15
DefaultLitmusGqlGrpcEndpoint = "localhost"
DefaultLitmusGqlGrpcPort = ":8000"
//DefaultLitmusGqlGrpcPortHttps = ":8001" // enable when in use
)

func getEnvAsInt(name string, defaultVal int) int {
Expand All @@ -50,3 +61,32 @@ func getEnvAsBool(name string, defaultVal bool) bool {
}
return defaultVal
}

func GetTlsConfig() *tls.Config {

// read ca's cert, verify to client's certificate
caPem, err := os.ReadFile(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
serverCert, err := tls.LoadX509KeyPair(CustomTlsCertPath, TlSKeyPath)
if err != nil {
log.Fatal(err)
}

// configuration of the certificate what we want to
conf := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: certPool,
}
return conf
}
2 changes: 2 additions & 0 deletions chaoscenter/graphql/server/pkg/authorization/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package authorization
import (
"context"
"errors"
"fmt"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/grpc"

Expand All @@ -20,6 +21,7 @@ func ValidateRole(ctx context.Context, projectID string,
requiredRoles,
invitation)
if err != nil {
fmt.Println("errrrrrrrrrrrr ", err)
Copy link
Contributor

@Jonsy13 Jonsy13 Jun 24, 2024

Choose a reason for hiding this comment

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

This can be removed or print it with logrus and make it error level?

return errors.New("permission_denied")
}
return nil
Expand Down
31 changes: 29 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,9 @@ package grpc
import (
"context"
"errors"
"strconv"

"google.golang.org/grpc/credentials"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/protos"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils"
Expand All @@ -13,10 +16,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.ServerTlsCertPath != "" {
// configuration of the certificate what we want
conf := utils.GetTlsConfig(utils.Config.ClientTlsCertPath, utils.Config.ClientTlsKeyPath, false)

tlsCredential := credentials.NewTLS(conf)

// Set up a connection to the server.
conn, err = grpc.NewClient(utils.Config.LitmusAuthGrpcEndpoint+utils.Config.LitmusAuthGrpcPortHttps, grpc.WithTransportCredentials(tlsCredential))
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
64 changes: 62 additions & 2 deletions chaoscenter/graphql/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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{})
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need log here as well same as above

log.Infof("graphql server running at https://localhost:%s", utils.Config.HttpsPort)

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
Expand All @@ -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))
}
47 changes: 47 additions & 0 deletions chaoscenter/graphql/server/utils/config.go
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
}
Loading
Loading