github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/daemon/id.go (about) 1 package daemon // import "github.com/docker/docker/daemon" 2 3 import ( 4 "os" 5 6 "github.com/docker/docker/pkg/ioutils" 7 "github.com/docker/libtrust" 8 "github.com/google/uuid" 9 "github.com/pkg/errors" 10 "github.com/sirupsen/logrus" 11 ) 12 13 // loadOrCreateID loads the engine's ID from idPath, or generates a new ID 14 // if it doesn't exist. It returns the ID, and any error that occurred when 15 // saving the file. 16 // 17 // Note that this function expects the daemon's root directory to already have 18 // been created with the right permissions and ownership (usually this would 19 // be done by daemon.CreateDaemonRoot(). 20 func loadOrCreateID(idPath string) (string, error) { 21 var id string 22 idb, err := os.ReadFile(idPath) 23 if os.IsNotExist(err) { 24 id = uuid.New().String() 25 if err := ioutils.AtomicWriteFile(idPath, []byte(id), os.FileMode(0600)); err != nil { 26 return "", errors.Wrap(err, "error saving ID file") 27 } 28 } else if err != nil { 29 return "", errors.Wrapf(err, "error loading ID file %s", idPath) 30 } else { 31 id = string(idb) 32 } 33 return id, nil 34 } 35 36 // migrateTrustKeyID migrates the daemon ID of existing installations. It returns 37 // an error when a trust-key was found, but we failed to read it, or failed to 38 // complete the migration. 39 // 40 // We migrate the ID so that engines don't get a new ID generated on upgrades, 41 // which may be unexpected (and users may be using the ID for various purposes). 42 func migrateTrustKeyID(deprecatedTrustKeyPath, idPath string) error { 43 if _, err := os.Stat(idPath); err == nil { 44 // engine ID file already exists; no migration needed 45 return nil 46 } 47 trustKey, err := libtrust.LoadKeyFile(deprecatedTrustKeyPath) 48 if err != nil { 49 if err == libtrust.ErrKeyFileDoesNotExist { 50 // no existing trust-key found; no migration needed 51 return nil 52 } 53 return err 54 } 55 id := trustKey.PublicKey().KeyID() 56 if err := ioutils.AtomicWriteFile(idPath, []byte(id), os.FileMode(0600)); err != nil { 57 return errors.Wrap(err, "error saving ID file") 58 } 59 logrus.Info("successfully migrated engine ID") 60 return nil 61 }