Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core-service] Rename enable_http to allow_http_base_urls #1061

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/dev/haproxy_local_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ docker run -d --name core-service-for-testing -p 8082:8082 \
-dump_requests \
-accepted_jwt_audiences core-service,localhost \
-enable_scd \
-enable_http
-allow_http_base_urls

echo " -------------- DUMMY OAUTH -------------- "
echo "Building dummy-oauth server container"
Expand Down
5 changes: 2 additions & 3 deletions build/dev/startup/core_service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if [ "$DEBUG_ON" = "1" ]; then
-addr :8082 \
-accepted_jwt_audiences localhost,host.docker.internal,local-dss-core-service,dss_sandbox-local-dss-core-service-1,core-service \
-enable_scd \
-enable_http
-allow_http_base_urls
else
echo "Debug Mode: off"

Expand All @@ -30,6 +30,5 @@ else
-addr :8082 \
-accepted_jwt_audiences localhost,host.docker.internal,local-dss-core-service,dss_sandbox-local-dss-core-service-1,core-service \
-enable_scd \
-enable_http
-allow_http_base_urls
fi

2 changes: 1 addition & 1 deletion cmds/core-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ go run ./cmds/core-service \
-dump_requests \
-accepted_jwt_audiences localhost \
-enable_scd \
-enable_http
-allow_http_base_urls
```

### Prerequisites
Expand Down
50 changes: 31 additions & 19 deletions cmds/core-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ import (
)

var (
address = flag.String("addr", ":8080", "Local address that the service binds to and listens on for incoming connections")
enableSCD = flag.Bool("enable_scd", false, "Enables the Strategic Conflict Detection API")
enableHTTP = flag.Bool("enable_http", false, "Enables http scheme for Strategic Conflict Detection API")
timeout = flag.Duration("server timeout", 10*time.Second, "Default timeout for server calls")
locality = flag.String("locality", "", "self-identification string used as CRDB table writer column")
address = flag.String("addr", ":8080", "Local address that the service binds to and listens on for incoming connections")
enableSCD = flag.Bool("enable_scd", false, "Enables the Strategic Conflict Detection API")
allowHTTPBaseUrls = flag.Bool("allow_http_base_urls", false, "Enables http scheme for Strategic Conflict Detection API")
enableHTTP = flag.Bool("enable_http", false, "DEPRECATED (replaced by allow_http_base_urls): Enables http scheme for Strategic Conflict Detection API")
timeout = flag.Duration("server timeout", 10*time.Second, "Default timeout for server calls")
locality = flag.String("locality", "", "self-identification string used as CRDB table writer column")

logFormat = flag.String("log_format", logging.DefaultFormat, "The log format in {json, console}")
logLevel = flag.String("log_level", logging.DefaultLevel.String(), "The log level")
Expand Down Expand Up @@ -158,17 +159,17 @@ func createRIDServers(ctx context.Context, locality string, logger *zap.Logger)

app := application.NewFromTransactor(ridStore, logger)
return &rid_v1.Server{
App: app,
Timeout: *timeout,
Locality: locality,
EnableHTTP: *enableHTTP,
Cron: ridCron,
App: app,
Timeout: *timeout,
Locality: locality,
AllowHTTPBaseUrls: *allowHTTPBaseUrls,
Cron: ridCron,
}, &rid_v2.Server{
App: app,
Timeout: *timeout,
Locality: locality,
EnableHTTP: *enableHTTP,
Cron: ridCron,
App: app,
Timeout: *timeout,
Locality: locality,
AllowHTTPBaseUrls: *allowHTTPBaseUrls,
Cron: ridCron,
}, nil
}

Expand Down Expand Up @@ -200,10 +201,10 @@ func createSCDServer(ctx context.Context, logger *zap.Logger) (*scd.Server, erro
scdCron.Start()

return &scd.Server{
Store: scdStore,
DSSReportHandler: &scd.JSONLoggingReceivedReportHandler{ReportLogger: logger},
Timeout: *timeout,
EnableHTTP: *enableHTTP,
Store: scdStore,
DSSReportHandler: &scd.JSONLoggingReceivedReportHandler{ReportLogger: logger},
Timeout: *timeout,
AllowHTTPBaseUrls: *allowHTTPBaseUrls,
}, nil
}

Expand Down Expand Up @@ -361,6 +362,15 @@ func (gcj RIDGarbageCollectorJob) Run() {
}
}

func SetDeprecatingHttpFlag(logger *zap.Logger, newFlag **bool, deprecatedFlag **bool) {
if **deprecatedFlag {
logger.Warn("DEPRECATED: enable_http has been renamed to allow_http_base_urls.")
if !**newFlag {
*newFlag = *deprecatedFlag
}
}
}

func main() {
flag.Parse()
if err := logging.Configure(*logLevel, *logFormat); err != nil {
Expand All @@ -373,6 +383,8 @@ func main() {
)
defer cancel()

SetDeprecatingHttpFlag(logger, &allowHTTPBaseUrls, &enableHTTP)

if *profServiceName != "" {
if err := profiler.Start(profiler.Config{Service: *profServiceName}); err != nil {
logger.Panic("Failed to start the profiler ", zap.Error(err))
Expand Down
72 changes: 72 additions & 0 deletions cmds/core-service/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
)

func NewObserver() (*zap.Logger, *observer.ObservedLogs) {
core, logs := observer.New(zap.InfoLevel)
return zap.New(core), logs
}

func RequireNoLogging(t *testing.T, logs *observer.ObservedLogs) {
require.Equal(t, len(logs.All()), 0)
}

func RequireDeprecationWarning(t *testing.T, logs *observer.ObservedLogs) {
entry := logs.All()[0]
require.Equal(t, entry.Level, zap.WarnLevel)
require.Equal(t, entry.Message, "DEPRECATED: enable_http has been renamed to allow_http_base_urls.")
}

func TestHttpDeprecationDoesNothingWhenBothFlagsAreOff(t *testing.T) {
logger, logs := NewObserver()
newFlag := new(bool)
oldFlag := new(bool)
SetDeprecatingHttpFlag(logger, &newFlag, &oldFlag)
RequireNoLogging(t, logs)
require.False(t, newFlag == oldFlag)
require.False(t, *newFlag)
require.False(t, *oldFlag)
}

func TestHttpDeprecationDoesNothingWhenNewFlagIsOn(t *testing.T) {
logger, logs := NewObserver()
newFlag := new(bool)
oldFlag := new(bool)
*newFlag = true
SetDeprecatingHttpFlag(logger, &newFlag, &oldFlag)
RequireNoLogging(t, logs)
require.False(t, newFlag == oldFlag)
require.True(t, *newFlag)
require.False(t, *oldFlag)
}

func TestHttpDeprecationReassignsAddressWhenOldFlagIsOn(t *testing.T) {
logger, logs := NewObserver()
newFlag := new(bool)
oldFlag := new(bool)
*oldFlag = true
SetDeprecatingHttpFlag(logger, &newFlag, &oldFlag)
RequireDeprecationWarning(t, logs)
require.True(t, newFlag == oldFlag)
require.True(t, *newFlag)
require.True(t, *oldFlag)
}

func TestHttpDeprecationPrefersNewFlagWhenBothAreOn(t *testing.T) {
logger, logs := NewObserver()
newFlag := new(bool)
oldFlag := new(bool)
*newFlag = true
*oldFlag = true
SetDeprecatingHttpFlag(logger, &newFlag, &oldFlag)
RequireDeprecationWarning(t, logs)
require.False(t, newFlag == oldFlag)
require.True(t, *newFlag)
require.True(t, *oldFlag)
}
2 changes: 1 addition & 1 deletion pkg/rid/server/v1/isa_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (s *Server) CreateIdentificationServiceArea(ctx context.Context, req *resta
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format"))}}
}

if !s.EnableHTTP {
if !s.AllowHTTPBaseUrls {
err = ridmodels.ValidateURL(string(req.Body.FlightsUrl))
if err != nil {
return restapi.CreateIdentificationServiceAreaResponseSet{Response400: &restapi.ErrorResponse{
Expand Down
10 changes: 5 additions & 5 deletions pkg/rid/server/v1/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

// Server implements ridv1.Implementation.
type Server struct {
App application.App
Timeout time.Duration
Locality string
EnableHTTP bool
Cron *cron.Cron
App application.App
Timeout time.Duration
Locality string
AllowHTTPBaseUrls bool
Cron *cron.Cron
}

func setAuthError(ctx context.Context, authErr error, resp401, resp403 **restapi.ErrorResponse, resp500 **api.InternalServerErrorBody) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rid/server/v1/subscription_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (s *Server) CreateSubscription(ctx context.Context, req *restapi.CreateSubs
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format"))}}
}

if !s.EnableHTTP {
if !s.AllowHTTPBaseUrls {
err = ridmodels.ValidateURL(string(*req.Body.Callbacks.IdentificationServiceAreaUrl))
if err != nil {
return restapi.CreateSubscriptionResponseSet{Response400: &restapi.ErrorResponse{
Expand Down
2 changes: 1 addition & 1 deletion pkg/rid/server/v2/isa_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *Server) CreateIdentificationServiceArea(ctx context.Context, req *resta
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format"))}}
}

if !s.EnableHTTP {
if !s.AllowHTTPBaseUrls {
err = ridmodels.ValidateURL(string(req.Body.UssBaseUrl))
if err != nil {
return restapi.CreateIdentificationServiceAreaResponseSet{Response400: &restapi.ErrorResponse{
Expand Down
10 changes: 5 additions & 5 deletions pkg/rid/server/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

// Server implements ridv2.Implementation.
type Server struct {
App application.App
Timeout time.Duration
Locality string
EnableHTTP bool
Cron *cron.Cron
App application.App
Timeout time.Duration
Locality string
AllowHTTPBaseUrls bool
Cron *cron.Cron
}

func setAuthError(ctx context.Context, authErr error, resp401, resp403 **restapi.ErrorResponse, resp500 **api.InternalServerErrorBody) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/rid/server/v2/subscription_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (s *Server) CreateSubscription(ctx context.Context, req *restapi.CreateSubs
Message: dsserr.Handle(ctx, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format"))}}
}

if !s.EnableHTTP {
if !s.AllowHTTPBaseUrls {
err = ridmodels.ValidateURL(string(req.Body.UssBaseUrl))
if err != nil {
return restapi.CreateSubscriptionResponseSet{Response400: &restapi.ErrorResponse{
Expand Down
2 changes: 1 addition & 1 deletion pkg/scd/constraints_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (a *Server) PutConstraintReference(ctx context.Context, manager string, ent
return nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Missing required UssBaseUrl")
}

if !a.EnableHTTP {
if !a.AllowHTTPBaseUrls {
err = scdmodels.ValidateUSSBaseURL(string(params.UssBaseUrl))
if err != nil {
return nil, stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Failed to validate base URL")
Expand Down
4 changes: 2 additions & 2 deletions pkg/scd/operational_intents_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (a *Server) upsertOperationalIntentReference(ctx context.Context, authorize
return nil, nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Missing required UssBaseUrl")
}

if !a.EnableHTTP {
if !a.AllowHTTPBaseUrls {
err = scdmodels.ValidateUSSBaseURL(string(params.UssBaseUrl))
if err != nil {
return nil, nil, stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Failed to validate base URL")
Expand Down Expand Up @@ -483,7 +483,7 @@ func (a *Server) upsertOperationalIntentReference(ctx context.Context, authorize
// an error will have been returned earlier.
// If they are not set at this point, continue without creating an implicit subscription.
if params.NewSubscription != nil && params.NewSubscription.UssBaseUrl != "" {
if !a.EnableHTTP {
if !a.AllowHTTPBaseUrls {
err := scdmodels.ValidateUSSBaseURL(string(params.NewSubscription.UssBaseUrl))
if err != nil {
return stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Failed to validate USS base URL")
Expand Down
8 changes: 4 additions & 4 deletions pkg/scd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func makeSubscribersToNotify(subscriptions []*scdmodels.Subscription) []restapi.

// Server implements scdv1.Implementation.
type Server struct {
Store scdstore.Store
DSSReportHandler ReceivedReportHandler
Timeout time.Duration
EnableHTTP bool
Store scdstore.Store
DSSReportHandler ReceivedReportHandler
Timeout time.Duration
AllowHTTPBaseUrls bool
}

func setAuthError(ctx context.Context, authErr error, resp401, resp403 **restapi.ErrorResponse, resp500 **api.InternalServerErrorBody) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/scd/subscriptions_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (a *Server) PutSubscription(ctx context.Context, manager string, subscripti
return nil, stacktrace.NewErrorWithCode(dsserr.BadRequest, "Invalid ID format: `%s`", subscriptionid)
}

if !a.EnableHTTP {
if !a.AllowHTTPBaseUrls {
err = scdmodels.ValidateUSSBaseURL(string(params.UssBaseUrl))
if err != nil {
return nil, stacktrace.PropagateWithCode(err, dsserr.BadRequest, "Failed to validate base URL")
Expand Down
2 changes: 1 addition & 1 deletion test/migrations/rid_db_post_migration_e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ docker run -d --name core-service-for-testing \
-dump_requests \
-accepted_jwt_audiences core-service \
-enable_scd \
-enable_http
-allow_http_base_urls

echo " -------------- DUMMY OAUTH -------------- "
echo "Building dummy-oauth server container"
Expand Down
Loading