<commit_before>package security

import (
	"bytes"
	"encoding/json"
	"fmt"
)

var (
	// ValidLabels is the set of all valid Labels for IPC methods.
	ValidLabels = []Label{ResolveLabel, ReadLabel, WriteLabel, AdminLabel, DebugLabel, MonitoringLabel}

	// AllLabels is a LabelSet containing all ValidLabels.
	AllLabels = LabelSet(ResolveLabel | ReadLabel | WriteLabel | AdminLabel | DebugLabel | MonitoringLabel)
)

// IsValidLabel tests whether a label is a member of the defined valid set.
func IsValidLabel(l Label) bool {
	for _, s := range ValidLabels {
		if s == l {
			return true
		}
	}
	return false
}

// String representation of a Label.
func (l Label) String() string {
	switch l {
	case ResolveLabel:
		return "X"
	case ReadLabel:
		return "R"
	case WriteLabel:
		return "W"
	case AdminLabel:
		return "A"
	case DebugLabel:
		return "D"
	case MonitoringLabel:
		return "M"
	}
	return ""
}

// HasLabel returns true iff the LabelSet contains at least one of the provided labels.
func (ls LabelSet) HasLabel(label Label, additionalLabels ...Label) bool {
	for _, l := range append([]Label{label}, additionalLabels...) {
		if l != Label(0) && (ls&LabelSet(l)) == LabelSet(l) {
			return true
		}
	}
	return false
}

// String representation of a LabelSet.
func (ls LabelSet) String() string {
	b := bytes.NewBufferString("")
	for _, l := range ValidLabels {
		if ls.HasLabel(l) {
			b.WriteString(l.String())
		}
	}
	return b.String()
}

func (ls *LabelSet) fromString(s string) error {
	for i := 0; i < len(s); i++ {
		switch s[i] {
		case 'X', 'x':
			*ls |= LabelSet(ResolveLabel)
		case 'R', 'r':
			*ls |= LabelSet(ReadLabel)
		case 'W', 'w':
			*ls |= LabelSet(WriteLabel)
		case 'A', 'a':
			*ls |= LabelSet(AdminLabel)
		case 'D', 'd':
			*ls |= LabelSet(DebugLabel)
		case 'M', 'm':
			*ls |= LabelSet(MonitoringLabel)
		default:
			return fmt.Errorf("invalid label: %q", s[i])
		}
	}
	return nil
}

// MarshalJSON implements the JSON.Marshaler interface. The returned JSON encoding
// is essentially a string over the set of characters {R, W, A, D, M}. The string
// contains character R iff ls has ReadLabel, W iff ls has WriteLabel, A iff ls
// has Adminlabel, D iff ls has DebugLabel, and M iff ls has MonitoringLabel.
func (ls LabelSet) MarshalJSON() ([]byte, error) {
	return json.Marshal(ls.String())
}

// UnmarshalJSON implements the JSON.Marshaler interface. The function is case-insensitive
// on the provided string encoding.
func (ls *LabelSet) UnmarshalJSON(b []byte) error {
	var s string
	if err := json.Unmarshal(b, &s); err != nil {
		return err
	}
	return ls.fromString(s)
}

// TODO(ashankar): Remove this when the new label mechanism is deployed.
func LabelFromMethodTags(tags []interface{}) Label {
	for _, t := range tags {
		if l, ok := t.(Label); ok {
			return l
		}
	}
	return AdminLabel
}<commit_msg>veyron/tools/mgmt/nodex: add commands for manipulating ACLs<commit_after>package security

import (
	"bytes"
	"encoding/json"
	"fmt"
)

var (
	// ValidLabels is the set of all valid Labels for IPC methods.
	ValidLabels = []Label{ResolveLabel, ReadLabel, WriteLabel, AdminLabel, DebugLabel, MonitoringLabel}

	// AllLabels is a LabelSet containing all ValidLabels.
	AllLabels = LabelSet(ResolveLabel | ReadLabel | WriteLabel | AdminLabel | DebugLabel | MonitoringLabel)
)

// IsValidLabel tests whether a label is a member of the defined valid set.
func IsValidLabel(l Label) bool {
	for _, s := range ValidLabels {
		if s == l {
			return true
		}
	}
	return false
}

// String representation of a Label.
func (l Label) String() string {
	switch l {
	case ResolveLabel:
		return "X"
	case ReadLabel:
		return "R"
	case WriteLabel:
		return "W"
	case AdminLabel:
		return "A"
	case DebugLabel:
		return "D"
	case MonitoringLabel:
		return "M"
	}
	return ""
}

// HasLabel returns true iff the LabelSet contains at least one of the provided labels.
func (ls LabelSet) HasLabel(label Label, additionalLabels ...Label) bool {
	for _, l := range append([]Label{label}, additionalLabels...) {
		if l != Label(0) && (ls&LabelSet(l)) == LabelSet(l) {
			return true
		}
	}
	return false
}

// String representation of a LabelSet.
func (ls LabelSet) String() string {
	b := bytes.NewBufferString("")
	for _, l := range ValidLabels {
		if ls.HasLabel(l) {
			b.WriteString(l.String())
		}
	}
	return b.String()
}

func (ls *LabelSet) FromString(s string) error {
	for i := 0; i < len(s); i++ {
		switch s[i] {
		case 'X', 'x':
			*ls |= LabelSet(ResolveLabel)
		case 'R', 'r':
			*ls |= LabelSet(ReadLabel)
		case 'W', 'w':
			*ls |= LabelSet(WriteLabel)
		case 'A', 'a':
			*ls |= LabelSet(AdminLabel)
		case 'D', 'd':
			*ls |= LabelSet(DebugLabel)
		case 'M', 'm':
			*ls |= LabelSet(MonitoringLabel)
		default:
			return fmt.Errorf("invalid label: %q", s[i])
		}
	}
	return nil
}

// MarshalJSON implements the JSON.Marshaler interface. The returned JSON encoding
// is essentially a string over the set of characters {R, W, A, D, M}. The string
// contains character R iff ls has ReadLabel, W iff ls has WriteLabel, A iff ls
// has Adminlabel, D iff ls has DebugLabel, and M iff ls has MonitoringLabel.
func (ls LabelSet) MarshalJSON() ([]byte, error) {
	return json.Marshal(ls.String())
}

// UnmarshalJSON implements the JSON.Marshaler interface. The function is case-insensitive
// on the provided string encoding.
func (ls *LabelSet) UnmarshalJSON(b []byte) error {
	var s string
	if err := json.Unmarshal(b, &s); err != nil {
		return err
	}
	return ls.FromString(s)
}

// TODO(ashankar): Remove this when the new label mechanism is deployed.
func LabelFromMethodTags(tags []interface{}) Label {
	for _, t := range tags {
		if l, ok := t.(Label); ok {
			return l
		}
	}
	return AdminLabel
}
{'fraction_non_alphanumeric': 0.12726638772663879, 'fraction_numerical': 0.000697350069735007, 'mean_word_length': 3.6680227827502034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7174966', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1894, 'n_words': 673}