Skip to content

Commit

Permalink
Adding hash password from json and creating local user (#6753)
Browse files Browse the repository at this point in the history
* Adding hash password from json and creating local user

Signed-off-by: Yashvi Jain <Yashvi.jain@progress.com>

* Lint issues fixed

Signed-off-by: Yashvi Jain <Yashvi.jain@progress.com>
  • Loading branch information
YashviJain01 authored and vinay033 committed May 9, 2022
1 parent 4b797d9 commit 70a7b68
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 29 deletions.
4 changes: 4 additions & 0 deletions api/external/infra_proxy/migrations/migrations.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 22 additions & 10 deletions api/external/infra_proxy/migrations/response/migrations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/external/infra_proxy/migrations/response/migrations.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ message User {
bool is_conflicting=9;
// user is admin or not
bool is_admin=10;
//Local user hashed password
string hash_password=11;
}

message ConfirmPreview {
Expand Down
31 changes: 21 additions & 10 deletions api/interservice/infra_proxy/migrations/response/migrations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ message User {
bool is_conflicting=9;
// user is admin or not
bool is_admin=10;
//Local User hash password
string hash_password=11;
}

message ConfirmPreview {
Expand Down
4 changes: 4 additions & 0 deletions components/automate-gateway/api/migrations.pb.swagger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func getStagedUser(user *infra_res.User) *gwres.User {
stagedUser.Connector = user.Connector
stagedUser.IsConflicting = user.IsConflicting
stagedUser.IsAdmin = user.IsAdmin
stagedUser.HashPassword = user.HashPassword
return stagedUser
}

Expand Down
1 change: 1 addition & 0 deletions components/infra-proxy-service/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func getStagedUser(user pipeline_model.User) *response.User {
stagedUser.AutomateUsername = user.AutomateUsername
stagedUser.Connector = user.Connector
stagedUser.IsConflicting = user.IsConflicting
stagedUser.HashPassword = user.HashPassword
return stagedUser
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func TestMigrations(t *testing.T) {
ctx := context.Background()
_, serviceRef, _, close, _, _ := test.SetupInfraProxyService(ctx, t)
infraMigrationMockClient := infra_migrations.NewMockInfraProxyMigrationClient(gomock.NewController(t))
infraMigrationMockClient := infra_migrations.NewMockInfraProxyMigrationServiceClient(gomock.NewController(t))

var migrationID = "Fake id"

Expand Down
52 changes: 46 additions & 6 deletions components/infra-proxy-service/migrations/pipeline/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,15 +597,17 @@ func keyDumpTOUser(keyDump []pipeline.KeyDump) []pipeline.User {
log.Errorf("failed to pasre user's first, middle and last name: %s", err.Error())
}
user := &pipeline.User{
Username: kd.Username,
Email: kd.Email,
DisplayName: sec["display_name"],
FirstName: sec["first_name"],
LastName: sec["last_name"],
MiddleName: sec["middle_name"],
Username: kd.Username,
Email: kd.Email,
DisplayName: sec["display_name"],
FirstName: sec["first_name"],
LastName: sec["last_name"],
MiddleName: sec["middle_name"],
HashPassword: kd.HashedPassword,
}
user.SetConnector(kd.ExternalAuthenticationUID)
user.SetAutomateUsername(kd.ExternalAuthenticationUID)

users = append(users, *user)
}
return users
Expand Down Expand Up @@ -697,3 +699,41 @@ func checkUserExist(ctx context.Context, localUserClient local_user.UsersMgmtSer
}
return true
}

//createLocalUser Function for reference, will be removed after PopulateUsers Method
func createLocalUsers(ctx context.Context, localUserClient local_user.UsersMgmtServiceClient, result pipeline.Result) (pipeline.Result, error) {
log.Info("Starting with creating local users in automate for migration id: ", result.Meta.MigrationID)
var totalSucceeded, totalSkipped, totalFailed int64
var err error
for _, user := range result.ParsedResult.Users {
if user.Connector == pipeline.Local && user.ActionOps == pipeline.Insert && !user.IsConflicting {
err = createLocalUser(ctx, localUserClient, user)
if err != nil {
totalFailed++
continue
}
}
if user.ActionOps == pipeline.Skip {
totalSkipped++
continue
}
totalSucceeded++
}
log.Info("Starting with creating local users in automate for migration id: ", result.Meta.MigrationID)
return result, err
}

func createLocalUser(ctx context.Context, localUserClient local_user.UsersMgmtServiceClient, user pipeline.User) error {
_, err := localUserClient.CreateUser(ctx, &local_user.CreateUserReq{
Name: user.DisplayName,
Id: user.AutomateUsername,
Password: user.HashPassword,
Email: user.AutomateUsername,
IsHashed: true,
})
if err != nil {
log.Errorf("Unable to create user in Automate for user : %s with error %s", user.AutomateUsername, err.Error())
return err
}
return nil
}
28 changes: 28 additions & 0 deletions components/infra-proxy-service/migrations/pipeline/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,31 @@ func TestUserExists(t *testing.T) {

}
}

func TestCreateNewUserInAutomate(t *testing.T) {
type args struct {
ctx context.Context
localUserClient *local_user.MockUsersMgmtServiceClient
User pipeline.User
MockResult *local_user.User
ErrorWant error
}
tests := []struct {
name string
args args
want1 error
}{
{name: "Test New User", args: args{ctx: context.Background(), localUserClient: local_user.NewMockUsersMgmtServiceClient(gomock.NewController(t)), User: pipeline.User{AutomateUsername: "test", Username: "test", HashPassword: "okokokokokokoko"}, MockResult: &local_user.User{Name: "test", Id: "test", Email: "test@ok"}, ErrorWant: nil}, want1: nil},
{name: "Test Already User Exists", args: args{ctx: context.Background(), localUserClient: local_user.NewMockUsersMgmtServiceClient(gomock.NewController(t)), User: pipeline.User{AutomateUsername: "test", Username: "test", HashPassword: "okokokokokokoko"}, MockResult: nil, ErrorWant: errors.New("User already exists")}, want1: errors.New("User already exists")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.localUserClient.EXPECT().CreateUser(tt.args.ctx, gomock.Any()).Return(tt.args.MockResult, tt.args.ErrorWant)
got := createLocalUser(tt.args.ctx, tt.args.localUserClient, tt.args.User)
if got != nil && got.Error() != tt.want1.Error() {
t.Errorf("createLocalUser() got = %v, want %v", got, tt.want1)
}
})
}

}
17 changes: 16 additions & 1 deletion components/infra-proxy-service/pipeline/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type KeyDump struct {
ExternalAuthenticationUID interface{} `json:"external_authentication_uid"`
RecoveryAuthenticationEnabled interface{} `json:"recovery_authentication_enabled"`
Admin bool `json:"admin"`
HashedPassword interface{} `json:"hashed_password"`
HashedPassword string `json:"hashed_password"`
Salt interface{} `json:"salt"`
HashType interface{} `json:"hash_type"`
}
Expand All @@ -109,6 +109,9 @@ type Org struct {

// ActionOps for Insert Skip Update and Delete
ActionOps ActionOps `json:"action_ops"`

//Counts for total,skipped and failed
Counts Counts `json:"counts"`
}

type User struct {
Expand All @@ -128,8 +131,14 @@ type User struct {
// IsConflicting for user's existence in db
IsConflicting bool `json:"is_conflicting"`

//hash password for the local user
HashPassword string `json:"hash_password"`

// ActionOps for Insert Skip Update and Delete
ActionOps ActionOps `json:"action_ops"`

//Counts for total,skipped and failed
Counts Counts `json:"counts"`
}

type OrgJson struct {
Expand All @@ -150,3 +159,9 @@ type AdminsJson struct {
type UsersJson struct {
Username string `json:"username"`
}

type Counts struct {
Succeeded int `json:"total_succeeded"`
Failed int `json:"total_failed"`
Skipped int `json:"total_skipped"`
}
2 changes: 1 addition & 1 deletion components/infra-proxy-service/pipeline/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func (usr *User) SetAutomateUsername(uid interface{}) {
if uid == nil {
usr.AutomateUsername = usr.Username
} else {
usr.Connector = uid.(string)
usr.AutomateUsername = uid.(string)
}
}

0 comments on commit 70a7b68

Please sign in to comment.