mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
command/server: initial working
This commit is contained in:
@@ -2,10 +2,15 @@ package command
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/vault/command/server"
|
"github.com/hashicorp/vault/command/server"
|
||||||
"github.com/hashicorp/vault/helper/flag-slice"
|
"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.
|
// 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
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ type Config struct {
|
|||||||
// Listener is the listener configuration for the server.
|
// Listener is the listener configuration for the server.
|
||||||
type Listener struct {
|
type Listener struct {
|
||||||
Type string
|
Type string
|
||||||
Config map[string]interface{}
|
Config map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Listener) GoString() string {
|
func (l *Listener) GoString() string {
|
||||||
@@ -31,7 +31,7 @@ func (l *Listener) GoString() string {
|
|||||||
// Backend is the backend configuration for the server.
|
// Backend is the backend configuration for the server.
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
Type string
|
Type string
|
||||||
Config map[string]interface{}
|
Config map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Backend) GoString() string {
|
func (b *Backend) GoString() string {
|
||||||
@@ -217,7 +217,7 @@ func loadListeners(os *hclobj.Object) ([]*Listener, error) {
|
|||||||
for _, obj := range allNames {
|
for _, obj := range allNames {
|
||||||
k := obj.Key
|
k := obj.Key
|
||||||
|
|
||||||
var config map[string]interface{}
|
var config map[string]string
|
||||||
if err := hcl.DecodeObject(&config, obj); err != nil {
|
if err := hcl.DecodeObject(&config, obj); err != nil {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"Error reading config for %s: %s",
|
"Error reading config for %s: %s",
|
||||||
@@ -267,7 +267,7 @@ func loadBackend(os *hclobj.Object) (*Backend, error) {
|
|||||||
obj := allNames[0]
|
obj := allNames[0]
|
||||||
result.Type = obj.Key
|
result.Type = obj.Key
|
||||||
|
|
||||||
var config map[string]interface{}
|
var config map[string]string
|
||||||
if err := hcl.DecodeObject(&config, obj); err != nil {
|
if err := hcl.DecodeObject(&config, obj); err != nil {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"Error reading config for backend %s: %s",
|
"Error reading config for backend %s: %s",
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func TestLoadConfigFile(t *testing.T) {
|
|||||||
Listeners: []*Listener{
|
Listeners: []*Listener{
|
||||||
&Listener{
|
&Listener{
|
||||||
Type: "tcp",
|
Type: "tcp",
|
||||||
Config: map[string]interface{}{
|
Config: map[string]string{
|
||||||
"address": "127.0.0.1:443",
|
"address": "127.0.0.1:443",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -23,7 +23,7 @@ func TestLoadConfigFile(t *testing.T) {
|
|||||||
|
|
||||||
Backend: &Backend{
|
Backend: &Backend{
|
||||||
Type: "consul",
|
Type: "consul",
|
||||||
Config: map[string]interface{}{
|
Config: map[string]string{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -43,7 +43,7 @@ func TestLoadConfigFile_json(t *testing.T) {
|
|||||||
Listeners: []*Listener{
|
Listeners: []*Listener{
|
||||||
&Listener{
|
&Listener{
|
||||||
Type: "tcp",
|
Type: "tcp",
|
||||||
Config: map[string]interface{}{
|
Config: map[string]string{
|
||||||
"address": "127.0.0.1:443",
|
"address": "127.0.0.1:443",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -51,7 +51,7 @@ func TestLoadConfigFile_json(t *testing.T) {
|
|||||||
|
|
||||||
Backend: &Backend{
|
Backend: &Backend{
|
||||||
Type: "consul",
|
Type: "consul",
|
||||||
Config: map[string]interface{}{
|
Config: map[string]string{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -71,7 +71,7 @@ func TestLoadConfigFile_json2(t *testing.T) {
|
|||||||
Listeners: []*Listener{
|
Listeners: []*Listener{
|
||||||
&Listener{
|
&Listener{
|
||||||
Type: "tcp",
|
Type: "tcp",
|
||||||
Config: map[string]interface{}{
|
Config: map[string]string{
|
||||||
"address": "127.0.0.1:443",
|
"address": "127.0.0.1:443",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -79,7 +79,7 @@ func TestLoadConfigFile_json2(t *testing.T) {
|
|||||||
|
|
||||||
Backend: &Backend{
|
Backend: &Backend{
|
||||||
Type: "consul",
|
Type: "consul",
|
||||||
Config: map[string]interface{}{
|
Config: map[string]string{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -99,7 +99,7 @@ func TestLoadConfigDir(t *testing.T) {
|
|||||||
Listeners: []*Listener{
|
Listeners: []*Listener{
|
||||||
&Listener{
|
&Listener{
|
||||||
Type: "tcp",
|
Type: "tcp",
|
||||||
Config: map[string]interface{}{
|
Config: map[string]string{
|
||||||
"address": "127.0.0.1:443",
|
"address": "127.0.0.1:443",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -107,7 +107,7 @@ func TestLoadConfigDir(t *testing.T) {
|
|||||||
|
|
||||||
Backend: &Backend{
|
Backend: &Backend{
|
||||||
Type: "consul",
|
Type: "consul",
|
||||||
Config: map[string]interface{}{
|
Config: map[string]string{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user