|
| 1 | +package rbac |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "golang.org/x/xerrors" |
| 9 | + |
| 10 | + "github.com/coder/coder/v2/coderd/rbac/policy" |
| 11 | +) |
| 12 | + |
| 13 | +// ParseAllowListEntry parses a single allow-list entry string in the form |
| 14 | +// "*:*", "<resource_type>:*", or "<resource_type>:<uuid>" into an |
| 15 | +// AllowListElement with validation. |
| 16 | +func ParseAllowListEntry(s string) (AllowListElement, error) { |
| 17 | + s = strings.TrimSpace(strings.ToLower(s)) |
| 18 | + res, id, ok := ParseResourceAction(s) |
| 19 | + if !ok { |
| 20 | + return AllowListElement{}, xerrors.Errorf("invalid allow_list entry %q: want <type>:<id>", s) |
| 21 | + } |
| 22 | + |
| 23 | + return NewAllowListElement(res, id) |
| 24 | +} |
| 25 | + |
| 26 | +func NewAllowListElement(resourceType string, id string) (AllowListElement, error) { |
| 27 | + if resourceType != policy.WildcardSymbol { |
| 28 | + if _, ok := policy.RBACPermissions[resourceType]; !ok { |
| 29 | + return AllowListElement{}, xerrors.Errorf("unknown resource type %q", resourceType) |
| 30 | + } |
| 31 | + } |
| 32 | + if id != policy.WildcardSymbol { |
| 33 | + if _, err := uuid.Parse(id); err != nil { |
| 34 | + return AllowListElement{}, xerrors.Errorf("invalid %s ID (must be UUID): %q", resourceType, id) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return AllowListElement{Type: resourceType, ID: id}, nil |
| 39 | +} |
| 40 | + |
| 41 | +// ParseAllowList parses, validates, normalizes, and deduplicates a list of |
| 42 | +// allow-list entries. If max is <=0, a default cap of 128 is applied. |
| 43 | +func ParseAllowList(inputs []string, max int) ([]AllowListElement, error) { |
| 44 | + if len(inputs) == 0 { |
| 45 | + return nil, nil |
| 46 | + } |
| 47 | + if max <= 0 { |
| 48 | + max = 128 |
| 49 | + } |
| 50 | + if len(inputs) > max { |
| 51 | + return nil, xerrors.Errorf("allow_list has %d entries; max allowed is %d", len(inputs), max) |
| 52 | + } |
| 53 | + |
| 54 | + elems := make([]AllowListElement, 0, len(inputs)) |
| 55 | + for _, s := range inputs { |
| 56 | + e, err := ParseAllowListEntry(s) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + // Global wildcard short-circuits |
| 61 | + if e.Type == policy.WildcardSymbol && e.ID == policy.WildcardSymbol { |
| 62 | + return []AllowListElement{AllowListAll()}, nil |
| 63 | + } |
| 64 | + elems = append(elems, e) |
| 65 | + } |
| 66 | + |
| 67 | + return NewAllowList(elems, max) |
| 68 | +} |
| 69 | + |
| 70 | +func NewAllowList(inputs []AllowListElement, max int) ([]AllowListElement, error) { |
| 71 | + if len(inputs) == 0 { |
| 72 | + return nil, nil |
| 73 | + } |
| 74 | + if max <= 0 { |
| 75 | + max = 128 |
| 76 | + } |
| 77 | + if len(inputs) > max { |
| 78 | + return nil, xerrors.Errorf("allow_list has %d entries; max allowed is %d", len(inputs), max) |
| 79 | + } |
| 80 | + |
| 81 | + // Collapse typed wildcards and drop shadowed IDs |
| 82 | + typedWildcard := map[string]struct{}{} |
| 83 | + idsByType := map[string]map[string]struct{}{} |
| 84 | + for _, e := range inputs { |
| 85 | + // Global wildcard short-circuits |
| 86 | + if e.Type == policy.WildcardSymbol && e.ID == policy.WildcardSymbol { |
| 87 | + return []AllowListElement{AllowListAll()}, nil |
| 88 | + } |
| 89 | + |
| 90 | + if e.ID == policy.WildcardSymbol { |
| 91 | + typedWildcard[e.Type] = struct{}{} |
| 92 | + continue |
| 93 | + } |
| 94 | + if idsByType[e.Type] == nil { |
| 95 | + idsByType[e.Type] = map[string]struct{}{} |
| 96 | + } |
| 97 | + idsByType[e.Type][e.ID] = struct{}{} |
| 98 | + } |
| 99 | + |
| 100 | + out := make([]AllowListElement, 0) |
| 101 | + for t, ids := range idsByType { |
| 102 | + if _, ok := typedWildcard[t]; ok { |
| 103 | + out = append(out, AllowListElement{Type: t, ID: policy.WildcardSymbol}) |
| 104 | + continue |
| 105 | + } |
| 106 | + for id := range ids { |
| 107 | + out = append(out, AllowListElement{Type: t, ID: id}) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + sort.Slice(out, func(i, j int) bool { |
| 112 | + if out[i].Type == out[j].Type { |
| 113 | + return out[i].ID < out[j].ID |
| 114 | + } |
| 115 | + return out[i].Type < out[j].Type |
| 116 | + }) |
| 117 | + return out, nil |
| 118 | +} |
0 commit comments