Skip to content
Snippets Groups Projects
Commit 74ccc8db authored by Valera Shaitorov's avatar Valera Shaitorov :alien:
Browse files

Добавлены исправления ошибки в настройках ролей, из-за которой не работали...

Добавлены исправления ошибки в настройках ролей, из-за которой не работали glob-выражения а указании доступа к окружениям
parent fb24acb4
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,6 @@ package auth ...@@ -3,7 +3,6 @@ package auth
import ( import (
"context" "context"
"git.perx.ru/perxis/perxis-go/pkg/data"
"git.perx.ru/perxis/perxis-go/pkg/environments" "git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/members" "git.perx.ru/perxis/perxis-go/pkg/members"
"git.perx.ru/perxis/perxis-go/pkg/permission" "git.perx.ru/perxis/perxis-go/pkg/permission"
...@@ -46,41 +45,5 @@ type OrganizationAccessor interface { ...@@ -46,41 +45,5 @@ type OrganizationAccessor interface {
} }
func hasEnvironmentAccess(ctx context.Context, envsrv environments.Environments, role *roles.Role, envID string) bool { func hasEnvironmentAccess(ctx context.Context, envsrv environments.Environments, role *roles.Role, envID string) bool {
if role == nil || role.SpaceID == "" || envID == "" { return role != nil && role.CanAccessEnvironment(ctx, &environments.Environment{SpaceID: role.SpaceID, ID: envID}, envsrv)
return false
}
if role.AllowManagement {
return true
}
envs := role.Environments
// Если явно не указаны доступные окружения - доступ по умолчанию к окружению master
if len(envs) == 0 {
envs = []string{environments.DefaultEnvironment}
}
for _, ce := range envs {
if envID == ce || data.GlobMatch(envID, ce) {
return true
}
}
e, err := envsrv.Get(WithSystem(ctx), role.SpaceID, envID)
if err != nil || e == nil {
return false
}
aliases := append(e.Aliases, e.ID)
for _, ce := range envs {
for _, al := range aliases {
if al == ce || data.GlobMatch(al, ce) {
return true
}
}
}
return false
} }
...@@ -34,8 +34,8 @@ type Role struct { ...@@ -34,8 +34,8 @@ type Role struct {
AllowManagement bool `json:"allow_management" bson:"allow_management"` AllowManagement bool `json:"allow_management" bson:"allow_management"`
} }
func (r Role) CanAccessEnvironment(ctx context.Context, service environments.Environments, spaceID, envID string) bool { func (r Role) CanAccessEnvironment(ctx context.Context, env *environments.Environment, service environments.Environments) bool {
if spaceID == "" || envID == "" { if env.SpaceID == "" || env.ID == "" {
return false return false
} }
...@@ -48,24 +48,24 @@ func (r Role) CanAccessEnvironment(ctx context.Context, service environments.Env ...@@ -48,24 +48,24 @@ func (r Role) CanAccessEnvironment(ctx context.Context, service environments.Env
r.Environments = []string{environments.DefaultEnvironment} r.Environments = []string{environments.DefaultEnvironment}
} }
for _, e := range r.Environments { // Если окружение передано не полное, это означает, что надо его перезапросить
if envID == e || data.GlobMatch(envID, e) { if env.Description == "" && env.Aliases == nil && env.StateInfo == nil {
if data.GlobMatch(env.ID, r.Environments...) {
return true return true
} }
}
env, err := service.Get(ctx, spaceID, envID) var err error
if err != nil || env == nil { env, err = service.Get(ctx, env.SpaceID, env.ID)
return false if err != nil || env == nil {
return false
}
} }
aliases := append(env.Aliases, env.ID) aliases := append(env.Aliases, env.ID)
for _, e := range r.Environments { for _, a := range aliases {
for _, a := range aliases { if data.GlobMatch(a, r.Environments...) {
if a == e || data.GlobMatch(a, e) { return true
return true
}
} }
} }
......
package auth package roles
import ( import (
"context" "context"
...@@ -6,16 +6,15 @@ import ( ...@@ -6,16 +6,15 @@ import (
"git.perx.ru/perxis/perxis-go/pkg/environments" "git.perx.ru/perxis/perxis-go/pkg/environments"
mocksenvs "git.perx.ru/perxis/perxis-go/pkg/environments/mocks" mocksenvs "git.perx.ru/perxis/perxis-go/pkg/environments/mocks"
"git.perx.ru/perxis/perxis-go/pkg/roles"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
) )
func Test_hasEnvironmentAccess(t *testing.T) { func TestRoleCanAccessEnvironment(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
envscall func(envsservice *mocksenvs.Environments) envscall func(envsservice *mocksenvs.Environments)
role *roles.Role role *Role
envID string env *environments.Environment
} }
tests := []struct { tests := []struct {
name string name string
...@@ -26,13 +25,13 @@ func Test_hasEnvironmentAccess(t *testing.T) { ...@@ -26,13 +25,13 @@ func Test_hasEnvironmentAccess(t *testing.T) {
name: "simple", name: "simple",
args: args{ args: args{
ctx: context.Background(), ctx: context.Background(),
role: &roles.Role{ role: &Role{
ID: "1", ID: "1",
SpaceID: "space", SpaceID: "space",
Description: "Current", Description: "Current",
Environments: []string{"env1", "env2"}, Environments: []string{"env1", "env2"},
}, },
envID: "env1", env: &environments.Environment{ID: "env1", SpaceID: "sp"},
}, },
want: true, want: true,
}, },
...@@ -47,13 +46,13 @@ func Test_hasEnvironmentAccess(t *testing.T) { ...@@ -47,13 +46,13 @@ func Test_hasEnvironmentAccess(t *testing.T) {
Aliases: []string{"master"}, Aliases: []string{"master"},
}, nil).Once() }, nil).Once()
}, },
role: &roles.Role{ role: &Role{
ID: "1", ID: "1",
SpaceID: "space", SpaceID: "space",
Description: "Current", Description: "Current",
Environments: []string{"e*"}, Environments: []string{"e*"},
}, },
envID: "env", env: &environments.Environment{ID: "env", SpaceID: "sp"},
}, },
want: true, want: true,
}, },
...@@ -68,13 +67,13 @@ func Test_hasEnvironmentAccess(t *testing.T) { ...@@ -68,13 +67,13 @@ func Test_hasEnvironmentAccess(t *testing.T) {
Aliases: []string{"master"}, Aliases: []string{"master"},
}, nil).Once() }, nil).Once()
}, },
role: &roles.Role{ role: &Role{
ID: "1", ID: "1",
SpaceID: "space", SpaceID: "space",
Description: "Current", Description: "Current",
Environments: []string{"*n*"}, Environments: []string{"*n*"},
}, },
envID: "env", env: &environments.Environment{ID: "env", SpaceID: "sp"},
}, },
want: true, want: true,
}, },
...@@ -89,13 +88,13 @@ func Test_hasEnvironmentAccess(t *testing.T) { ...@@ -89,13 +88,13 @@ func Test_hasEnvironmentAccess(t *testing.T) {
Aliases: []string{"master"}, Aliases: []string{"master"},
}, nil).Once() }, nil).Once()
}, },
role: &roles.Role{ role: &Role{
ID: "1", ID: "1",
SpaceID: "space", SpaceID: "space",
Description: "Current", Description: "Current",
Environments: []string{"*1"}, Environments: []string{"*1"},
}, },
envID: "env", env: &environments.Environment{ID: "env", SpaceID: "sp"},
}, },
want: true, want: true,
}, },
...@@ -110,13 +109,13 @@ func Test_hasEnvironmentAccess(t *testing.T) { ...@@ -110,13 +109,13 @@ func Test_hasEnvironmentAccess(t *testing.T) {
Aliases: []string{"master"}, Aliases: []string{"master"},
}, nil).Once() }, nil).Once()
}, },
role: &roles.Role{ role: &Role{
ID: "1", ID: "1",
SpaceID: "space", SpaceID: "space",
Description: "Current", Description: "Current",
Environments: []string{"ma*"}, Environments: []string{"ma*"},
}, },
envID: "env1", env: &environments.Environment{ID: "env1", SpaceID: "sp"},
}, },
want: true, want: true,
}, },
...@@ -131,13 +130,13 @@ func Test_hasEnvironmentAccess(t *testing.T) { ...@@ -131,13 +130,13 @@ func Test_hasEnvironmentAccess(t *testing.T) {
Aliases: []string{"master"}, Aliases: []string{"master"},
}, nil).Once() }, nil).Once()
}, },
role: &roles.Role{ role: &Role{
ID: "1", ID: "1",
SpaceID: "space", SpaceID: "space",
Description: "Current", Description: "Current",
Environments: []string{"*"}, Environments: []string{"*"},
}, },
envID: "env1", env: &environments.Environment{ID: "env1", SpaceID: "sp"},
}, },
want: true, want: true,
}, },
...@@ -152,13 +151,13 @@ func Test_hasEnvironmentAccess(t *testing.T) { ...@@ -152,13 +151,13 @@ func Test_hasEnvironmentAccess(t *testing.T) {
Aliases: []string{"master"}, Aliases: []string{"master"},
}, nil).Once() }, nil).Once()
}, },
role: &roles.Role{ role: &Role{
ID: "1", ID: "1",
SpaceID: "space", SpaceID: "space",
Description: "Current", Description: "Current",
Environments: []string{"q*"}, Environments: []string{"q*"},
}, },
envID: "env1", env: &environments.Environment{ID: "env1", SpaceID: "sp"},
}, },
want: false, want: false,
}, },
...@@ -170,7 +169,7 @@ func Test_hasEnvironmentAccess(t *testing.T) { ...@@ -170,7 +169,7 @@ func Test_hasEnvironmentAccess(t *testing.T) {
tt.args.envscall(envsservice) tt.args.envscall(envsservice)
} }
if got := hasEnvironmentAccess(tt.args.ctx, envsservice, tt.args.role, tt.args.envID); got != tt.want { if got := tt.args.role.CanAccessEnvironment(tt.args.ctx, tt.args.env, envsservice); got != tt.want {
t.Errorf("hasEnvironmentAccess() = %v, want %v", got, tt.want) t.Errorf("hasEnvironmentAccess() = %v, want %v", got, tt.want)
} }
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment