|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Copyright (C) 2018-2023 SCANOSS.COM |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 2 of the License, or |
| 8 | + * (at your option) any later version. |
| 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 General Public License for more details. |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +package service |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/go-co-op/gocron" |
| 27 | + zlog "github.com/scanoss/zap-logging-helper/pkg/logger" |
| 28 | +) |
| 29 | + |
| 30 | +// Structure for parsing KB & Engine version from scan response. |
| 31 | +type matchStructure []struct { |
| 32 | + Server struct { |
| 33 | + Hostname string `json:"hostname"` |
| 34 | + KbVersion struct { |
| 35 | + Daily string `json:"daily"` |
| 36 | + Monthly string `json:"monthly"` |
| 37 | + } `json:"kb_version"` |
| 38 | + Version string `json:"version"` |
| 39 | + } `json:"server"` |
| 40 | +} |
| 41 | + |
| 42 | +var kbDetails string // KB Details JSON string |
| 43 | + |
| 44 | +// SetupKBDetailsCron sets up a background cron to update the KB version once an hour. |
| 45 | +func (s APIService) SetupKBDetailsCron() { |
| 46 | + scheduler := gocron.NewScheduler(time.UTC) |
| 47 | + _, err := scheduler.Every(60).Minutes().Do(s.loadKBDetails) |
| 48 | + if err != nil { |
| 49 | + zlog.S.Warnf("Problem setting up KB details cron: %v", err) |
| 50 | + return |
| 51 | + } |
| 52 | + scheduler.StartAsync() |
| 53 | +} |
| 54 | + |
| 55 | +// KBDetails retrieves the KB details and send back to the requester. |
| 56 | +func (s APIService) KBDetails(w http.ResponseWriter, r *http.Request) { |
| 57 | + reqID := getReqID(r) |
| 58 | + w.Header().Set(ResponseIDKey, reqID) |
| 59 | + zs := sugaredLogger(context.WithValue(r.Context(), RequestContextKey{}, reqID)) // Setup logger with context |
| 60 | + zs.Infof("%v request from %v", r.URL.Path, r.RemoteAddr) |
| 61 | + w.Header().Set(ContentTypeKey, ApplicationJSON) |
| 62 | + w.WriteHeader(http.StatusOK) |
| 63 | + printResponse(w, fmt.Sprintf("%s\n", kbDetails), zlog.S, true) |
| 64 | +} |
| 65 | + |
| 66 | +// loadKBDetails attempts to scan a file to load the latest KB details from the server. |
| 67 | +func (s APIService) loadKBDetails() { |
| 68 | + zs := sugaredLogger(context.TODO()) // Setup logger without context |
| 69 | + zs.Debugf("Loading latest KB details...") |
| 70 | + if len(kbDetails) == 0 { |
| 71 | + kbDetails = fmt.Sprintf(`{"kb_version": { "monthly": "%v", "daily": "%v"}}`, "unknown", "unknown") |
| 72 | + } |
| 73 | + // Load a random (hopefully non-existent) file match to extract the KB version details |
| 74 | + result, err := s.scanWfp("file=7c53a2de7dfeaa20d057db98468d6670,2321,path/to/dummy/file.txt", "", "", "", zs) |
| 75 | + if err != nil { |
| 76 | + zs.Warnf("Failed to detect KB version from eninge: %v", err) |
| 77 | + return |
| 78 | + } |
| 79 | + if len(result) > 0 { |
| 80 | + if !json.Valid([]byte(result)) { |
| 81 | + zs.Warnf("Invalid JSON response from engine for KB version: %v", result) |
| 82 | + return |
| 83 | + } |
| 84 | + resDataAny := map[string]interface{}{} |
| 85 | + err = json.Unmarshal([]byte(result), &resDataAny) // parse the response JSON into an interface map |
| 86 | + if err != nil { |
| 87 | + zs.Warnf("Failed to parse KB version from eninge response: %v - %v", result, err) |
| 88 | + return |
| 89 | + } |
| 90 | + if s.config.App.Trace { |
| 91 | + zs.Debugf("KB details JSON: %v", resDataAny) |
| 92 | + } |
| 93 | + var ms matchStructure |
| 94 | + // Go through the list of file results and extract one set of KB details |
| 95 | + for _, key := range resDataAny { |
| 96 | + data, err := json.Marshal(key) // convert the given interface to JSON |
| 97 | + if err != nil { |
| 98 | + zs.Warnf("Failed to convert KB version map to json: %v - %v", key, err) |
| 99 | + return |
| 100 | + } |
| 101 | + err = json.Unmarshal(data, &ms) |
| 102 | + if err != nil { |
| 103 | + zs.Warnf("Failed to parse KB version from eninge result: %v - %v", data, err) |
| 104 | + return |
| 105 | + } |
| 106 | + } |
| 107 | + if len(ms) > 0 { |
| 108 | + kbDetails = fmt.Sprintf(`{"kb_version": { "monthly": "%v", "daily": "%v"}}`, ms[0].Server.KbVersion.Monthly, ms[0].Server.KbVersion.Daily) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments