forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
172 lines (159 loc) · 5.37 KB
/
template.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package catalog
import (
"bytes"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/templatecontent"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
managementschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
client "github.com/rancher/types/client/management/v3"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func getChartIconURL(t TemplateWrapper, templateVersionName string) (string, error) {
templateVersion, err := t.TemplateVersions.Get("", templateVersionName)
if err != nil {
return "", err
}
idStruct, err := url.Parse(templateVersion.Spec.ExternalID)
if err != nil {
return "", errors.WithMessage(err, "failed to parse external ID")
}
id := idStruct.Query().Get("template")
tag, ok := templateVersion.Spec.Files[id+"/Chart.yaml"]
if !ok {
return "", fmt.Errorf("unable to get chart data from template version for %s", id)
}
data, err := templatecontent.GetTemplateFromTag(tag, t.TemplateContentClients)
if err != nil {
return "", err
}
resp := findIconURL(data)
if resp == "" {
return "", fmt.Errorf("icon not found")
}
return resp, nil
}
func findIconURL(s string) string {
i := strings.Index(s, "icon:")
if i < 0 {
return ""
}
s = s[i:]
target := strings.Split(s, "\n")[0]
i = strings.Index(target, "http")
if i < 0 {
return ""
}
target = target[i:]
return target
}
// indexInterfaceSlice takes a list of interfaces, returns the first, asserts its a map[string]interface{}
// and uses the provided index to retrieve the value
func indexInterfaceSlice(value interface{}, key string) string {
v := convert.Singular(value)
if v != nil {
v2, ok := v.(map[string]interface{})
if ok {
return v2[key].(string)
}
}
return ""
}
func (t TemplateWrapper) TemplateFormatter(apiContext *types.APIContext, resource *types.RawResource) {
// version links
resource.Values["versionLinks"] = extractVersionLinks(apiContext, resource)
//icon
delete(resource.Values, "icon")
ic, ok := resource.Values["iconFilename"]
if ok {
if strings.HasPrefix(ic.(string), "http:") || strings.HasPrefix(ic.(string), "https:") {
resource.Links["icon"] = ic.(string)
} else {
// before marking an icon as from file, ensure this is not a chart from an upgrade
// if so, use the url in the chart.yaml
ver, ok := resource.Values["versions"]
if ok {
v := indexInterfaceSlice(ver, "version")
res, err := getChartIconURL(t, resource.ID+"-"+v)
if err == nil {
resource.Links["icon"] = res
// also update the template itself
template, err := t.TemplateClients.Get(resource.ID, metav1.GetOptions{})
if err != nil {
logrus.Warnf("unable to get template %s", err)
} else {
template = template.DeepCopy()
template.Spec.IconFilename = res
_, err = t.TemplateClients.Update(template)
if err != nil {
logrus.Warnf("unable to update template icon %s", template.Name)
}
}
} else { //this is chart with a bundled icon
resource.Links["icon"] = apiContext.URLBuilder.Link("icon", resource)
}
} else { //if no versions are found, fallback to hosting the icon
resource.Links["icon"] = apiContext.URLBuilder.Link("icon", resource)
}
}
} else {
resource.Links["icon"] = apiContext.URLBuilder.Link("icon", resource)
}
//catalog link
catalogSchema := apiContext.Schemas.Schema(&managementschema.Version, client.CatalogType)
catalogName := strings.Split(resource.ID, "-")[0]
resource.Links["catalog"] = apiContext.URLBuilder.ResourceLinkByID(catalogSchema, catalogName)
// delete category
delete(resource.Values, "category")
// delete versions
delete(resource.Values, "versions")
}
type TemplateWrapper struct {
TemplateContentClients v3.TemplateContentInterface
TemplateVersions v3.TemplateVersionLister
TemplateClients v3.TemplateInterface
}
func (t TemplateWrapper) TemplateIconHandler(apiContext *types.APIContext, next types.RequestHandler) error {
switch apiContext.Link {
case "icon":
template := &client.Template{}
if err := access.ByID(apiContext, apiContext.Version, apiContext.Type, apiContext.ID, template); err != nil {
return err
}
if strings.HasPrefix(template.IconFilename, "http:") || strings.HasPrefix(template.IconFilename, "https:") {
http.Error(apiContext.Response, "", http.StatusNoContent)
return nil
}
data, err := templatecontent.GetTemplateFromTag(template.Icon, t.TemplateContentClients)
if err != nil {
return err
}
t, err := time.Parse(time.RFC3339, template.Created)
if err != nil {
return err
}
value, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return err
}
iconReader := bytes.NewReader(value)
apiContext.Response.Header().Set("Cache-Control", "private, max-age=604800")
// add security headers (similar to raw.githubusercontent)
apiContext.Response.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox")
apiContext.Response.Header().Set("X-Content-Type-Options", "nosniff")
http.ServeContent(apiContext.Response, apiContext.Request, template.IconFilename, t, iconReader)
return nil
default:
return httperror.NewAPIError(httperror.NotFound, "not found")
}
}