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

fix(kuma-cp): validation error with user tokens #4507

Merged
merged 1 commit into from
Jun 27, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (d *userTokenWebService) handleIdentityRequest(request *restful.Request, re

var validFor time.Duration
if idReq.ValidFor == "" {
verr.AddViolation("name", "cannot be empty")
verr.AddViolation("validFor", "cannot be empty")
} else {
dur, err := time.ParseDuration(idReq.ValidFor)
if err != nil {
Expand Down
36 changes: 35 additions & 1 deletion pkg/plugins/authn/api-server/tokens/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package ws_test

import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"time"

"github.com/emicklei/go-restful"
Expand Down Expand Up @@ -34,6 +37,7 @@ var _ = Describe("Auth Tokens WS", func() {

var userTokenClient client.UserTokenClient
var userTokenValidator issuer.UserTokenValidator
var httpClient util_http.Client

BeforeEach(func() {
resManager := manager.NewResourceManager(memory.NewStore())
Expand All @@ -56,7 +60,8 @@ var _ = Describe("Auth Tokens WS", func() {

baseURL, err := url.Parse(srv.URL)
Expect(err).ToNot(HaveOccurred())
userTokenClient = client.NewHTTPUserTokenClient(util_http.ClientWithBaseURL(http.DefaultClient, baseURL, nil))
httpClient = util_http.ClientWithBaseURL(http.DefaultClient, baseURL, nil)
userTokenClient = client.NewHTTPUserTokenClient(httpClient)

// wait for the server
Eventually(func() error {
Expand Down Expand Up @@ -93,4 +98,33 @@ var _ = Describe("Auth Tokens WS", func() {
},
}))
})

It("should throw an validFor is not present", func() {
// given invalid request (cannot be implemented using UserTokenClient)
req, err := http.NewRequest("POST", "/tokens/user", strings.NewReader(`{"name": "xyz"}`))
req.Header.Add("content-type", "application/json")
Expect(err).ToNot(HaveOccurred())

// when
resp, err := httpClient.Do(req)
defer resp.Body.Close()
Expect(err).ToNot(HaveOccurred())

// then
respBytes, err := io.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
respErr := &error_types.Error{}
Expect(json.Unmarshal(respBytes, respErr)).To(Succeed())

Expect(respErr).To(Equal(&error_types.Error{
Title: "Invalid request",
Details: "Resource is not valid",
Causes: []error_types.Cause{
{
Field: "validFor",
Message: "cannot be empty",
},
},
}))
})
})