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