Skip to content

Commit

Permalink
feat: Add CORS policy to REST API server (#1924)
Browse files Browse the repository at this point in the history
## Description:
Add CORS policy to REST API server.

## Is this change user facing?
NO
  • Loading branch information
lostbean authored Dec 12, 2023
1 parent 30ddc1e commit a934b1e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
9 changes: 9 additions & 0 deletions cli/cli/helpers/engine_manager/engine_existence_guarantor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine_manager
import (
"context"
"fmt"

"github.com/Masterminds/semver/v3"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/lib/kurtosis_context"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
Expand Down Expand Up @@ -71,6 +72,8 @@ type engineExistenceGuarantor struct {
poolSize uint8

enclaveEnvVars string

allowedCORSOrigins *[]string
}

func newEngineExistenceGuarantorWithDefaultVersion(
Expand All @@ -85,6 +88,7 @@ func newEngineExistenceGuarantorWithDefaultVersion(
onBastionHost bool,
poolSize uint8,
enclaveEnvVars string,
allowedCORSOrigins *[]string,
) *engineExistenceGuarantor {
return newEngineExistenceGuarantorWithCustomVersion(
ctx,
Expand All @@ -99,6 +103,7 @@ func newEngineExistenceGuarantorWithDefaultVersion(
onBastionHost,
poolSize,
enclaveEnvVars,
allowedCORSOrigins,
)
}

Expand All @@ -115,6 +120,7 @@ func newEngineExistenceGuarantorWithCustomVersion(
onBastionHost bool,
poolSize uint8,
enclaveEnvVars string,
allowedCORSOrigins *[]string,
) *engineExistenceGuarantor {
return &engineExistenceGuarantor{
ctx: ctx,
Expand All @@ -131,6 +137,7 @@ func newEngineExistenceGuarantorWithCustomVersion(
onBastionHost: onBastionHost,
poolSize: poolSize,
enclaveEnvVars: enclaveEnvVars,
allowedCORSOrigins: allowedCORSOrigins,
}
}

Expand Down Expand Up @@ -165,6 +172,7 @@ func (guarantor *engineExistenceGuarantor) VisitStopped() error {
metrics_client.IsCI(),
maybeCloudUserId,
maybeCloudInstanceId,
guarantor.allowedCORSOrigins,
)
} else {
_, _, engineLaunchErr = guarantor.engineServerLauncher.LaunchWithCustomVersion(
Expand All @@ -181,6 +189,7 @@ func (guarantor *engineExistenceGuarantor) VisitStopped() error {
metrics_client.IsCI(),
maybeCloudUserId,
maybeCloudInstanceId,
guarantor.allowedCORSOrigins,
)
}
if engineLaunchErr != nil {
Expand Down
10 changes: 7 additions & 3 deletions cli/cli/helpers/engine_manager/engine_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type EngineManager struct {
clusterConfig *resolved_config.KurtosisClusterConfig
onBastionHost bool
enclaveEnvVars string
allowedCORSOrigins *[]string
// Make engine IP, port, and protocol configurable in the future
}

Expand Down Expand Up @@ -98,9 +99,10 @@ func NewEngineManager(ctx context.Context) (*EngineManager, error) {
kurtosisBackend: kurtosisBackend,
shouldSendMetrics: kurtosisConfig.GetShouldSendMetrics(),
engineServerKurtosisBackendConfigSupplier: engineBackendConfigSupplier,
clusterConfig: clusterConfig,
onBastionHost: onBastionHost,
enclaveEnvVars: enclaveEnvVars,
clusterConfig: clusterConfig,
onBastionHost: onBastionHost,
enclaveEnvVars: enclaveEnvVars,
allowedCORSOrigins: nil,
}, nil
}

Expand Down Expand Up @@ -191,6 +193,7 @@ func (manager *EngineManager) StartEngineIdempotentlyWithDefaultVersion(ctx cont
manager.onBastionHost,
poolSize,
manager.enclaveEnvVars,
manager.allowedCORSOrigins,
)
// TODO Need to handle the Kubernetes case, where a gateway needs to be started after the engine is started but
// before we can return an EngineClient
Expand Down Expand Up @@ -222,6 +225,7 @@ func (manager *EngineManager) StartEngineIdempotentlyWithCustomVersion(ctx conte
manager.onBastionHost,
poolSize,
manager.enclaveEnvVars,
manager.allowedCORSOrigins,
)
engineClient, engineClientCloseFunc, err := manager.startEngineWithGuarantor(ctx, status, engineGuarantor)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion engine/launcher/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package args

import (
"encoding/json"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"
"reflect"
"strings"

"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"

"github.com/kurtosis-tech/kurtosis/engine/launcher/args/kurtosis_backend_config"
"github.com/kurtosis-tech/stacktrace"
)
Expand Down Expand Up @@ -57,6 +58,9 @@ type EngineServerArgs struct {

// The Cloud Instance ID of the current user if available
CloudInstanceID metrics_client.CloudInstanceID `json:"cloud_instance_id"`

// List of allowed origins to validate CORS requests on the REST API. If undefined, defaults to '*' (any origin).
AllowedCORSOrigins *[]string `json:"allowed_cors_origins,omitempty"`
}

var skipValidation = map[string]bool{
Expand Down Expand Up @@ -111,6 +115,7 @@ func NewEngineServerArgs(
isCI bool,
cloudUserID metrics_client.CloudUserID,
cloudInstanceID metrics_client.CloudInstanceID,
allowedCORSOrigins *[]string,
) (*EngineServerArgs, error) {
if enclaveEnvVars == "" {
enclaveEnvVars = emptyJsonField
Expand All @@ -129,6 +134,7 @@ func NewEngineServerArgs(
IsCI: isCI,
CloudUserID: cloudUserID,
CloudInstanceID: cloudInstanceID,
AllowedCORSOrigins: allowedCORSOrigins,
}
if err := result.validate(); err != nil {
return nil, stacktrace.Propagate(err, "An error occurred validating engine server args")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ package engine_server_launcher

import (
"context"
"net"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/port_spec"
"github.com/kurtosis-tech/kurtosis/engine/launcher/args"
"github.com/kurtosis-tech/kurtosis/kurtosis_version"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"net"
)

const (
Expand Down Expand Up @@ -43,6 +44,7 @@ func (launcher *EngineServerLauncher) LaunchWithDefaultVersion(
isCI bool,
cloudUserID metrics_client.CloudUserID,
cloudInstanceID metrics_client.CloudInstanceID,
allowedCORSOrigins *[]string,
) (
resultPublicIpAddr net.IP,
resultPublicGrpcPortSpec *port_spec.PortSpec,
Expand All @@ -62,6 +64,7 @@ func (launcher *EngineServerLauncher) LaunchWithDefaultVersion(
isCI,
cloudUserID,
cloudInstanceID,
allowedCORSOrigins,
)
if err != nil {
return nil, nil, stacktrace.Propagate(err, "An error occurred launching the engine server container with default version tag '%v'", kurtosis_version.KurtosisVersion)
Expand All @@ -83,6 +86,7 @@ func (launcher *EngineServerLauncher) LaunchWithCustomVersion(
isCI bool,
cloudUserID metrics_client.CloudUserID,
cloudInstanceID metrics_client.CloudInstanceID,
allowedCORSOrigins *[]string,
) (
resultPublicIpAddr net.IP,
resultPublicGrpcPortSpec *port_spec.PortSpec,
Expand All @@ -103,6 +107,7 @@ func (launcher *EngineServerLauncher) LaunchWithCustomVersion(
isCI,
cloudUserID,
cloudInstanceID,
allowedCORSOrigins,
)
if err != nil {
return nil, nil, stacktrace.Propagate(err, "An error occurred creating the engine server args")
Expand Down
6 changes: 5 additions & 1 deletion engine/server/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/kurtosis-tech/kurtosis/engine/server/engine/server"
restApi "github.com/kurtosis-tech/kurtosis/engine/server/engine/server"
"github.com/kurtosis-tech/kurtosis/engine/server/engine/streaming"
"github.com/kurtosis-tech/kurtosis/engine/server/engine/utils"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/analytics_logger"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/source"
Expand Down Expand Up @@ -408,9 +409,12 @@ func restApiServer(
echoRouter.Use(echomiddleware.Logger())

// Setup CORS policies for the REST API server
allowOrigins := utils.DerefWith(serverArgs.AllowedCORSOrigins, defaultCORSOrigins)
logrus.Infof("Setting-up CORS policy to accept requests from origins: %v", allowOrigins)

// nolint:exhaustruct
echoRouter.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: defaultCORSOrigins,
AllowOrigins: allowOrigins,
AllowHeaders: defaultCORSHeaders,
}))

Expand Down

0 comments on commit a934b1e

Please sign in to comment.