package file // This file holds an encapsulator for the json files used to store different sets of roles for different bannerids. // A bannerid cannot be duplicated across different json files. // For development testing: export RCLI_RES_DATA_DIR=path/to/edgeinfra/pkg/sds/emergencyaccess/rules/server/testdata import ( "context" "fmt" "edge-infra.dev/pkg/lib/fog" "edge-infra.dev/pkg/sds/emergencyaccess/eaconst" rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules" ) type jsonData struct { BannerID string `json:"bannerid"` Roles map[string][]string `json:"roles"` } // EARoles searches through the dataset and returns roles the command is associated to. func (ds Dataset) EARoles(ctx context.Context, bannerID string, command rulesengine.Command) ([]string, error) { reqlog := fog.FromContext(ctx).WithName("dataset") if command.Type != eaconst.Command { return nil, fmt.Errorf("file based dataset currently does not support non-command types: %q", command.Type) } roleMap := ds.bannerRoleMap[bannerID] roles := checkCommand(command.Name, ds.bannerRoleMap[defaultRolesName]) if roleMap == nil { reqlog.V(1).Info("banner id not found", "bannerID", bannerID) return roles, nil } roles = append(roles, checkCommand(command.Name, roleMap)...) return roles, nil } // runs through a rolemap loaded to memory and checks if the name provided matches any // in the rolemap func checkCommand(command string, data map[string][]string) []string { res := []string{} for priv, commands := range data { if inList(command, commands) { res = append(res, priv) } } return res } func inList(val string, lst []string) bool { for _, item := range lst { if val == item { return true } } return false }