-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filters/auth: add setRequestHeaderFromSecret filter (#2740)
Add a filter to set request header value from secret with optional prefix and suffix. It is similar to `bearerinjector` which is equivalent to `setRequestHeaderFromSecret("Authorization", "/tokens/my-token", "Bearer ")` For #1952 Signed-off-by: Alexander Yastrebov <[email protected]>
- Loading branch information
1 parent
3e18858
commit ec3881d
Showing
5 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/zalando/skipper/filters" | ||
"github.com/zalando/skipper/secrets" | ||
) | ||
|
||
type ( | ||
secretHeaderSpec struct { | ||
secretsReader secrets.SecretsReader | ||
} | ||
|
||
secretHeaderFilter struct { | ||
headerName string | ||
secretName string | ||
prefix string | ||
suffix string | ||
|
||
secretsReader secrets.SecretsReader | ||
} | ||
) | ||
|
||
func NewSetRequestHeaderFromSecret(sr secrets.SecretsReader) filters.Spec { | ||
return &secretHeaderSpec{secretsReader: sr} | ||
} | ||
|
||
func (*secretHeaderSpec) Name() string { | ||
return filters.SetRequestHeaderFromSecretName | ||
} | ||
|
||
func (s *secretHeaderSpec) CreateFilter(args []interface{}) (filters.Filter, error) { | ||
if len(args) < 2 || len(args) > 4 { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
var ok bool | ||
|
||
f := &secretHeaderFilter{ | ||
secretsReader: s.secretsReader, | ||
} | ||
|
||
f.headerName, ok = args[0].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
f.secretName, ok = args[1].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
if len(args) > 2 { | ||
f.prefix, ok = args[2].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
} | ||
|
||
if len(args) > 3 { | ||
f.suffix, ok = args[3].(string) | ||
if !ok { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
} | ||
|
||
return f, nil | ||
} | ||
|
||
func (f *secretHeaderFilter) Request(ctx filters.FilterContext) { | ||
value, ok := f.secretsReader.GetSecret(f.secretName) | ||
if ok { | ||
ctx.Request().Header.Set(f.headerName, f.prefix+string(value)+f.suffix) | ||
} | ||
} | ||
|
||
func (*secretHeaderFilter) Response(filters.FilterContext) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package auth_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/zalando/skipper/eskip" | ||
"github.com/zalando/skipper/filters/auth" | ||
"github.com/zalando/skipper/filters/filtertest" | ||
) | ||
|
||
type testSecretsReader struct { | ||
name string | ||
secret string | ||
} | ||
|
||
func (tsr *testSecretsReader) GetSecret(name string) ([]byte, bool) { | ||
if name == tsr.name { | ||
return []byte(tsr.secret), true | ||
} | ||
return nil, false | ||
} | ||
|
||
func (*testSecretsReader) Close() {} | ||
|
||
func TestSetRequestHeaderFromSecretInvalidArgs(t *testing.T) { | ||
spec := auth.NewSetRequestHeaderFromSecret(nil) | ||
for _, def := range []string{ | ||
`setRequestHeaderFromSecret()`, | ||
`setRequestHeaderFromSecret("X-Secret")`, | ||
`setRequestHeaderFromSecret("X-Secret", 1)`, | ||
`setRequestHeaderFromSecret(1, "/my-secret")`, | ||
`setRequestHeaderFromSecret("X-Secret", "/my-secret", 1)`, | ||
`setRequestHeaderFromSecret("X-Secret", "/my-secret", "prefix", 1)`, | ||
`setRequestHeaderFromSecret("X-Secret", "/my-secret", "prefix", "suffix", "garbage")`, | ||
} { | ||
t.Run(def, func(t *testing.T) { | ||
ff := eskip.MustParseFilters(def) | ||
require.Len(t, ff, 1) | ||
|
||
_, err := spec.CreateFilter(ff[0].Args) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestSetRequestHeaderFromSecret(t *testing.T) { | ||
spec := auth.NewSetRequestHeaderFromSecret(&testSecretsReader{ | ||
name: "/my-secret", | ||
secret: "secret-value", | ||
}) | ||
|
||
assert.Equal(t, "setRequestHeaderFromSecret", spec.Name()) | ||
|
||
for _, tc := range []struct { | ||
def, header, value string | ||
}{ | ||
{ | ||
def: `setRequestHeaderFromSecret("X-Secret", "/my-secret")`, | ||
header: "X-Secret", | ||
value: "secret-value", | ||
}, | ||
{ | ||
def: `setRequestHeaderFromSecret("X-Secret", "/my-secret", "foo-")`, | ||
header: "X-Secret", | ||
value: "foo-secret-value", | ||
}, | ||
{ | ||
def: `setRequestHeaderFromSecret("X-Secret", "/my-secret", "foo-", "-bar")`, | ||
header: "X-Secret", | ||
value: "foo-secret-value-bar", | ||
}, | ||
{ | ||
def: `setRequestHeaderFromSecret("X-Secret", "/does-not-exist")`, | ||
header: "X-Secret", | ||
value: "", | ||
}, | ||
} { | ||
t.Run(tc.def, func(t *testing.T) { | ||
ff := eskip.MustParseFilters(tc.def) | ||
require.Len(t, ff, 1) | ||
|
||
f, err := spec.CreateFilter(ff[0].Args) | ||
assert.NoError(t, err) | ||
|
||
ctx := &filtertest.Context{ | ||
FRequest: &http.Request{ | ||
Header: http.Header{}, | ||
}, | ||
} | ||
f.Request(ctx) | ||
|
||
if tc.value != "" { | ||
assert.Equal(t, tc.value, ctx.FRequest.Header.Get(tc.header)) | ||
} else { | ||
assert.NotContains(t, ctx.FRequest.Header, tc.header) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters