Skip to content

Commit 296d15d

Browse files
committed
Forward original grpc authority host:port to downstream plugins
1 parent 08fbaa2 commit 296d15d

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

pkg/server/plugin/nodeattestor/v1.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import (
99
"github.com/spiffe/spire/pkg/common/plugin"
1010
"github.com/spiffe/spire/proto/spire/common"
1111
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/metadata"
1213
"google.golang.org/grpc/status"
1314
)
1415

16+
const (
17+
OriginalAuthorityKey = "original-authority"
18+
)
19+
1520
type V1 struct {
1621
plugin.Facade
1722
nodeattestorv1.NodeAttestorPluginClient
@@ -28,6 +33,12 @@ func (v1 *V1) Attest(ctx context.Context, payload []byte, challengeFn func(ctx c
2833
ctx, cancel := context.WithCancel(ctx)
2934
defer cancel()
3035

36+
// forward original request authority to downstream plugins
37+
originalAuthority := getOriginalAuthority(ctx)
38+
if originalAuthority != "" {
39+
ctx = metadata.AppendToOutgoingContext(ctx, OriginalAuthorityKey, originalAuthority)
40+
}
41+
3142
stream, err := v1.NodeAttestorPluginClient.Attest(ctx)
3243
if err != nil {
3344
return nil, v1.WrapErr(err)
@@ -101,3 +112,13 @@ func (v1 *V1) streamError(err error) error {
101112
}
102113
return v1.WrapErr(err)
103114
}
115+
116+
func getOriginalAuthority(ctx context.Context) string {
117+
authority := metadata.ValueFromIncomingContext(ctx, ":authority")
118+
if len(authority) == 0 {
119+
return ""
120+
}
121+
// should be just one in a slice
122+
// example value: spire-server-xyz.spiffe.io:8081
123+
return authority[0]
124+
}

pkg/server/plugin/nodeattestor/v1_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
1616
"google.golang.org/grpc/codes"
17+
"google.golang.org/grpc/metadata"
1718
"google.golang.org/grpc/status"
1819
)
1920

@@ -34,6 +35,7 @@ func TestV1(t *testing.T) {
3435
for _, tt := range []struct {
3536
test string
3637
plugin *fakeV1Plugin
38+
ctx context.Context
3739
payload string
3840
responseErr error
3941
expectAnyError bool
@@ -128,10 +130,32 @@ func TestV1(t *testing.T) {
128130
expectMessage: "",
129131
expectResult: resultWithSelectorsAndCanReattest,
130132
},
133+
{
134+
test: "attestation succeeds with original authority forwarded",
135+
plugin: &fakeV1Plugin{challenges: challenges, agentID: agentID, selectorValues: selectorValues},
136+
ctx: metadata.NewIncomingContext(
137+
context.Background(),
138+
metadata.New(map[string]string{":authority": "spire-server-xyz.spiffe.io:8081"}),
139+
),
140+
payload: "without-challenge",
141+
expectCode: codes.OK,
142+
expectMessage: "",
143+
expectResult: &nodeattestor.AttestResult{
144+
AgentID: agentID,
145+
Selectors: []*common.Selector{
146+
{Type: "test", Value: "value"},
147+
{Type: "test", Value: "spire-server-xyz.spiffe.io:8081"},
148+
},
149+
},
150+
},
131151
} {
132152
t.Run(tt.test, func(t *testing.T) {
133153
nodeattestor := loadV1Plugin(t, tt.plugin)
134-
result, err := nodeattestor.Attest(context.Background(), []byte(tt.payload),
154+
ctx := tt.ctx
155+
if ctx == nil {
156+
ctx = context.Background()
157+
}
158+
result, err := nodeattestor.Attest(ctx, []byte(tt.payload),
135159
func(ctx context.Context, challenge []byte) ([]byte, error) {
136160
// echo the challenge back
137161
return challenge, tt.responseErr
@@ -219,11 +243,17 @@ func (plugin *fakeV1Plugin) Attest(stream nodeattestorv1.NodeAttestor_AttestServ
219243
}
220244
}
221245

246+
selectorValues := plugin.selectorValues
247+
originalAuthority := metadata.ValueFromIncomingContext(stream.Context(), nodeattestor.OriginalAuthorityKey)
248+
if len(originalAuthority) != 0 {
249+
selectorValues = append(selectorValues, originalAuthority[0])
250+
}
251+
222252
return stream.Send(&nodeattestorv1.AttestResponse{
223253
Response: &nodeattestorv1.AttestResponse_AgentAttributes{
224254
AgentAttributes: &nodeattestorv1.AgentAttributes{
225255
SpiffeId: plugin.agentID,
226-
SelectorValues: plugin.selectorValues,
256+
SelectorValues: selectorValues,
227257
CanReattest: plugin.canReattest,
228258
},
229259
},

0 commit comments

Comments
 (0)