Docker template generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dotege.go 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/docker/docker/client"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. "io/ioutil"
  10. "os"
  11. "os/signal"
  12. "path"
  13. "strings"
  14. "syscall"
  15. "time"
  16. )
  17. // Container models a docker container that is running on the system.
  18. type Container struct {
  19. Id string
  20. Name string
  21. Labels map[string]string
  22. }
  23. // Hostname describes a DNS name used for proxying, retrieving certificates, etc.
  24. type Hostname struct {
  25. Name string
  26. Alternatives map[string]bool
  27. Containers []*Container
  28. CertDestination string
  29. RequiresAuth bool
  30. AuthGroup string
  31. }
  32. var (
  33. logger *zap.SugaredLogger
  34. certificateManager *CertificateManager
  35. dockerClient *client.Client
  36. config *Config
  37. containers = make(map[string]*Container)
  38. )
  39. func monitorSignals() <-chan bool {
  40. signals := make(chan os.Signal, 1)
  41. done := make(chan bool, 1)
  42. signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
  43. go func() {
  44. sig := <-signals
  45. fmt.Printf("Received %s signal\n", sig)
  46. done <- true
  47. }()
  48. return done
  49. }
  50. func createLogger() *zap.SugaredLogger {
  51. zapConfig := zap.NewDevelopmentConfig()
  52. zapConfig.DisableCaller = true
  53. zapConfig.DisableStacktrace = true
  54. zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
  55. zapConfig.OutputPaths = []string{"stdout"}
  56. zapConfig.ErrorOutputPaths = []string{"stdout"}
  57. logger, _ := zapConfig.Build()
  58. return logger.Sugar()
  59. }
  60. func createTemplateGenerator(templates []TemplateConfig) *TemplateGenerator {
  61. templateGenerator := NewTemplateGenerator()
  62. for _, template := range templates {
  63. templateGenerator.AddTemplate(template)
  64. }
  65. return templateGenerator
  66. }
  67. func createCertificateManager(config AcmeConfig) {
  68. certificateManager = NewCertificateManager(logger, config.Endpoint, config.KeyType, config.DnsProvider, config.CacheLocation)
  69. err := certificateManager.Init(config.Email)
  70. if err != nil {
  71. panic(err)
  72. }
  73. }
  74. func main() {
  75. logger = createLogger()
  76. logger.Info("Dotege is starting")
  77. doneChan := monitorSignals()
  78. config = createConfig()
  79. var err error
  80. dockerStopChan := make(chan struct{})
  81. dockerClient, err = client.NewEnvClient()
  82. if err != nil {
  83. panic(err)
  84. }
  85. templateGenerator := createTemplateGenerator(config.Templates)
  86. createCertificateManager(config.Acme)
  87. jitterTimer := time.NewTimer(time.Minute)
  88. redeployTimer := time.NewTicker(time.Hour * 24)
  89. updatedContainers := make(map[string]*Container)
  90. go func() {
  91. err := monitorContainers(dockerClient, dockerStopChan, func(container *Container) {
  92. containers[container.Name] = container
  93. updatedContainers[container.Name] = container
  94. jitterTimer.Reset(100 * time.Millisecond)
  95. }, func(name string) {
  96. delete(updatedContainers, name)
  97. delete(containers, name)
  98. jitterTimer.Reset(100 * time.Millisecond)
  99. })
  100. if err != nil {
  101. logger.Fatal("Error monitoring containers: ", err.Error())
  102. }
  103. }()
  104. go func() {
  105. for {
  106. select {
  107. case <-jitterTimer.C:
  108. hostnames := getHostnames(containers)
  109. updated := templateGenerator.Generate(Context{
  110. Containers: containers,
  111. Hostnames: hostnames,
  112. })
  113. for name, container := range updatedContainers {
  114. certDeployed := deployCertForContainer(container)
  115. updated = updated || certDeployed
  116. delete(updatedContainers, name)
  117. }
  118. if updated {
  119. signalContainer()
  120. }
  121. case <-redeployTimer.C:
  122. logger.Info("Performing periodic certificate refresh")
  123. for _, container := range containers {
  124. deployCertForContainer(container)
  125. signalContainer()
  126. }
  127. }
  128. }
  129. }()
  130. <-doneChan
  131. dockerStopChan <- struct{}{}
  132. err = dockerClient.Close()
  133. if err != nil {
  134. panic(err)
  135. }
  136. }
  137. func signalContainer() {
  138. for _, s := range config.Signals {
  139. container, ok := containers[s.Name]
  140. if ok {
  141. logger.Debugf("Killing container %s with signal %s", s.Name, s.Signal)
  142. err := dockerClient.ContainerKill(context.Background(), container.Id, s.Signal)
  143. if err != nil {
  144. logger.Errorf("Unable to send signal %s to container %s: %s", s.Signal, s.Name, err.Error())
  145. }
  146. } else {
  147. logger.Warnf("Couldn't signal container %s as it is not running", s.Name)
  148. }
  149. }
  150. }
  151. func getHostnamesForContainer(container *Container) []string {
  152. if label, ok := container.Labels[config.Labels.Hostnames]; ok {
  153. return applyWildcards(splitList(label), config.WildCardDomains)
  154. } else {
  155. return []string{}
  156. }
  157. }
  158. func applyWildcards(domains []string, wildcards []string) (result []string) {
  159. result = []string{}
  160. required := make(map[string]bool)
  161. for _, domain := range domains {
  162. found := false
  163. for _, wildcard := range wildcards {
  164. if wildcardMatches(wildcard, domain) {
  165. if !required["*."+wildcard] {
  166. result = append(result, "*."+wildcard)
  167. required["*."+wildcard] = true
  168. }
  169. found = true
  170. break
  171. }
  172. }
  173. if !found && !required[domain] {
  174. result = append(result, domain)
  175. required[domain] = true
  176. }
  177. }
  178. return
  179. }
  180. func wildcardMatches(wildcard, domain string) bool {
  181. if len(domain) <= len(wildcard) {
  182. return false
  183. }
  184. pivot := len(domain) - len(wildcard) - 1
  185. start := domain[:pivot]
  186. end := domain[pivot+1:]
  187. return domain[pivot] == '.' && end == wildcard && !strings.ContainsRune(start, '.')
  188. }
  189. func getHostnames(containers map[string]*Container) (hostnames map[string]*Hostname) {
  190. hostnames = make(map[string]*Hostname)
  191. for _, container := range containers {
  192. if label, ok := container.Labels[config.Labels.Hostnames]; ok {
  193. names := splitList(label)
  194. if hostname, ok := hostnames[names[0]]; ok {
  195. hostname.Containers = append(hostname.Containers, container)
  196. } else {
  197. hostnames[names[0]] = &Hostname{
  198. Name: names[0],
  199. Alternatives: make(map[string]bool),
  200. Containers: []*Container{container},
  201. CertDestination: config.DefaultCertDestination,
  202. }
  203. }
  204. addAlternatives(hostnames[names[0]], names[1:])
  205. if label, ok = container.Labels[config.Labels.RequireAuth]; ok {
  206. hostnames[names[0]].RequiresAuth = true
  207. hostnames[names[0]].AuthGroup = label
  208. }
  209. }
  210. }
  211. return
  212. }
  213. func addAlternatives(hostname *Hostname, alternatives []string) {
  214. for _, alternative := range alternatives {
  215. hostname.Alternatives[alternative] = true
  216. }
  217. }
  218. func deployCertForContainer(container *Container) bool {
  219. hostnames := getHostnamesForContainer(container)
  220. if len(hostnames) == 0 {
  221. logger.Debugf("No labels found for container %s", container.Name)
  222. return false
  223. }
  224. err, cert := certificateManager.GetCertificate(hostnames)
  225. if err != nil {
  226. logger.Warnf("Unable to generate certificate for %s: %s", container.Name, err.Error())
  227. return false
  228. } else {
  229. return deployCert(cert)
  230. }
  231. }
  232. func deployCert(certificate *SavedCertificate) bool {
  233. name := fmt.Sprintf("%s.pem", strings.ReplaceAll(certificate.Domains[0], "*", "_"))
  234. target := path.Join(config.DefaultCertDestination, name)
  235. content := append(certificate.Certificate, certificate.PrivateKey...)
  236. buf, _ := ioutil.ReadFile(target)
  237. if bytes.Equal(buf, content) {
  238. logger.Debugf("Certificate was up to date: %s", target)
  239. return false
  240. }
  241. err := ioutil.WriteFile(target, content, 0700)
  242. if err != nil {
  243. logger.Warnf("Unable to write certificate %s - %s", target, err.Error())
  244. return false
  245. } else {
  246. logger.Infof("Updated certificate file %s", target)
  247. return true
  248. }
  249. }