|
| 1 | +// This file is part of MinIO Console Server |
| 2 | +// Copyright (c) 2021 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package operatorapi |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "crypto/sha1" |
| 22 | + "crypto/tls" |
| 23 | + "encoding/json" |
| 24 | + "fmt" |
| 25 | + "io" |
| 26 | + "log" |
| 27 | + "net/http" |
| 28 | + "net/http/cookiejar" |
| 29 | + url2 "net/url" |
| 30 | + "strings" |
| 31 | + |
| 32 | + v2 "github.com/minio/operator/pkg/apis/minio.min.io/v2" |
| 33 | + |
| 34 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 35 | + |
| 36 | + "github.com/minio/console/cluster" |
| 37 | + |
| 38 | + "github.com/minio/console/pkg/auth" |
| 39 | +) |
| 40 | + |
| 41 | +func serveProxy(responseWriter http.ResponseWriter, req *http.Request) { |
| 42 | + urlParts := strings.Split(req.URL.Path, "/") |
| 43 | + |
| 44 | + if len(urlParts) < 5 { |
| 45 | + log.Println(len(urlParts)) |
| 46 | + return |
| 47 | + } |
| 48 | + namespace := urlParts[3] |
| 49 | + tenantName := urlParts[4] |
| 50 | + |
| 51 | + // validate the tenantName |
| 52 | + |
| 53 | + token, err := auth.GetTokenFromRequest(req) |
| 54 | + if err != nil { |
| 55 | + log.Println(err) |
| 56 | + responseWriter.WriteHeader(401) |
| 57 | + return |
| 58 | + } |
| 59 | + claims, err := auth.SessionTokenAuthenticate(token) |
| 60 | + if err != nil { |
| 61 | + log.Printf("Unable to validate the session token %s: %v\n", token, err) |
| 62 | + responseWriter.WriteHeader(401) |
| 63 | + |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + //STSSessionToken := currToken.Value |
| 68 | + STSSessionToken := claims.STSSessionToken |
| 69 | + |
| 70 | + opClientClientSet, err := cluster.OperatorClient(STSSessionToken) |
| 71 | + if err != nil { |
| 72 | + log.Println(err) |
| 73 | + responseWriter.WriteHeader(404) |
| 74 | + return |
| 75 | + } |
| 76 | + opClient := operatorClient{ |
| 77 | + client: opClientClientSet, |
| 78 | + } |
| 79 | + tenant, err := opClient.TenantGet(req.Context(), namespace, tenantName, metav1.GetOptions{}) |
| 80 | + if err != nil { |
| 81 | + log.Println(err) |
| 82 | + responseWriter.WriteHeader(502) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + nsTenant := fmt.Sprintf("%s/%s", tenant.Namespace, tenant.Name) |
| 87 | + |
| 88 | + tenantSchema := "http" |
| 89 | + tenantPort := ":9090" |
| 90 | + if tenant.AutoCert() || tenant.ConsoleExternalCert() { |
| 91 | + tenantSchema = "https" |
| 92 | + tenantPort = ":9443" |
| 93 | + } |
| 94 | + tenantURL := fmt.Sprintf("%s://%s.%s.svc.%s%s", tenantSchema, tenant.ConsoleCIServiceName(), tenant.Namespace, v2.GetClusterDomain(), tenantPort) |
| 95 | + // for development |
| 96 | + //tenantURL = "http://localhost:9091" |
| 97 | + //tenantURL = "https://localhost:9443" |
| 98 | + |
| 99 | + h := sha1.New() |
| 100 | + h.Write([]byte(nsTenant)) |
| 101 | + log.Printf("Proxying request for %s/%s", namespace, tenantName) |
| 102 | + tenantCookieName := fmt.Sprintf("token-%x", string(h.Sum(nil))) |
| 103 | + tenantCookie, err := req.Cookie(tenantCookieName) |
| 104 | + if err != nil { |
| 105 | + // login to tenantName |
| 106 | + loginURL := fmt.Sprintf("%s/api/v1/login", tenantURL) |
| 107 | + |
| 108 | + // get the tenant credentials |
| 109 | + clientSet, err := cluster.K8sClient(STSSessionToken) |
| 110 | + if err != nil { |
| 111 | + log.Println(err) |
| 112 | + responseWriter.WriteHeader(500) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + currentSecret, err := clientSet.CoreV1().Secrets(namespace).Get(req.Context(), tenant.Spec.CredsSecret.Name, metav1.GetOptions{}) |
| 117 | + if err != nil { |
| 118 | + log.Println(err) |
| 119 | + responseWriter.WriteHeader(500) |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + data := map[string]string{ |
| 124 | + "accessKey": string(currentSecret.Data["accesskey"]), |
| 125 | + "secretKey": string(currentSecret.Data["secretkey"]), |
| 126 | + } |
| 127 | + payload, _ := json.Marshal(data) |
| 128 | + |
| 129 | + loginReq, err := http.NewRequest(http.MethodPost, loginURL, bytes.NewReader(payload)) |
| 130 | + if err != nil { |
| 131 | + log.Println(err) |
| 132 | + responseWriter.WriteHeader(500) |
| 133 | + return |
| 134 | + } |
| 135 | + loginReq.Header.Add("Content-Type", "application/json") |
| 136 | + |
| 137 | + // FIXME: in the future we should use restapi.GetConsoleSTSClient() |
| 138 | + tr := &http.Transport{ |
| 139 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 140 | + } |
| 141 | + client := &http.Client{Transport: tr} |
| 142 | + |
| 143 | + loginResp, err := client.Do(loginReq) |
| 144 | + if err != nil { |
| 145 | + log.Println(err) |
| 146 | + responseWriter.WriteHeader(500) |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + for _, c := range loginResp.Cookies() { |
| 151 | + if c.Name == "token" { |
| 152 | + tenantCookie = c |
| 153 | + c := &http.Cookie{ |
| 154 | + Name: tenantCookieName, |
| 155 | + Value: c.Value, |
| 156 | + Path: c.Path, |
| 157 | + Expires: c.Expires, |
| 158 | + HttpOnly: c.HttpOnly, |
| 159 | + } |
| 160 | + |
| 161 | + http.SetCookie(responseWriter, c) |
| 162 | + break |
| 163 | + } |
| 164 | + } |
| 165 | + defer loginResp.Body.Close() |
| 166 | + } |
| 167 | + |
| 168 | + targetURL, err := url2.Parse(tenantURL) |
| 169 | + if err != nil { |
| 170 | + log.Println(err) |
| 171 | + responseWriter.WriteHeader(500) |
| 172 | + return |
| 173 | + } |
| 174 | + targetURL.Path = strings.Replace(req.URL.Path, fmt.Sprintf("/api/proxy/%s/%s", tenant.Namespace, tenant.Name), "", -1) |
| 175 | + |
| 176 | + proxiedCookie := &http.Cookie{ |
| 177 | + Name: "token", |
| 178 | + Value: tenantCookie.Value, |
| 179 | + Path: tenantCookie.Path, |
| 180 | + Expires: tenantCookie.Expires, |
| 181 | + HttpOnly: tenantCookie.HttpOnly, |
| 182 | + } |
| 183 | + |
| 184 | + proxyCookieJar, _ := cookiejar.New(nil) |
| 185 | + proxyCookieJar.SetCookies(targetURL, []*http.Cookie{proxiedCookie}) |
| 186 | + |
| 187 | + tr := &http.Transport{ |
| 188 | + // FIXME: use restapi.GetConsoleSTSClient() |
| 189 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 190 | + } |
| 191 | + client := &http.Client{Transport: tr, |
| 192 | + Jar: proxyCookieJar, |
| 193 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 194 | + return http.ErrUseLastResponse |
| 195 | + }} |
| 196 | + |
| 197 | + proxRequest, err := http.NewRequest(req.Method, targetURL.String(), req.Body) |
| 198 | + if err != nil { |
| 199 | + log.Println(err) |
| 200 | + responseWriter.WriteHeader(500) |
| 201 | + return |
| 202 | + } |
| 203 | + |
| 204 | + for _, v := range req.Header.Values("Content-Type") { |
| 205 | + proxRequest.Header.Add("Content-Type", v) |
| 206 | + } |
| 207 | + |
| 208 | + resp, err := client.Do(proxRequest) |
| 209 | + if err != nil { |
| 210 | + log.Println(err) |
| 211 | + responseWriter.WriteHeader(500) |
| 212 | + return |
| 213 | + } |
| 214 | + |
| 215 | + for hk, hv := range resp.Header { |
| 216 | + if hk != "X-Frame-Options" { |
| 217 | + for _, v := range hv { |
| 218 | + responseWriter.Header().Add(hk, v) |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + // Allow iframes |
| 223 | + responseWriter.Header().Set("X-Frame-Options", "SAMEORIGIN") |
| 224 | + responseWriter.Header().Set("X-XSS-Protection", "1") |
| 225 | + |
| 226 | + io.Copy(responseWriter, resp.Body) |
| 227 | + |
| 228 | +} |
0 commit comments