Skip to content

Commit

Permalink
feat: change the way of get the roles
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanCaamano committed Oct 21, 2024
1 parent 8d1e630 commit 2292bbd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
4 changes: 1 addition & 3 deletions config/sso_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ func RbacDelegateToLabel(ctx context.Context, mail string, apiUrl, apiEndpoint,
}
resourcesToFilterPopulated.Group = servicesAndGroup.Group
if servicesAndGroup.Services != nil {
for service := range servicesAndGroup.Services {
resourcesToFilterPopulated.ArrayLabels = append(resourcesToFilterPopulated.ArrayLabels, service)
}
resourcesToFilterPopulated.ArrayLabels = append(resourcesToFilterPopulated.ArrayLabels, servicesAndGroup.Services...)
resourcesToFilterPopulated.LabelsFilter = fmt.Sprintf("%s in (%s)", label, strings.Join(resourcesToFilterPopulated.ArrayLabels[:], ","))
}
return resourcesToFilterPopulated, nil
Expand Down
55 changes: 34 additions & 21 deletions server/auth/devhub/aplication.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type GroupAndServices struct {
Services map[string]string
Services []string
Group string
}

func GetServicesAndGroup(devhubclient *Client, apiUrl, apiEndpoint, apiPassword, userToIdentify string, writeGroups []string) (*GroupAndServices, error) {
var result map[string]interface{}
roles := make(map[string]string)
services := make(map[string]string)
var roles []string
var services []string
servicesAndGroup := &GroupAndServices{}
apiDevhub := fmt.Sprintf("%s/%s/%s", apiUrl, apiEndpoint, userToIdentify)
res, err := HandleRequestApiInditex(devhubclient, apiDevhub, "GET", apiPassword, map[string]interface{}{})
Expand All @@ -24,39 +24,52 @@ func GetServicesAndGroup(devhubclient *Client, apiUrl, apiEndpoint, apiPassword,
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return nil, err
}
if teams, ok := result["teams"].([]interface{}); ok {
roles, services = GetRolesAndServices(teams, services, roles)
}

roles, services = GetRolesAndServices(result, services, roles)

servicesAndGroup.Group = GetGroupByRole(writeGroups, roles)
servicesAndGroup.Services = services
return servicesAndGroup, nil
}

func GetRolesAndServices(teams []interface{}, services, roles map[string]string) (map[string]string, map[string]string) {
func GetRolesAndServices(result map[string]interface{}, services, roles []string) ([]string, []string) {
teams, ok := result["teams"].([]interface{})
if !ok {
return services, roles
}
for _, team := range teams {
if len(team.(map[string]interface{})["projects"].([]interface{})) > 0 {
for _, project := range team.(map[string]interface{})["projects"].([]interface{}) {
if relationshipType, ok := project.(map[string]interface{})["relationshipType"].(map[string]interface{}); ok && relationshipType["name"] == "Owner" {
services[project.(map[string]interface{})["key"].(string)] = "service"
for _, profile := range team.(map[string]interface{})["profiles"].([]interface{}) {
roles[profile.(map[string]interface{})["name"].(string)] = "role"
}
if len(team.(map[string]interface{})["effectiveCrossProfiles"].([]string)) > 0 {
for _, effectiveCrossProfile := range team.(map[string]interface{})["effectiveCrossProfiles"].([]string) {
roles[effectiveCrossProfile] = "role"
}
}
if len(team.(map[string]interface{})["applications"].([]interface{})) <= 0 {
continue
}
for _, project := range team.(map[string]interface{})["applications"].([]interface{}) {
if project.(map[string]interface{})["relationshipType"].(string) != "Owner" {
continue
}
if !slices.Contains(services, project.(map[string]interface{})["key"].(string)) {
services = append(services, project.(map[string]interface{})["key"].(string))
}
for _, profile := range team.(map[string]interface{})["profiles"].([]interface{}) {
if !slices.Contains(roles, profile.(map[string]interface{})["name"].(string)) {
roles = append(roles, profile.(map[string]interface{})["name"].(string))
}
}
crossprofiles, ok := result["crossProfiles"].([]interface{})
if !ok {
continue
}
for _, crossprofile := range crossprofiles {
if !slices.Contains(roles, crossprofile.(map[string]interface{})["name"].(string)) {
roles = append(roles, crossprofile.(map[string]interface{})["name"].(string))
}
}
}
}
return roles, services
}

func GetGroupByRole(writeGroups []string, roles map[string]string) string {
func GetGroupByRole(writeGroups []string, roles []string) string {
groupByRole := "reader"
for role := range roles {
for _, role := range roles {
if slices.Contains(writeGroups, role) {
groupByRole = "writer"
}
Expand Down

0 comments on commit 2292bbd

Please sign in to comment.