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