Skip to content
Snippets Groups Projects
Commit d8aa753c authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6514 VorgangId Decryption fuer Collaboration Manager Adresse

parent 29c2ab6f
No related branches found
No related tags found
No related merge requests found
Showing with 214 additions and 66 deletions
......@@ -39,12 +39,16 @@ go run cmd/fachstellen-proxy/main.go
```
http:
server:
port: Port des HTTP Servers (int)
port: Port des HTTP Gateways (int)
grpc:
server:
port: Port des gRPC Servers (int)
mock: gRPC Registration und Collaboration Server mocken (bool)
collaboration:
server:
port: Port des gRPC Collaboration Servers (int)
router:
port: Port des gRPC Collaboration Routers (int)
registration:
server:
url: URL des gRPC Registration Servers im host:port Format (string)
logging:
level: "ERROR" | "WARN" | "INFO" | "DEBUG"
......
......@@ -40,6 +40,6 @@ func main() {
go mock.SetUpGrpcServer()
}
go server.StartCollaborationRouter(conf)
server.StartHttpGateway(conf)
go server.StartCollaborationRouter()
server.StartHttpGateway()
}
......@@ -49,12 +49,17 @@ type Config struct {
Grpc struct {
Mock bool `yaml:"mock" envconfig:"GRPC_MOCK"`
Collaboration struct {
Server struct {
Port int `yaml:"port" envconfig:"GRPC_COLLABORATION_SERVER_PORT"`
} `yaml:"server"`
Router struct {
Port int `yaml:"port" envconfig:"GRPC_COLLABORATION_ROUTER_PORT"`
} `yaml:"router"`
} `yaml:"collaboration"`
Registration struct {
Url string `yaml:"url" envconfig:"GRPC_REGISTRATION_URL"`
Server struct {
Url string `yaml:"url" envconfig:"GRPC_REGISTRATION_SERVER_URL"`
} `yaml:"server"`
} `yaml:"registration"`
} `yaml:"grpc"`
Logging struct {
......@@ -87,8 +92,8 @@ func LoadConfig(configFilePath ...string) Config {
log.Fatalf("FATAL: error reading env: %v", err)
}
if len(config.Grpc.Registration.Url) > 0 && !ValidateGrpcUrl(config.Grpc.Registration.Url) {
log.Fatalf("FATAL: gRPC Registration URL is not in host:port format")
if len(config.Grpc.Registration.Server.Url) > 0 && !ValidateGrpcUrl(config.Grpc.Registration.Server.Url) {
log.Fatalf("FATAL: gRPC Registration Server URL is not in host:port format")
}
return config
......
......@@ -38,8 +38,9 @@ func TestLoadConfig(t *testing.T) {
expectedConfig := Config{}
expectedConfig.Http.Server.Port = 8080
expectedConfig.Grpc.Collaboration.Server.Port = 50052
expectedConfig.Grpc.Collaboration.Router.Port = 50051
expectedConfig.Grpc.Registration.Url = "localhost:50052"
expectedConfig.Grpc.Registration.Server.Url = "localhost:50052"
expectedConfig.Grpc.Mock = false
expectedConfig.Logging.Level = "DEBUG"
......@@ -49,8 +50,9 @@ func TestLoadConfig(t *testing.T) {
t.Run("should load config from env", func(t *testing.T) {
envVars := map[string]string{
"HTTP_SERVER_PORT": "9090",
"GRPC_COLLABORATION_SERVER_PORT": "50052",
"GRPC_COLLABORATION_ROUTER_PORT": "50051",
"GRPC_REGISTRATION_URL": "localhost:99999",
"GRPC_REGISTRATION_SERVER_URL": "localhost:99999",
"LOGGING_LEVEL": "ERROR",
}
......@@ -66,8 +68,9 @@ func TestLoadConfig(t *testing.T) {
expectedConfig := Config{}
expectedConfig.Http.Server.Port = 9090
expectedConfig.Grpc.Collaboration.Server.Port = 50052
expectedConfig.Grpc.Collaboration.Router.Port = 50051
expectedConfig.Grpc.Registration.Url = "localhost:99999"
expectedConfig.Grpc.Registration.Server.Url = "localhost:99999"
expectedConfig.Grpc.Mock = false
expectedConfig.Logging.Level = "ERROR"
......@@ -77,7 +80,7 @@ func TestLoadConfig(t *testing.T) {
t.Run("should overwrite config with env", func(t *testing.T) {
envVars := map[string]string{
"HTTP_SERVER_PORT": "9090",
"GRPC_REGISTRATION_URL": "localhost:99999",
"GRPC_REGISTRATION_SERVER_URL": "localhost:99999",
}
for key, value := range envVars {
......@@ -92,8 +95,9 @@ func TestLoadConfig(t *testing.T) {
expectedConfig := Config{}
expectedConfig.Http.Server.Port = 9090
expectedConfig.Grpc.Collaboration.Server.Port = 50052
expectedConfig.Grpc.Collaboration.Router.Port = 50051
expectedConfig.Grpc.Registration.Url = "localhost:99999"
expectedConfig.Grpc.Registration.Server.Url = "localhost:99999"
expectedConfig.Grpc.Mock = false
expectedConfig.Logging.Level = "DEBUG"
......
......@@ -3,9 +3,12 @@ http:
port: 8080
grpc:
collaboration:
server:
port: 50052
router:
port: 50051
registration:
server:
url: "localhost:50052"
logging:
level: "DEBUG"
\ No newline at end of file
......@@ -31,7 +31,7 @@ import (
"testing"
)
func setUpLogger(t *testing.T, level string, msg string) *bytes.Buffer {
func logMessage(level string, msg string) *bytes.Buffer {
logger := GetLogger()
var buf bytes.Buffer
......@@ -57,25 +57,25 @@ func setUpLogger(t *testing.T, level string, msg string) *bytes.Buffer {
}
func TestError(t *testing.T) {
buf := setUpLogger(t, LogLevelError, "test error")
logOutput := buf.String()
assert.Contains(t, logOutput, "ERROR: test error")
buf := logMessage(LogLevelError, "test error")
assert.Contains(t, buf.String(), "ERROR: test error")
}
func TestWarning(t *testing.T) {
buf := setUpLogger(t, LogLevelWarning, "test warning")
logOutput := buf.String()
assert.Contains(t, logOutput, "WARNING: test warning")
buf := logMessage(LogLevelWarning, "test warning")
assert.Contains(t, buf.String(), "WARNING: test warning")
}
func TestInfo(t *testing.T) {
buf := setUpLogger(t, LogLevelInfo, "test info")
logOutput := buf.String()
assert.Contains(t, logOutput, "INFO: test info")
buf := logMessage(LogLevelInfo, "test info")
assert.Contains(t, buf.String(), "INFO: test info")
}
func TestDebug(t *testing.T) {
buf := setUpLogger(t, LogLevelDebug, "test debug")
logOutput := buf.String()
assert.Empty(t, logOutput)
buf := logMessage(LogLevelDebug, "test debug")
assert.Empty(t, buf.String())
}
......@@ -3,9 +3,12 @@ http:
port: 8080
grpc:
collaboration:
server:
port: 50052
router:
port: 50051
registration:
server:
url: "localhost:50052"
logging:
level: "INFO"
\ No newline at end of file
......@@ -3,9 +3,12 @@ http:
port: 8080
grpc:
collaboration:
server:
port: 50052
router:
port: 50051
registration:
server:
url: "localhost:50052"
logging:
level: "DEBUG"
\ No newline at end of file
package server
import "fachstellen-proxy/internal/logging"
import (
"fachstellen-proxy/internal/config"
"fachstellen-proxy/internal/logging"
)
var conf = config.LoadConfig()
var logger = logging.GetLogger()
......@@ -27,6 +27,8 @@ package server
import (
"context"
"encoding/base64"
"errors"
pb "fachstellen-proxy/gen/go"
"fachstellen-proxy/internal/config"
"fachstellen-proxy/internal/mock"
......@@ -36,8 +38,11 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"net"
"strings"
)
const AddressAttributeSeparator = "/"
type collaborationRouter struct {
pb.UnimplementedCollaborationServiceServer
}
......@@ -47,18 +52,25 @@ func (s *collaborationRouter) FindVorgang(ctx context.Context, in *pb.GrpcFindVo
return nil, status.Error(codes.InvalidArgument, "VorgangId is missing")
}
conn, err := grpc.NewClient(getCollaborationServerAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
collaborationInfo, decodeErr := DecodeCollaborationInfo(in.VorgangId)
if decodeErr != nil {
return nil, status.Error(codes.InvalidArgument, "VorgangId has invalid format, expected base64 encoded 'collaborationServerAddress/vorgangId'")
}
target := GetCollaborationServerUrl(collaborationInfo.CollaborationServerAddress, conf)
conn, err := grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
logger.Fatal("collaboration router failed to route: %v", err)
}
defer conn.Close()
client := pb.NewCollaborationServiceClient(conn)
updatedRequest := &pb.GrpcFindVorgangRequest{VorgangId: collaborationInfo.Attribute, SamlToken: in.SamlToken}
return client.FindVorgang(context.Background(), in)
return client.FindVorgang(context.Background(), updatedRequest)
}
func StartCollaborationRouter(conf config.Config) *grpc.Server {
func StartCollaborationRouter() *grpc.Server {
s := grpc.NewServer()
pb.RegisterCollaborationServiceServer(s, &collaborationRouter{})
......@@ -75,7 +87,33 @@ func StartCollaborationRouter(conf config.Config) *grpc.Server {
return s
}
func getCollaborationServerAddress() string {
// TODO: Nur wenn GRPC_MOCK == true, sonst Adresse aus VorgangId lesen
return fmt.Sprintf("localhost:%d", mock.GrpcMockPort)
type CollaborationInfo struct {
CollaborationServerAddress string
Attribute string
}
func DecodeCollaborationInfo(encodedCollaborationInfo string) (*CollaborationInfo, error) {
decodedBytes, err := base64.StdEncoding.DecodeString(encodedCollaborationInfo)
if err != nil {
return nil, errors.New("collaborationInfo has invalid base64 encoding")
}
parts := strings.SplitN(string(decodedBytes), AddressAttributeSeparator, 2)
if len(parts) != 2 {
return nil, errors.New("collaborationInfo has invalid format, expected 'collaborationServerAddress/attribute'")
}
return &CollaborationInfo{
CollaborationServerAddress: parts[0],
Attribute: parts[1],
}, nil
}
func GetCollaborationServerUrl(collaborationServerAddress string, c config.Config) string {
collaborationServerPort := c.Grpc.Collaboration.Server.Port
if c.Grpc.Mock {
collaborationServerPort = mock.GrpcMockPort
}
return fmt.Sprintf("%v:%d", collaborationServerAddress, collaborationServerPort)
}
......@@ -28,7 +28,9 @@ package server
import (
"context"
pb "fachstellen-proxy/gen/go"
"fachstellen-proxy/internal/config"
"fachstellen-proxy/internal/mock"
"fmt"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
......@@ -38,16 +40,6 @@ import (
)
func TestStartCollaborationRouter(t *testing.T) {
t.Run("should start collaboration router", func(t *testing.T) {
SetUpCollaborationRouter()
conn, err := net.DialTimeout("tcp", "localhost:50051", 2*time.Second)
assert.NoError(t, err)
conn.Close()
})
setUpGrpcRouterEnv := func() (pb.CollaborationServiceClient, func()) {
mock.SetUpGrpcServer()
SetUpCollaborationRouter()
......@@ -63,11 +55,21 @@ func TestStartCollaborationRouter(t *testing.T) {
return client, cleanup
}
t.Run("should start collaboration router", func(t *testing.T) {
SetUpCollaborationRouter()
conn, err := net.DialTimeout("tcp", "localhost:50051", 2*time.Second)
assert.NoError(t, err)
conn.Close()
})
t.Run("should have no error", func(t *testing.T) {
client, cleanUp := setUpGrpcRouterEnv()
defer cleanUp()
resp, err := client.FindVorgang(context.Background(), &pb.GrpcFindVorgangRequest{VorgangId: "testVorgangId", SamlToken: "testSamlToken"})
resp, err := client.FindVorgang(context.Background(), &pb.GrpcFindVorgangRequest{VorgangId: "bG9jYWxob3N0L3Rlc3RWb3JnYW5nSWQ=", SamlToken: "testSamlToken"})
assert.NoError(t, err)
assert.NotNil(t, resp)
......@@ -77,9 +79,10 @@ func TestStartCollaborationRouter(t *testing.T) {
client, cleanUp := setUpGrpcRouterEnv()
defer cleanUp()
resp, err := client.FindVorgang(context.Background(), &pb.GrpcFindVorgangRequest{VorgangId: "testVorgangId"})
resp, err := client.FindVorgang(context.Background(), &pb.GrpcFindVorgangRequest{VorgangId: "bG9jYWxob3N0L3Rlc3RWb3JnYW5nSWQ="})
assert.Error(t, err)
assert.Contains(t, err.Error(), "SamlToken is missing")
assert.Nil(t, resp)
})
......@@ -90,6 +93,66 @@ func TestStartCollaborationRouter(t *testing.T) {
resp, err := client.FindVorgang(context.Background(), &pb.GrpcFindVorgangRequest{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "VorgangId is missing")
assert.Nil(t, resp)
})
}
func TestDecodeCollaborationInfo(t *testing.T) {
t.Run("should have no error", func(t *testing.T) {
result, err := DecodeCollaborationInfo("bG9jYWxob3N0L3Rlc3RWb3JnYW5nSWQ=")
assert.NoError(t, err)
assert.NotNil(t, result)
})
t.Run("should have collaboration server dddress", func(t *testing.T) {
result, err := DecodeCollaborationInfo("bG9jYWxob3N0L3Rlc3RWb3JnYW5nSWQ=")
assert.NoError(t, err)
assert.Equal(t, "localhost", result.CollaborationServerAddress)
})
t.Run("should have attribute", func(t *testing.T) {
result, err := DecodeCollaborationInfo("bG9jYWxob3N0L3Rlc3RWb3JnYW5nSWQ=")
assert.NoError(t, err)
assert.Equal(t, "testVorgangId", result.Attribute)
})
t.Run("should have base64 error", func(t *testing.T) {
result, err := DecodeCollaborationInfo("invalidBase64String")
assert.Error(t, err)
assert.Equal(t, "collaborationInfo has invalid base64 encoding", err.Error())
assert.Nil(t, result)
})
t.Run("should have format error", func(t *testing.T) {
result, err := DecodeCollaborationInfo("bG9jYWxob3N0")
assert.Error(t, err)
assert.Equal(t, "collaborationInfo has invalid format, expected 'collaborationServerAddress/attribute'", err.Error())
assert.Nil(t, result)
})
}
func TestGetCollaborationServerUrl(t *testing.T) {
t.Run("should have configured server url", func(t *testing.T) {
c := config.Config{}
c.Grpc.Collaboration.Server.Port = 99999
result := GetCollaborationServerUrl("localhost", c)
assert.Equal(t, "localhost:99999", result)
})
t.Run("should have mock server url", func(t *testing.T) {
c := config.Config{}
c.Grpc.Mock = true
result := GetCollaborationServerUrl("localhost", c)
assert.Equal(t, fmt.Sprintf("localhost:%d", mock.GrpcMockPort), result)
})
}
......@@ -59,7 +59,7 @@ func TestRegisterCollaborationEndpoints(t *testing.T) {
SetUpHttpGateway()
SetUpCollaborationRouter()
resp, err := http.Get(fmt.Sprintf("http://localhost:8080%v/testVorgangId/testSamlToken", VorgangUrlBasePath))
resp, err := http.Get(fmt.Sprintf("http://localhost:8080%v/bG9jYWxob3N0L3Rlc3RWb3JnYW5nSWQ=/testSamlToken", VorgangUrlBasePath))
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
......
......@@ -34,21 +34,16 @@ import (
"net/http"
)
func StartHttpGateway(conf config.Config) *http.Server {
func StartHttpGateway() *http.Server {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux(runtime.WithErrorHandler(ErrorHandler))
collaborationRouterUrl := fmt.Sprintf("localhost:%d", conf.Grpc.Collaboration.Router.Port)
registrationServerUrl := conf.Grpc.Registration.Url
if conf.Grpc.Mock {
registrationServerUrl = fmt.Sprintf("localhost:%d", mock.GrpcMockPort)
}
RegisterHomeEndpoint(mux)
RegisterFachstelleRegistrationEndpoints(ctx, mux, registrationServerUrl)
RegisterFachstelleRegistrationEndpoints(ctx, mux, GetRegistrationServerUrl(conf))
RegisterCollaborationEndpoints(ctx, mux, collaborationRouterUrl)
httpServer := &http.Server{
......@@ -63,3 +58,11 @@ func StartHttpGateway(conf config.Config) *http.Server {
return httpServer
}
func GetRegistrationServerUrl(c config.Config) string {
if c.Grpc.Mock {
return fmt.Sprintf("localhost:%d", mock.GrpcMockPort)
}
return c.Grpc.Registration.Server.Url
}
......@@ -26,6 +26,9 @@
package server
import (
"fachstellen-proxy/internal/config"
"fachstellen-proxy/internal/mock"
"fmt"
"github.com/stretchr/testify/assert"
"net"
"testing"
......@@ -41,3 +44,23 @@ func TestStartHttpGateway(t *testing.T) {
conn.Close()
}
func TestGetRegistrationServerUrl(t *testing.T) {
t.Run("should have configured server url", func(t *testing.T) {
c := config.Config{}
c.Grpc.Registration.Server.Url = "localhost:99999"
result := GetRegistrationServerUrl(c)
assert.Equal(t, "localhost:99999", result)
})
t.Run("should have mock server url", func(t *testing.T) {
c := config.Config{}
c.Grpc.Mock = true
result := GetRegistrationServerUrl(c)
assert.Equal(t, fmt.Sprintf("localhost:%d", mock.GrpcMockPort), result)
})
}
......@@ -26,7 +26,6 @@
package server
import (
"fachstellen-proxy/internal/config"
"sync"
"time"
)
......@@ -36,18 +35,14 @@ var setUpCollaborationRouterOnce sync.Once
func SetUpHttpGateway() {
setUpHttpGatewayOnce.Do(func() {
conf := config.LoadConfig()
go StartHttpGateway(conf)
go StartHttpGateway()
time.Sleep(time.Second) // Wait for the server to start
})
}
func SetUpCollaborationRouter() {
conf := config.LoadConfig()
setUpCollaborationRouterOnce.Do(func() {
go StartCollaborationRouter(conf)
go StartCollaborationRouter()
time.Sleep(time.Second) // Wait for the router to start
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment