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