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