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