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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 = createConfig()
  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. var err error
  79. dockerStopChan := make(chan struct{})
  80. dockerClient, err = client.NewEnvClient()
  81. if err != nil {
  82. panic(err)
  83. }
  84. templateGenerator := createTemplateGenerator(config.Templates)
  85. createCertificateManager(config.Acme)
  86. jitterTimer := time.NewTimer(time.Minute)
  87. redeployTimer := time.NewTicker(time.Hour * 24)
  88. updatedContainers := make(map[string]*Container)
  89. go func() {
  90. err := monitorContainers(dockerClient, dockerStopChan, func(container *Container) {
  91. containers[container.Name] = container
  92. updatedContainers[container.Name] = container
  93. jitterTimer.Reset(100 * time.Millisecond)
  94. }, func(name string) {
  95. delete(updatedContainers, name)
  96. delete(containers, name)
  97. jitterTimer.Reset(100 * time.Millisecond)
  98. })
  99. if err != nil {
  100. logger.Fatal("Error monitoring containers: ", err.Error())
  101. }
  102. }()
  103. go func() {
  104. for {
  105. select {
  106. case <-jitterTimer.C:
  107. hostnames := getHostnames(containers)
  108. updated := templateGenerator.Generate(Context{
  109. Containers: containers,
  110. Hostnames: hostnames,
  111. })
  112. for name, container := range updatedContainers {
  113. certDeployed := deployCertForContainer(container)
  114. updated = updated || certDeployed
  115. delete(updatedContainers, name)
  116. }
  117. if updated {
  118. signalContainer()
  119. }
  120. case <-redeployTimer.C:
  121. logger.Info("Performing periodic certificate refresh")
  122. for _, container := range containers {
  123. deployCertForContainer(container)
  124. signalContainer()
  125. }
  126. }
  127. }
  128. }()
  129. <-doneChan
  130. dockerStopChan <- struct{}{}
  131. err = dockerClient.Close()
  132. if err != nil {
  133. panic(err)
  134. }
  135. }
  136. func signalContainer() {
  137. for _, s := range config.Signals {
  138. container, ok := containers[s.Name]
  139. if ok {
  140. logger.Debugf("Killing container %s with signal %s", s.Name, s.Signal)
  141. err := dockerClient.ContainerKill(context.Background(), container.Id, s.Signal)
  142. if err != nil {
  143. logger.Errorf("Unable to send signal %s to container %s: %s", s.Signal, s.Name, err.Error())
  144. }
  145. } else {
  146. logger.Warnf("Couldn't signal container %s as it is not running", s.Name)
  147. }
  148. }
  149. }
  150. func getHostnamesForContainer(container *Container) []string {
  151. if label, ok := container.Labels[config.Labels.Hostnames]; ok {
  152. return applyWildcards(splitList(label), config.WildCardDomains)
  153. } else {
  154. return []string{}
  155. }
  156. }
  157. func applyWildcards(domains []string, wildcards []string) (result []string) {
  158. result = []string{}
  159. required := make(map[string]bool)
  160. for _, domain := range domains {
  161. found := false
  162. for _, wildcard := range wildcards {
  163. if wildcardMatches(wildcard, domain) {
  164. if !required["*."+wildcard] {
  165. result = append(result, "*."+wildcard)
  166. required["*."+wildcard] = true
  167. }
  168. found = true
  169. break
  170. }
  171. }
  172. if !found && !required[domain] {
  173. result = append(result, domain)
  174. required[domain] = true
  175. }
  176. }
  177. return
  178. }
  179. func wildcardMatches(wildcard, domain string) bool {
  180. if len(domain) <= len(wildcard) {
  181. return false
  182. }
  183. pivot := len(domain) - len(wildcard) - 1
  184. start := domain[:pivot]
  185. end := domain[pivot+1:]
  186. return domain[pivot] == '.' && end == wildcard && !strings.ContainsRune(start, '.')
  187. }
  188. func getHostnames(containers map[string]*Container) (hostnames map[string]*Hostname) {
  189. hostnames = make(map[string]*Hostname)
  190. for _, container := range containers {
  191. if label, ok := container.Labels[config.Labels.Hostnames]; ok {
  192. names := splitList(label)
  193. if hostname, ok := hostnames[names[0]]; ok {
  194. hostname.Containers = append(hostname.Containers, container)
  195. } else {
  196. hostnames[names[0]] = &Hostname{
  197. Name: names[0],
  198. Alternatives: make(map[string]bool),
  199. Containers: []*Container{container},
  200. CertDestination: config.DefaultCertDestination,
  201. }
  202. }
  203. addAlternatives(hostnames[names[0]], names[1:])
  204. if label, ok = container.Labels[config.Labels.RequireAuth]; ok {
  205. hostnames[names[0]].RequiresAuth = true
  206. hostnames[names[0]].AuthGroup = label
  207. }
  208. }
  209. }
  210. return
  211. }
  212. func addAlternatives(hostname *Hostname, alternatives []string) {
  213. for _, alternative := range alternatives {
  214. hostname.Alternatives[alternative] = true
  215. }
  216. }
  217. func deployCertForContainer(container *Container) bool {
  218. hostnames := getHostnamesForContainer(container)
  219. if len(hostnames) == 0 {
  220. logger.Debugf("No labels found for container %s", container.Name)
  221. return false
  222. }
  223. err, cert := certificateManager.GetCertificate(hostnames)
  224. if err != nil {
  225. logger.Warnf("Unable to generate certificate for %s: %s", container.Name, err.Error())
  226. return false
  227. } else {
  228. return deployCert(cert)
  229. }
  230. }
  231. func deployCert(certificate *SavedCertificate) bool {
  232. name := fmt.Sprintf("%s.pem", strings.ReplaceAll("*", "_", certificate.Domains[0]))
  233. target := path.Join(config.DefaultCertDestination, name)
  234. content := append(certificate.Certificate, certificate.PrivateKey...)
  235. buf, _ := ioutil.ReadFile(target)
  236. if bytes.Equal(buf, content) {
  237. logger.Debugf("Certificate was up to date: %s", target)
  238. return false
  239. }
  240. err := ioutil.WriteFile(target, content, 0700)
  241. if err != nil {
  242. logger.Warnf("Unable to write certificate %s - %s", target, err.Error())
  243. return false
  244. } else {
  245. logger.Infof("Updated certificate file %s", target)
  246. return true
  247. }
  248. }