-
Notifications
You must be signed in to change notification settings - Fork 13
/
clients.go
124 lines (106 loc) · 2.93 KB
/
clients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Copyright 2014, Sander Botman
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"crypto/md5"
"crypto/sha256"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"regexp"
"sort"
"strconv"
"github.com/gorilla/mux"
)
// Add files type and functions for the Sort interface
type files []string
func (f files) Len() int {
return len(f)
}
func (f files) Less(i, j int) bool {
re := regexp.MustCompile(`.*?(\d+)\.(\d+)\.(\d+)-?(\d+)?.*`)
parts_i := re.FindStringSubmatch(f[i])
parts_j := re.FindStringSubmatch(f[j])
for idx := 1; idx < len(parts_i); idx++ {
// Convert the part to a int
i, err := strconv.Atoi(parts_i[idx])
if err != nil {
return false
}
// Convert the part to a int
j, err := strconv.Atoi(parts_j[idx])
if err != nil {
return false
}
// If equal, move on to the next part
if i == j {
continue
}
// Compare and do a descending sort
if i > j {
return true
}
return false
}
return false
}
func (f files) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
func processDownload(w http.ResponseWriter, r *http.Request) {
path := getFilePath(r)
dir := filepath.Join(cfg.ChefClients.Path, path)
targetfile, err := getTargetFile(dir, r.FormValue("v"))
if err != nil {
errorHandler(w, err.Error(), http.StatusBadRequest)
}
if targetfile != "" {
targetpath := path + targetfile[len(dir):]
targeturl := getChefBaseURL() + "/chef-guard/clients/" + targetpath
// For download calls, redirect to the actuall file
if mux.Vars(r)["type"] == "download" {
http.Redirect(w, r, targeturl, http.StatusFound)
}
// For metadata calls, return the requested meta data
if mux.Vars(r)["type"] == "metadata" {
data, err := ioutil.ReadFile(targetfile)
if err != nil {
errorHandler(w, "Failed to read client file: %s"+err.Error(), http.StatusBadRequest)
}
targetmd5 := md5.Sum(data)
targetsha := sha256.Sum256(data)
fmt.Fprintf(w, "url %s\nmd5 %x\nsha256 %x", targeturl, targetmd5, targetsha)
}
}
}
func getFilePath(r *http.Request) string {
return filepath.Join(r.FormValue("p"), r.FormValue("pv"), r.FormValue("m"))
}
func getTargetFile(dir, version string) (string, error) {
if version == "latest" {
version = "."
}
filelist, err := filepath.Glob(dir + "/*" + version + "*")
if err != nil {
return "", fmt.Errorf("Failed to read clients from disk: %s", err)
}
if filelist != nil {
sort.Sort(files(filelist))
return filelist[0], nil
}
return "", nil
}