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

feat: Improving the env vars management in the graphql-server #3868

Merged
merged 6 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions litmus-portal/graphql-server/graph/cluster.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
clusterHandler "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster/handler"
data_store "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/data-store"
dbOperationsCluster "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/cluster"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/handlers"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
)
Expand Down Expand Up @@ -95,7 +94,7 @@ func (r *queryResolver) GetManifest(ctx context.Context, projectID string, clust
return "", err
}

response, err := handlers.GetManifestWithClusterID(clusterID, accessKey)
response, err := cluster.GetManifestWithClusterID(clusterID, accessKey)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion litmus-portal/graphql-server/pkg/analytics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type PromSeries struct {
DSdetails *PromDSDetails
}

//Portal Dashboard Types
// Portal Dashboard Types
type PortalDashboard struct {
DashboardID string `json:"dashboardID"`
Name string `json:"name"`
Expand Down
12 changes: 5 additions & 7 deletions litmus-portal/graphql-server/pkg/authorization/user_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import (
"errors"
"fmt"
"log"
"os"

"github.com/golang-jwt/jwt"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
)

var secret = os.Getenv("JWT_SECRET")

// UserValidateJWT validates the cluster jwt
func UserValidateJWT(token string) (jwt.MapClaims, error) {
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if ok := token.Method.Alg() == jwt.SigningMethodHS512.Alg(); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(secret), nil
return []byte(utils.Config.JwtSecret), nil
})

if err != nil {
Expand All @@ -40,18 +38,18 @@ func UserValidateJWT(token string) (jwt.MapClaims, error) {
// GetUsername returns the username from the jwt token
func GetUsername(token string) (string, error) {
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
return []byte(utils.Config.JwtSecret), nil
})

if err != nil {
log.Print("USER JWT ERROR: ", err)
return "", errors.New("Invalid Token")
return "", errors.New("invalid Token")
}

claims, ok := tkn.Claims.(jwt.MapClaims)
if ok {
return claims["username"].(string), nil
}

return "", errors.New("Invalid Token")
return "", errors.New("invalid Token")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -239,7 +238,7 @@ func SendWorkflowToSubscriber(workflow *model.ChaosWorkFlowRequest, username *st
workflowNamespace := gjson.Get(workflow.WorkflowManifest, "metadata.namespace").String()

if workflowNamespace == "" {
workflowNamespace = os.Getenv("AGENT_NAMESPACE")
workflowNamespace = utils.Config.AgentNamespace
}
clusterHandler.SendRequestToSubscriber(clusterOps.SubscriberRequests{
K8sManifest: workflow.WorkflowManifest,
Expand Down
8 changes: 3 additions & 5 deletions litmus-portal/graphql-server/pkg/cluster/cluster_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ package cluster
import (
"errors"
"fmt"
"os"

"github.com/golang-jwt/jwt"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
)

var secret = os.Getenv("JWT_SECRET")

// ClusterCreateJWT generates jwt used in cluster registration
func ClusterCreateJWT(id string) (string, error) {
claims := jwt.MapClaims{}
claims["cluster_id"] = id
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

tokenString, err := token.SignedString([]byte(secret))
tokenString, err := token.SignedString([]byte(utils.Config.JwtSecret))
if err != nil {
return "", err
}
Expand All @@ -30,7 +28,7 @@ func ClusterValidateJWT(token string) (string, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(secret), nil
return []byte(utils.Config.JwtSecret), nil
})

if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions litmus-portal/graphql-server/pkg/cluster/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/handlers"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"

"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/authorization"

Expand All @@ -30,7 +29,7 @@ import (

// RegisterCluster creates an entry for a new cluster in DB and generates the url used to apply manifest
func RegisterCluster(request model.RegisterClusterRequest) (*model.RegisterClusterResponse, error) {
endpoint, err := handlers.GetEndpoint(request.ClusterType)
endpoint, err := cluster.GetEndpoint(request.ClusterType)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -104,7 +103,7 @@ func RegisterCluster(request model.RegisterClusterRequest) (*model.RegisterClust

// ConfirmClusterRegistration takes the cluster_id and access_key from the subscriber and validates it, if validated generates and sends new access_key
func ConfirmClusterRegistration(request model.ClusterIdentity, r store.StateData) (*model.ConfirmClusterRegistrationResponse, error) {
currentVersion := os.Getenv("VERSION")
currentVersion := utils.Config.Version
if currentVersion != request.Version {
return nil, fmt.Errorf("ERROR: CLUSTER VERSION MISMATCH (need %v got %v)", currentVersion, request.Version)
}
Expand Down Expand Up @@ -275,7 +274,7 @@ func SendClusterEvent(eventType, eventName, description string, cluster model.Cl

// SendRequestToSubscriber sends events from the graphQL server to the subscribers listening for the requests
func SendRequestToSubscriber(subscriberRequest clusterOps.SubscriberRequests, r store.StateData) {
if os.Getenv("AGENT_SCOPE") == "cluster" {
if utils.Config.AgentScope == "cluster" {
/*
namespace = Obtain from WorkflowManifest or
from frontend as a separate workflowNamespace field under ChaosWorkFlowRequest model
Expand Down