| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 1 | package installer |
| 2 | |
| 3 | import ( |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 4 | "fmt" |
| gio | efa0ed4 | 2024-06-13 12:31:43 +0400 | [diff] [blame] | 5 | "strings" |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 6 | |
| 7 | "cuelang.org/go/cue" |
| 8 | "cuelang.org/go/cue/cuecontext" |
| 9 | ) |
| 10 | |
| 11 | type Kind int |
| 12 | |
| 13 | const ( |
| gio | 4ece99c | 2024-07-18 11:05:50 +0400 | [diff] [blame] | 14 | KindBoolean Kind = 0 |
| 15 | KindInt = 7 |
| 16 | KindString = 1 |
| 17 | KindStruct = 2 |
| 18 | KindNetwork = 3 |
| 19 | KindMultiNetwork = 10 |
| 20 | KindAuth = 5 |
| 21 | KindSSHKey = 6 |
| 22 | KindNumber = 4 |
| 23 | KindArrayString = 8 |
| 24 | KindPort = 9 |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 25 | KindVPNAuthKey = 11 |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 26 | ) |
| 27 | |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 28 | type Field struct { |
| 29 | Name string |
| 30 | Schema Schema |
| 31 | } |
| 32 | |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 33 | type Schema interface { |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 34 | Name() string |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 35 | Kind() Kind |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 36 | Fields() []Field |
| 37 | Advanced() bool |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 38 | Meta() map[string]string |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 39 | } |
| 40 | |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 41 | var AuthSchema Schema = structSchema{ |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 42 | name: "Auth", |
| 43 | fields: []Field{ |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 44 | Field{"enabled", basicSchema{"Enabled", KindBoolean, false, nil}}, |
| 45 | Field{"groups", basicSchema{"Groups", KindString, false, nil}}, |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 46 | }, |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 47 | advanced: false, |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 48 | } |
| 49 | |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 50 | var SSHKeySchema Schema = structSchema{ |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 51 | name: "SSH Key", |
| 52 | fields: []Field{ |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 53 | Field{"public", basicSchema{"Public Key", KindString, false, nil}}, |
| 54 | Field{"private", basicSchema{"Private Key", KindString, false, nil}}, |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 55 | }, |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 56 | advanced: true, |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 57 | } |
| 58 | |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 59 | const networkSchema = ` |
| 60 | #Network: { |
| Giorgi Lekveishvili | 9b52ab9 | 2024-01-05 13:12:48 +0400 | [diff] [blame] | 61 | name: string |
| 62 | ingressClass: string |
| Giorgi Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 63 | certificateIssuer: string | *"" |
| Giorgi Lekveishvili | 9b52ab9 | 2024-01-05 13:12:48 +0400 | [diff] [blame] | 64 | domain: string |
| Giorgi Lekveishvili | b59b7c2 | 2024-04-03 22:17:50 +0400 | [diff] [blame] | 65 | allocatePortAddr: string |
| gio | efa0ed4 | 2024-06-13 12:31:43 +0400 | [diff] [blame] | 66 | reservePortAddr: string |
| gio | cdfa372 | 2024-06-13 20:10:14 +0400 | [diff] [blame] | 67 | deallocatePortAddr: string |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 68 | } |
| 69 | |
| Giorgi Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 70 | value: { %s } |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 71 | ` |
| 72 | |
| 73 | func isNetwork(v cue.Value) bool { |
| 74 | if v.Value().Kind() != cue.StructKind { |
| 75 | return false |
| 76 | } |
| Giorgi Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 77 | s := fmt.Sprintf(networkSchema, fmt.Sprintf("%#v", v)) |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 78 | c := cuecontext.New() |
| 79 | u := c.CompileString(s) |
| Giorgi Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 80 | network := u.LookupPath(cue.ParsePath("#Network")) |
| 81 | vv := u.LookupPath(cue.ParsePath("value")) |
| 82 | if err := network.Subsume(vv); err == nil { |
| 83 | return true |
| 84 | } |
| 85 | return false |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 86 | } |
| 87 | |
| gio | 4ece99c | 2024-07-18 11:05:50 +0400 | [diff] [blame] | 88 | const multiNetworkSchema = ` |
| 89 | #Network: { |
| 90 | name: string |
| 91 | ingressClass: string |
| 92 | certificateIssuer: string | *"" |
| 93 | domain: string |
| 94 | allocatePortAddr: string |
| 95 | reservePortAddr: string |
| 96 | deallocatePortAddr: string |
| 97 | } |
| 98 | |
| 99 | #Networks: [...#Network] |
| 100 | |
| 101 | value: %s |
| 102 | ` |
| 103 | |
| 104 | func isMultiNetwork(v cue.Value) bool { |
| 105 | if v.Value().IncompleteKind() != cue.ListKind { |
| 106 | return false |
| 107 | } |
| 108 | s := fmt.Sprintf(multiNetworkSchema, fmt.Sprintf("%#v", v)) |
| 109 | c := cuecontext.New() |
| 110 | u := c.CompileString(s) |
| 111 | networks := u.LookupPath(cue.ParsePath("#Networks")) |
| 112 | vv := u.LookupPath(cue.ParsePath("value")) |
| 113 | if err := networks.Subsume(vv); err == nil { |
| 114 | return true |
| 115 | } |
| 116 | return false |
| 117 | } |
| 118 | |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 119 | const authSchema = ` |
| 120 | #Auth: { |
| 121 | enabled: bool | false |
| 122 | groups: string | *"" |
| 123 | } |
| 124 | |
| 125 | value: { %s } |
| 126 | ` |
| 127 | |
| 128 | func isAuth(v cue.Value) bool { |
| 129 | if v.Value().Kind() != cue.StructKind { |
| 130 | return false |
| 131 | } |
| 132 | s := fmt.Sprintf(authSchema, fmt.Sprintf("%#v", v)) |
| 133 | c := cuecontext.New() |
| 134 | u := c.CompileString(s) |
| 135 | auth := u.LookupPath(cue.ParsePath("#Auth")) |
| 136 | vv := u.LookupPath(cue.ParsePath("value")) |
| 137 | if err := auth.Subsume(vv); err == nil { |
| 138 | return true |
| 139 | } |
| 140 | return false |
| 141 | } |
| 142 | |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 143 | const sshKeySchema = ` |
| 144 | #SSHKey: { |
| 145 | public: string |
| 146 | private: string |
| 147 | } |
| 148 | |
| 149 | value: { %s } |
| 150 | ` |
| 151 | |
| 152 | func isSSHKey(v cue.Value) bool { |
| 153 | if v.Value().Kind() != cue.StructKind { |
| 154 | return false |
| 155 | } |
| 156 | s := fmt.Sprintf(sshKeySchema, fmt.Sprintf("%#v", v)) |
| 157 | c := cuecontext.New() |
| 158 | u := c.CompileString(s) |
| 159 | sshKey := u.LookupPath(cue.ParsePath("#SSHKey")) |
| 160 | vv := u.LookupPath(cue.ParsePath("value")) |
| 161 | if err := sshKey.Subsume(vv); err == nil { |
| 162 | return true |
| 163 | } |
| 164 | return false |
| 165 | } |
| 166 | |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 167 | type basicSchema struct { |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 168 | name string |
| 169 | kind Kind |
| 170 | advanced bool |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 171 | meta map[string]string |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | func (s basicSchema) Name() string { |
| 175 | return s.name |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | func (s basicSchema) Kind() Kind { |
| 179 | return s.kind |
| 180 | } |
| 181 | |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 182 | func (s basicSchema) Fields() []Field { |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 183 | return nil |
| 184 | } |
| 185 | |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 186 | func (s basicSchema) Advanced() bool { |
| 187 | return s.advanced |
| 188 | } |
| 189 | |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 190 | func (s basicSchema) Meta() map[string]string { |
| 191 | return s.meta |
| 192 | } |
| 193 | |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 194 | type structSchema struct { |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 195 | name string |
| 196 | fields []Field |
| 197 | advanced bool |
| 198 | } |
| 199 | |
| 200 | func (s structSchema) Name() string { |
| 201 | return s.name |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | func (s structSchema) Kind() Kind { |
| 205 | return KindStruct |
| 206 | } |
| 207 | |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 208 | func (s structSchema) Fields() []Field { |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 209 | return s.fields |
| 210 | } |
| 211 | |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 212 | func (s structSchema) Advanced() bool { |
| 213 | return s.advanced |
| 214 | } |
| 215 | |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 216 | func (s structSchema) Meta() map[string]string { |
| 217 | return map[string]string{} |
| 218 | } |
| 219 | |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 220 | func NewCueSchema(name string, v cue.Value) (Schema, error) { |
| 221 | nameAttr := v.Attribute("name") |
| 222 | if nameAttr.Err() == nil { |
| 223 | name = nameAttr.Contents() |
| 224 | } |
| gio | efa0ed4 | 2024-06-13 12:31:43 +0400 | [diff] [blame] | 225 | role := "" |
| 226 | roleAttr := v.Attribute("role") |
| 227 | if roleAttr.Err() == nil { |
| 228 | role = strings.ToLower(roleAttr.Contents()) |
| 229 | } |
| Giorgi Lekveishvili | 9b52ab9 | 2024-01-05 13:12:48 +0400 | [diff] [blame] | 230 | switch v.IncompleteKind() { |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 231 | case cue.StringKind: |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 232 | if role == "vpnauthkey" { |
| 233 | meta := map[string]string{} |
| gio | 7fbd4ad | 2024-08-27 10:06:39 +0400 | [diff] [blame] | 234 | usernameFieldAttr := v.Attribute("usernameField") |
| 235 | if usernameFieldAttr.Err() == nil { |
| 236 | meta["usernameField"] = strings.ToLower(usernameFieldAttr.Contents()) |
| 237 | } |
| 238 | usernameAttr := v.Attribute("username") |
| 239 | if usernameAttr.Err() == nil { |
| 240 | meta["username"] = strings.ToLower(usernameAttr.Contents()) |
| 241 | } |
| 242 | if len(meta) != 1 { |
| 243 | return nil, fmt.Errorf("invalid vpn auth key field meta: %+v", meta) |
| 244 | } |
| gio | 29f6b87 | 2024-09-08 16:14:58 +0400 | [diff] [blame^] | 245 | enabledFieldAttr := v.Attribute("enabledField") |
| 246 | if enabledFieldAttr.Err() == nil { |
| 247 | meta["enabledField"] = strings.ToLower(enabledFieldAttr.Contents()) |
| 248 | } |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 249 | return basicSchema{name, KindVPNAuthKey, true, meta}, nil |
| 250 | } else { |
| 251 | return basicSchema{name, KindString, false, nil}, nil |
| 252 | } |
| Giorgi Lekveishvili | 9b52ab9 | 2024-01-05 13:12:48 +0400 | [diff] [blame] | 253 | case cue.BoolKind: |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 254 | return basicSchema{name, KindBoolean, false, nil}, nil |
| Giorgi Lekveishvili | e009a5d | 2024-01-05 14:10:11 +0400 | [diff] [blame] | 255 | case cue.NumberKind: |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 256 | return basicSchema{name, KindNumber, false, nil}, nil |
| Giorgi Lekveishvili | b59b7c2 | 2024-04-03 22:17:50 +0400 | [diff] [blame] | 257 | case cue.IntKind: |
| gio | efa0ed4 | 2024-06-13 12:31:43 +0400 | [diff] [blame] | 258 | if role == "port" { |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 259 | return basicSchema{name, KindPort, true, nil}, nil |
| gio | efa0ed4 | 2024-06-13 12:31:43 +0400 | [diff] [blame] | 260 | } else { |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 261 | return basicSchema{name, KindInt, false, nil}, nil |
| gio | efa0ed4 | 2024-06-13 12:31:43 +0400 | [diff] [blame] | 262 | } |
| gio | e72b54f | 2024-04-22 10:44:41 +0400 | [diff] [blame] | 263 | case cue.ListKind: |
| gio | 4ece99c | 2024-07-18 11:05:50 +0400 | [diff] [blame] | 264 | if isMultiNetwork(v) { |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 265 | return basicSchema{name, KindMultiNetwork, false, nil}, nil |
| gio | 4ece99c | 2024-07-18 11:05:50 +0400 | [diff] [blame] | 266 | } |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 267 | return basicSchema{name, KindArrayString, false, nil}, nil |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 268 | case cue.StructKind: |
| 269 | if isNetwork(v) { |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 270 | return basicSchema{name, KindNetwork, false, nil}, nil |
| Giorgi Lekveishvili | a09fad7 | 2024-03-21 15:24:35 +0400 | [diff] [blame] | 271 | } else if isAuth(v) { |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 272 | return basicSchema{name, KindAuth, false, nil}, nil |
| Giorgi Lekveishvili | b6a5806 | 2024-04-02 16:49:19 +0400 | [diff] [blame] | 273 | } else if isSSHKey(v) { |
| gio | 36b23b3 | 2024-08-25 12:20:54 +0400 | [diff] [blame] | 274 | return basicSchema{name, KindSSHKey, true, nil}, nil |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 275 | } |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 276 | s := structSchema{name, make([]Field, 0), false} |
| Giorgi Lekveishvili | 9b52ab9 | 2024-01-05 13:12:48 +0400 | [diff] [blame] | 277 | f, err := v.Fields(cue.Schema()) |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 278 | if err != nil { |
| 279 | return nil, err |
| 280 | } |
| 281 | for f.Next() { |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 282 | scm, err := NewCueSchema(f.Selector().String(), f.Value()) |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 283 | if err != nil { |
| 284 | return nil, err |
| 285 | } |
| gio | 44f621b | 2024-04-29 09:44:38 +0400 | [diff] [blame] | 286 | s.fields = append(s.fields, Field{f.Selector().String(), scm}) |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 287 | } |
| 288 | return s, nil |
| 289 | default: |
| 290 | return nil, fmt.Errorf("SHOULD NOT REACH!") |
| 291 | } |
| 292 | } |