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