aboutsummaryrefslogtreecommitdiff
path: root/util.go
blob: 30d7a67a670e890352f334194eb038c1e9669da0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	uuid "github.com/google/uuid"
	consul "github.com/hashicorp/consul/api"
	log "github.com/sirupsen/logrus"
)

func dnToConsul(dn string) (string, error) {
	if strings.Contains(dn, "/") {
		return "", fmt.Errorf("DN %s contains a /", dn)
	}

	rdns := strings.Split(dn, ",")

	// Reverse rdns
	for i, j := 0, len(rdns)-1; i < j; i, j = i+1, j-1 {
		rdns[i], rdns[j] = rdns[j], rdns[i]
	}

	return strings.Join(rdns, "/"), nil
}

func consulToDN(key string) (string, string, error) {
	path := strings.Split(key, "/")
	dn := ""
	for _, cpath := range path {
		if cpath == "" {
			continue
		}
		kv := strings.Split(cpath, "=")
		if len(kv) == 2 && kv[0] == "attribute" {
			return dn, kv[1], nil
		}
		if dn != "" {
			dn = "," + dn
		}
		dn = cpath + dn
	}
	return "", "", fmt.Errorf("Consul key %s does not end with attribute=something", key)
}

func parseValue(value []byte) ([]string, error) {
	val := []string{}
	err := json.Unmarshal(value, &val)
	if err == nil {
		return val, nil
	}

	val2 := ""
	err = json.Unmarshal(value, &val2)
	if err == nil {
		return []string{val2}, nil
	}

	return nil, fmt.Errorf("Not a string or list of strings: %s", value)
}

func parseConsulResult(data []*consul.KVPair) (map[string]Entry, error) {
	aggregator := map[string]Entry{}

	for _, pair := range data {
		dn, attr, err := consulToDN(pair.Key)
		if err != nil {
			continue
		}
		if _, exists := aggregator[dn]; !exists {
			aggregator[dn] = Entry{}
		}
		value, err := parseValue(pair.Value)
		if err != nil {
			return nil, err
		}
		aggregator[dn][attr] = value
	}

	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,
		ATTR_ENTRYUUID,
		ATTR_CREATORSNAME,
		ATTR_CREATETIMESTAMP,
		ATTR_MODIFIERSNAME,
		ATTR_MODIFYTIMESTAMP,
	}

	if strings.EqualFold(attr, ATTR_MEMBEROF) {
		return fmt.Errorf("memberOf cannot be defined directly, membership must be specified in the group itself")
	}

	for _, s := range RESTRICTED_ATTRS {
		if strings.EqualFold(attr, s) {
			return fmt.Errorf("Attribute %s is restricted and may only be set by the system", s)
		}
	}
	return nil
}

func genTimestamp() string {
	return time.Now().Format("20060102150405Z")
}

func genUuid() string {
	uuid, err := uuid.NewRandom()
	if err != nil {
		log.Panicf("UUID generation error: %s", err)
	}
	return uuid.String()
}

func valueMatch(attr, val1, val2 string) bool {
	if strings.EqualFold(attr, ATTR_USERPASSWORD) {
		return val1 == val2
	} else {
		return strings.EqualFold(val1, val2)
	}
}

func listContains(list []string, key string) bool {
	for _, v := range list {
		if key == v {
			return true
		}
	}
	return false
}