2016-03-29 16:55:17 +00:00
|
|
|
package utils
|
2015-06-08 00:36:07 +00:00
|
|
|
|
|
|
|
import (
|
2016-01-27 01:01:39 +00:00
|
|
|
"fmt"
|
2016-03-29 16:55:17 +00:00
|
|
|
"log"
|
2015-06-08 00:36:07 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2016-03-29 16:55:17 +00:00
|
|
|
"github.com/spf13/cobra"
|
2016-01-15 04:06:25 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2015-06-08 00:36:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Configuration type from CLI and config files.
|
|
|
|
type Configuration struct {
|
2016-03-29 16:55:17 +00:00
|
|
|
context *cobra.Command
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfiguration creates a new configuration from CLI data.
|
2016-03-29 16:55:17 +00:00
|
|
|
func NewConfiguration(c *cobra.Command) *Configuration {
|
2015-06-08 00:36:07 +00:00
|
|
|
return &Configuration{context: c}
|
|
|
|
}
|
|
|
|
|
2016-01-27 01:01:39 +00:00
|
|
|
// KeyType the type from which private keys should be generated
|
|
|
|
func (c *Configuration) KeyType() (acme.KeyType, error) {
|
2016-03-29 16:55:17 +00:00
|
|
|
keytype, err := c.context.PersistentFlags().GetString("key-type")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
switch strings.ToUpper(keytype) {
|
2016-01-27 01:01:39 +00:00
|
|
|
case "RSA2048":
|
|
|
|
return acme.RSA2048, nil
|
|
|
|
case "RSA4096":
|
|
|
|
return acme.RSA4096, nil
|
|
|
|
case "RSA8192":
|
|
|
|
return acme.RSA8192, nil
|
|
|
|
case "EC256":
|
|
|
|
return acme.EC256, nil
|
|
|
|
case "EC384":
|
|
|
|
return acme.EC384, nil
|
|
|
|
}
|
|
|
|
|
2016-03-29 16:55:17 +00:00
|
|
|
return "", fmt.Errorf("Unsupported KeyType: %s", keytype)
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 06:24:19 +00:00
|
|
|
// ExcludedSolvers is a list of solvers that are to be excluded.
|
2016-01-15 04:06:25 +00:00
|
|
|
func (c *Configuration) ExcludedSolvers() (cc []acme.Challenge) {
|
2016-03-29 16:55:17 +00:00
|
|
|
exclude, err := c.context.PersistentFlags().GetStringSlice("exclude")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
|
|
|
for _, s := range exclude {
|
2016-01-15 04:06:25 +00:00
|
|
|
cc = append(cc, acme.Challenge(s))
|
|
|
|
}
|
|
|
|
return
|
2015-06-12 22:16:49 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 00:36:07 +00:00
|
|
|
// ServerPath returns the OS dependent path to the data for a specific CA
|
|
|
|
func (c *Configuration) ServerPath() string {
|
2016-03-29 16:55:17 +00:00
|
|
|
server, err := c.context.PersistentFlags().GetString("server")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
|
|
|
srv, _ := url.Parse(server)
|
2015-09-26 18:00:19 +00:00
|
|
|
srvStr := strings.Replace(srv.Host, ":", "_", -1)
|
2015-06-08 00:36:07 +00:00
|
|
|
return strings.Replace(srvStr, "/", string(os.PathSeparator), -1)
|
|
|
|
}
|
|
|
|
|
2016-02-14 06:24:19 +00:00
|
|
|
// CertPath gets the path for certificates.
|
2015-06-13 01:56:34 +00:00
|
|
|
func (c *Configuration) CertPath() string {
|
2016-03-29 16:55:17 +00:00
|
|
|
pathS, err := c.context.PersistentFlags().GetString("path")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
|
|
|
return path.Join(pathS, "certificates")
|
2015-06-13 01:56:34 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 00:36:07 +00:00
|
|
|
// AccountsPath returns the OS dependent path to the
|
|
|
|
// local accounts for a specific CA
|
|
|
|
func (c *Configuration) AccountsPath() string {
|
2016-03-29 16:55:17 +00:00
|
|
|
pathS, err := c.context.PersistentFlags().GetString("path")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
|
|
|
}
|
|
|
|
return path.Join(pathS, "accounts", c.ServerPath())
|
2015-06-08 00:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AccountPath returns the OS dependent path to a particular account
|
|
|
|
func (c *Configuration) AccountPath(acc string) string {
|
|
|
|
return path.Join(c.AccountsPath(), acc)
|
|
|
|
}
|
|
|
|
|
2016-02-14 06:24:19 +00:00
|
|
|
// AccountKeysPath returns the OS dependent path to the keys of a particular account
|
2015-06-08 00:36:07 +00:00
|
|
|
func (c *Configuration) AccountKeysPath(acc string) string {
|
|
|
|
return path.Join(c.AccountPath(acc), "keys")
|
|
|
|
}
|