Exemplo n.º 1
0
func (s *server) initEndpoints(ctx types.Context) error {

	endpointsObj := s.config.Get(types.ConfigEndpoints)
	if endpointsObj == nil {
		if err := s.initDefaultEndpoint(); err != nil {
			return goof.WithError("no endpoints defined", err)
		}
		endpointsObj = s.config.Get(types.ConfigEndpoints)
	}

	endpoints, ok := endpointsObj.(map[string]interface{})
	if !ok {
		return goof.New("endpoints invalid type")
	}

	if len(endpoints) == 0 {
		if err := s.initDefaultEndpoint(); err != nil {
			return err
		}
	}

	for endpointName := range endpoints {

		endpoint := fmt.Sprintf("%s.%s", types.ConfigEndpoints, endpointName)
		address := fmt.Sprintf("%s.address", endpoint)
		laddr := s.config.GetString(address)
		if laddr == "" {
			return goof.WithField("endpoint", endpoint, "missing address")
		}

		laddrET := types.ParseEndpointType(laddr)
		switch laddrET {

		case types.TCPEndpoint:

			var tcpPort int
			func() {
				tcpPortLock.Lock()
				defer tcpPortLock.Unlock()
				tcpPort = gotil.RandomTCPPort()
			}()

			laddr = fmt.Sprintf("tcp://127.0.0.1:%d", tcpPort)
			s.ctx.WithField("endpoint", endpoint).Info(
				"initializing auto tcp endpoint")

		case types.UnixEndpoint:

			laddr = fmt.Sprintf("unix://%s", utils.GetTempSockFile())
			s.ctx.WithField("endpoint", endpoint).Info(
				"initializing auto unix endpoint")

		}

		s.ctx.WithFields(log.Fields{
			"endpoint": endpoint, "address": laddr}).Debug("endpoint info")

		s.addrs = append(s.addrs, laddr)

		proto, addr, err := gotil.ParseAddress(laddr)
		if err != nil {
			return err
		}

		logFields := map[string]interface{}{
			"endpoint": endpointName,
			"address":  laddr,
		}

		tlsConfig, err :=
			utils.ParseTLSConfig(s.config.Scope(endpoint), logFields, endpoint)
		if err != nil {
			return err
		}

		ctx.WithFields(logFields).Info("configured endpoint")

		srv, err := s.newHTTPServer(proto, addr, tlsConfig)
		if err != nil {
			return err
		}

		ctx.Info("server created")
		s.servers = append(s.servers, srv)
	}

	return nil
}
Exemplo n.º 2
0
func initTestConfigs(config map[string]interface{}) {
	tcpHost := fmt.Sprintf("tcp://127.0.0.1:%d", gotil.RandomTCPPort())
	tcpTLSHost := fmt.Sprintf("tcp://127.0.0.1:%d", gotil.RandomTCPPort())
	unixHost := fmt.Sprintf("unix://%s", utils.GetTempSockFile())
	unixTLSHost := fmt.Sprintf("unix://%s", utils.GetTempSockFile())

	clientTLSConfig := func() map[string]interface{} {
		return map[string]interface{}{
			"serverName":       "libstorage-server",
			"certFile":         clientCrt,
			"keyFile":          clientKey,
			"trustedCertsFile": trustedCerts,
		}
	}

	serverTLSConfig := func() map[string]interface{} {
		return map[string]interface{}{
			"serverName":         "libstorage-server",
			"certFile":           serverCrt,
			"keyFile":            serverKey,
			"trustedCertsFile":   trustedCerts,
			"clientCertRequired": true,
		}
	}

	config["tests"] = map[string]interface{}{

		"tcp": map[string]interface{}{
			"libstorage": map[string]interface{}{
				"host": tcpHost,
				"server": map[string]interface{}{
					"endpoints": map[string]interface{}{
						"localhost": map[string]interface{}{
							"address": tcpHost,
						},
					},
				},
			},
		},

		"tcpTLS": map[string]interface{}{
			"libstorage": map[string]interface{}{
				"host": tcpTLSHost,
				"server": map[string]interface{}{
					"endpoints": map[string]interface{}{
						"localhost": map[string]interface{}{
							"address": tcpTLSHost,
							"tls":     serverTLSConfig(),
						},
					},
				},
				"client": map[string]interface{}{
					"tls": clientTLSConfig(),
				},
			},
		},

		"unix": map[string]interface{}{
			"libstorage": map[string]interface{}{
				"host": unixHost,
				"server": map[string]interface{}{
					"endpoints": map[string]interface{}{
						"localhost": map[string]interface{}{
							"address": unixHost,
						},
					},
				},
			},
		},

		"unixTLS": map[string]interface{}{
			"libstorage": map[string]interface{}{
				"host": unixTLSHost,
				"server": map[string]interface{}{
					"endpoints": map[string]interface{}{
						"localhost": map[string]interface{}{
							"address": unixTLSHost,
							"tls":     serverTLSConfig(),
						},
					},
				},
				"client": map[string]interface{}{
					"tls": clientTLSConfig(),
				},
			},
		},
	}
}