diff options
Diffstat (limited to 'util.go')
-rw-r--r-- | util.go | 92 |
1 files changed, 49 insertions, 43 deletions
@@ -11,6 +11,13 @@ import ( log "github.com/sirupsen/logrus" ) +// DNs ---- + +type dnComponent struct { + Type string + Value string +} + func dnToConsul(dn string) (string, error) { if strings.Contains(dn, "/") { return "", fmt.Errorf("DN %s contains a /", dn) @@ -45,6 +52,48 @@ func consulToDN(key string) (string, string, error) { return "", "", fmt.Errorf("Consul key %s does not end with attribute=something", key) } +func parseDN(dn string) ([]dnComponent, error) { + rdns := strings.Split(dn, ",") + + ret := []dnComponent{} + + for _, rdn := range rdns { + splits := strings.Split(rdn, "=") + if len(splits) != 2 { + return nil, fmt.Errorf("Wrong DN component: %s (expected type=value)", rdn) + } + ret = append(ret, dnComponent{ + Type: strings.ToLower(strings.TrimSpace(splits[0])), + Value: strings.ToLower(strings.TrimSpace(splits[1])), + }) + } + return ret, nil +} + +func unparseDN(path []dnComponent) string { + ret := "" + for _, c := range path { + if ret != "" { + ret = ret + "," + } + ret = ret + c.Type + "=" + c.Value + } + return ret +} + +func canonicalDN(dn string) (string, error) { + path, err := parseDN(dn) + if err != nil { + return "", err + } + + return unparseDN(path), nil +} + +// Values + +type Entry map[string][]string + func parseValue(value []byte) ([]string, error) { val := []string{} err := json.Unmarshal(value, &val) @@ -82,49 +131,6 @@ func parseConsulResult(data []*consul.KVPair) (map[string]Entry, error) { return aggregator, nil } -type DNComponent struct { - Type string - Value string -} - -func parseDN(dn string) ([]DNComponent, error) { - rdns := strings.Split(dn, ",") - - ret := []DNComponent{} - - for _, rdn := range rdns { - splits := strings.Split(rdn, "=") - if len(splits) != 2 { - return nil, fmt.Errorf("Wrong DN component: %s (expected type=value)", rdn) - } - ret = append(ret, DNComponent{ - Type: strings.ToLower(strings.TrimSpace(splits[0])), - Value: strings.ToLower(strings.TrimSpace(splits[1])), - }) - } - return ret, nil -} - -func unparseDN(path []DNComponent) string { - ret := "" - for _, c := range path { - if ret != "" { - ret = ret + "," - } - ret = ret + c.Type + "=" + c.Value - } - return ret -} - -func canonicalDN(dn string) (string, error) { - path, err := parseDN(dn) - if err != nil { - return "", err - } - - return unparseDN(path), nil -} - func checkRestrictedAttr(attr string) error { RESTRICTED_ATTRS := []string{ ATTR_MEMBEROF, |