github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/cmd/jenkins-operator/logs.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "net/http" 22 "regexp" 23 "strings" 24 25 "github.com/sirupsen/logrus" 26 27 "k8s.io/test-infra/prow/jenkins" 28 ) 29 30 // Matches letters, numbers, hyphens, and underscores. 31 var objReg = regexp.MustCompile(`^[\w-]+$`) 32 33 func handleLog(jc *jenkins.Client) http.HandlerFunc { 34 return func(w http.ResponseWriter, r *http.Request) { 35 w.Header().Set("Cache-Control", "no-cache") 36 w.Header().Set("Access-Control-Allow-Origin", "*") 37 w.Header().Set("Access-Control-Allow-Methods", "GET") 38 39 // Needs to be a GET request. 40 if r.Method != http.MethodGet { 41 http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed) 42 return 43 } 44 45 // Needs to get Jenkins logs. 46 if !strings.HasSuffix(r.URL.Path, "consoleText") { 47 http.Error(w, "403 Forbidden: Request may only access raw Jenkins logs", http.StatusForbidden) 48 return 49 } 50 51 log, err := jc.GetSkipMetrics(r.URL.Path) 52 if err != nil { 53 http.Error(w, fmt.Sprintf("Log not found: %v", err), http.StatusNotFound) 54 logrus.WithError(err).Warning(fmt.Sprintf("Cannot get logs from Jenkins (GET %s).", r.URL.Path)) 55 return 56 } 57 58 if _, err = w.Write(log); err != nil { 59 logrus.WithError(err).Warning("Error writing log.") 60 } 61 } 62 }