in grove/grovetccfg/grovetccfg.go [857:1050]
func createRulesOld(
hostname string,
dses []tc.DeliveryServiceNullable,
parents []tc.ServerV30,
dsRegexes map[string][]tc.DeliveryServiceRegex,
cdns map[string]tc.CDN,
hostParams []tc.Parameter,
dsCerts map[string]tc.CDNSSLKeys,
certDir string,
) (remap.RemapRules, error) {
rules := []remapdata.RemapRule{}
allowedIPs, err := getAllowIP(hostParams)
if err != nil {
return remap.RemapRules{}, fmt.Errorf("getting allowed IPs: %v", err)
}
weight := DefaultRuleWeight
retryNum := DefaultRetryNum
timeout := DefaultTimeout
parentSelection := DefaultRuleParentSelection
for _, ds := range dses {
protocol := *ds.Protocol
queryStringRule, err := getQueryStringRule(ds.QStringIgnore)
if err != nil {
return remap.RemapRules{}, fmt.Errorf("getting deliveryservice %v Query String Rule: %v", ds.XMLID, err)
}
cdn, ok := cdns[*ds.CDNName]
if !ok {
return remap.RemapRules{}, fmt.Errorf("deliveryservice '%v' CDN '%v' not found", *ds.XMLID, *ds.CDNName)
}
protocolStrs := []ProtocolStr{}
switch protocol {
case ProtocolHTTP:
protocolStrs = append(protocolStrs, ProtocolStr{From: "http", To: "http"})
case ProtocolHTTPS:
protocolStrs = append(protocolStrs, ProtocolStr{From: "https", To: "https"})
case ProtocolHTTPAndHTTPS:
protocolStrs = append(protocolStrs, ProtocolStr{From: "http", To: "http"})
protocolStrs = append(protocolStrs, ProtocolStr{From: "https", To: "https"})
case ProtocolHTTPToHTTPS:
protocolStrs = append(protocolStrs, ProtocolStr{From: "http", To: "https"})
protocolStrs = append(protocolStrs, ProtocolStr{From: "https", To: "https"})
}
cert, hasCert := dsCerts[*ds.XMLID]
if protocol != ProtocolHTTP {
if !hasCert {
fmt.Fprint(os.Stderr, time.Now().Format(time.RFC3339Nano)+" HTTPS delivery service: "+*ds.XMLID+" has no certificate!\n")
} else if err := createCertificateFiles(cert, certDir); err != nil {
fmt.Fprint(os.Stderr, time.Now().Format(time.RFC3339Nano)+" HTTPS delivery service "+*ds.XMLID+" failed to create certificate: "+err.Error()+"\n")
}
}
dsType := strings.ToLower(string(*ds.Type))
if !strings.HasPrefix(dsType, "http") && !strings.HasPrefix(dsType, "dns") {
fmt.Printf(time.Now().Format(time.RFC3339Nano)+" createRules skipping deliveryservice %v - unknown type %v", *ds.XMLID, *ds.Type)
continue
}
toClientHeaders, toOriginHeaders, err := makeModHdrs(ds.EdgeHeaderRewrite, ds.RemapText)
if err != nil {
return remap.RemapRules{}, errors.New("Making headers for delivery service '" + *ds.XMLID + "':" + err.Error())
}
dsRemap := ""
if ds.RemapText != nil {
dsRemap = *ds.RemapText
}
acl, err := makeACL(dsRemap)
if err != nil {
fmt.Println(time.Now().Format(time.RFC3339Nano) + " createRules skipping deliveryservice '" + *ds.XMLID + "' - unsupported ACL " + dsRemap)
continue
}
orgServerFQDN := ""
if ds.OrgServerFQDN != nil {
orgServerFQDN = *ds.OrgServerFQDN
}
for _, protocolStr := range protocolStrs {
regexes, ok := dsRegexes[*ds.XMLID]
if !ok {
return remap.RemapRules{}, fmt.Errorf("deliveryservice '%v' has no regexes", *ds.XMLID)
}
for _, dsRegex := range regexes {
rule := remapdata.RemapRule{}
pattern, patternLiteralRegex := trimLiteralRegex(dsRegex.Pattern)
rule.Name = fmt.Sprintf("%s.%s.%s.%s", *ds.XMLID, protocolStr.From, protocolStr.To, pattern)
rule.From = buildFrom(protocolStr.From, pattern, patternLiteralRegex, hostname, dsType, cdn.DomainName)
if protocolStr.From == "https" && hasCert {
rule.CertificateFile = getCertFileName(cert, certDir)
rule.CertificateKeyFile = getCertKeyFileName(cert, certDir)
// fmt.Fprintf(os.Stderr, "HTTPS delivery service: "+ds.XMLID+" certificate %+v\n", cert)
}
rule.PluginsShared = map[string]json.RawMessage{}
// if the delivery service skips the mid's ie, http_no_cache, http_live, and dns_live
// only add the url rule to the origin.
if dsTypeSkipsMid(dsType) {
var proxyURLStr = ""
proxyURL, err := url.Parse(proxyURLStr)
if err != nil {
return remap.RemapRules{}, fmt.Errorf("error parsing deliveryservice %v proxy_url: %v", *ds.XMLID, proxyURLStr)
}
ruleTo := remapdata.RemapRuleTo{
RemapRuleToBase: remapdata.RemapRuleToBase{
URL: orgServerFQDN,
Weight: &weight,
RetryNum: &retryNum,
},
RetryCodes: DefaultRetryCodes(),
ProxyURL: proxyURL,
Timeout: &timeout,
}
rule.To = append(rule.To, ruleTo)
rule.RetryNum = &retryNum
rule.Timeout = &timeout
rule.RetryCodes = DefaultRetryCodes()
rule.QueryString = queryStringRule
rule.DSCP = *ds.DSCP
rule.ConnectionClose = DefaultRuleConnectionClose
rule.Allow = acl
rule.Plugins = map[string]interface{}{}
rule.Plugins["modify_headers"] = toClientHeaders
rule.Plugins["modify_parent_request_headers"] = toOriginHeaders
remapTextJSON, err := json.Marshal(dsRemap)
if err != nil {
return remap.RemapRules{}, fmt.Errorf("parsing deliveryservice '%v' remap text '%v' marshalling JSON: %v", *ds.XMLID, dsRemap, err)
}
rule.PluginsShared[web.RemapTextKey] = remapTextJSON
} else {
for _, parent := range parents {
to, proxyURLStr := buildTo(parent, protocolStr.To, orgServerFQDN, dsType)
proxyURL, err := url.Parse(proxyURLStr)
if err != nil {
return remap.RemapRules{}, fmt.Errorf("error parsing deliveryservice %v parent %v proxy_url: %v", *ds.XMLID, parent.HostName, proxyURLStr)
}
ruleTo := remapdata.RemapRuleTo{
RemapRuleToBase: remapdata.RemapRuleToBase{
URL: to,
Weight: &weight,
RetryNum: &retryNum,
},
ProxyURL: proxyURL,
RetryCodes: DefaultRetryCodes(),
Timeout: &timeout,
}
rule.To = append(rule.To, ruleTo)
// TODO get from TO?
rule.RetryNum = &retryNum
rule.Timeout = &timeout
rule.RetryCodes = DefaultRetryCodes()
rule.QueryString = queryStringRule
rule.DSCP = *ds.DSCP
rule.ConnectionClose = DefaultRuleConnectionClose
rule.ParentSelection = &parentSelection
rule.Allow = acl
rule.Plugins = map[string]interface{}{}
rule.Plugins["modify_headers"] = toClientHeaders
rule.Plugins["modify_parent_request_headers"] = toOriginHeaders
remapTextJSON, err := json.Marshal(dsRemap)
if err != nil {
return remap.RemapRules{}, fmt.Errorf("parsing deliveryservice '%v' remap text '%v' marshalling JSON: %v", *ds.XMLID, dsRemap, err)
}
rule.PluginsShared[web.RemapTextKey] = remapTextJSON
}
}
rules = append(rules, rule)
}
}
}
globalPlugins := map[string]interface{}{}
serverHeader := web.Hdr{Name: "Server", Value: "Grove/0.33"}
setHeaders := []web.Hdr{}
setHeaders = append(setHeaders, serverHeader)
globalHeaders := web.ModHdrs{Set: setHeaders}
globalPlugins["modify_response_headers_global"] = globalHeaders
remapRules := remap.RemapRules{
Rules: rules,
RetryCodes: DefaultRetryCodes(),
Timeout: &timeout,
ParentSelection: &parentSelection,
Stats: remapdata.RemapRulesStats{Allow: allowedIPs},
Plugins: globalPlugins,
}
return remapRules, nil
}