1 // Copyright 2021 Chaos Mesh Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 16 package tproxyconfig 17 18 import ( 19 "encoding/json" 20 ) 21 22 type Config struct { 23 ProxyPorts []uint32 `json:"proxy_ports,omitempty"` 24 Rules []PodHttpChaosBaseRule `json:"rules"` 25 TLS *TLSConfig `json:"tls,omitempty"` 26 } 27 28 type TLSConfig struct { 29 CertFile TLSConfigItem `json:"cert_file,omitempty"` 30 KeyFile TLSConfigItem `json:"key_file,omitempty"` 31 CAFile *TLSConfigItem `json:"ca_file,omitempty"` 32 } 33 34 type TLSConfigItem struct { 35 Type string `json:"type"` 36 Value []byte `json:"value"` 37 } 38 39 // PodHttpChaosBaseRule defines the injection rule without source and port. 40 type PodHttpChaosBaseRule struct { 41 // Target is the object to be selected and injected, <Request|Response>. 42 Target PodHttpChaosTarget `json:"target"` 43 44 // Selector contains the rules to select target. 45 Selector PodHttpChaosSelector `json:"selector"` 46 47 // Actions contains rules to inject target. 48 Actions PodHttpChaosActions `json:"actions"` 49 } 50 51 type PodHttpChaosSelector struct { 52 // Port is a rule to select server listening on specific port. 53 // +optional 54 Port *int32 `json:"port,omitempty"` 55 56 // Path is a rule to select target by uri path in http request. 57 // +optional 58 Path *string `json:"path,omitempty"` 59 60 // Method is a rule to select target by http method in request. 61 // +optional 62 Method *string `json:"method,omitempty"` 63 64 // Code is a rule to select target by http status code in response. 65 // +optional 66 Code *int32 `json:"code,omitempty"` 67 68 // RequestHeaders is a rule to select target by http headers in request. 69 // The key-value pairs represent header name and header value pairs. 70 // +optional 71 RequestHeaders map[string]string `json:"request_headers,omitempty"` 72 73 // ResponseHeaders is a rule to select target by http headers in response. 74 // The key-value pairs represent header name and header value pairs. 75 // +optional 76 ResponseHeaders map[string]string `json:"response_headers,omitempty"` 77 } 78 79 // PodHttpChaosActions defines possible actions of HttpChaos. 80 type PodHttpChaosActions struct { 81 // Abort is a rule to abort a http session. 82 // +optional 83 Abort *bool `json:"abort,omitempty"` 84 85 // Delay represents the delay of the target request/response. 86 // A duration string is a possibly unsigned sequence of 87 // decimal numbers, each with optional fraction and a unit suffix, 88 // such as "300ms", "2h45m". 89 // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". 90 // +optional 91 Delay *string `json:"delay,omitempty"` 92 93 // Replace is a rule to replace some contents in target. 94 // +optional 95 Replace *PodHttpChaosReplaceActions `json:"replace,omitempty"` 96 97 // Patch is a rule to patch some contents in target. 98 // +optional 99 Patch *PodHttpChaosPatchActions `json:"patch,omitempty"` 100 } 101 102 // PodHttpChaosPatchBody defines the patch-body action of HttpChaos. 103 type PodHttpChaosPatchBody struct { 104 Contents PodHttpChaosBodyPatchContent `json:"contents"` 105 } 106 107 type PodHttpChaosBodyPatchContent struct { 108 // Type represents the patch type, only support `JSON` as [merge patch json](https://tools.ietf.org/html/rfc7396) currently. 109 Type string `json:"type"` 110 111 // Value is the patch contents. 112 Value string `json:"value"` 113 } 114 115 func (p *PodHttpChaosPatchBody) UnmarshalJSON(data []byte) error { 116 var pp PodHttpChaosBodyPatchContent 117 err := json.Unmarshal(data, &pp) 118 if err != nil { 119 return err 120 } 121 p.Contents = pp 122 return nil 123 } 124 125 // PodHttpChaosPatchActions defines possible patch-actions of HttpChaos. 126 type PodHttpChaosPatchActions struct { 127 // Body is a rule to patch message body of target. 128 // +optional 129 Body *PodHttpChaosPatchBody `json:"body,omitempty"` 130 131 // Queries is a rule to append uri queries of target(Request only). 132 // For example: `[["foo", "bar"], ["foo", "unknown"]]`. 133 // +optional 134 Queries [][]string `json:"queries,omitempty"` 135 136 // Headers is a rule to append http headers of target. 137 // For example: `[["Set-Cookie", "<one cookie>"], ["Set-Cookie", "<another cookie>"]]`. 138 // +optional 139 Headers [][]string `json:"headers,omitempty"` 140 } 141 142 // PodHttpChaosReplaceBody defines the replace-body of HttpChaos. 143 type PodHttpChaosReplaceBody struct { 144 Contents PodHttpChaosBodyReplaceContent `json:"contents"` 145 } 146 147 type PodHttpChaosBodyReplaceContent struct { 148 // Type represents the patch type, only support `JSON` as [merge patch json](https://tools.ietf.org/html/rfc7396) currently. 149 Type string `json:"type"` 150 151 // Value is the patch contents. 152 Value string `json:"value"` 153 } 154 155 func (p *PodHttpChaosReplaceBody) UnmarshalJSON(data []byte) error { 156 var pp PodHttpChaosBodyReplaceContent 157 err := json.Unmarshal(data, &pp) 158 if err == nil { 159 p.Contents = pp 160 return nil 161 } 162 var bys []byte 163 164 err = json.Unmarshal(data, &bys) 165 if err == nil { 166 p.Contents = PodHttpChaosBodyReplaceContent{ 167 Type: "TEXT", 168 Value: string(bys), 169 } 170 return nil 171 } 172 return err 173 } 174 175 // PodHttpChaosReplaceActions defines possible replace-actions of HttpChaos. 176 type PodHttpChaosReplaceActions struct { 177 // Path is rule to to replace uri path in http request. 178 // +optional 179 Path *string `json:"path,omitempty"` 180 181 // Method is a rule to replace http method in request. 182 // +optional 183 Method *string `json:"method,omitempty"` 184 185 // Code is a rule to replace http status code in response. 186 // +optional 187 Code *int32 `json:"code,omitempty"` 188 189 // Body is a rule to replace http message body in target. 190 // +optional 191 Body *PodHttpChaosReplaceBody `json:"body,omitempty"` 192 193 // Queries is a rule to replace uri queries in http request. 194 // For example, with value `{ "foo": "unknown" }`, the `/?foo=bar` will be altered to `/?foo=unknown`, 195 // +optional 196 Queries map[string]string `json:"queries,omitempty"` 197 198 // Headers is a rule to replace http headers of target. 199 // The key-value pairs represent header name and header value pairs. 200 // +optional 201 Headers map[string]string `json:"headers,omitempty"` 202 } 203 204 // PodHttpChaosTarget represents the type of an HttpChaos Action 205 type PodHttpChaosTarget string 206