Skip to content

Commit

Permalink
Merge branch 'main' of ../private
Browse files Browse the repository at this point in the history
  • Loading branch information
lunar-devops committed Dec 4, 2024
2 parents 6c2cce6 + fe725eb commit bf1441b
Show file tree
Hide file tree
Showing 56 changed files with 602 additions and 540 deletions.
1 change: 1 addition & 0 deletions proxy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ ENV LUNAR_SERVER_TIMEOUT_SEC 15
ENV LUNAR_SPOE_HELLO_TIMEOUT_MS 1200
ENV LUNAR_SPOE_IDLE_TIMEOUT_SEC 30
ENV LUNAR_SPOE_PROCESSING_TIMEOUT_SEC 30
ENV LUNAR_SPOE_SERVER_TIMEOUT_SEC 60

# Diagnosis Failsafe
ENV DIAGNOSIS_FAILSAFE_MIN_SEC_BETWEEN_CALLS 1
Expand Down
2 changes: 1 addition & 1 deletion proxy/rootfs/etc/haproxy/haproxy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ backend unmanage_global
backend lunar
mode tcp
timeout connect 20s # greater than hello timeout
timeout server 3m # greater than idle timeout
timeout server "$LUNAR_SPOE_SERVER_TIMEOUT_SEC"s # greater than idle timeout
option spop-check
server agent localhost:12345

Expand Down
48 changes: 48 additions & 0 deletions proxy/src/libs/toolkit-core/network/listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package network

import (
"context"
"net"
"time"

"github.com/rs/zerolog/log"
)

type TimeoutListener struct {
net.Listener
deadlineTimeout time.Duration
}

func (tl *TimeoutListener) Accept() (net.Conn, error) {
conn, err := tl.Listener.Accept()
if err != nil {
return nil, err
}
if err = conn.SetDeadline(time.Now().Add(tl.deadlineTimeout)); err != nil {
return nil, err
}

return conn, nil
}

func NewTimeoutListener(
network, address string,
timeout time.Duration,
) (net.Listener, error) {
lc := net.ListenConfig{}
listener, err := lc.Listen(context.Background(), network, address)
if err != nil {
return nil, err
}
return &TimeoutListener{
Listener: listener,
deadlineTimeout: timeout,
}, nil
}

