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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/csmith/dotege/model"
  6. "github.com/docker/docker/client"
  7. "github.com/xenolf/lego/certcrypto"
  8. "github.com/xenolf/lego/lego"
  9. "go.uber.org/zap"
  10. "go.uber.org/zap/zapcore"
  11. "io/ioutil"
  12. "os"
  13. "os/signal"
  14. "path"
  15. "strings"
  16. "syscall"
  17. "time"
  18. )
  19. const (
  20. envCertDestinationKey = "DOTEGE_CERT_DESTINATION"
  21. envCertDestinationDefault = "/data/certs/"
  22. envDnsProviderKey = "DOTEGE_DNS_PROVIDER"
  23. envAcmeEmailKey = "DOTEGE_ACME_EMAIL"
  24. envAcmeEndpointKey = "DOTEGE_ACME_ENDPOINT"
  25. envAcmeKeyTypeKey = "DOTEGE_ACME_KEY_TYPE"
  26. envAcmeKeyTypeDefault = "P384"
  27. envAcmeCacheLocationKey = "DOTEGE_ACME_CACHE_FILE"
  28. envAcmeCacheLocationDefault = "/data/config/certs.json"
  29. envSignalContainerKey = "DOTEGE_SIGNAL_CONTAINER"
  30. envSignalContainerDefault = ""
  31. envSignalTypeKey = "DOTEGE_SIGNAL_TYPE"
  32. envSignalTypeDefault = "HUP"
  33. envTemplateDestinationKey = "DOTEGE_TEMPLATE_DESTINATION"
  34. envTemplateDestinationDefault = "/data/output/haproxy.cfg"
  35. envTemplateSourceKey = "DOTEGE_TEMPLATE_SOURCE"
  36. envTemplateSourceDefault = "./templates/haproxy.cfg.tpl"
  37. )
  38. var (
  39. logger *zap.SugaredLogger
  40. certificateManager *CertificateManager
  41. config *model.Config
  42. dockerClient *client.Client
  43. containers = make(map[string]*model.Container)
  44. )
  45. func requiredVar(key string) (value string) {
  46. value, ok := os.LookupEnv(key)
  47. if !ok {
  48. panic(fmt.Errorf("required environmental variable not defined: %s", key))
  49. }
  50. return
  51. }
  52. func optionalVar(key string, fallback string) (value string) {
  53. value, ok := os.LookupEnv(key)
  54. if !ok {
  55. value = fallback
  56. }
  57. return
  58. }
  59. func monitorSignals() <-chan bool {
  60. signals := make(chan os.Signal, 1)
  61. done := make(chan bool, 1)
  62. signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
  63. go func() {
  64. sig := <-signals
  65. fmt.Printf("Received %s signal\n", sig)
  66. done <- true
  67. }()
  68. return done
  69. }
  70. func createLogger() *zap.SugaredLogger {
  71. zapConfig := zap.NewDevelopmentConfig()
  72. zapConfig.DisableCaller = true
  73. zapConfig.DisableStacktrace = true
  74. zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
  75. zapConfig.OutputPaths = []string{"stdout"}
  76. zapConfig.ErrorOutputPaths = []string{"stdout"}
  77. logger, _ := zapConfig.Build()
  78. return logger.Sugar()
  79. }
  80. func createSignalConfig() []model.ContainerSignal {
  81. name := optionalVar(envSignalContainerKey, envSignalContainerDefault)
  82. if name == envSignalContainerDefault {
  83. return []model.ContainerSignal{}
  84. } else {
  85. return []model.ContainerSignal{
  86. {
  87. Name: name,
  88. Signal: optionalVar(envSignalTypeKey, envSignalTypeDefault),
  89. },
  90. }
  91. }
  92. }
  93. func createConfig() {
  94. config = &model.Config{
  95. Templates: []model.TemplateConfig{
  96. {
  97. Source: optionalVar(envTemplateSourceKey, envTemplateSourceDefault),
  98. Destination: optionalVar(envTemplateDestinationKey, envTemplateDestinationDefault),
  99. },
  100. },
  101. Labels: model.LabelConfig{
  102. Hostnames: "com.chameth.vhost",
  103. RequireAuth: "com.chameth.auth",
  104. },
  105. Acme: model.AcmeConfig{
  106. DnsProvider: requiredVar(envDnsProviderKey),
  107. Email: requiredVar(envAcmeEmailKey),
  108. Endpoint: optionalVar(envAcmeEndpointKey, lego.LEDirectoryProduction),
  109. KeyType: certcrypto.KeyType(optionalVar(envAcmeKeyTypeKey, envAcmeKeyTypeDefault)),
  110. CacheLocation: optionalVar(envAcmeCacheLocationKey, envAcmeCacheLocationDefault),
  111. },
  112. Signals: createSignalConfig(),
  113. DefaultCertActions: model.COMBINE | model.FLATTEN,
  114. DefaultCertDestination: optionalVar(envCertDestinationKey, envCertDestinationDefault),
  115. }
  116. }
  117. func createTemplateGenerator(templates []model.TemplateConfig) *TemplateGenerator {
  118. templateGenerator := NewTemplateGenerator(logger)
  119. for _, template := range templates {
  120. templateGenerator.AddTemplate(template)
  121. }
  122. return templateGenerator
  123. }
  124. func createCertificateManager(config model.AcmeConfig) {
  125. certificateManager = NewCertificateManager(logger, config.Endpoint, config.KeyType, config.DnsProvider, config.CacheLocation)
  126. err := certificateManager.Init(config.Email)
  127. if err != nil {
  128. panic(err)
  129. }
  130. }
  131. func main() {
  132. logger = createLogger()
  133. logger.Info("Dotege is starting")
  134. doneChan := monitorSignals()
  135. createConfig()
  136. dockerStopChan := make(chan struct{})
  137. dockerClient, err := client.NewEnvClient()
  138. if err != nil {
  139. panic(err)
  140. }
  141. templateGenerator := createTemplateGenerator(config.Templates)
  142. createCertificateManager(config.Acme)
  143. jitterTimer := time.NewTimer(time.Minute)
  144. redeployTimer := time.NewTicker(time.Hour * 24)
  145. go func() {
  146. err := monitorContainers(dockerClient, dockerStopChan, func(container *model.Container) {
  147. containers[container.Name] = container
  148. jitterTimer.Reset(100 * time.Millisecond)
  149. deployCertForContainer(container)
  150. signalContainer()
  151. }, func(name string) {
  152. delete(containers, name)
  153. jitterTimer.Reset(100 * time.Millisecond)
  154. })
  155. if err != nil {
  156. logger.Fatal("Error monitoring containers: ", err.Error())
  157. }
  158. }()
  159. go func() {
  160. for {
  161. select {
  162. case <-jitterTimer.C:
  163. hostnames := getHostnames(containers, *config)
  164. updated := templateGenerator.Generate(Context{
  165. Containers: containers,
  166. Hostnames: hostnames,
  167. })
  168. if updated {
  169. signalContainer()
  170. }
  171. case <-redeployTimer.C:
  172. logger.Info("Performing periodic certificate refresh")
  173. for _, container := range containers {
  174. deployCertForContainer(container)
  175. signalContainer()
  176. }
  177. }
  178. }
  179. }()
  180. <-doneChan
  181. dockerStopChan <- struct{}{}
  182. err = dockerClient.Close()
  183. if err != nil {
  184. panic(err)
  185. }
  186. }
  187. func signalContainer() {
  188. for _, s := range config.Signals {
  189. container, ok := containers[s.Name]
  190. if ok {
  191. err := dockerClient.ContainerKill(context.Background(), container.Id, s.Signal)
  192. if err != nil {
  193. logger.Errorf("Unable to send signal %s to container %s: %s", s.Signal, s.Name, err.Error())
  194. }
  195. } else {
  196. logger.Warnf("Couldn't signal container %s as it is not running", s.Name)
  197. }
  198. }
  199. }
  200. func getHostnamesForContainer(container *model.Container) []string {
  201. if label, ok := container.Labels[config.Labels.Hostnames]; ok {
  202. return strings.Split(strings.Replace(label, ",", " ", -1), " ")
  203. } else {
  204. return []string{}
  205. }
  206. }
  207. func getHostnames(containers map[string]*model.Container, config model.Config) (hostnames map[string]*model.Hostname) {
  208. hostnames = make(map[string]*model.Hostname)
  209. for _, container := range containers {
  210. if label, ok := container.Labels[config.Labels.Hostnames]; ok {
  211. names := strings.Split(strings.Replace(label, ",", " ", -1), " ")
  212. if hostname, ok := hostnames[names[0]]; ok {
  213. hostname.Containers = append(hostname.Containers, container)
  214. } else {
  215. hostnames[names[0]] = &model.Hostname{
  216. Name: names[0],
  217. Alternatives: make(map[string]bool),
  218. Containers: []*model.Container{container},
  219. CertActions: config.DefaultCertActions,
  220. CertDestination: config.DefaultCertDestination,
  221. }
  222. }
  223. addAlternatives(hostnames[names[0]], names[1:])
  224. if label, ok = container.Labels[config.Labels.RequireAuth]; ok {
  225. hostnames[names[0]].RequiresAuth = true
  226. hostnames[names[0]].AuthGroup = label
  227. }
  228. }
  229. }
  230. return
  231. }
  232. func addAlternatives(hostname *model.Hostname, alternatives []string) {
  233. for _, alternative := range alternatives {
  234. hostname.Alternatives[alternative] = true
  235. }
  236. }
  237. func deployCertForContainer(container *model.Container) {
  238. hostnames := getHostnamesForContainer(container)
  239. if len(hostnames) == 0 {
  240. logger.Debugf("No labels found for container %s", container.Name)
  241. return
  242. }
  243. err, cert := certificateManager.GetCertificate(hostnames)
  244. if err != nil {
  245. logger.Warnf("Unable to generate certificate for %s: %s", container.Name, err.Error())
  246. } else {
  247. deployCert(cert)
  248. }
  249. }
  250. func deployCert(certificate *SavedCertificate) {
  251. target := path.Join(config.DefaultCertDestination, fmt.Sprintf("%s.pem", certificate.Domains[0]))
  252. err := ioutil.WriteFile(target, append(certificate.Certificate, certificate.PrivateKey...), 0700)
  253. if err != nil {
  254. logger.Warnf("Unable to write certificate %s - %s", target, err.Error())
  255. } else {
  256. logger.Infof("Updated certificate file %s", target)
  257. }
  258. }