Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
caddytls: Remove old asset migration code (close #3894)
Matthew Holt committed
Feb 22, 2021 at 15:19 UTC
f6bb02b303d4a24c6932fd832f7aa9de224b6833
4 files changed
+1
-202
cmd/commandfuncs.go
-3
@@ -174,9 +174,6 @@ func cmdRun(fl Flags) (int, error) {
174
printEnvironment()
175
}
176
177
- // TODO: This is TEMPORARY, until the RCs
178
- moveStorage()
179
-
177
// load the config, depending on flags
178
var config []byte
179
var err error
cmd/main.go
-68
@@ -450,71 +450,3 @@ func caddyVersion() string {
450
}
451
return ver
452
}
453
-
454
-// moveStorage moves the old default dataDir to the new default dataDir.
455
-// TODO: This is TEMPORARY until the release candidates.
456
-func moveStorage() {
457
- // get the home directory (the old way)
458
- oldHome := os.Getenv("HOME")
459
- if oldHome == "" && runtime.GOOS == "windows" {
460
- drive := os.Getenv("HOMEDRIVE")
461
- path := os.Getenv("HOMEPATH")
462
- oldHome = drive + path
463
- if drive == "" || path == "" {
464
- oldHome = os.Getenv("USERPROFILE")
465
- }
466
- }
467
- if oldHome == "" {
468
- oldHome = "."
469
- }
470
- oldDataDir := filepath.Join(oldHome, ".local", "share", "caddy")
471
-
472
- // nothing to do if old data dir doesn't exist
473
- _, err := os.Stat(oldDataDir)
474
- if os.IsNotExist(err) {
475
- return
476
- }
477
-
478
- // nothing to do if the new data dir is the same as the old one
479
- newDataDir := caddy.AppDataDir()
480
- if oldDataDir == newDataDir {
481
- return
482
- }
483
-
484
- logger := caddy.Log().Named("automigrate").With(
485
- zap.String("old_dir", oldDataDir),
486
- zap.String("new_dir", newDataDir))
487
-
488
- logger.Info("beginning one-time data directory migration",
489
- zap.String("details", "https://github.com/caddyserver/caddy/issues/2955"))
490
-
491
- // if new data directory exists, avoid auto-migration as a conservative safety measure
492
- _, err = os.Stat(newDataDir)
493
- if !os.IsNotExist(err) {
494
- logger.Error("new data directory already exists; skipping auto-migration as conservative safety measure",
495
- zap.Error(err),
496
- zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333"))
497
- return
498
- }
499
-
500
- // construct the new data directory's parent folder
501
- err = os.MkdirAll(filepath.Dir(newDataDir), 0700)
502
- if err != nil {
503
- logger.Error("unable to make new datadirectory - follow link for instructions",
504
- zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333"),
505
- zap.Error(err))
506
- return
507
- }
508
-
509
- // folder structure is same, so just try to rename (move) it;
510
- // this fails if the new path is on a separate device
511
- err = os.Rename(oldDataDir, newDataDir)
512
- if err != nil {
513
- logger.Error("new data directory already exists; skipping auto-migration as conservative safety measure - follow link for instructions",
514
- zap.String("instructions", "https://github.com/caddyserver/caddy/issues/2955#issuecomment-570000333"),
515
- zap.Error(err))
516
- }
517
-
518
- logger.Info("successfully completed one-time migration of data directory",
519
- zap.String("details", "https://github.com/caddyserver/caddy/issues/2955"))
520
-}
go.mod
+1
-1
@@ -6,7 +6,7 @@ require (
6
github.com/Masterminds/sprig/v3 v3.1.0
7
github.com/alecthomas/chroma v0.8.2
8
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a
9
- github.com/caddyserver/certmagic v0.12.1-0.20210211020017-ebb8d8b435b4
9
+ github.com/caddyserver/certmagic v0.12.1-0.20210222221710-6131a445f6bf
10
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac
11
github.com/go-chi/chi v4.1.2+incompatible
12
github.com/google/cel-go v0.6.0
modules/caddytls/tls.go
-130
@@ -18,13 +18,9 @@ import (
18
"crypto/tls"
19
"encoding/json"
20
"fmt"
21
- "io/ioutil"
21
"log"
22
"net/http"
24
- "os"
25
- "path/filepath"
23
"runtime/debug"
27
- "strings"
24
"sync"
25
"time"
26
@@ -191,14 +187,6 @@ func (t *TLS) Provision(ctx caddy.Context) error {
187
}
188
}
189
194
- // TODO: TEMPORARY UNTIL RELEASE CANDIDATES:
195
- // MIGRATE MANAGED CERTIFICATE ASSETS TO NEW PATH
196
- err = t.moveCertificates()
197
- if err != nil {
198
- t.logger.Error("migrating certificates", zap.Error(err))
199
- }
200
- // END TODO: TEMPORARY.
201
-
190
return nil
191
}
192
@@ -529,121 +517,3 @@ var (
517
_ caddy.Validator = (*TLS)(nil)
518
_ caddy.CleanerUpper = (*TLS)(nil)
519
)
532
-
533
-// TODO: This is temporary until the release candidates
534
-// (beta 16 changed the storage path for certificates),
535
-// after which this function can be deleted
536
-func (t *TLS) moveCertificates() error {
537
- logger := t.logger.Named("automigrate")
538
-
539
- baseDir := caddy.AppDataDir()
540
-
541
- // if custom storage path was defined, use that instead
542
- if fs, ok := t.ctx.Storage().(*certmagic.FileStorage); ok && fs.Path != "" {
543
- baseDir = fs.Path
544
- }
545
-
546
- oldAcmeDir := filepath.Join(baseDir, "acme")
547
- oldAcmeCas, err := ioutil.ReadDir(oldAcmeDir)
548
- if err != nil {
549
- if os.IsNotExist(err) {
550
- return nil
551
- }
552
- return fmt.Errorf("listing used ACME CAs: %v", err)
553
- }
554
-
555
- // get list of used CAs
556
- oldCANames := make([]string, 0, len(oldAcmeCas))
557
- for _, fi := range oldAcmeCas {
558
- if !fi.IsDir() {
559
- continue
560
- }
561
- oldCANames = append(oldCANames, fi.Name())
562
- }
563
-
564
- for _, oldCA := range oldCANames {
565
- // make new destination path
566
- newCAName := oldCA
567
- if strings.Contains(oldCA, "api.letsencrypt.org") &&
568
- !strings.HasSuffix(oldCA, "-directory") {
569
- newCAName += "-directory"
570
- }
571
- newBaseDir := filepath.Join(baseDir, "certificates", newCAName)
572
- err := os.MkdirAll(newBaseDir, 0700)
573
- if err != nil {
574
- return fmt.Errorf("making new certs directory: %v", err)
575
- }
576
-
577
- // list sites in old path
578
- oldAcmeSitesDir := filepath.Join(oldAcmeDir, oldCA, "sites")
579
- oldAcmeSites, err := ioutil.ReadDir(oldAcmeSitesDir)
580
- if err != nil {
581
- if os.IsNotExist(err) {
582
- continue
583
- }
584
- return fmt.Errorf("listing sites: %v", err)
585
- }
586
-
587
- if len(oldAcmeSites) > 0 {
588
- logger.Warn("certificate storage path has changed; attempting one-time auto-migration",
589
- zap.String("old_folder", oldAcmeSitesDir),
590
- zap.String("new_folder", newBaseDir),
591
- zap.String("details", "https://github.com/caddyserver/caddy/issues/2955"))
592
- }
593
-
594
- // for each site, move its folder and re-encode its metadata
595
- for _, siteInfo := range oldAcmeSites {
596
- if !siteInfo.IsDir() {
597
- continue
598
- }
599
-
600
- // move the folder
601
- oldPath := filepath.Join(oldAcmeSitesDir, siteInfo.Name())
602
- newPath := filepath.Join(newBaseDir, siteInfo.Name())
603
- logger.Info("moving certificate assets",
604
- zap.String("ca", oldCA),
605
- zap.String("site", siteInfo.Name()),
606
- zap.String("destination", newPath))
607
- err = os.Rename(oldPath, newPath)
608
- if err != nil {
609
- logger.Error("failed moving site to new path; skipping",
610
- zap.String("old_path", oldPath),
611
- zap.String("new_path", newPath),
612
- zap.Error(err))
613
- continue
614
- }
615
-
616
- // re-encode metadata file
617
- metaFilePath := filepath.Join(newPath, siteInfo.Name()+".json")
618
- metaContents, err := ioutil.ReadFile(metaFilePath)
619
- if err != nil {
620
- logger.Error("could not read metadata file",
621
- zap.String("filename", metaFilePath),
622
- zap.Error(err))
623
- continue
624
- }
625
- if len(metaContents) == 0 {
626
- continue
627
- }
628
- cr := certmagic.CertificateResource{
629
- SANs: []string{siteInfo.Name()},
630
- IssuerData: json.RawMessage(metaContents),
631
- }
632
- newMeta, err := json.MarshalIndent(cr, "", "\t")
633
- if err != nil {
634
- logger.Error("encoding new metadata file", zap.Error(err))
635
- continue
636
- }
637
- err = ioutil.WriteFile(metaFilePath, newMeta, 0600)
638
- if err != nil {
639
- logger.Error("writing new metadata file", zap.Error(err))
640
- continue
641
- }
642
- }
643
-
644
- // delete now-empty old sites dir (OK if fails)
645
- os.Remove(oldAcmeSitesDir)
646
- }
647
-
648
- return nil
649
-}