|
| 1 | +package libmachine |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + |
| 6 | + "github.com/docker/machine/libmachine/auth" |
| 7 | + "github.com/docker/machine/libmachine/engine" |
| 8 | + "github.com/docker/machine/libmachine/swarm" |
| 9 | + "github.com/docker/machine/utils" |
| 10 | +) |
| 11 | + |
| 12 | +// In the 0.0.1 => 0.0.2 transition, the JSON representation of |
| 13 | +// machines changed from a "flat" to a more "nested" structure |
| 14 | +// for various options and configuration settings. To preserve |
| 15 | +// compatibility with existing machines, these migration functions |
| 16 | +// have been introduced. They preserve backwards compat at the expense |
| 17 | +// of some duplicated information. |
| 18 | + |
| 19 | +// validates host config and modifies if needed |
| 20 | +// this is used for configuration updates |
| 21 | +func FillNestedHost(host *Host) *Host { |
| 22 | + certInfo := getCertInfoFromHost(host) |
| 23 | + |
| 24 | + if host.HostOptions == nil { |
| 25 | + host.HostOptions = &HostOptions{} |
| 26 | + } |
| 27 | + if host.HostOptions.EngineOptions == nil { |
| 28 | + host.HostOptions.EngineOptions = &engine.EngineOptions{} |
| 29 | + } |
| 30 | + |
| 31 | + if host.HostOptions.SwarmOptions == nil { |
| 32 | + host.HostOptions.SwarmOptions = &swarm.SwarmOptions{ |
| 33 | + Address: "", |
| 34 | + Discovery: host.SwarmDiscovery, |
| 35 | + Host: host.SwarmHost, |
| 36 | + Master: host.SwarmMaster, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + host.HostOptions.AuthOptions = &auth.AuthOptions{ |
| 41 | + StorePath: host.StorePath, |
| 42 | + CaCertPath: certInfo.CaCertPath, |
| 43 | + CaCertRemotePath: "", |
| 44 | + ServerCertPath: certInfo.ServerCertPath, |
| 45 | + ServerKeyPath: certInfo.ServerKeyPath, |
| 46 | + ClientKeyPath: certInfo.ClientKeyPath, |
| 47 | + ServerCertRemotePath: "", |
| 48 | + ServerKeyRemotePath: "", |
| 49 | + PrivateKeyPath: certInfo.CaKeyPath, |
| 50 | + ClientCertPath: certInfo.ClientCertPath, |
| 51 | + } |
| 52 | + |
| 53 | + return host |
| 54 | +} |
| 55 | + |
| 56 | +// fills nested host metadata and modifies if needed |
| 57 | +// this is used for configuration updates |
| 58 | +func FillNestedHostMetadata(m *HostMetadata) *HostMetadata { |
| 59 | + if m.HostOptions.EngineOptions == nil { |
| 60 | + m.HostOptions.EngineOptions = &engine.EngineOptions{} |
| 61 | + } |
| 62 | + |
| 63 | + if m.HostOptions.AuthOptions == nil { |
| 64 | + m.HostOptions.AuthOptions = &auth.AuthOptions{ |
| 65 | + StorePath: m.StorePath, |
| 66 | + CaCertPath: m.CaCertPath, |
| 67 | + CaCertRemotePath: "", |
| 68 | + ServerCertPath: m.ServerCertPath, |
| 69 | + ServerKeyPath: m.ServerKeyPath, |
| 70 | + ClientKeyPath: "", |
| 71 | + ServerCertRemotePath: "", |
| 72 | + ServerKeyRemotePath: "", |
| 73 | + PrivateKeyPath: m.PrivateKeyPath, |
| 74 | + ClientCertPath: m.ClientCertPath, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return m |
| 79 | +} |
| 80 | + |
| 81 | +func getCertInfoFromHost(h *Host) CertPathInfo { |
| 82 | + // setup cert paths |
| 83 | + caCertPath := h.CaCertPath |
| 84 | + caKeyPath := h.PrivateKeyPath |
| 85 | + clientCertPath := h.ClientCertPath |
| 86 | + clientKeyPath := h.ClientKeyPath |
| 87 | + serverCertPath := h.ServerCertPath |
| 88 | + serverKeyPath := h.ServerKeyPath |
| 89 | + |
| 90 | + if caCertPath == "" { |
| 91 | + caCertPath = filepath.Join(utils.GetMachineCertDir(), "ca.pem") |
| 92 | + } |
| 93 | + |
| 94 | + if caKeyPath == "" { |
| 95 | + caKeyPath = filepath.Join(utils.GetMachineCertDir(), "ca-key.pem") |
| 96 | + } |
| 97 | + |
| 98 | + if clientCertPath == "" { |
| 99 | + clientCertPath = filepath.Join(utils.GetMachineCertDir(), "cert.pem") |
| 100 | + } |
| 101 | + |
| 102 | + if clientKeyPath == "" { |
| 103 | + clientKeyPath = filepath.Join(utils.GetMachineCertDir(), "key.pem") |
| 104 | + } |
| 105 | + |
| 106 | + if serverCertPath == "" { |
| 107 | + serverCertPath = filepath.Join(utils.GetMachineCertDir(), "server.pem") |
| 108 | + } |
| 109 | + |
| 110 | + if serverKeyPath == "" { |
| 111 | + serverKeyPath = filepath.Join(utils.GetMachineCertDir(), "server-key.pem") |
| 112 | + } |
| 113 | + |
| 114 | + return CertPathInfo{ |
| 115 | + CaCertPath: caCertPath, |
| 116 | + CaKeyPath: caKeyPath, |
| 117 | + ClientCertPath: clientCertPath, |
| 118 | + ClientKeyPath: clientKeyPath, |
| 119 | + ServerCertPath: serverCertPath, |
| 120 | + ServerKeyPath: serverKeyPath, |
| 121 | + } |
| 122 | +} |
0 commit comments