This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
/
disable_uaa_hsts.go
91 lines (76 loc) · 2.03 KB
/
disable_uaa_hsts.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
package commands
import (
"bytes"
"encoding/xml"
"io/ioutil"
"os"
"strings"
"golang.org/x/net/html/charset"
"provisioner/provisioner"
)
type DisableUAAHSTS struct {
WebXMLPath string
}
func (d *DisableUAAHSTS) Run() error {
var webXMLData WebApp
webXMLContents, err := ioutil.ReadFile(d.WebXMLPath)
if err != nil {
return err
}
decoder := xml.NewDecoder(bytes.NewReader(webXMLContents))
decoder.CharsetReader = charset.NewReaderLabel
if err := decoder.Decode(&webXMLData); err != nil {
return err
}
hstsFilter := Filter{
FilterName: "httpHeaderSecurity",
FilterClass: "org.apache.catalina.filters.HttpHeaderSecurityFilter",
InitParam: InitParam{
ParamName: "hstsEnabled",
ParamValue: "false",
},
AsyncSupported: true,
}
hstsFilterExists := false
for _, filter := range webXMLData.Filters {
if strings.TrimSpace(filter.FilterName) == strings.TrimSpace(hstsFilter.FilterName) &&
strings.TrimSpace(filter.FilterClass) == strings.TrimSpace(hstsFilter.FilterClass) &&
strings.TrimSpace(filter.InitParam.ParamName) == strings.TrimSpace(hstsFilter.InitParam.ParamName) {
hstsFilterExists = true
}
}
if hstsFilterExists {
webXMLData.Filters = nil
} else {
webXMLData.Filters = []Filter{hstsFilter}
}
webXMLFile, err := os.OpenFile(d.WebXMLPath, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
defer webXMLFile.Close()
encoder := xml.NewEncoder(webXMLFile)
encoder.Indent("", " ")
if err := encoder.Encode(&webXMLData); err != nil {
panic(err)
}
return nil
}
func (*DisableUAAHSTS) Distro() string {
return provisioner.DistributionPCF
}
type WebApp struct {
XMLName xml.Name `xml:"web-app"`
Filters []Filter `xml:"filter"`
AllXML string `xml:",innerxml"`
}
type Filter struct {
FilterName string `xml:"filter-name"`
FilterClass string `xml:"filter-class"`
InitParam InitParam `xml:"init-param"`
AsyncSupported bool `xml:"async-supported"`
}
type InitParam struct {
ParamName string `xml:"param-name"`
ParamValue string `xml:"param-value"`
}