...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/file/validation.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/file

     1  package file
     2  
     3  // This file holds an encapsulator for the json files used to store different sets of roles for different bannerids.
     4  // A bannerid cannot be duplicated across different json files.
     5  // For development testing: export RCLI_RES_DATA_DIR=path/to/edgeinfra/pkg/sds/emergencyaccess/rules/server/testdata
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"edge-infra.dev/pkg/lib/fog"
    11  	"edge-infra.dev/pkg/sds/emergencyaccess/eaconst"
    12  	rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules"
    13  )
    14  
    15  type jsonData struct {
    16  	BannerID string              `json:"bannerid"`
    17  	Roles    map[string][]string `json:"roles"`
    18  }
    19  
    20  // EARoles searches through the dataset and returns roles the command is associated to.
    21  func (ds Dataset) EARoles(ctx context.Context, bannerID string, command rulesengine.Command) ([]string, error) {
    22  	reqlog := fog.FromContext(ctx).WithName("dataset")
    23  
    24  	if command.Type != eaconst.Command {
    25  		return nil, fmt.Errorf("file based dataset currently does not support non-command types: %q", command.Type)
    26  	}
    27  
    28  	roleMap := ds.bannerRoleMap[bannerID]
    29  	roles := checkCommand(command.Name, ds.bannerRoleMap[defaultRolesName])
    30  	if roleMap == nil {
    31  		reqlog.V(1).Info("banner id not found", "bannerID", bannerID)
    32  		return roles, nil
    33  	}
    34  
    35  	roles = append(roles, checkCommand(command.Name, roleMap)...)
    36  	return roles, nil
    37  }
    38  
    39  // runs through a rolemap loaded to memory and checks if the name provided matches any
    40  // in the rolemap
    41  func checkCommand(command string, data map[string][]string) []string {
    42  	res := []string{}
    43  	for priv, commands := range data {
    44  		if inList(command, commands) {
    45  			res = append(res, priv)
    46  		}
    47  	}
    48  	return res
    49  }
    50  
    51  func inList(val string, lst []string) bool {
    52  	for _, item := range lst {
    53  		if val == item {
    54  			return true
    55  		}
    56  	}
    57  	return false
    58  }
    59  

View as plain text