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