|
1 | 1 | package connector |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
4 | 7 | "strings" |
5 | 8 |
|
6 | 9 | v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" |
7 | 10 | "github.com/conductorone/baton-sdk/pkg/annotations" |
8 | 11 | "github.com/conductorone/baton-sdk/pkg/pagination" |
9 | 12 | mapset "github.com/deckarep/golang-set/v2" |
10 | 13 | "github.com/go-ldap/ldap/v3" |
| 14 | + "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" |
| 15 | + "go.uber.org/zap" |
| 16 | + "golang.org/x/exp/slices" |
11 | 17 | ) |
12 | 18 |
|
13 | 19 | var ResourcesPageSize uint32 = 50 |
@@ -67,3 +73,149 @@ func parseValue(entry *ldap.Entry, targetAttrs []string) string { |
67 | 73 |
|
68 | 74 | return "" |
69 | 75 | } |
| 76 | + |
| 77 | +// We assume that all values are of the same type. |
| 78 | +func toVals(vals []any) []string { |
| 79 | + if len(vals) == 0 { |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + switch vals[0].(type) { |
| 84 | + case string: |
| 85 | + ret := make([]string, len(vals)) |
| 86 | + for i, v := range vals { |
| 87 | + ret[i] = v.(string) |
| 88 | + } |
| 89 | + return ret |
| 90 | + case []byte: |
| 91 | + ret := make([]string, len(vals)) |
| 92 | + for i, v := range vals { |
| 93 | + ret[i] = string(v.([]byte)) |
| 94 | + } |
| 95 | + return ret |
| 96 | + default: |
| 97 | + ret := make([]string, len(vals)) |
| 98 | + for i, v := range vals { |
| 99 | + ret[i] = fmt.Sprintf("%v", v) |
| 100 | + } |
| 101 | + return ret |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func toAttr(k string, v interface{}) ldap.Attribute { |
| 106 | + switch v := v.(type) { |
| 107 | + case []string: |
| 108 | + return ldap.Attribute{ |
| 109 | + Type: k, |
| 110 | + Vals: v, |
| 111 | + } |
| 112 | + case []any: |
| 113 | + return ldap.Attribute{ |
| 114 | + Type: k, |
| 115 | + Vals: toVals(v), |
| 116 | + } |
| 117 | + case string: |
| 118 | + return ldap.Attribute{ |
| 119 | + Type: k, |
| 120 | + Vals: []string{v}, |
| 121 | + } |
| 122 | + case []byte: |
| 123 | + return ldap.Attribute{ |
| 124 | + Type: k, |
| 125 | + Vals: []string{string(v)}, |
| 126 | + } |
| 127 | + case bool: |
| 128 | + return ldap.Attribute{ |
| 129 | + Type: k, |
| 130 | + Vals: []string{strconv.FormatBool(v)}, |
| 131 | + } |
| 132 | + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: |
| 133 | + return ldap.Attribute{ |
| 134 | + Type: k, |
| 135 | + Vals: []string{fmt.Sprintf("%d", v)}, |
| 136 | + } |
| 137 | + case float32, float64: |
| 138 | + return ldap.Attribute{ |
| 139 | + Type: k, |
| 140 | + Vals: []string{fmt.Sprintf("%f", v)}, |
| 141 | + } |
| 142 | + default: |
| 143 | + // l.Warn("unsupported attribute type", zap.Any("type", v)) |
| 144 | + return ldap.Attribute{ |
| 145 | + Type: k, |
| 146 | + Vals: []string{fmt.Sprintf("%v", v)}, |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func extractProfile(ctx context.Context, accountInfo *v2.AccountInfo) (string, []ldap.Attribute, error) { |
| 152 | + l := ctxzap.Extract(ctx) |
| 153 | + |
| 154 | + prof := accountInfo.GetProfile() |
| 155 | + if prof == nil { |
| 156 | + return "", nil, fmt.Errorf("missing profile") |
| 157 | + } |
| 158 | + data := prof.AsMap() |
| 159 | + l.Debug("baton-ldap: create-account profile", zap.Any("data", data)) |
| 160 | + |
| 161 | + suffix, ok := data["suffix"].(string) |
| 162 | + if !ok { |
| 163 | + return "", nil, fmt.Errorf("invalid/missing suffix") |
| 164 | + } |
| 165 | + path, ok := data["path"].(string) |
| 166 | + if !ok { |
| 167 | + return "", nil, fmt.Errorf("invalid/missing path") |
| 168 | + } |
| 169 | + rdnKey, ok := data["rdnKey"].(string) |
| 170 | + if !ok { |
| 171 | + return "", nil, fmt.Errorf("invalid/missing rdnKey") |
| 172 | + } |
| 173 | + rdnValue, ok := data["rdnValue"].(string) |
| 174 | + if !ok { |
| 175 | + return "", nil, fmt.Errorf("invalid/missing rdnValue") |
| 176 | + } |
| 177 | + |
| 178 | + var dn string |
| 179 | + if path != "" { |
| 180 | + dn = strings.Join([]string{fmt.Sprintf("%s=%s", rdnKey, rdnValue), path, suffix}, ",") |
| 181 | + } else { |
| 182 | + dn = strings.Join([]string{fmt.Sprintf("%s=%s", rdnKey, rdnValue), suffix}, ",") |
| 183 | + } |
| 184 | + |
| 185 | + objectClass, ok := data["objectClass"].([]any) |
| 186 | + if !ok { |
| 187 | + return "", nil, fmt.Errorf("invalid/missing objectClass") |
| 188 | + } |
| 189 | + for _, oc := range objectClass { |
| 190 | + if _, ok := oc.(string); !ok { |
| 191 | + return "", nil, fmt.Errorf("invalid objectClass") |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + attrs := []ldap.Attribute{} |
| 196 | + |
| 197 | + for k, v := range data { |
| 198 | + if slices.Contains([]string{ |
| 199 | + "additionalAttributes", |
| 200 | + "rdnKey", |
| 201 | + "rdnValue", |
| 202 | + "path", |
| 203 | + "suffix", |
| 204 | + "login", |
| 205 | + }, k) { |
| 206 | + continue |
| 207 | + } |
| 208 | + attrs = append(attrs, toAttr(k, v)) |
| 209 | + } |
| 210 | + |
| 211 | + additionalAttributes, ok := data["additionalAttributes"].(map[string]interface{}) |
| 212 | + if ok { |
| 213 | + for k, v := range additionalAttributes { |
| 214 | + attrs = append(attrs, toAttr(k, v)) |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + l.Debug("baton-ldap: create-account attributes", zap.Any("attrs", attrs)) |
| 219 | + |
| 220 | + return dn, attrs, nil |
| 221 | +} |
0 commit comments