1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
package cliconfig
import (
"fmt"
"io"
"os"
localtls "github.com/lxc/incus/v6/shared/tls"
"github.com/lxc/incus/v6/shared/util"
)
// HasClientCertificate will return true if a client certificate has already been generated.
func (c *Config) HasClientCertificate() bool {
certf := c.ConfigPath("client.crt")
keyf := c.ConfigPath("client.key")
if !util.PathExists(certf) || !util.PathExists(keyf) {
return false
}
return true
}
// HasRemoteClientCertificate will return true if a remote-specific client certificate is present.
func (c *Config) HasRemoteClientCertificate(name string) bool {
certf := c.ConfigPath("clientcerts", fmt.Sprintf("%s.crt", name))
keyf := c.ConfigPath("clientcerts", fmt.Sprintf("%s.key", name))
if !util.PathExists(certf) || !util.PathExists(keyf) {
return false
}
return true
}
// GenerateClientCertificate will generate the needed client.crt and client.key if needed.
func (c *Config) GenerateClientCertificate() error {
if c.HasClientCertificate() {
return nil
}
certf := c.ConfigPath("client.crt")
keyf := c.ConfigPath("client.key")
return localtls.FindOrGenCert(certf, keyf, true, false)
}
// CopyGlobalCert will copy global (system-wide) certificates to the user config path.
func (c *Config) CopyGlobalCert(src string, dst string) error {
copyFile := func(oldPath string, newPath string, mode os.FileMode) error {
sourceFile, err := os.Open(oldPath)
if err != nil {
return err
}
defer sourceFile.Close()
// Get the mode from the source file if not specified.
if mode == 0 {
fInfo, err := sourceFile.Stat()
if err != nil {
return err
}
mode = fInfo.Mode()
}
// Create new file.
newFile, err := os.Create(newPath)
if err != nil {
return err
}
defer newFile.Close()
// Apply the file mode.
err = newFile.Chmod(mode)
if err != nil {
return err
}
// Copy the content.
_, err = io.Copy(newFile, sourceFile)
if err != nil {
return err
}
return nil
}
// Server certificate.
oldPath := c.GlobalConfigPath("servercerts", fmt.Sprintf("%s.crt", src))
if util.PathExists(oldPath) {
newPath := c.ConfigPath("servercerts", fmt.Sprintf("%s.crt", dst))
err := copyFile(oldPath, newPath, 0)
if err != nil {
return err
}
}
// Client certificate.
oldPath = c.GlobalConfigPath("clientcerts", fmt.Sprintf("%s.crt", src))
if util.PathExists(oldPath) {
newPath := c.ConfigPath("clientcerts", fmt.Sprintf("%s.crt", dst))
err := copyFile(oldPath, newPath, 0)
if err != nil {
return err
}
}
// Client key.
oldPath = c.GlobalConfigPath("clientcerts", fmt.Sprintf("%s.key", src))
if util.PathExists(oldPath) {
newPath := c.ConfigPath("clientcerts", fmt.Sprintf("%s.key", dst))
err := copyFile(oldPath, newPath, 0o600)
if err != nil {
return err
}
}
// Client CA.
oldPath = c.GlobalConfigPath("clientcerts", fmt.Sprintf("%s.ca", src))
if util.PathExists(oldPath) {
newPath := c.ConfigPath("clientcerts", fmt.Sprintf("%s.ca", dst))
err := copyFile(oldPath, newPath, 0)
if err != nil {
return err
}
}
return nil
}
|