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: Added enhancement in authentication server #4024

Merged
merged 10 commits into from Jun 29, 2023

Conversation

SarthakJain26
Copy link
Contributor

Proposed changes

  • Refactored authentication module to accommodate 3.0.0 UX changes.

Types of changes

What types of changes does your code introduce to Litmus? Put an x in the boxes that apply

  • New feature (non-breaking change which adds functionality)
  • Bugfix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices applies)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have signed the commit for DCO to be passed.
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have added necessary documentation (if appropriate)

Dependency

  • Please add the links to the dependent PR need to be merged before this (if any).

Special notes for your reviewer:

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>
Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>
Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>
Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>
Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>
Copy link
Contributor

@Saranya-jena Saranya-jena left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the PR 🚀


func (s *ServerGrpc) GetUserById(ctx context.Context,
inputRequest *protos.GetUserByIdRequest) (*protos.GetUserByIdResponse, error) {
fmt.Println("req", inputRequest.UserID)
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 remove this

log.Error(err)
return nil, err
}
fmt.Println("Resp", user)
Copy link
Contributor

Choose a reason for hiding this comment

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

this also

Comment on lines 111 to 113
if err != nil {
return
}
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 remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes @Jonsy13 thanks for pointing out, I will remove this

projects, err := service.GetProjectsByUserID(outputUser.ID, false)
if err != nil {
log.Error(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add error for "project not found" just like we have for "user not found" in line 37?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Jonsy13 the error here will not necessarily be project not found, it can be also some other error coming from the mongo side.

}
authUsers, err := service.FindUsersByUID(uids)
if err != nil || authUsers == nil {
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we return some error here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thanks for pointing out

Comment on lines 308 to 329
// Extracting user role
//role := c.MustGet("role").(string)
//
//var conn *grpc.ClientConn
//client, conn := utils.GetProjectGRPCSvcClient(conn)
//err = utils.ProjectInitializer(c, client, pID, role)
//
//defer func(conn *grpc.ClientConn) {
// err := conn.Close()
// if err != nil {
// log.Errorf("could not close gRPC client connection: %v", err)
// c.JSON(500, "could not close gRPC client connection")
// return
// }
//}(conn)
//
//if err != nil {
// log.Errorf("could not initialize project %v: %v", userRequest.ProjectName, err)
// c.JSON(500, "could not initialize project")
// return
//}

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it required?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is not required as of now

return
}
c.JSON(200, gin.H{
"message": "password has been reset",
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
"message": "password has been reset",
"message": "password has been updated successfully",

We have different handler for reset below this function

Comment on lines 98 to 102
for configName, configValue := range configs {
if configValue == "" {
log.Fatalf("Config %s has not been set", configName)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be required if we already have all these fields as required in envconfig in line 29. envConfig itself will give error if they are not provided being required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes good catch! looks like duplicate check.

if err == utils.ErrUserExists {
log.Println("Admin already exists in the database, not creating a new admin")
}
if err != nil && err != utils.ErrUserExists {
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
if err != nil && err != utils.ErrUserExists {
else if err != nil {

We already check equality in line 126. So again checking inequality is not required, it won't be equal for sure.

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 server due to %s", 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 server due to %s", err)
log.Fatalf("Failure to start litmus-portal authentication REST server due to %s", err)

// Starting gRPC server
lis, err := net.Listen("tcp", utils.GrpcPort)
if err != nil {
log.Fatalf("Failure to start litmus-portal authentication server due"+
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 server due"+
log.Fatalf("Failure to start litmus-portal authentication GRPC server due"+

log.Infof("Listening and serving gRPC on %s", utils.GrpcPort)
err = grpcServer.Serve(lis)
if err != nil {
log.Fatalf("Failure to start litmus-portal authentication server due"+
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 server due"+
log.Fatalf("Failure to start litmus-portal authentication GRPC server due"+

}
}

log.Info(collectionName + "'s mongo collection created")
Copy link
Contributor

Choose a reason for hiding this comment

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

I saw in some places we used fmt.print as well. if we have log, then it's better to use the same

Comment on lines +8 to +34
var (
JwtSecret = os.Getenv("JWT_SECRET")
AdminName = os.Getenv("ADMIN_USERNAME")
AdminPassword = os.Getenv("ADMIN_PASSWORD")
DBUrl = os.Getenv("DB_SERVER")
DBUser = os.Getenv("DB_USER")
DBPassword = os.Getenv("DB_PASSWORD")
JWTExpiryDuration = getEnvAsInt("JWT_EXPIRY_MINS", 1440)
OAuthJWTExpDuration = getEnvAsInt("OAUTH_JWT_EXP_MINS", 5)
OAuthJwtSecret = os.Getenv("OAUTH_SECRET")
StrictPasswordPolicy = getEnvAsBool("STRICT_PASSWORD_POLICY", false)
DexEnabled = getEnvAsBool("DEX_ENABLED", false)
DexCallBackURL = os.Getenv("DEX_OAUTH_CALLBACK_URL")
DexClientID = os.Getenv("DEX_OAUTH_CLIENT_ID")
DexClientSecret = os.Getenv("DEX_OAUTH_CLIENT_SECRET")
DexOIDCIssuer = os.Getenv("OIDC_ISSUER")
DBName = "auth"
Port = ":3000"
GrpcPort = ":3030"
UserCollection = "users"
ProjectCollection = "project"
UsernameField = "username"
PasswordEncryptionCost = 15
DefaultLitmusGqlGrpcEndpoint = "localhost"
DefaultLitmusGqlGrpcPort = ":8000"
)

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 use envConfig for values we are taking as envs

Copy link
Contributor

@Jonsy13 Jonsy13 Jun 29, 2023

Choose a reason for hiding this comment

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

We can take it later as discussed.

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>
@Jonsy13 Jonsy13 merged commit 2e98b19 into litmuschaos:master Jun 29, 2023
16 checks passed
SohamRatnaparkhi pushed a commit to SohamRatnaparkhi/litmus that referenced this pull request Jun 30, 2023
* base commit to add dockerfile

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Added  utility functions

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Added schemas

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Added presenters

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Added pkg, handlers and roles

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Code optimisation

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

---------

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>
Co-authored-by: Saranya Jena <saranya.jena@harness.io>
SohamRatnaparkhi pushed a commit to SohamRatnaparkhi/litmus that referenced this pull request Jun 30, 2023
* base commit to add dockerfile

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Added  utility functions

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Added schemas

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Added presenters

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Added pkg, handlers and roles

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

* Code optimisation

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>

---------

Signed-off-by: Sarthak Jain <sarthak.jain@harness.io>
Co-authored-by: Saranya Jena <saranya.jena@harness.io>
Signed-off-by: SohamRatnaparkhi <soham.ratnaparkhi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants