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

feat: add static token auth #15

Merged
merged 1 commit into from
Jan 25, 2023
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
13 changes: 13 additions & 0 deletions seeder/internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,18 @@ func TestMuxServer(t *testing.T) http.Handler {
mux.HandleFunc("/post", postHandle(t))
mux.HandleFunc("/delete", deletetHandle(t))
mux.HandleFunc("/delete/", deletetHandle(t))
mux.HandleFunc("/staticToken", getStaticTokenHandle(t))
return mux
}

func getStaticTokenHandle(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {

if _, ok := r.Header["Token"]; !ok {
t.Errorf("expected token to be set in header, got <nil>")
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{}`))

}
}
10 changes: 10 additions & 0 deletions seeder/pkg/rest/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
OAuth AuthType = "OAuthClientCredentials"
OAuthPassword AuthType = "OAuthPassCredentials"
CustomToToken AuthType = "CustomToToken"
StaticToken AuthType = "StaticToken"
)

// +k8s:deepcopy-gen=true
Expand Down Expand Up @@ -91,6 +92,7 @@ type auth struct {
passwordGrantConfig *passwordGrantConfig
basicAuth *basicAuth
customToToken *customToToken
staticToken *staticToken
// currentToken string
}

Expand Down Expand Up @@ -121,6 +123,11 @@ type customToToken struct {
currentToken string
}

type staticToken struct {
headerKey string
staticToken string
}

type actionAuthMap map[string]auth

func NewAuth(am *AuthMap) *actionAuthMap {
Expand Down Expand Up @@ -191,6 +198,9 @@ func NewAuth(am *AuthMap) *actionAuthMap {
a.customToToken.responseKey = v.CustomToken.ResponseKey
}
ac[k] = a
case StaticToken:
a.staticToken = &staticToken{headerKey: v.Username, staticToken: v.Password}
ac[k] = a
default:
a.basicAuth = &basicAuth{username: v.Username, password: v.Password}
ac[k] = a
Expand Down
2 changes: 2 additions & 0 deletions seeder/pkg/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ func (r *SeederImpl) doAuth(req *http.Request, action *Action) *http.Request {
r.log.Errorf("failed to obtain custom token: %v", err)
}
enrichedReq.Header.Set(token.HeaderKey, fmt.Sprintf("%s %s", token.TokenPrefix, token.TokenValue))
case StaticToken:
enrichedReq.Header.Set(cam.staticToken.headerKey, cam.staticToken.staticToken)
}
return enrichedReq
}
Expand Down
16 changes: 16 additions & 0 deletions seeder/pkg/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func Test_getSeeder(t *testing.T) {
},
expect: "32",
},
{
name: "getRestFunc_with_static_token_header",
auth: &AuthMap{"foo": {AuthStrategy: StaticToken, Username: "token", Password: "bar"}},
client: &http.Client{},
rimpl: &SeederImpl{runtimeVars: runtimeVars{}},
action: &Action{
PayloadTemplate: "{}",
Strategy: "GET",
Endpoint: ts.URL,
GetEndpointSuffix: String("/staticToken"),
FindByJsonPathExpr: "$.args.id",
AuthMapRef: "foo",
HttpHeaders: &map[string]string{"foo": "bar"},
},
expect: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down