command/server: initial working
This commit is contained in:
parent
cb3e91b338
commit
f71f29b801
|
@ -2,10 +2,15 @@ package command
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/vault/command/server"
|
||||
"github.com/hashicorp/vault/helper/flag-slice"
|
||||
vaulthttp "github.com/hashicorp/vault/http"
|
||||
"github.com/hashicorp/vault/physical"
|
||||
"github.com/hashicorp/vault/vault"
|
||||
)
|
||||
|
||||
// ServerCommand is a Command that starts the Vault server.
|
||||
|
@ -46,8 +51,43 @@ func (c *ServerCommand) Run(args []string) int {
|
|||
}
|
||||
}
|
||||
|
||||
// Initialize the listeners
|
||||
// Initialize the backend
|
||||
backend, err := physical.NewBackend(
|
||||
config.Backend.Type, config.Backend.Config)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error initializing backend of type %s: %s",
|
||||
config.Backend.Type, err))
|
||||
return 1
|
||||
}
|
||||
|
||||
// Initialize the core
|
||||
core, err := vault.NewCore(&vault.CoreConfig{
|
||||
Physical: backend,
|
||||
})
|
||||
|
||||
// Initialize the listeners
|
||||
lns := make([]net.Listener, 0, len(config.Listeners))
|
||||
for _, lnConfig := range config.Listeners {
|
||||
ln, err := server.NewListener(lnConfig.Type, lnConfig.Config)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error initializing listener of type %s: %s",
|
||||
lnConfig.Type, err))
|
||||
return 1
|
||||
}
|
||||
|
||||
lns = append(lns, ln)
|
||||
}
|
||||
|
||||
// Initialize the HTTP server
|
||||
server := &http.Server{}
|
||||
server.Handler = vaulthttp.Handler(core)
|
||||
for _, ln := range lns {
|
||||
go server.Serve(ln)
|
||||
}
|
||||
|
||||
<-make(chan struct{})
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ type Config struct {
|
|||
// Listener is the listener configuration for the server.
|
||||
type Listener struct {
|
||||
Type string
|
||||
Config map[string]interface{}
|
||||
Config map[string]string
|
||||
}
|
||||
|
||||
func (l *Listener) GoString() string {
|
||||
|
@ -31,7 +31,7 @@ func (l *Listener) GoString() string {
|
|||
// Backend is the backend configuration for the server.
|
||||
type Backend struct {
|
||||
Type string
|
||||
Config map[string]interface{}
|
||||
Config map[string]string
|
||||
}
|
||||
|
||||
func (b *Backend) GoString() string {
|
||||
|
@ -217,7 +217,7 @@ func loadListeners(os *hclobj.Object) ([]*Listener, error) {
|
|||
for _, obj := range allNames {
|
||||
k := obj.Key
|
||||
|
||||
var config map[string]interface{}
|
||||
var config map[string]string
|
||||
if err := hcl.DecodeObject(&config, obj); err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"Error reading config for %s: %s",
|
||||
|
@ -267,7 +267,7 @@ func loadBackend(os *hclobj.Object) (*Backend, error) {
|
|||
obj := allNames[0]
|
||||
result.Type = obj.Key
|
||||
|
||||
var config map[string]interface{}
|
||||
var config map[string]string
|
||||
if err := hcl.DecodeObject(&config, obj); err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"Error reading config for backend %s: %s",
|
||||
|
|
|
@ -15,7 +15,7 @@ func TestLoadConfigFile(t *testing.T) {
|
|||
Listeners: []*Listener{
|
||||
&Listener{
|
||||
Type: "tcp",
|
||||
Config: map[string]interface{}{
|
||||
Config: map[string]string{
|
||||
"address": "127.0.0.1:443",
|
||||
},
|
||||
},
|
||||
|
@ -23,7 +23,7 @@ func TestLoadConfigFile(t *testing.T) {
|
|||
|
||||
Backend: &Backend{
|
||||
Type: "consul",
|
||||
Config: map[string]interface{}{
|
||||
Config: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
|
@ -43,7 +43,7 @@ func TestLoadConfigFile_json(t *testing.T) {
|
|||
Listeners: []*Listener{
|
||||
&Listener{
|
||||
Type: "tcp",
|
||||
Config: map[string]interface{}{
|
||||
Config: map[string]string{
|
||||
"address": "127.0.0.1:443",
|
||||
},
|
||||
},
|
||||
|
@ -51,7 +51,7 @@ func TestLoadConfigFile_json(t *testing.T) {
|
|||
|
||||
Backend: &Backend{
|
||||
Type: "consul",
|
||||
Config: map[string]interface{}{
|
||||
Config: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
|
@ -71,7 +71,7 @@ func TestLoadConfigFile_json2(t *testing.T) {
|
|||
Listeners: []*Listener{
|
||||
&Listener{
|
||||
Type: "tcp",
|
||||
Config: map[string]interface{}{
|
||||
Config: map[string]string{
|
||||
"address": "127.0.0.1:443",
|
||||
},
|
||||
},
|
||||
|
@ -79,7 +79,7 @@ func TestLoadConfigFile_json2(t *testing.T) {
|
|||
|
||||
Backend: &Backend{
|
||||
Type: "consul",
|
||||
Config: map[string]interface{}{
|
||||
Config: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
|
@ -99,7 +99,7 @@ func TestLoadConfigDir(t *testing.T) {
|
|||
Listeners: []*Listener{
|
||||
&Listener{
|
||||
Type: "tcp",
|
||||
Config: map[string]interface{}{
|
||||
Config: map[string]string{
|
||||
"address": "127.0.0.1:443",
|
||||
},
|
||||
},
|
||||
|
@ -107,7 +107,7 @@ func TestLoadConfigDir(t *testing.T) {
|
|||
|
||||
Backend: &Backend{
|
||||
Type: "consul",
|
||||
Config: map[string]interface{}{
|
||||
Config: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue