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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
package serverclient
import (
"context"
"crypto/tls"
"fmt"
"os"
"time"
"github.com/hashicorp/go-hclog"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/hashicorp/vagrant/internal/clicontext"
"github.com/hashicorp/vagrant/internal/protocolversion"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"github.com/hashicorp/vagrant/internal/serverconfig"
)
type VagrantClient struct {
vagrant_server.VagrantClient
conn *grpc.ClientConn
}
func NewVagrantClient(ctx context.Context, log hclog.Logger, addr string) (*VagrantClient, error) {
log = log.Named("vagrant.client")
conn, err := Connect(ctx,
WithAddr(addr),
)
if err != nil {
return nil, err
}
return &VagrantClient{
VagrantClient: vagrant_server.NewVagrantClient(conn),
conn: conn,
}, nil
}
func WrapVagrantClient(conn *grpc.ClientConn) *VagrantClient {
return &VagrantClient{
VagrantClient: vagrant_server.NewVagrantClient(conn),
conn: conn,
}
}
func (c *VagrantClient) ServerTarget() string {
return c.conn.Target()
}
func (c *VagrantClient) Conn() *grpc.ClientConn {
return c.conn
}
// ConnectOption is used to configure how Vagrant server connection
// configuration is sourced.
type ConnectOption func(*connectConfig) error
// Connect connects to the Vagrant server. This returns the raw gRPC connection.
// You'll have to wrap it in NewVagrantClient to get the Vagrant client.
// We return the raw connection so that you have control over how to close it,
// and to support potentially alternate services in the future.
func Connect(ctx context.Context, opts ...ConnectOption) (*grpc.ClientConn, error) {
// Defaults
var cfg connectConfig
cfg.Timeout = 5 * time.Second
// Set config
for _, opt := range opts {
if err := opt(&cfg); err != nil {
return nil, err
}
}
if cfg.Addr == "" {
if cfg.Optional {
return nil, nil
}
return nil, fmt.Errorf("no server credentials found")
}
ctx, cancel := context.WithTimeout(ctx, cfg.Timeout)
defer cancel()
// Build our options
grpcOpts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithUnaryInterceptor(protocolversion.UnaryClientInterceptor(protocolversion.Current())),
grpc.WithStreamInterceptor(protocolversion.StreamClientInterceptor(protocolversion.Current())),
}
if !cfg.Tls {
grpcOpts = append(grpcOpts, grpc.WithInsecure())
} else if cfg.TlsSkipVerify {
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(
credentials.NewTLS(&tls.Config{InsecureSkipVerify: true}),
))
}
if cfg.Auth {
token := cfg.Token
if v := os.Getenv(EnvServerToken); v != "" {
token = v
}
if token == "" {
return nil, fmt.Errorf("No token available at the VAGRANT_SERVER_TOKEN environment variable")
}
grpcOpts = append(grpcOpts, grpc.WithPerRPCCredentials(StaticToken(token)))
}
// Connect to this server
return grpc.DialContext(ctx, cfg.Addr, grpcOpts...)
}
// ContextConfig will return the context configuration for the given connection
// options.
func ContextConfig(opts ...ConnectOption) (*clicontext.Config, error) {
// Setup config
var cfg connectConfig
for _, opt := range opts {
if err := opt(&cfg); err != nil {
return nil, err
}
}
// Build it
return &clicontext.Config{
Server: serverconfig.Client{
Address: cfg.Addr,
Tls: cfg.Tls,
TlsSkipVerify: cfg.TlsSkipVerify,
RequireAuth: cfg.Token != "",
AuthToken: cfg.Token,
},
}, nil
}
type connectConfig struct {
Addr string
Tls bool
TlsSkipVerify bool
Auth bool
Token string
Optional bool // See Optional func
Timeout time.Duration
}
func WithAddr(addr string) ConnectOption {
return func(c *connectConfig) error {
c.Addr = addr
return nil
}
}
// FromEnv sources the connection information from the environment
// using standard environment variables.
func FromEnv() ConnectOption {
return func(c *connectConfig) error {
if v := os.Getenv(EnvServerAddr); v != "" {
c.Addr = v
c.Tls = os.Getenv(EnvServerTls) != ""
c.TlsSkipVerify = os.Getenv(EnvServerTlsSkipVerify) != ""
c.Auth = os.Getenv(EnvServerToken) != ""
}
return nil
}
}
// FromContextConfig loads a specific context config.
func FromContextConfig(cfg *clicontext.Config) ConnectOption {
return func(c *connectConfig) error {
if cfg != nil && cfg.Server.Address != "" {
c.Addr = cfg.Server.Address
c.Tls = cfg.Server.Tls
c.TlsSkipVerify = cfg.Server.TlsSkipVerify
if cfg.Server.RequireAuth {
c.Auth = true
c.Token = cfg.Server.AuthToken
}
}
return nil
}
}
// FromContext loads the context. This will prefer the given name. If name
// is empty, we'll respect the VAGRANT_CONTEXT env var followed by the
// default context.
func FromContext(st *clicontext.Storage, n string) ConnectOption {
return func(c *connectConfig) error {
// Figure out what context to load. We prefer to load a manually
// specified one. If that isn't set, we prefer the env var. If that
// isn't set, we load the default.
if n == "" {
if v := os.Getenv(EnvContext); v != "" {
n = v
} else {
def, err := st.Default()
if err != nil {
return err
}
n = def
}
}
// If we still have no name, then we do nothing. We also accept
// "-" as a valid name that means "do nothing".
if n == "" || n == "-" {
return nil
}
// Load it and set it.
cfg, err := st.Load(n)
if err != nil {
return err
}
opt := FromContextConfig(cfg)
return opt(c)
}
}
// Auth specifies that this server should require auth and therefore
// a token should be sourced from the environment and sent.
func Auth() ConnectOption {
return func(c *connectConfig) error {
c.Auth = true
return nil
}
}
// Optional specifies that getting server connection information is
// optional. If this is specified and no credentials are found, Connect
// will return (nil, nil). If this is NOT specified and no credentials are
// found, it is an error.
func Optional() ConnectOption {
return func(c *connectConfig) error {
c.Optional = true
return nil
}
}
// Timeout specifies a connection timeout. This defaults to 5 seconds.
func Timeout(t time.Duration) ConnectOption {
return func(c *connectConfig) error {
c.Timeout = t
return nil
}
}
// Common environment variables.
const (
// ServerAddr is the address for the Vagrant server. This should be
// in the format of "ip:port" for TCP.
EnvServerAddr = "VAGRANT_SERVER_ADDR"
// ServerTls should be any value that strconv.ParseBool parses as
// true to connect to the server with TLS.
EnvServerTls = "VAGRANT_SERVER_TLS"
EnvServerTlsSkipVerify = "VAGRANT_SERVER_TLS_SKIP_VERIFY"
// EnvServerToken is the token for authenticated with the server.
EnvServerToken = "VAGRANT_SERVER_TOKEN"
// EnvContext specifies a named context to load.
EnvContext = "VAGRANT_CONTEXT"
)
// This is a weird type that only exists to satisify the interface required by
// grpc.WithPerRPCCredentials. That api is designed to incorporate things like OAuth
// but in our case, we really just want to send this static token through, but we still
// need to the dance.
type StaticToken string
func (t StaticToken) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"authorization": string(t),
}, nil
}
func (t StaticToken) RequireTransportSecurity() bool {
return false
}
|