Skip to content

Commit 826ab20

Browse files
authored
feat: added upstream service name instead of cerberus to X-Service-Name header (#57)
1 parent 7344074 commit 826ab20

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

pkg/auth/authenticator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,15 @@ func setupUpstreamAuthRequest(upstreamHttpAuth *v1alpha1.UpstreamHttpAuthService
311311
if err != nil {
312312
return nil, err
313313
}
314+
315+
svcName := getServiceName()
316+
if ctxServiceName := request.Context["serviceName"]; ctxServiceName != "" {
317+
svcName = ctxServiceName
318+
}
319+
314320
req.Header = http.Header{
315321
upstreamHttpAuth.WriteTokenTo: {token},
316-
"X-Service-Name": {getServiceName()},
322+
"X-Service-Name": {svcName},
317323
"Content-Type": {"application/json"},
318324
}
319325
return req, nil

pkg/auth/authenticator_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,30 @@ func TestSetupUpstreamAuthRequest(t *testing.T) {
981981
assert.Error(t, actualErr, "Error should occur when service is empty")
982982
}
983983

984+
func TestSetupUpstreamAuthRequest_ServiceName_Context(t *testing.T) {
985+
upstreamAuth := &cerberusv1alpha1.UpstreamHttpAuthService{
986+
ReadTokenFrom: "X-Token-Read",
987+
WriteTokenTo: "X-Token-Write",
988+
Address: "http://example.com",
989+
Timeout: 1000,
990+
}
991+
992+
request := &Request{
993+
Context: map[string]string{
994+
"serviceName": "test-service",
995+
},
996+
Request: http.Request{
997+
Header: http.Header{
998+
"X-Token-Read": {"value"},
999+
},
1000+
},
1001+
}
1002+
1003+
httpReq, err := setupUpstreamAuthRequest(upstreamAuth, request)
1004+
assert.NoError(t, err)
1005+
assert.Equal(t, "test-service", httpReq.Header.Get("X-Service-Name"))
1006+
}
1007+
9841008
func TestCheck_SuccessfulAuthentication(t *testing.T) {
9851009
mockHTTPClient := &http.Client{
9861010
Transport: &MockTransport{

0 commit comments

Comments
 (0)