예제 #1
0
func main() {
	serverIPAddr, err := platform.GetMyIp()

	if err != nil {
		logger.Fatalf("Could not resolve server IP address: %s", err)
	}

	if err := ioutil.WriteFile(SSL_CERT_FILE, []byte(strings.Replace(os.Getenv("SSL_CERT"), "\\n", "\n", -1)), 0755); err != nil {
		log.Fatalf("> failed to write SSL cert file: %s", err)
	}

	if err := ioutil.WriteFile(SSL_KEY_FILE, []byte(strings.Replace(os.Getenv("SSL_KEY"), "\\n", "\n", -1)), 0755); err != nil {
		log.Fatalf("> failed to write SSL cert file: %s", err)
	}

	server, _ := NewServer(&Options{
		IPAddr:      platform.Getenv("SERVER_IP", serverIPAddr),
		MicroIpAddr: serverIPAddr,
		Port:        serverPort,
		TLSCertFile: SSL_CERT_FILE,
		TLSKeyFile:  SSL_KEY_FILE,
	})

	connectionManager := platform.NewAmqpConnectionManager(rabbitUser, rabbitPass, rabbitAddr+":"+rabbitPort, "")
	publisher = getDefaultPublisher(connectionManager)
	subscriber = getDefaultSubscriber(connectionManager, server.GetRouterURI())

	router := platform.NewStandardRouter(publisher, subscriber)
	server.SetRouter(router)

	manageRouterState(&platform.RouterConfigList{
		RouterConfigs: []*platform.RouterConfig{
			&platform.RouterConfig{
				RouterType:   platform.RouterConfig_ROUTER_TYPE_HTTP.Enum(),
				ProtocolType: platform.RouterConfig_PROTOCOL_TYPE_HTTPS.Enum(),
				Host:         platform.String(serverIPAddr),
				Port:         platform.String(serverPort),
			},
		},
	})

	mux := http.NewServeMux()

	mux.Handle("/", EnforceHeadersMiddleware(http.HandlerFunc(MicroplatformEndpointHandler(server))))
	mux.Handle("/server", EnforceHeadersMiddleware(http.HandlerFunc(ServerDiscoveryHandler(server))))

	if err := server.ListenAndServeTLS(mux); err != nil {
		logger.Fatalf("Failed to listen and serve: %s", err)
	}

}
func TestMicroplatformHandler(t *testing.T) {
	server, _ := getServer(testPortMicro)

	connectionManager := platform.NewAmqpConnectionManager(rabbitUser, rabbitPass, rabbitAddr+":"+rabbitPort, "")
	publisher = getDefaultPublisher(connectionManager)
	subscriber = getDefaultSubscriber(connectionManager, server.GetRouterURI())

	router := platform.NewStandardRouter(publisher, subscriber)
	server.SetRouter(router)

	Convey("By accessing / with hex encoded string we should get back proper endpoint response", t, func() {

		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			w.WriteHeader(200)
			w.Header().Set("Content-Type", "text/plain")

			contents, err := ioutil.ReadAll(req.Body)

			if err != nil {
				w.Write(ErrorResponse(fmt.Sprintf("Failed to read body: %s", err)))
				return
			}

			platformRequestBytes, err := hex.DecodeString(fmt.Sprintf("%s", contents))

			if err != nil {
				w.Write(ErrorResponse(fmt.Sprintf("Failed to decode body: %s", err)))
				return
			}

			platformRequest := &platform.Request{}

			if err := platform.Unmarshal(platformRequestBytes, platformRequest); err != nil {
				w.Write(ErrorResponse(fmt.Sprintf("Failed to unmarshal platform request: %s", err)))
				return
			}

			if platformRequest.Routing == nil {
				platformRequest.Routing = &platform.Routing{}
			}

			if platformRequest.Routing.RouteFrom != nil {
				platformRequest.Routing.RouteFrom = []*platform.Route{}
			}

			if !platform.RouteToSchemeMatches(platformRequest, "microservice") {
				w.Write(ErrorResponse(fmt.Sprintf("Unsupported scheme provided: %s", platformRequest.Routing.RouteTo)))
				return
			}

			w.Header().Set("Content-Type", "text/plain")

			mainReq := platform.Request{
				Routing: &platform.Routing{
					RouteTo: []*platform.Route{&platform.Route{Uri: teltech.String("resource:///testing/reply/http-router")}},
				},
			}

			w.Write([]byte(hex.EncodeToString(GetProtoBytes(platform.GenerateResponse(&mainReq, &mainReq)))))
			return
		}))

		defer server.Close()

		// Give it time to start the listener
		time.Sleep(300 * time.Millisecond)

		client := &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			},
		}

		Convey("By passing valid / we should get back valid response", func() {
			req := &platform.Request{
				Routing: &platform.Routing{
					RouteTo: []*platform.Route{&platform.Route{Uri: teltech.String("microservice:///testing/get/http-router")}},
				},
				Completed: teltech.Bool(true),
			}

			responseBytes, err := platform.Marshal(req)
			So(err, ShouldBeNil)

			reqHex := []byte(hex.EncodeToString(responseBytes))
			request, err := http.NewRequest("POST", server.URL, bytes.NewBuffer(reqHex))
			So(err, ShouldBeNil)

			request.Header.Set("Content-Type", "text/plain")

			resp, err := client.Do(request)
			So(err, ShouldBeNil)

			defer resp.Body.Close()

			contents, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)

			platformResponseBytes, err := hex.DecodeString(string(contents))
			So(err, ShouldBeNil)

			response := &platform.Request{}
			err = platform.Unmarshal(platformResponseBytes, response)
			So(err, ShouldBeNil)

			So(response.GetRouting().GetRouteTo()[0].GetUri(), ShouldNotEqual, "resource:///meta/reply/error")
			So(response.GetRouting().GetRouteTo()[0].GetUri(), ShouldEqual, "resource:///testing/reply/http-router")
		})

		Convey("By passing invalid hex message towards / we should get back hex decode errors", func() {
			reqHex := []byte("I am not valid request")
			request, err := http.NewRequest("POST", server.URL, bytes.NewBuffer(reqHex))
			So(err, ShouldBeNil)

			request.Header.Set("Content-Type", "text/plain")

			resp, err := client.Do(request)
			So(err, ShouldBeNil)

			defer resp.Body.Close()

			contents, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)

			platformResponseBytes, err := hex.DecodeString(string(contents))
			So(err, ShouldBeNil)

			response := &platform.Request{}
			err = platform.Unmarshal(platformResponseBytes, response)
			So(err, ShouldBeNil)

			So(response.GetRouting().GetRouteTo()[0].GetUri(), ShouldNotEqual, "resource:///meta/reply/error")

			responseErr := &platform.Error{}
			err = platform.Unmarshal(response.GetPayload(), responseErr)
			So(err, ShouldBeNil)
			So(responseErr.GetMessage(), ShouldContainSubstring, "Failed to decode body")
		})

		Convey("By passing invalid hex request towards / we should get back unmarshal errors", func() {
			reqHex := []byte(hex.EncodeToString([]byte("Somewhere over the rainbow...")))
			request, err := http.NewRequest("POST", server.URL, bytes.NewBuffer(reqHex))
			So(err, ShouldBeNil)

			request.Header.Set("Content-Type", "text/plain")

			resp, err := client.Do(request)
			So(err, ShouldBeNil)

			defer resp.Body.Close()

			contents, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)

			platformResponseBytes, err := hex.DecodeString(string(contents))
			So(err, ShouldBeNil)

			response := &platform.Request{}
			err = platform.Unmarshal(platformResponseBytes, response)
			So(err, ShouldBeNil)

			So(response.GetRouting().GetRouteTo()[0].GetUri(), ShouldNotEqual, "resource:///meta/reply/error")

			responseErr := &platform.Error{}
			err = platform.Unmarshal(response.GetPayload(), responseErr)
			So(err, ShouldBeNil)
			So(responseErr.GetMessage(), ShouldContainSubstring, "Failed to unmarshal platform request")
		})

		Convey("By passing valid hex and request but invalid router scheme we should see invald scheme error", func() {
			req := &platform.Request{
				Routing: &platform.Routing{
					RouteTo: []*platform.Route{&platform.Route{Uri: teltech.String("microservice-invalid:///testing/get/http-router")}},
				},
				Completed: teltech.Bool(true),
			}

			responseBytes, err := platform.Marshal(req)
			So(err, ShouldBeNil)

			reqHex := []byte(hex.EncodeToString(responseBytes))
			request, err := http.NewRequest("POST", server.URL, bytes.NewBuffer(reqHex))
			So(err, ShouldBeNil)

			request.Header.Set("Content-Type", "text/plain")

			resp, err := client.Do(request)
			So(err, ShouldBeNil)

			defer resp.Body.Close()

			contents, err := ioutil.ReadAll(resp.Body)
			So(err, ShouldBeNil)

			platformResponseBytes, err := hex.DecodeString(string(contents))
			So(err, ShouldBeNil)

			response := &platform.Request{}
			err = platform.Unmarshal(platformResponseBytes, response)
			So(err, ShouldBeNil)

			So(response.GetRouting().GetRouteTo()[0].GetUri(), ShouldNotEqual, "resource:///meta/reply/error")

			responseErr := &platform.Error{}
			err = platform.Unmarshal(response.GetPayload(), responseErr)
			So(err, ShouldBeNil)
			So(responseErr.GetMessage(), ShouldContainSubstring, "Unsupported scheme provided")
		})
	})
}