func CloseListener(l net.Listener) {
err := l.Close()
if err != nil {
log.Error().Err(err).Msg("Failed to close listener")
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package actions

import (
"lunar/engine/messages"
lunarMessages "lunar/engine/messages"
sharedActions "lunar/shared-model/actions"

spoe "github.com/TheLunarCompany/haproxy-spoe-go"
"github.com/negasus/haproxy-spoe-go/action"
)

const ResponseHeadersActionName = "response_headers"

// NoOpAction

func (*NoOpAction) ReqToSpoeActions() []spoe.Action {
return []spoe.Action{}
func (*NoOpAction) ReqToSpoeActions() action.Actions {
return action.Actions{}
}

func (*NoOpAction) RespToSpoeActions() []spoe.Action {
return []spoe.Action{}
func (*NoOpAction) RespToSpoeActions() action.Actions {
return action.Actions{}
}

func (*NoOpAction) ReqRunResult() sharedActions.RemedyReqRunResult {
Expand All @@ -27,8 +27,8 @@ func (*NoOpAction) RespRunResult() sharedActions.RemedyRespRunResult {
return sharedActions.RespNoOp
}

func (*NoOpAction) EnsureRequestIsUpdated(_ *messages.OnRequest) {
func (*NoOpAction) EnsureRequestIsUpdated(_ *lunarMessages.OnRequest) {
}

func (*NoOpAction) EnsureResponseIsUpdated(_ *messages.OnResponse) {
func (*NoOpAction) EnsureResponseIsUpdated(_ *lunarMessages.OnResponse) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import (
"fmt"
"testing"

spoe "github.com/TheLunarCompany/haproxy-spoe-go"
"github.com/negasus/haproxy-spoe-go/action"
lo "github.com/samber/lo"
"github.com/stretchr/testify/assert"
)

func TestNoOpActionTransformer(t *testing.T) {
t.Parallel()
action := NoOpAction{}
res := action.ReqToSpoeActions()
want := []spoe.Action{}
currentAction := NoOpAction{}
res := currentAction.ReqToSpoeActions()
want := action.Actions{}

assert.Equal(t, res, want)
}

func getSetVarActionByName(
allActions []spoe.Action,
allActions action.Actions,
name string,
) (spoe.ActionSetVar, error) {
) (action.Action, error) {
var err error

isRequestedHeader := func(action spoe.Action, _ int) bool {
setVarAction, _ := action.(spoe.ActionSetVar)
isRequestedHeader := func(action action.Action, _ int) bool {
setVarAction := action
return setVarAction.Name == name
}

Expand All @@ -41,7 +41,7 @@ func getSetVarActionByName(
}

action := relevantActions[0]
setVarAction, _ := action.(spoe.ActionSetVar)
setVarAction := action

return setVarAction, err
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package actions

import (
"lunar/engine/messages"
lunarMessages "lunar/engine/messages"
"lunar/engine/utils"
sharedActions "lunar/shared-model/actions"
"strings"

spoe "github.com/TheLunarCompany/haproxy-spoe-go"
"github.com/negasus/haproxy-spoe-go/action"
)

const (
Expand All @@ -23,107 +23,70 @@ const (
)

// EarlyResponseAction
func (action *EarlyResponseAction) ReqToSpoeActions() []spoe.Action {
actions := []spoe.Action{
spoe.ActionSetVar{
Name: ReturnEarlyResponseActionName,
Scope: spoe.VarScopeTransaction,
Value: true,
},
spoe.ActionSetVar{
Name: StatusCodeActionName,
Scope: spoe.VarScopeTransaction,
Value: action.Status,
},
spoe.ActionSetVar{
Name: ResponseBodyActionName,
Scope: spoe.VarScopeTransaction,
Value: []byte(action.Body),
},
spoe.ActionSetVar{
Name: ResponseHeadersActionName,
Scope: spoe.VarScopeTransaction,
Value: utils.DumpHeaders(action.Headers),
},
}

func (a *EarlyResponseAction) ReqToSpoeActions() action.Actions {
actions := action.Actions{}
actions.SetVar(action.ScopeTransaction, ReturnEarlyResponseActionName, true)
actions.SetVar(action.ScopeTransaction, StatusCodeActionName, a.Status)
actions.SetVar(action.ScopeTransaction, ResponseBodyActionName, []byte(a.Body))
actions.SetVar(action.ScopeTransaction, ResponseHeadersActionName, utils.DumpHeaders(a.Headers))
return actions
}

func (*EarlyResponseAction) ReqRunResult() sharedActions.RemedyReqRunResult {
return sharedActions.ReqObtainedResponse
}

func (*EarlyResponseAction) EnsureRequestIsUpdated(_ *messages.OnRequest) {
func (*EarlyResponseAction) EnsureRequestIsUpdated(_ *lunarMessages.OnRequest) {
}

// ModifyRequestAction
func (action *ModifyRequestAction) ReqToSpoeActions() []spoe.Action {
actions := []spoe.Action{
spoe.ActionSetVar{
Name: ModifyRequestActionName,
Scope: spoe.VarScopeRequest,
Value: true,
},
spoe.ActionSetVar{
Name: RequestHeadersActionName,
Scope: spoe.VarScopeRequest,
Value: utils.DumpHeaders(action.HeadersToSet),
},
}
func (lunarAction *ModifyRequestAction) ReqToSpoeActions() action.Actions {
actions := action.Actions{}
actions.SetVar(action.ScopeRequest, ModifyRequestActionName, true)
actions.SetVar(action.ScopeRequest,
RequestHeadersActionName, utils.DumpHeaders(lunarAction.HeadersToSet))
return actions
}

func (action *ModifyRequestAction) ReqRunResult() sharedActions.RemedyReqRunResult {
func (lunarAction *ModifyRequestAction) ReqRunResult() sharedActions.RemedyReqRunResult {
return sharedActions.ReqModifiedRequest
}

func (action *ModifyRequestAction) EnsureRequestIsUpdated(
onRequest *messages.OnRequest,
func (lunarAction *ModifyRequestAction) EnsureRequestIsUpdated(
onRequest *lunarMessages.OnRequest,
) {
for name, value := range action.HeadersToSet {
for name, value := range lunarAction.HeadersToSet {
onRequest.Headers[name] = value
}
}

func (action *GenerateRequestAction) ReqToSpoeActions() []spoe.Action {
actions := []spoe.Action{
spoe.ActionSetVar{
Name: GenerateRequestActionName,
Scope: spoe.VarScopeRequest,
Value: true,
},
spoe.ActionSetVar{
Name: RequestHeadersActionName,
Scope: spoe.VarScopeRequest,
Value: utils.DumpHeaders(action.HeadersToSet),
},
spoe.ActionSetVar{
Name: RequestBodyActionName,
Scope: spoe.VarScopeRequest,
Value: []byte(action.Body),
},
}
func (lunarAction *GenerateRequestAction) ReqToSpoeActions() action.Actions {
actions := action.Actions{}
actions.SetVar(action.ScopeRequest, GenerateRequestActionName, true)
actions.SetVar(action.ScopeRequest,
RequestHeadersActionName, utils.DumpHeaders(lunarAction.HeadersToSet))
actions.SetVar(action.ScopeRequest,
RequestBodyActionName, []byte(lunarAction.Body))
return actions
}

func (action *GenerateRequestAction) ReqRunResult() sharedActions.RemedyReqRunResult {
func (lunarAction *GenerateRequestAction) ReqRunResult() sharedActions.RemedyReqRunResult {
return sharedActions.ReqGenerateRequest
}

func (action *GenerateRequestAction) EnsureRequestIsUpdated(
onRequest *messages.OnRequest,
func (lunarAction *GenerateRequestAction) EnsureRequestIsUpdated(
onRequest *lunarMessages.OnRequest,
) {
for name, value := range onRequest.Headers {
delete(onRequest.Headers, name)
onRequest.Headers[strings.ToLower(name)] = value
}

for name, value := range action.HeadersToSet {
for name, value := range lunarAction.HeadersToSet {
onRequest.Headers[strings.ToLower(name)] = value
}

for _, value := range action.HeadersToRemove {
for _, value := range lunarAction.HeadersToRemove {
delete(onRequest.Headers, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ import (
"strings"
"testing"

spoe "github.com/TheLunarCompany/haproxy-spoe-go"
"github.com/negasus/haproxy-spoe-go/action"
lo "github.com/samber/lo"
"github.com/stretchr/testify/assert"
)

func TestEarlyResponseActionTransformerOnlySetsTransactionVars(t *testing.T) {
t.Parallel()
action := EarlyResponseAction{
lunarAction := EarlyResponseAction{
Status: 429,
Body: "hello",
Headers: map[string]string{"Auth": "ABC123"},
}
allActions := action.ReqToSpoeActions()
allActions := lunarAction.ReqToSpoeActions()

getScope := func(action spoe.Action, _ int) byte {
setVarAction, _ := action.(spoe.ActionSetVar)
getScope := func(spoeAction action.Action, _ int) byte {
setVarAction := spoeAction
return byte(setVarAction.Scope)
}

assert.Condition(
t,
testutils.SliceAllEquals(
lo.Map(allActions, getScope),
byte(spoe.VarScopeTransaction),
byte(action.ScopeTransaction),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package actions

import (
"lunar/engine/messages"
lunarMessages "lunar/engine/messages"
sharedActions "lunar/shared-model/actions"

spoe "github.com/TheLunarCompany/haproxy-spoe-go"
"github.com/negasus/haproxy-spoe-go/action"
)

type ReqLunarAction interface {
ReqToSpoeActions() []spoe.Action
ReqToSpoeActions() action.Actions
ReqRunResult() sharedActions.RemedyReqRunResult
ReqPrioritize(ReqLunarAction) ReqLunarAction
EnsureRequestIsUpdated(onRequest *messages.OnRequest)
EnsureRequestIsUpdated(onRequest *lunarMessages.OnRequest)
IsEarlyReturnType() bool
}

Expand Down
Loading

0 comments on commit bf1441b

Please sign in to comment.