2019-06-30 16:07:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Copyright 2015 Matthew Holt and The Caddy Authors  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// 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.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								package  caddytls  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import  (  
						 
					
						
							
								
									
										
										
										
											2022-08-31 17:01:30 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"context" 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"crypto/tls" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"encoding/json" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"fmt" 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-12 11:36:20 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"log" 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"net" 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"net/http" 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-12 11:36:20 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"runtime/debug" 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"strings" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"sync" 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 20:36:29 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"time" 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/caddyserver/certmagic" 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/libdns/libdns" 
							 
						 
					
						
							
								
									
										
										
										
											2019-10-28 14:39:37 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"go.uber.org/zap" 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-13 19:16:37 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"go.uber.org/zap/zapcore" 
							 
						 
					
						
							
								
									
										
										
										
											2023-08-14 23:41:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/caddyserver/caddy/v2" 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/caddyserver/caddy/v2/internal" 
							 
						 
					
						
							
								
									
										
										
										
											2023-08-14 23:41:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/caddyserver/caddy/v2/modules/caddyevents" 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  init ( )  {  
						 
					
						
							
								
									
										
										
										
											2019-08-21 10:46:35 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									caddy . RegisterModule ( TLS { } ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									caddy . RegisterModule ( AutomateLoader { } ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								var  (  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									certCache    * certmagic . Cache 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									certCacheMu  sync . RWMutex 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// TLS provides TLS facilities including certificate  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// loading and management, client auth, and more.  
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								type  TLS  struct  {  
						 
					
						
							
								
									
										
										
										
											2023-06-04 22:15:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Certificates to load into memory for quick recall during 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// TLS handshakes. Each key is the name of a certificate 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-04 22:15:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// loader module. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// The "automate" certificate loader module can be used to 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// specify a list of subjects that need certificates to be 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// managed automatically, including subdomains that may 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// already be covered by a managed wildcard certificate. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// The first matching automation policy will be used 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// to manage automated certificate(s). 
							 
						 
					
						
							
								
									
										
										
										
											2023-06-04 22:15:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// All loaded certificates get pooled 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// into the same cache and may be used to complete TLS 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// handshakes for the relevant server names (SNI). 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Certificates loaded manually (anything other than 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// "automate") are not automatically managed and will 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// have to be refreshed manually before they expire. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									CertificatesRaw  caddy . ModuleMap  ` json:"certificates,omitempty" caddy:"namespace=tls.certificates" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-24 18:31:01 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Configures certificate automation. 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									Automation  * AutomationConfig  ` json:"automation,omitempty" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Configures session ticket ephemeral keys (STEKs). 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									SessionTickets  * SessionTicketService  ` json:"session_tickets,omitempty" ` 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-05 11:14:39 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Configures the in-memory certificate cache. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Cache  * CertCacheOptions  ` json:"cache,omitempty" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-12 16:09:02 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Disables OCSP stapling for manually-managed certificates only. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// To configure OCSP stapling for automated certificates, use an 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// automation policy instead. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Disabling OCSP stapling puts clients at greater risk, reduces their 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// privacy, and usually lowers client performance. It is NOT recommended 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// to disable this unless you are able to justify the costs. 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-12 16:09:02 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// EXPERIMENTAL. Subject to change. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									DisableOCSPStapling  bool  ` json:"disable_ocsp_stapling,omitempty" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-04 15:00:15 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Disables checks in certmagic that the configured storage is ready 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// and able to handle writing new content to it. These checks are 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// intended to prevent information loss (newly issued certificates), but 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// can be expensive on the storage. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Disabling these checks should only be done when the storage 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// can be trusted to have enough capacity and no other problems. 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-04 15:00:15 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// EXPERIMENTAL. Subject to change. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									DisableStorageCheck  bool  ` json:"disable_storage_check,omitempty" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-11-05 12:47:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Disables the automatic cleanup of the storage backend. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// This is useful when TLS is not being used to store certificates 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// and the user wants run their server in a read-only mode. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Storage cleaning creates two files: instance.uuid and last_clean.json. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// The instance.uuid file is used to identify the instance of Caddy 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// in a cluster. The last_clean.json file is used to store the last 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// time the storage was cleaned. 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
									
										
										
										
											2024-11-05 12:47:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// EXPERIMENTAL. Subject to change. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									DisableStorageClean  bool  ` json:"disable_storage_clean,omitempty" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Enable Encrypted ClientHello (ECH). ECH protects the server name 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// (SNI) and other sensitive parameters of a normally-plaintext TLS 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// ClientHello during a handshake. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// EXPERIMENTAL: Subject to change. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									EncryptedClientHello  * ECH  ` json:"encrypted_client_hello,omitempty" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// The default DNS provider module to use when a DNS module is needed. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// EXPERIMENTAL: Subject to change. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									DNSRaw  json . RawMessage  ` json:"dns,omitempty" caddy:"namespace=dns.providers inline_key=name" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									dns     any              // technically, it should be any/all of the libdns interfaces (RecordSetter, RecordAppender, etc.) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									certificateLoaders  [ ] CertificateLoader 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									automateNames       map [ string ] struct { } 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-14 11:58:28 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ctx                 caddy . Context 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									storageCleanTicker  * time . Ticker 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									storageCleanStop    chan  struct { } 
							 
						 
					
						
							
								
									
										
										
										
											2019-10-28 14:39:37 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									logger              * zap . Logger 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-31 17:01:30 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									events              * caddyevents . App 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									serverNames    map [ string ] struct { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									serverNamesMu  * sync . Mutex 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// set of subjects with managed certificates, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// and hashes of manually-loaded certificates 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// (managing's value is an optional issuer key, for distinction) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									managing ,  loaded  map [ string ] string 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-08-21 10:46:35 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// CaddyModule returns the Caddy module information.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( TLS )  CaddyModule ( )  caddy . ModuleInfo  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  caddy . ModuleInfo { 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ID :   "tls" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										New :  func ( )  caddy . Module  {  return  new ( TLS )  } , 
							 
						 
					
						
							
								
									
										
										
										
											2019-08-21 10:46:35 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-26 12:35:39 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Provision sets up the configuration for the TLS app.  
						 
					
						
							
								
									
										
										
										
											2019-06-14 11:58:28 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( t  * TLS )  Provision ( ctx  caddy . Context )  error  {  
						 
					
						
							
								
									
										
										
										
											2022-08-31 17:01:30 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									eventsAppIface ,  err  :=  ctx . App ( "events" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  fmt . Errorf ( "getting events app: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									t . events  =  eventsAppIface . ( * caddyevents . App ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-05-16 16:05:38 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									t . ctx  =  ctx 
							 
						 
					
						
							
								
									
										
										
										
											2022-09-16 16:55:30 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									t . logger  =  ctx . Logger ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-05-09 00:37:27 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									repl  :=  caddy . NewReplacer ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									t . managing ,  t . loaded  =  make ( map [ string ] string ) ,  make ( map [ string ] string ) 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									t . serverNames  =  make ( map [ string ] struct { } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									t . serverNamesMu  =  new ( sync . Mutex ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// set up default DNS module, if any, and make sure it implements all the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// common libdns interfaces, since it could be used for a variety of things 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// (do this before provisioning other modules, since they may rely on this) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  len ( t . DNSRaw )  >  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										dnsMod ,  err  :=  ctx . LoadModule ( t ,  "DNSRaw" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "loading overall DNS provider module: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										switch  dnsMod . ( type )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										case  interface  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											libdns . RecordAppender 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											libdns . RecordDeleter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											libdns . RecordGetter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											libdns . RecordSetter 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										default : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "DNS module does not implement the most common libdns interfaces: %T" ,  dnsMod ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . dns  =  dnsMod 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// set up a new certificate cache; this (re)loads all certificates 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cacheOpts  :=  certmagic . CacheOptions { 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										GetConfigForCert :  func ( cert  certmagic . Certificate )  ( * certmagic . Config ,  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  t . getConfigForName ( cert . Names [ 0 ] ) ,  nil 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} , 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-30 15:18:14 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Logger :  t . logger . Named ( "cache" ) , 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . Automation  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										cacheOpts . OCSPCheckInterval  =  time . Duration ( t . Automation . OCSPCheckInterval ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										cacheOpts . RenewCheckInterval  =  time . Duration ( t . Automation . RenewCheckInterval ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2020-06-05 11:14:39 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  t . Cache  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										cacheOpts . Capacity  =  t . Cache . Capacity 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-30 15:18:14 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  cacheOpts . Capacity  <=  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										cacheOpts . Capacity  =  10000 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									certCacheMu . Lock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  certCache  ==  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										certCache  =  certmagic . NewCache ( cacheOpts ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										certCache . SetOptions ( cacheOpts ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									certCacheMu . Unlock ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// certificate loaders 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									val ,  err  :=  ctx . LoadModule ( t ,  "CertificatesRaw" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "loading certificate loader modules: %s" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-02 16:39:09 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									for  modName ,  modIface  :=  range  val . ( map [ string ] any )  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  modName  ==  "automate"  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-09 13:09:48 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// special case; these will be loaded in later using our automation facilities, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// which we want to avoid doing during provisioning 
							 
						 
					
						
							
								
									
										
										
										
											2020-01-06 08:10:20 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  automateNames ,  ok  :=  modIface . ( * AutomateLoader ) ;  ok  &&  automateNames  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  t . automateNames  ==  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													t . automateNames  =  make ( map [ string ] struct { } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-27 17:16:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												repl  :=  caddy . NewReplacer ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												for  _ ,  sub  :=  range  * automateNames  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													t . automateNames [ repl . ReplaceAll ( sub ,  "" ) ]  =  struct { } { } 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-27 17:16:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2020-01-06 08:10:20 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  fmt . Errorf ( "loading certificates with 'automate' requires array of strings, got: %T" ,  modIface ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										t . certificateLoaders  =  append ( t . certificateLoaders ,  modIface . ( CertificateLoader ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-07 11:17:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// using the certificate loaders we just initialized, load 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// manual/static (unmanaged) certificates - we do this in 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// provision so that other apps (such as http) can know which 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// certificates have been manually loaded, and also so that 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// commands like validate can be a better test 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									certCacheMu . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									magic  :=  certmagic . New ( certCache ,  certmagic . Config { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Storage :  ctx . Storage ( ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Logger :   t . logger , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										OnEvent :  t . onEvent , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										OCSP :  certmagic . OCSPConfig { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											DisableStapling :  t . DisableOCSPStapling , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										DisableStorageCheck :  t . DisableStorageCheck , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									certCacheMu . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  loader  :=  range  t . certificateLoaders  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										certs ,  err  :=  loader . LoadCertificates ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-30 16:11:29 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-07 11:17:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "loading certificates: %v" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-13 17:08:11 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-07 11:17:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  _ ,  cert  :=  range  certs  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											hash ,  err  :=  magic . CacheUnmanagedTLSCertificate ( ctx ,  cert . Certificate ,  cert . Tags ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  fmt . Errorf ( "caching unmanaged certificate: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											t . loaded [ hash ]  =  "" 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-13 17:08:11 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-30 16:11:29 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-17 12:02:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// on-demand permission module 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . Automation  !=  nil  &&  t . Automation . OnDemand  !=  nil  &&  t . Automation . OnDemand . PermissionRaw  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  t . Automation . OnDemand . Ask  !=  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "on-demand TLS config conflict: both 'ask' endpoint and a 'permission' module are specified; 'ask' is deprecated, so use only the permission module" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										val ,  err  :=  ctx . LoadModule ( t . Automation . OnDemand ,  "PermissionRaw" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "loading on-demand TLS permission module: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . Automation . OnDemand . permission  =  val . ( OnDemandPermission ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-09 13:09:48 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// automation/management policies 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . Automation  ==  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . Automation  =  new ( AutomationConfig ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									t . Automation . defaultPublicAutomationPolicy  =  new ( AutomationPolicy ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									err  =  t . Automation . defaultPublicAutomationPolicy . Provision ( t ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  fmt . Errorf ( "provisioning default public automation policy: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									for  n  :=  range  t . automateNames  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-09 13:09:48 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// if any names specified by the "automate" loader do not qualify for a public 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// certificate, we should initialize a default internal automation policy 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// (but we don't want to do this unnecessarily, since it may prompt for password!) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  certmagic . SubjectQualifiesForPublicCert ( n )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . Automation . defaultInternalAutomationPolicy  =  & AutomationPolicy { 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-16 11:05:55 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											IssuersRaw :  [ ] json . RawMessage { json . RawMessage ( ` { "module":"internal"} ` ) } , 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-09 13:09:48 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										err  =  t . Automation . defaultInternalAutomationPolicy . Provision ( t ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "provisioning default internal automation policy: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										break 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  i ,  ap  :=  range  t . Automation . Policies  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										err  :=  ap . Provision ( t ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "provisioning automation policy %d: %v" ,  i ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-07 11:17:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// run replacer on ask URL (for environment variables) -- return errors to prevent surprises (#5036) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . Automation  !=  nil  &&  t . Automation . OnDemand  !=  nil  &&  t . Automation . OnDemand . Ask  !=  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . Automation . OnDemand . Ask ,  err  =  repl . ReplaceOrErr ( t . Automation . OnDemand . Ask ,  true ,  true ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "preparing 'ask' endpoint: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										perm  :=  PermissionByHTTP { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Endpoint :  t . Automation . OnDemand . Ask , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  :=  perm . Provision ( ctx ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "provisioning 'ask' module: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . Automation . OnDemand . permission  =  perm 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-05-29 23:11:46 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// session ticket ephemeral keys (STEK) service and provider 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  t . SessionTickets  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										err  :=  t . SessionTickets . provision ( ctx ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "provisioning session tickets configuration: %v" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-05-29 23:11:46 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-07 11:17:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// ECH (Encrypted ClientHello) initialization 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . EncryptedClientHello  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . EncryptedClientHello . configs  =  make ( map [ string ] [ ] echConfig ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										outerNames ,  err  :=  t . EncryptedClientHello . Provision ( ctx ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-07 11:17:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "provisioning Encrypted ClientHello components: %v" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-07 11:17:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// outer names should have certificates to reduce client brittleness 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  outerName  :=  range  outerNames  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  outerName  ==  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-07 11:17:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  ! t . HasCertificateForSubject ( outerName )  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  t . automateNames  ==  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													t . automateNames  =  make ( map [ string ] struct { } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												t . automateNames [ outerName ]  =  struct { } { } 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-08-09 12:05:47 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-13 11:06:08 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Validate validates t's configuration.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  Validate ( )  error  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . Automation  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// ensure that host aren't repeated; since only the first 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// automation policy is used, repeating a host in the lists 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-17 21:00:45 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// isn't useful and is probably a mistake; same for two 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// catch-all/default policies 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										var  hasDefault  bool 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-13 11:06:08 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										hostSet  :=  make ( map [ string ] int ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  i ,  ap  :=  range  t . Automation . Policies  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-27 17:16:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  len ( ap . subjects )  ==  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-17 21:00:45 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  hasDefault  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													return  fmt . Errorf ( "automation policy %d is the second policy that acts as default/catch-all, but will never be used" ,  i ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												hasDefault  =  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-27 17:16:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											for  _ ,  h  :=  range  ap . subjects  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-13 11:06:08 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  first ,  ok  :=  hostSet [ h ] ;  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													return  fmt . Errorf ( "automation policy %d: cannot apply more than one automation policy to host: %s (first match in policy %d)" ,  i ,  h ,  first ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												hostSet [ h ]  =  i 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2020-06-05 11:14:39 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  t . Cache  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  t . Cache . Capacity  <  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  fmt . Errorf ( "cache capacity must be >= 0" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-13 11:06:08 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-08-09 12:05:47 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Start activates the TLS module.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  Start ( )  error  {  
						 
					
						
							
								
									
										
										
										
											2021-02-16 13:31:53 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// warn if on-demand TLS is enabled but no restrictions are in place 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-30 16:11:29 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  t . Automation . OnDemand  ==  nil  ||  ( t . Automation . OnDemand . Ask  ==  ""  &&  t . Automation . OnDemand . permission  ==  nil )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-02-16 13:31:53 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  _ ,  ap  :=  range  t . Automation . Policies  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-20 12:06:00 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  ap . OnDemand  &&  ap . isWildcardOrDefault ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-13 19:16:37 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  c  :=  t . logger . Check ( zapcore . WarnLevel ,  "YOUR SERVER MAY BE VULNERABLE TO ABUSE: on-demand TLS is enabled, but no protections are in place" ) ;  c  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													c . Write ( zap . String ( "docs" ,  "https://caddyserver.com/docs/automatic-https#on-demand-tls" ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2021-02-16 13:31:53 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												break 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// now that we are running, and all manual certificates have 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// been loaded, time to load the automated/managed certificates 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									err  :=  t . Manage ( t . automateNames ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  fmt . Errorf ( "automate: managing %v: %v" ,  t . automateNames ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// publish ECH configs in the background; does not need to block 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// server startup, as it could take a while 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . EncryptedClientHello  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										go  func ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  err  :=  t . publishECHConfigs ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												t . logger . Named ( "ech" ) . Error ( "publication(s) failed" ,  zap . Error ( err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-11-05 12:47:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ! t . DisableStorageClean  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// start the storage cleaner goroutine and ticker, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// which cleans out expired certificates and more 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . keepStorageClean ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Stop stops the TLS module and cleans up any allocations.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  Stop ( )  error  {  
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// stop the storage cleaner goroutine and ticker 
							 
						 
					
						
							
								
									
										
										
										
											2019-10-02 23:39:32 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  t . storageCleanStop  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										close ( t . storageCleanStop ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . storageCleanTicker  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . storageCleanTicker . Stop ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Cleanup frees up resources allocated during Provision.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  Cleanup ( )  error  {  
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// stop the session ticket rotation goroutine 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  t . SessionTickets  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . SessionTickets . stop ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// if a new TLS app was loaded, remove certificates from the cache that are no longer 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// being managed or loaded by the new config; if there is no more TLS app running, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// then stop cert maintenance and let the cert cache be GC'ed 
							 
						 
					
						
							
								
									
										
										
										
											2024-05-20 13:14:58 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  nextTLS ,  err  :=  caddy . ActiveContext ( ) . AppIfConfigured ( "tls" ) ;  err  ==  nil  &&  nextTLS  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										nextTLSApp  :=  nextTLS . ( * TLS ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// compute which certificates were managed or loaded into the cert cache by this 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// app instance (which is being stopped) that are not managed or loaded by the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// new app instance (which just started), and remove them from the cache 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										var  noLongerManaged  [ ] certmagic . SubjectIssuer 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										var  noLongerLoaded  [ ] string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										reManage  :=  make ( map [ string ] struct { } ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  subj ,  currentIssuerKey  :=  range  t . managing  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// It's a bit nuanced: managed certs can sometimes be different enough that we have to 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// swap them out for a different one, even if they are for the same subject/domain. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// We consider "private" certs (internal CA/locally-trusted/etc) to be significantly 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// distinct from "public" certs (production CAs/globally-trusted/etc) because of the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// implications when it comes to actual deployments: switching between an internal CA 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// and a production CA, for example, is quite significant. Switching from one public CA 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// to another, however, is not, and for our purposes we consider those to be the same. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// Anyway, if the next TLS app does not manage a cert for this name at all, definitely 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// remove it from the cache. But if it does, and it's not the same kind of issuer/CA 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// as we have, also remove it, so that it can swap it out for the right one. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  nextIssuerKey ,  ok  :=  nextTLSApp . managing [ subj ] ;  ! ok  ||  nextIssuerKey  !=  currentIssuerKey  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// next app is not managing a cert for this domain at all or is using a different issuer, so remove it 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												noLongerManaged  =  append ( noLongerManaged ,  certmagic . SubjectIssuer { Subject :  subj ,  IssuerKey :  currentIssuerKey } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// then, if the next app is managing a cert for this name, but with a different issuer, re-manage it 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  ok  &&  nextIssuerKey  !=  currentIssuerKey  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													reManage [ subj ]  =  struct { } { } 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  hash  :=  range  t . loaded  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  _ ,  ok  :=  nextTLSApp . loaded [ hash ] ;  ! ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												noLongerLoaded  =  append ( noLongerLoaded ,  hash ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// remove the certs 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										certCacheMu . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										certCache . RemoveManaged ( noLongerManaged ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										certCache . Remove ( noLongerLoaded ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										certCacheMu . RUnlock ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// give the new TLS app a "kick" to manage certs that it is configured for 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// with its own configuration instead of the one we just evicted 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  :=  nextTLSApp . Manage ( reManage ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-13 19:16:37 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  c  :=  t . logger . Check ( zapcore . ErrorLevel ,  "re-managing unloaded certificates with new config" ) ;  c  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												c . Write ( 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													zap . Strings ( "subjects" ,  internal . MaxSizeSubjectsListForLog ( reManage ,  1000 ) ) , 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-13 19:16:37 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													zap . Error ( err ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									}  else  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// no more TLS app running, so delete in-memory cert cache, if it was created yet 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										certCacheMu . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										hasCache  :=  certCache  !=  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										certCacheMu . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  hasCache  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											certCache . Stop ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											certCacheMu . Lock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											certCache  =  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											certCacheMu . Unlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Manage immediately begins managing subjects according to the  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// matching automation policy. The subjects are given in a map  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// to prevent duplication and also because quick lookups are  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// needed to assess wildcard coverage, if any, depending on  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// certain config parameters (with lots of subjects, computing  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// wildcard coverage over a slice can be highly inefficient).  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  Manage ( subjects  map [ string ] struct { } )  error  {  
						 
					
						
							
								
									
										
										
										
											2020-02-14 11:14:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// for a large number of names, we can be more memory-efficient 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// by making only one certmagic.Config for all the names that 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-26 14:02:29 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// use that config, rather than calling ManageAsync once for 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// every name; so first, bin names by AutomationPolicy 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-14 11:14:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									policyToNames  :=  make ( map [ * AutomationPolicy ] [ ] string ) 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									for  subj  :=  range  subjects  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ap  :=  t . getAutomationPolicyForName ( subj ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// by default, if a wildcard that covers the subj is also being 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// managed, either by a previous call to Manage or by this one, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// prefer using that over individual certs for its subdomains; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// but users can disable this and force getting a certificate for 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// subdomains by adding the name to the 'automate' cert loader 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  t . managingWildcardFor ( subj ,  subjects )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  _ ,  ok  :=  t . automateNames [ subj ] ;  ! ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										policyToNames [ ap ]  =  append ( policyToNames [ ap ] ,  subj ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-14 11:14:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// now that names are grouped by policy, we can simply make one 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// certmagic.Config for each (potentially large) group of names 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-26 14:02:29 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// and call ManageAsync just once for the whole batch 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-14 11:14:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									for  ap ,  names  :=  range  policyToNames  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-26 14:02:29 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										err  :=  ap . magic . ManageAsync ( t . ctx . Context ,  names ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-01 20:31:30 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											const  maxNamesToDisplay  =  100 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  len ( names )  >  maxNamesToDisplay  { 
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												names  =  append ( names [ : maxNamesToDisplay ] ,  fmt . Sprintf ( "(and %d more...)" ,  len ( names ) - maxNamesToDisplay ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-01 20:31:30 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-14 11:14:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  fmt . Errorf ( "automate: manage %v: %v" ,  names ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  _ ,  name  :=  range  names  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-30 16:15:54 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											// certs that are issued solely by our internal issuer are a little bit of 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// a special case: if you have an initial config that manages example.com 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// using internal CA, then after testing it you switch to a production CA, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// you wouldn't want to keep using the same self-signed cert, obviously; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// so we differentiate these by associating the subject with its issuer key; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// we do this because CertMagic has no notion of "InternalIssuer" like we 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// do, so we have to do this logic ourselves 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											var  issuerKey  string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  len ( ap . Issuers )  ==  1  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  intIss ,  ok  :=  ap . Issuers [ 0 ] . ( * InternalIssuer ) ;  ok  &&  intIss  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													issuerKey  =  intIss . IssuerKey ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											t . managing [ name ]  =  issuerKey 
							 
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-14 11:14:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-18 11:44:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// managingWildcardFor returns true if the app is managing a certificate that covers that  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// subject name (including consideration of wildcards), either from its internal list of  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// names that it IS managing certs for, or from the otherSubjsToManage which includes names  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// that WILL be managed.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  managingWildcardFor ( subj  string ,  otherSubjsToManage  map [ string ] struct { } )  bool  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// TODO: we could also consider manually-loaded certs using t.HasCertificateForSubject(), 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// but that does not account for how manually-loaded certs may be restricted as to which 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// hostnames or ClientHellos they can be used with by tags, etc; I don't *think* anyone 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// necessarily wants this anyway, but I thought I'd note this here for now (if we did 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// consider manually-loaded certs, we'd probably want to rename the method since it 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// wouldn't be just about managed certs anymore) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// IP addresses must match exactly 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  ip  :=  net . ParseIP ( subj ) ;  ip  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										_ ,  managing  :=  t . managing [ subj ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  managing 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// replace labels of the domain with wildcards until we get a match 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									labels  :=  strings . Split ( subj ,  "." ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  i  :=  range  labels  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  labels [ i ]  ==  "*"  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										labels [ i ]  =  "*" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										candidate  :=  strings . Join ( labels ,  "." ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  _ ,  ok  :=  t . managing [ candidate ] ;  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  _ ,  ok  :=  otherSubjsToManage [ candidate ] ;  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  false 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// RegisterServerNames registers the provided DNS names with the TLS app.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// This is currently used to auto-publish Encrypted ClientHello (ECH)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// configurations, if enabled. Use of this function by apps using the TLS  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// app removes the need for the user to redundantly specify domain names  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// in their configuration. This function separates hostname and port  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// (keeping only the hotsname) and filters IP addresses, which can't be  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// used with ECH.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//  
						 
					
						
							
								
									
										
										
										
											2025-03-12 16:33:03 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// EXPERIMENTAL: This function and its semantics/behavior are subject to change.  
						 
					
						
							
								
									
										
										
										
											2025-03-05 17:04:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( t  * TLS )  RegisterServerNames ( dnsNames  [ ] string )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									t . serverNamesMu . Lock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  name  :=  range  dnsNames  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										host ,  _ ,  err  :=  net . SplitHostPort ( name ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											host  =  name 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  strings . TrimSpace ( host )  !=  ""  &&  ! certmagic . SubjectIsIP ( host )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											t . serverNames [ strings . ToLower ( host ) ]  =  struct { } { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									t . serverNamesMu . Unlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-13 21:31:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// HandleHTTPChallenge ensures that the ACME HTTP challenge or ZeroSSL HTTP  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// validation request is handled for the certificate named by r.Host, if it  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// is an HTTP challenge request. It requires that the automation policy for  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// r.Host has an issuer that implements GetACMEIssuer() or is a *ZeroSSLIssuer.  
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  HandleHTTPChallenge ( w  http . ResponseWriter ,  r  * http . Request )  bool  {  
						 
					
						
							
								
									
										
										
										
											2024-04-13 21:31:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									acmeChallenge  :=  certmagic . LooksLikeHTTPChallenge ( r ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									zerosslValidation  :=  certmagic . LooksLikeZeroSSLHTTPValidation ( r ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
											 
										
											
												admin: Identity management, remote admin, config loaders (#3994)
This commits dds 3 separate, but very related features:
1. Automated server identity management
How do you know you're connecting to the server you think you are? How do you know the server connecting to you is the server instance you think it is? Mutually-authenticated TLS (mTLS) answers both of these questions. Using TLS to authenticate requires a public/private key pair (and the peer must trust the certificate you present to it).
Fortunately, Caddy is really good at managing certificates by now. We tap into that power to make it possible for Caddy to obtain and renew its own identity credentials, or in other words, a certificate that can be used for both server verification when clients connect to it, and client verification when it connects to other servers. Its associated private key is essentially its identity, and TLS takes care of possession proofs.
This configuration is simply a list of identifiers and an optional list of custom certificate issuers. Identifiers are things like IP addresses or DNS names that can be used to access the Caddy instance. The default issuers are ZeroSSL and Let's Encrypt, but these are public CAs, so they won't issue certs for private identifiers. Caddy will simply manage credentials for these, which other parts of Caddy can use, for example: remote administration or dynamic config loading (described below).
2. Remote administration over secure connection
This feature adds generic remote admin functionality that is safe to expose on a public interface.
- The "remote" (or "secure") endpoint is optional. It does not affect the standard/local/plaintext endpoint.
- It's the same as the [API endpoint on localhost:2019](https://caddyserver.com/docs/api), but over TLS.
- TLS cannot be disabled on this endpoint.
- TLS mutual auth is required, and cannot be disabled.
- The server's certificate _must_ be obtained and renewed via automated means, such as ACME. It cannot be manually loaded.
- The TLS server takes care of verifying the client.
- The admin handler takes care of application-layer permissions (methods and paths that each client is allowed to use).\
- Sensible defaults are still WIP.
- Config fields subject to change/renaming.
3. Dyanmic config loading at startup
Since this feature was planned in tandem with remote admin, and depends on its changes, I am combining them into one PR.
Dynamic config loading is where you tell Caddy how to load its config, and then it loads and runs that. First, it will load the config you give it (and persist that so it can be optionally resumed later). Then, it will try pulling its _actual_ config using the module you've specified (dynamically loaded configs are _not_ persisted to storage, since resuming them doesn't make sense).
This PR comes with a standard config loader module called `caddy.config_loaders.http`.
Caddyfile config for all of this can probably be added later.
COMMITS:
* admin: Secure socket for remote management
Functional, but still WIP.
Optional secure socket for the admin endpoint is designed
for remote management, i.e. to be exposed on a public
port. It enforces TLS mutual authentication which cannot
be disabled. The default port for this is :2021. The server
certificate cannot be specified manually, it MUST be
obtained from a certificate issuer (i.e. ACME).
More polish and sensible defaults are still in development.
Also cleaned up and consolidated the code related to
quitting the process.
* Happy lint
* Implement dynamic config loading; HTTP config loader module
This allows Caddy to load a dynamic config when it starts.
Dynamically-loaded configs are intentionally not persisted to storage.
Includes an implementation of the standard config loader, HTTPLoader.
Can be used to download configs over HTTP(S).
* Refactor and cleanup; prevent recursive config pulls
Identity management is now separated from remote administration.
There is no need to enable remote administration if all you want is identity
management, but you will need to configure identity management
if you want remote administration.
* Fix lint warnings
* Rename identities->identifiers for consistency
											 
										 
										
											2021-01-27 16:16:04 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// no-op if it's not an ACME challenge request 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-13 21:31:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ! acmeChallenge  &&  ! zerosslValidation  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return  false 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
											 
										
											
												admin: Identity management, remote admin, config loaders (#3994)
This commits dds 3 separate, but very related features:
1. Automated server identity management
How do you know you're connecting to the server you think you are? How do you know the server connecting to you is the server instance you think it is? Mutually-authenticated TLS (mTLS) answers both of these questions. Using TLS to authenticate requires a public/private key pair (and the peer must trust the certificate you present to it).
Fortunately, Caddy is really good at managing certificates by now. We tap into that power to make it possible for Caddy to obtain and renew its own identity credentials, or in other words, a certificate that can be used for both server verification when clients connect to it, and client verification when it connects to other servers. Its associated private key is essentially its identity, and TLS takes care of possession proofs.
This configuration is simply a list of identifiers and an optional list of custom certificate issuers. Identifiers are things like IP addresses or DNS names that can be used to access the Caddy instance. The default issuers are ZeroSSL and Let's Encrypt, but these are public CAs, so they won't issue certs for private identifiers. Caddy will simply manage credentials for these, which other parts of Caddy can use, for example: remote administration or dynamic config loading (described below).
2. Remote administration over secure connection
This feature adds generic remote admin functionality that is safe to expose on a public interface.
- The "remote" (or "secure") endpoint is optional. It does not affect the standard/local/plaintext endpoint.
- It's the same as the [API endpoint on localhost:2019](https://caddyserver.com/docs/api), but over TLS.
- TLS cannot be disabled on this endpoint.
- TLS mutual auth is required, and cannot be disabled.
- The server's certificate _must_ be obtained and renewed via automated means, such as ACME. It cannot be manually loaded.
- The TLS server takes care of verifying the client.
- The admin handler takes care of application-layer permissions (methods and paths that each client is allowed to use).\
- Sensible defaults are still WIP.
- Config fields subject to change/renaming.
3. Dyanmic config loading at startup
Since this feature was planned in tandem with remote admin, and depends on its changes, I am combining them into one PR.
Dynamic config loading is where you tell Caddy how to load its config, and then it loads and runs that. First, it will load the config you give it (and persist that so it can be optionally resumed later). Then, it will try pulling its _actual_ config using the module you've specified (dynamically loaded configs are _not_ persisted to storage, since resuming them doesn't make sense).
This PR comes with a standard config loader module called `caddy.config_loaders.http`.
Caddyfile config for all of this can probably be added later.
COMMITS:
* admin: Secure socket for remote management
Functional, but still WIP.
Optional secure socket for the admin endpoint is designed
for remote management, i.e. to be exposed on a public
port. It enforces TLS mutual authentication which cannot
be disabled. The default port for this is :2021. The server
certificate cannot be specified manually, it MUST be
obtained from a certificate issuer (i.e. ACME).
More polish and sensible defaults are still in development.
Also cleaned up and consolidated the code related to
quitting the process.
* Happy lint
* Implement dynamic config loading; HTTP config loader module
This allows Caddy to load a dynamic config when it starts.
Dynamically-loaded configs are intentionally not persisted to storage.
Includes an implementation of the standard config loader, HTTPLoader.
Can be used to download configs over HTTP(S).
* Refactor and cleanup; prevent recursive config pulls
Identity management is now separated from remote administration.
There is no need to enable remote administration if all you want is identity
management, but you will need to configure identity management
if you want remote administration.
* Fix lint warnings
* Rename identities->identifiers for consistency
											 
										 
										
											2021-01-27 16:16:04 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-16 11:05:55 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// try all the issuers until we find the one that initiated the challenge 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									ap  :=  t . getAutomationPolicyForName ( r . Host ) 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-13 21:31:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  acmeChallenge  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										type  acmeCapable  interface {  GetACMEIssuer ( )  * ACMEIssuer  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  iss  :=  range  ap . magic . Issuers  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  acmeIssuer ,  ok  :=  iss . ( acmeCapable ) ;  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  acmeIssuer . GetACMEIssuer ( ) . issuer . HandleHTTPChallenge ( w ,  r )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													return  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-16 11:05:55 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
											 
										
											
												admin: Identity management, remote admin, config loaders (#3994)
This commits dds 3 separate, but very related features:
1. Automated server identity management
How do you know you're connecting to the server you think you are? How do you know the server connecting to you is the server instance you think it is? Mutually-authenticated TLS (mTLS) answers both of these questions. Using TLS to authenticate requires a public/private key pair (and the peer must trust the certificate you present to it).
Fortunately, Caddy is really good at managing certificates by now. We tap into that power to make it possible for Caddy to obtain and renew its own identity credentials, or in other words, a certificate that can be used for both server verification when clients connect to it, and client verification when it connects to other servers. Its associated private key is essentially its identity, and TLS takes care of possession proofs.
This configuration is simply a list of identifiers and an optional list of custom certificate issuers. Identifiers are things like IP addresses or DNS names that can be used to access the Caddy instance. The default issuers are ZeroSSL and Let's Encrypt, but these are public CAs, so they won't issue certs for private identifiers. Caddy will simply manage credentials for these, which other parts of Caddy can use, for example: remote administration or dynamic config loading (described below).
2. Remote administration over secure connection
This feature adds generic remote admin functionality that is safe to expose on a public interface.
- The "remote" (or "secure") endpoint is optional. It does not affect the standard/local/plaintext endpoint.
- It's the same as the [API endpoint on localhost:2019](https://caddyserver.com/docs/api), but over TLS.
- TLS cannot be disabled on this endpoint.
- TLS mutual auth is required, and cannot be disabled.
- The server's certificate _must_ be obtained and renewed via automated means, such as ACME. It cannot be manually loaded.
- The TLS server takes care of verifying the client.
- The admin handler takes care of application-layer permissions (methods and paths that each client is allowed to use).\
- Sensible defaults are still WIP.
- Config fields subject to change/renaming.
3. Dyanmic config loading at startup
Since this feature was planned in tandem with remote admin, and depends on its changes, I am combining them into one PR.
Dynamic config loading is where you tell Caddy how to load its config, and then it loads and runs that. First, it will load the config you give it (and persist that so it can be optionally resumed later). Then, it will try pulling its _actual_ config using the module you've specified (dynamically loaded configs are _not_ persisted to storage, since resuming them doesn't make sense).
This PR comes with a standard config loader module called `caddy.config_loaders.http`.
Caddyfile config for all of this can probably be added later.
COMMITS:
* admin: Secure socket for remote management
Functional, but still WIP.
Optional secure socket for the admin endpoint is designed
for remote management, i.e. to be exposed on a public
port. It enforces TLS mutual authentication which cannot
be disabled. The default port for this is :2021. The server
certificate cannot be specified manually, it MUST be
obtained from a certificate issuer (i.e. ACME).
More polish and sensible defaults are still in development.
Also cleaned up and consolidated the code related to
quitting the process.
* Happy lint
* Implement dynamic config loading; HTTP config loader module
This allows Caddy to load a dynamic config when it starts.
Dynamically-loaded configs are intentionally not persisted to storage.
Includes an implementation of the standard config loader, HTTPLoader.
Can be used to download configs over HTTP(S).
* Refactor and cleanup; prevent recursive config pulls
Identity management is now separated from remote administration.
There is no need to enable remote administration if all you want is identity
management, but you will need to configure identity management
if you want remote administration.
* Fix lint warnings
* Rename identities->identifiers for consistency
											 
										 
										
											2021-01-27 16:16:04 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-13 21:31:43 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// it's possible another server in this process initiated the challenge; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// users have requested that Caddy only handle HTTP challenges it initiated, 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// so that users can proxy the others through to their backends; but we 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// might not have an automation policy for all identifiers that are trying 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// to get certificates (e.g. the admin endpoint), so we do this manual check 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  challenge ,  ok  :=  certmagic . GetACMEChallenge ( r . Host ) ;  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  certmagic . SolveHTTPChallenge ( t . logger ,  w ,  r ,  challenge . Challenge ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}  else  if  zerosslValidation  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  iss  :=  range  ap . magic . Issuers  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  ziss ,  ok  :=  iss . ( * ZeroSSLIssuer ) ;  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  ziss . issuer . HandleZeroSSLHTTPValidation ( w ,  r )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													return  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
											 
										
											
												admin: Identity management, remote admin, config loaders (#3994)
This commits dds 3 separate, but very related features:
1. Automated server identity management
How do you know you're connecting to the server you think you are? How do you know the server connecting to you is the server instance you think it is? Mutually-authenticated TLS (mTLS) answers both of these questions. Using TLS to authenticate requires a public/private key pair (and the peer must trust the certificate you present to it).
Fortunately, Caddy is really good at managing certificates by now. We tap into that power to make it possible for Caddy to obtain and renew its own identity credentials, or in other words, a certificate that can be used for both server verification when clients connect to it, and client verification when it connects to other servers. Its associated private key is essentially its identity, and TLS takes care of possession proofs.
This configuration is simply a list of identifiers and an optional list of custom certificate issuers. Identifiers are things like IP addresses or DNS names that can be used to access the Caddy instance. The default issuers are ZeroSSL and Let's Encrypt, but these are public CAs, so they won't issue certs for private identifiers. Caddy will simply manage credentials for these, which other parts of Caddy can use, for example: remote administration or dynamic config loading (described below).
2. Remote administration over secure connection
This feature adds generic remote admin functionality that is safe to expose on a public interface.
- The "remote" (or "secure") endpoint is optional. It does not affect the standard/local/plaintext endpoint.
- It's the same as the [API endpoint on localhost:2019](https://caddyserver.com/docs/api), but over TLS.
- TLS cannot be disabled on this endpoint.
- TLS mutual auth is required, and cannot be disabled.
- The server's certificate _must_ be obtained and renewed via automated means, such as ACME. It cannot be manually loaded.
- The TLS server takes care of verifying the client.
- The admin handler takes care of application-layer permissions (methods and paths that each client is allowed to use).\
- Sensible defaults are still WIP.
- Config fields subject to change/renaming.
3. Dyanmic config loading at startup
Since this feature was planned in tandem with remote admin, and depends on its changes, I am combining them into one PR.
Dynamic config loading is where you tell Caddy how to load its config, and then it loads and runs that. First, it will load the config you give it (and persist that so it can be optionally resumed later). Then, it will try pulling its _actual_ config using the module you've specified (dynamically loaded configs are _not_ persisted to storage, since resuming them doesn't make sense).
This PR comes with a standard config loader module called `caddy.config_loaders.http`.
Caddyfile config for all of this can probably be added later.
COMMITS:
* admin: Secure socket for remote management
Functional, but still WIP.
Optional secure socket for the admin endpoint is designed
for remote management, i.e. to be exposed on a public
port. It enforces TLS mutual authentication which cannot
be disabled. The default port for this is :2021. The server
certificate cannot be specified manually, it MUST be
obtained from a certificate issuer (i.e. ACME).
More polish and sensible defaults are still in development.
Also cleaned up and consolidated the code related to
quitting the process.
* Happy lint
* Implement dynamic config loading; HTTP config loader module
This allows Caddy to load a dynamic config when it starts.
Dynamically-loaded configs are intentionally not persisted to storage.
Includes an implementation of the standard config loader, HTTPLoader.
Can be used to download configs over HTTP(S).
* Refactor and cleanup; prevent recursive config pulls
Identity management is now separated from remote administration.
There is no need to enable remote administration if all you want is identity
management, but you will need to configure identity management
if you want remote administration.
* Fix lint warnings
* Rename identities->identifiers for consistency
											 
										 
										
											2021-01-27 16:16:04 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  false 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// AddAutomationPolicy provisions and adds ap to the list of the app's  
						 
					
						
							
								
									
										
										
										
											2020-03-13 11:06:08 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// automation policies. If an existing automation policy exists that has  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// fewer hosts in its list than ap does, ap will be inserted before that  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// other policy (this helps ensure that ap will be prioritized/chosen  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// over, say, a catch-all policy).  
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( t  * TLS )  AddAutomationPolicy ( ap  * AutomationPolicy )  error  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . Automation  ==  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										t . Automation  =  new ( AutomationConfig ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-20 20:25:46 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  :=  ap . Provision ( t ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-22 12:40:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// sort new automation policies just before any other which is a superset 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// of this one; if we find an existing policy that covers every subject in 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// ap but less specifically (e.g. a catch-all policy, or one with wildcards 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// or with fewer subjects), insert ap just before it, otherwise ap would 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// never be used because the first matching policy is more general 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  i ,  existing  :=  range  t . Automation . Policies  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// first see if existing is superset of ap for all names 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										var  otherIsSuperset  bool 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									outer : 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-27 17:16:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  _ ,  thisSubj  :=  range  ap . subjects  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											for  _ ,  otherSubj  :=  range  existing . subjects  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-10-22 12:40:23 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  certmagic . MatchWildcard ( thisSubj ,  otherSubj )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													otherIsSuperset  =  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													break  outer 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// if existing AP is a superset or if it contains fewer names (i.e. is 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// more general), then new AP is more specific, so insert before it 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-27 17:16:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  otherIsSuperset  ||  len ( existing . SubjectsRaw )  <  len ( ap . SubjectsRaw )  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-13 11:06:08 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											t . Automation . Policies  =  append ( t . Automation . Policies [ : i ] , 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-17 21:00:45 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												append ( [ ] * AutomationPolicy { ap } ,  t . Automation . Policies [ i : ] ... ) ... ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-13 11:06:08 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// otherwise just append the new one 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									t . Automation . Policies  =  append ( t . Automation . Policies ,  ap ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( t  * TLS )  getConfigForName ( name  string )  * certmagic . Config  {  
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									ap  :=  t . getAutomationPolicyForName ( name ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  ap . magic 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
											 
										
											
												httpcaddyfile, caddytls: Multiple edge case fixes; add tests
- Create two default automation policies; if the TLS app is used in
  isolation with the 'automate' certificate loader, it will now use
  an internal issuer for internal-only names, and an ACME issuer for
  all other names by default.
- If the HTTP Caddyfile adds an 'automate' loader, it now also adds an
  automation policy for any names in that loader that do not qualify
  for public certificates so that they will be issued internally. (It
  might be nice if this wasn't necessary, but the alternative is to
  either make auto-HTTPS logic way more complex by scanning the names in
  the 'automate' loader, or to have an automation policy without an
  issuer switch between default issuer based on the name being issued
  a certificate - I think I like the latter option better, right now we
  do something kind of like that but at a level above each individual
  automation policies, we do that switch only when no automation
  policies match, rather than when a policy without an issuer does
  match.)
- Set the default LoggerName rather than a LoggerNames with an empty
  host value, which is now taken literally rather than as a catch-all.
- hostsFromKeys, the function that gets a list of hosts from server
  block keys, no longer returns an empty string in its resulting slice,
  ever.
											 
										 
										
											2020-04-08 14:46:44 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// getAutomationPolicyForName returns the first matching automation policy  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// for the given subject name. If no matching policy can be found, the  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// default policy is used, depending on whether the name qualifies for a  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// public certificate or not.  
						 
					
						
							
								
									
										
										
										
											2020-02-14 11:14:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( t  * TLS )  getAutomationPolicyForName ( name  string )  * AutomationPolicy  {  
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									for  _ ,  ap  :=  range  t . Automation . Policies  { 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-27 17:16:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  len ( ap . subjects )  ==  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-06 23:15:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  ap  // no host filter is an automatic match 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-03-27 17:16:22 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  _ ,  h  :=  range  ap . subjects  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-26 14:01:38 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  certmagic . MatchWildcard ( name ,  h )  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
												return  ap 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-09 13:09:48 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  certmagic . SubjectQualifiesForPublicCert ( name )  ||  t . Automation . defaultInternalAutomationPolicy  ==  nil  { 
							 
						 
					
						
							
								
									
										
											 
										
											
												httpcaddyfile, caddytls: Multiple edge case fixes; add tests
- Create two default automation policies; if the TLS app is used in
  isolation with the 'automate' certificate loader, it will now use
  an internal issuer for internal-only names, and an ACME issuer for
  all other names by default.
- If the HTTP Caddyfile adds an 'automate' loader, it now also adds an
  automation policy for any names in that loader that do not qualify
  for public certificates so that they will be issued internally. (It
  might be nice if this wasn't necessary, but the alternative is to
  either make auto-HTTPS logic way more complex by scanning the names in
  the 'automate' loader, or to have an automation policy without an
  issuer switch between default issuer based on the name being issued
  a certificate - I think I like the latter option better, right now we
  do something kind of like that but at a level above each individual
  automation policies, we do that switch only when no automation
  policies match, rather than when a policy without an issuer does
  match.)
- Set the default LoggerName rather than a LoggerNames with an empty
  host value, which is now taken literally rather than as a catch-all.
- hostsFromKeys, the function that gets a list of hosts from server
  block keys, no longer returns an empty string in its resulting slice,
  ever.
											 
										 
										
											2020-04-08 14:46:44 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  t . Automation . defaultPublicAutomationPolicy 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  t . Automation . defaultInternalAutomationPolicy 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// AllMatchingCertificates returns the list of all certificates in  
						 
					
						
							
								
									
										
										
										
											2019-09-13 11:46:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// the cache which could be used to satisfy the given SAN.  
						 
					
						
							
								
									
										
										
										
											2023-07-11 13:10:58 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  AllMatchingCertificates ( san  string )  [ ] certmagic . Certificate  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  certCache . AllMatchingCertificates ( san ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  HasCertificateForSubject ( subject  string )  bool  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									certCacheMu . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									allMatchingCerts  :=  certCache . AllMatchingCertificates ( subject ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									certCacheMu . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  cert  :=  range  allMatchingCerts  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// check if the cert is manually loaded by this config 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  _ ,  ok  :=  t . loaded [ cert . Hash ( ) ] ;  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// check if the cert is automatically managed by this config 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  name  :=  range  cert . Names  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  _ ,  ok  :=  t . managing [ name ] ;  ok  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  false 
							 
						 
					
						
							
								
									
										
										
										
											2019-08-09 12:05:47 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-08 10:59:49 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// keepStorageClean starts a goroutine that immediately cleans up all  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// known storage units if it was not recently done, and then runs the  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// operation at every tick from t.storageCleanTicker.  
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( t  * TLS )  keepStorageClean ( )  {  
						 
					
						
							
								
									
										
										
										
											2021-04-12 15:41:22 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									t . storageCleanTicker  =  time . NewTicker ( t . storageCleanInterval ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									t . storageCleanStop  =  make ( chan  struct { } ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									go  func ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-12 11:36:20 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										defer  func ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  err  :=  recover ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												log . Printf ( "[PANIC] storage cleaner: %v\n%s" ,  err ,  debug . Stack ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-08 10:59:49 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										t . cleanStorageUnits ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											select  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											case  <- t . storageCleanStop : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											case  <- t . storageCleanTicker . C : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												t . cleanStorageUnits ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  cleanStorageUnits ( )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									storageCleanMu . Lock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defer  storageCleanMu . Unlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-12-07 11:00:02 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// TODO: This check might not be needed anymore now that CertMagic syncs 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// and throttles storage cleaning globally across the cluster. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// The original comment below might be outdated: 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-12 15:41:22 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// If storage was cleaned recently, don't do it again for now. Although the ticker 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-02 08:27:25 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// calling this function drops missed ticks for us, config reloads discard the old 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// ticker and replace it with a new one, possibly invoking a cleaning to happen again 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// too soon. (We divide the interval by 2 because the actual cleaning takes non-zero 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// time, and we don't want to skip cleanings if we don't have to; whereas if a cleaning 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// took most of the interval, we'd probably want to skip the next one so we aren't 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-12 15:41:22 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// constantly cleaning. This allows cleanings to take up to half the interval's 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// duration before we decide to skip the next one.) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  ! storageClean . IsZero ( )  &&  time . Since ( storageClean )  <  t . storageCleanInterval ( ) / 2  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-12-07 11:00:02 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									id ,  err  :=  caddy . InstanceID ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-13 19:16:37 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  c  :=  t . logger . Check ( zapcore . WarnLevel ,  "unable to get instance ID; storage clean stamps will be incomplete" ) ;  c  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											c . Write ( zap . Error ( err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-07 11:00:02 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									options  :=  certmagic . CleanStorageOptions { 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-07 11:00:02 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Logger :                  t . logger , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										InstanceID :              id . String ( ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Interval :                t . storageCleanInterval ( ) , 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										OCSPStaples :             true , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ExpiredCerts :            true , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ExpiredCertGracePeriod :  24  *  time . Hour  *  14 , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-12 15:41:22 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// start with the default/global storage 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-07 11:00:02 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  =  certmagic . CleanStorage ( t . ctx ,  t . ctx . Storage ( ) ,  options ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// probably don't want to return early, since we should still 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// see if any other storages can get cleaned up 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-13 19:16:37 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  c  :=  t . logger . Check ( zapcore . ErrorLevel ,  "could not clean default/global storage" ) ;  c  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											c . Write ( zap . Error ( err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-07 11:00:02 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// then clean each storage defined in ACME automation policies 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  t . Automation  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  ap  :=  range  t . Automation . Policies  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-04-12 15:41:22 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  ap . storage  ==  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-07 11:00:02 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  err  :=  certmagic . CleanStorage ( t . ctx ,  ap . storage ,  options ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2024-09-13 19:16:37 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  c  :=  t . logger . Check ( zapcore . ErrorLevel ,  "could not clean storage configured in automation policy" ) ;  c  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													c . Write ( zap . Error ( err ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-02 08:27:25 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// remember last time storage was finished cleaning 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									storageClean  =  time . Now ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-12 15:41:22 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									t . logger . Info ( "finished cleaning storage units" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-12 15:41:22 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( t  * TLS )  storageCleanInterval ( )  time . Duration  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  t . Automation  !=  nil  &&  t . Automation . StorageCleanInterval  >  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  time . Duration ( t . Automation . StorageCleanInterval ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  defaultStorageCleanInterval 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-31 17:01:30 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// onEvent translates CertMagic events into Caddy events then dispatches them.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( t  * TLS )  onEvent ( ctx  context . Context ,  eventName  string ,  data  map [ string ] any )  error  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									evt  :=  t . events . Emit ( t . ctx ,  eventName ,  data ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  evt . Aborted 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// CertificateLoader is a type that can load certificates.  
						 
					
						
							
								
									
										
										
										
											2019-06-24 12:16:10 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Certificates can optionally be associated with tags.  
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								type  CertificateLoader  interface  {  
						 
					
						
							
								
									
										
										
										
											2019-06-24 12:16:10 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									LoadCertificates ( )  ( [ ] Certificate ,  error ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Certificate is a TLS certificate, optionally  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// associated with arbitrary tags.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  Certificate  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									tls . Certificate 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Tags  [ ] string 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-25 13:54:48 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-09-24 18:31:01 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// AutomateLoader will automatically manage certificates for the names in the  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// list, including obtaining and renewing certificates. Automated certificates  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// are managed according to their matching automation policy, configured  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// elsewhere in this app.  
						 
					
						
							
								
									
										
										
										
											2021-02-26 19:27:58 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								//  
						 
					
						
							
								
									
										
										
										
											2021-09-24 18:31:01 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Technically, this is a no-op certificate loader module that is treated as  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// a special case: it uses this app's automation features to load certificates  
						 
					
						
							
								
									
										
										
										
											2023-06-04 22:15:50 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// for the list of hostnames, rather than loading certificates manually. But  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// the end result is the same: certificates for these subject names will be  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// loaded into the in-memory cache and may then be used.  
						 
					
						
							
								
									
										
										
										
											2019-12-10 13:36:46 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  AutomateLoader  [ ] string  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// CaddyModule returns the Caddy module information.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( AutomateLoader )  CaddyModule ( )  caddy . ModuleInfo  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  caddy . ModuleInfo { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ID :   "tls.certificates.automate" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										New :  func ( )  caddy . Module  {  return  new ( AutomateLoader )  } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-05 11:14:39 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// CertCacheOptions configures the certificate cache.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  CertCacheOptions  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Maximum number of certificates to allow in the 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// cache. If reached, certificates will be randomly 
							 
						 
					
						
							
								
									
										
										
										
											2021-09-24 18:31:01 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// evicted to make room for new ones. Default: 10,000 
							 
						 
					
						
							
								
									
										
										
										
											2020-06-05 11:14:39 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									Capacity  int  ` json:"capacity,omitempty" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Variables related to storage cleaning.  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var  (  
						 
					
						
							
								
									
										
										
										
											2021-04-12 15:41:22 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									defaultStorageCleanInterval  =  24  *  time . Hour 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-17 16:00:15 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									storageClean    time . Time 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									storageCleanMu  sync . Mutex 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Interface guards  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var  (  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									_  caddy . App           =  ( * TLS ) ( nil ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									_  caddy . Provisioner   =  ( * TLS ) ( nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-15 21:22:26 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									_  caddy . Validator     =  ( * TLS ) ( nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-30 09:07:43 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									_  caddy . CleanerUpper  =  ( * TLS ) ( nil ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								)