2019-01-11 14:59:35 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-01-18 10:36:58 +00:00
|
|
|
"errors"
|
2019-01-11 14:59:35 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/fpath"
|
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/process"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
setupCmd = &cobra.Command{
|
|
|
|
Use: "setup",
|
|
|
|
Short: "Setup a certificate signing server",
|
|
|
|
RunE: cmdSetup,
|
|
|
|
Annotations: map[string]string{"type": "setup"},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(setupCmd)
|
2019-01-22 14:34:40 +00:00
|
|
|
cfgstruct.BindSetup(setupCmd.Flags(), &config, cfgstruct.ConfDir(defaultConfDir), cfgstruct.IdentityDir(defaultIdentityDir))
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdSetup(cmd *cobra.Command, args []string) error {
|
2019-01-22 12:35:48 +00:00
|
|
|
setupDir, err := filepath.Abs(confDir)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(setupDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := fpath.IsValidSetupDir(setupDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-22 12:35:48 +00:00
|
|
|
if !config.Overwrite && !valid {
|
2019-01-11 14:59:35 +00:00
|
|
|
fmt.Printf("certificate signer configuration already exists (%v). rerun with --overwrite\n", setupDir)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-22 12:35:48 +00:00
|
|
|
if config.Overwrite {
|
|
|
|
config.CA.Overwrite = true
|
|
|
|
config.Identity.Overwrite = true
|
|
|
|
config.Signer.Overwrite = true
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 12:35:48 +00:00
|
|
|
if _, err := config.Signer.NewAuthDB(); err != nil {
|
2019-01-11 14:59:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-22 12:35:48 +00:00
|
|
|
if config.Identity.Status() != identity.CertKey {
|
2019-01-18 10:36:58 +00:00
|
|
|
return errors.New("identity is missing")
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 14:48:49 +00:00
|
|
|
overrides := map[string]interface{}{
|
2019-01-22 12:35:48 +00:00
|
|
|
"ca.cert-path": config.CA.CertPath,
|
|
|
|
"ca.key-path": config.CA.KeyPath,
|
|
|
|
"identity.cert-path": config.Identity.CertPath,
|
|
|
|
"identity.key-path": config.Identity.KeyPath,
|
2019-01-11 14:59:35 +00:00
|
|
|
"log.level": "info",
|
|
|
|
}
|
2019-01-28 14:48:49 +00:00
|
|
|
return process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides)
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|