Skip to content

Commit

Permalink
PSUPCLPL-17184. Add monitor_main privileges for dml and ro users
Browse files Browse the repository at this point in the history
  • Loading branch information
alty1224 committed Dec 26, 2024
1 parent b684ecc commit 1f4fae3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions basic/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (bp BaseProvider) CreateRoleWithDMLPermissions() error {
ClusterScrollClearPermission,
ClusterMonitorTaskGetPermission,
ClusterMonitorStatePermission,
ClusterMonitorMainPermission,
}
return bp.createRole(clusterPermissions, indexPermissions, []string{}, DmlRoleType)
}
Expand All @@ -161,6 +162,7 @@ func (bp BaseProvider) CreateRoleWithReadOnlyPermissions() error {
strings.ToUpper(ClusterReadOnlyPermissions),
ClusterScrollClearPermission,
ClusterMonitorStatePermission,
ClusterMonitorMainPermission,
}
return bp.createRole(clusterPermissions, indexPermissions, []string{}, ReadOnlyRoleType)
}
Expand Down
13 changes: 7 additions & 6 deletions basic/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,21 @@ func (bp BaseProvider) CreateUserHandler() func(w http.ResponseWriter, r *http.R
}

func (bp BaseProvider) ensureUser(username string, userCreateRequest dao.UserCreateRequest, ctx context.Context) (*CreatedUser, error) {
indexName := userCreateRequest.DbName
dbName := userCreateRequest.DbName
roleType := userCreateRequest.Role
if roleType == "" {
roleType = AdminRoleType
}
username, password, resources, err :=
bp.createOrUpdateUser(username, userCreateRequest.Password, indexName, roleType, ctx)
bp.createOrUpdateUser(username, userCreateRequest.Password, dbName, roleType, ctx)
if err != nil {
return nil, err
}
if indexName != "" {
resources = append(resources, dao.DbResource{Kind: common.IndexKind, Name: indexName})
if dbName != "" {
resources = append(resources, dao.DbResource{Kind: common.MetadataKind, Name: dbName})
resources = append(resources, dao.DbResource{Kind: common.ResourcePrefixKind, Name: dbName})
}
connectionProperties := bp.GetExtendedConnectionProperties(indexName, username, password, "", roleType)
connectionProperties := bp.GetExtendedConnectionProperties("", username, password, "", roleType)
user, err := bp.GetUser(username)
if err != nil {
return nil, err
Expand All @@ -125,7 +126,7 @@ func (bp BaseProvider) ensureUser(username string, userCreateRequest dao.UserCre

response := &CreatedUser{
ConnectionProperties: connectionProperties,
Name: indexName,
Name: dbName,
Resources: resources,
}
return response, nil
Expand Down
22 changes: 12 additions & 10 deletions basic/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package basic

import (
"fmt"
"github.com/Netcracker/dbaas-opensearch-adapter/common"
"github.com/Netcracker/qubership-dbaas-adapter-core/pkg/dao"
"github.com/stretchr/testify/assert"
Expand All @@ -32,15 +31,16 @@ func TestUpdateUserWithDbNameAndPassword(t *testing.T) {
assert.Empty(t, err)
assert.Equal(t, username, response.ConnectionProperties.ResourcePrefix)
assert.Equal(t, userCreateRequest.DbName, response.Name)
assert.Equal(t, userCreateRequest.DbName, response.ConnectionProperties.DbName)
expectedUrl := fmt.Sprintf("http://localhost:9200/%s", userCreateRequest.DbName)
assert.Equal(t, "", response.ConnectionProperties.DbName)
expectedUrl := "http://localhost:9200/"
assert.Equal(t, expectedUrl, response.ConnectionProperties.Url)
assert.Equal(t, username, response.ConnectionProperties.Username)
assert.Equal(t, userCreateRequest.Password, response.ConnectionProperties.Password)
assert.Equal(t, AdminRoleType, response.ConnectionProperties.Role)
expectedResources := []dao.DbResource{
{Kind: common.UserKind, Name: username},
{Kind: common.IndexKind, Name: userCreateRequest.DbName},
{Kind: common.MetadataKind, Name: userCreateRequest.DbName},
{Kind: common.ResourcePrefixKind, Name: userCreateRequest.DbName},
}
assert.ElementsMatch(t, expectedResources, response.Resources)
}
Expand All @@ -54,15 +54,16 @@ func TestCreateUserWithoutUsername(t *testing.T) {
assert.Empty(t, err)
assert.Empty(t, response.ConnectionProperties.ResourcePrefix)
assert.Equal(t, userCreateRequest.DbName, response.Name)
assert.Equal(t, userCreateRequest.DbName, response.ConnectionProperties.DbName)
expectedUrl := fmt.Sprintf("http://localhost:9200/%s", userCreateRequest.DbName)
assert.Equal(t, "", response.ConnectionProperties.DbName)
expectedUrl := "http://localhost:9200/"
assert.Equal(t, expectedUrl, response.ConnectionProperties.Url)
assert.Contains(t, response.ConnectionProperties.Username, "dbaas_")
assert.Equal(t, userCreateRequest.Password, response.ConnectionProperties.Password)
assert.Equal(t, AdminRoleType, response.ConnectionProperties.Role)
expectedResources := []dao.DbResource{
{Kind: common.UserKind, Name: response.ConnectionProperties.Username},
{Kind: common.IndexKind, Name: userCreateRequest.DbName},
{Kind: common.MetadataKind, Name: userCreateRequest.DbName},
{Kind: common.ResourcePrefixKind, Name: userCreateRequest.DbName},
}
assert.ElementsMatch(t, expectedResources, response.Resources)
}
Expand All @@ -76,15 +77,16 @@ func TestUpdateUserWithDbName(t *testing.T) {
assert.Empty(t, err)
assert.Equal(t, username, response.ConnectionProperties.ResourcePrefix)
assert.Equal(t, userCreateRequest.DbName, response.Name)
assert.Equal(t, userCreateRequest.DbName, response.ConnectionProperties.DbName)
expectedUrl := fmt.Sprintf("http://localhost:9200/%s", userCreateRequest.DbName)
assert.Equal(t, "", response.ConnectionProperties.DbName)
expectedUrl := "http://localhost:9200/"
assert.Equal(t, expectedUrl, response.ConnectionProperties.Url)
assert.Equal(t, username, response.ConnectionProperties.Username)
assert.NotEmpty(t, response.ConnectionProperties.Password)
assert.Equal(t, AdminRoleType, response.ConnectionProperties.Role)
expectedResources := []dao.DbResource{
{Kind: common.UserKind, Name: username},
{Kind: common.IndexKind, Name: userCreateRequest.DbName},
{Kind: common.MetadataKind, Name: userCreateRequest.DbName},
{Kind: common.ResourcePrefixKind, Name: userCreateRequest.DbName},
}
assert.ElementsMatch(t, expectedResources, response.Resources)
}
Expand Down

0 comments on commit 1f4fae3

Please sign in to comment.