Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 53 additions & 31 deletions services/proxy/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,9 +872,17 @@ func (m *DefaultManager) registerWithResult(route route) (created bool, err erro

func (m *DefaultManager) registerUnlocked(route route) (bool, error) {
serverID := "kool-" + strconv.Itoa(route.Listen)
server := caddyServerConfig(route)
serverBody, _ := json.Marshal(server)
serverURL := m.adminURL + "/config/apps/http/servers/" + serverID
routeConfig := map[string]interface{}{
"@id": m.routeID(route),
"match": []interface{}{map[string]interface{}{"host": m.routeHosts(route)}},
"handle": []interface{}{map[string]interface{}{
"@id": m.projectMarker() + "-" + m.routeID(route) + "-" + m.generation,
"handler": "reverse_proxy",
"upstreams": []interface{}{map[string]string{"dial": m.alias(route.Service) + ":" + strconv.Itoa(route.Target)}},
}},
"terminal": true,
}
response, err := m.request(http.MethodGet, serverURL, nil)
if err != nil {
return false, err
Expand Down Expand Up @@ -906,6 +914,11 @@ func (m *DefaultManager) registerUnlocked(route route) (bool, error) {
}
}
if serverMissing {
// Create the server with its listener mode and route in one change: each
// change replaces Caddy's admin server, which can drop the next request.
server := caddyServerConfig(route)
server["routes"] = []interface{}{routeConfig}
serverBody, _ := json.Marshal(server)
response, err = m.request(http.MethodPost, serverURL, serverBody)
if err != nil {
return false, err
Expand All @@ -914,6 +927,7 @@ func (m *DefaultManager) registerUnlocked(route route) (bool, error) {
if response.StatusCode >= 400 {
return false, responseError(response)
}
return true, nil
}
if route.HTTPS {
if err = m.setTLSPolicy(serverURL); err != nil {
Expand All @@ -922,17 +936,6 @@ func (m *DefaultManager) registerUnlocked(route route) (bool, error) {
} else if err = m.disableAutomaticHTTPS(serverURL); err != nil {
return false, err
}

routeConfig := map[string]interface{}{
"@id": m.routeID(route),
"match": []interface{}{map[string]interface{}{"host": m.routeHosts(route)}},
"handle": []interface{}{map[string]interface{}{
"@id": m.projectMarker() + "-" + m.routeID(route) + "-" + m.generation,
"handler": "reverse_proxy",
"upstreams": []interface{}{map[string]string{"dial": m.alias(route.Service) + ":" + strconv.Itoa(route.Target)}},
}},
"terminal": true,
}
return m.upsertRouteUnlocked(serverURL, routeConfig)
}

Expand Down Expand Up @@ -1411,31 +1414,46 @@ func (m *DefaultManager) registerTLSUnlocked(routes []route) error {
if err != nil {
return err
}
status := response.StatusCode
_ = response.Body.Close()
if status == http.StatusNotFound {
body := []byte(`{"automation":{"policies":[]}}`)
if response, err = m.request(http.MethodPost, tlsURL, body); err != nil {
return err
}
if response.StatusCode >= 400 && response.StatusCode != http.StatusNotFound {
defer func() { _ = response.Body.Close() }()
if response.StatusCode >= 400 {
return responseError(response)
return responseError(response)
}
// Caddy answers null for a missing app; a 404 means the same.
var tlsApp map[string]interface{}
if response.StatusCode != http.StatusNotFound {
if err = json.NewDecoder(response.Body).Decode(&tlsApp); err != nil && err != io.EOF {
_ = response.Body.Close()
return err
}
} else if status >= 400 {
return fmt.Errorf("caddy Admin API returned %s", response.Status)
}

if err = m.deleteRoute(m.tlsID()); err != nil {
if err = response.Body.Close(); err != nil {
return err
}

policy := map[string]interface{}{
"@id": m.tlsID(),
"subjects": subjects,
"issuers": []interface{}{map[string]string{"module": "internal"}},
}
body, _ := json.Marshal(policy)
response, err = m.request(http.MethodPost, tlsURL+"/automation/policies", body)
// Replace the policy by ID rather than appending it, so that sending this
// write twice never leaves two policies with the same ID. PATCH replaces an
// existing value and POST sets a missing one; neither appends.
apps := map[string]interface{}{}
if tlsApp != nil {
apps["tls"] = tlsApp
}
policies, automation := caddyPolicies(apps)
policies = replaceObjectByID(policies, m.tlsID(), policy, true)
method, url, value := http.MethodPatch, tlsURL+"/automation/policies", interface{}(policies)
if _, listExists := automation["policies"].([]interface{}); !listExists {
automation["policies"] = policies
method, url, value = http.MethodPatch, tlsURL, apps["tls"]
if tlsApp == nil {
method = http.MethodPost
}
}
body, _ := json.Marshal(value)
response, err = m.request(method, url, body)
if err != nil {
return err
}
Expand Down Expand Up @@ -1655,7 +1673,8 @@ func replaceObjectByID(objects []interface{}, id string, replacement interface{}
replaced := false
for _, object := range objects {
if caddyObjectID(object) == id {
if replacementPresent {
// Caddy before 2.11 accepts duplicate IDs; keep only one.
if replacementPresent && !replaced {
result = append(result, replacement)
}
replaced = true
Expand All @@ -1676,9 +1695,12 @@ func (m *DefaultManager) request(method, url string, body []byte) (*http.Respons
return nil, err
}
request.Header.Set("Content-Type", "application/json")
response, err := m.http.Do(request)
// Each configuration change replaces Caddy's admin server, and the old
// server can close a connection before it reads the request on it.
// server closes the connections it served, so never reuse one.
request.Close = true
response, err := m.http.Do(request)
// The old server can also drop a new connection before it reads the
// request on it.
dropped := errors.Is(err, io.EOF) || errors.Is(err, syscall.ECONNRESET)
if !dropped || attempt == 2 {
return response, err
Expand Down
Loading
Loading