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