-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for multiple issuers / generic token issuer (#176)
- Loading branch information
Showing
6 changed files
with
557 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/metal-stack/masterdata-api/api/rest/mapper" | ||
v1 "github.com/metal-stack/masterdata-api/api/rest/v1" | ||
mdmv1 "github.com/metal-stack/masterdata-api/api/v1" | ||
mdm "github.com/metal-stack/masterdata-api/pkg/client" | ||
|
||
"github.com/metal-stack/metal-api/cmd/metal-api/internal/utils" | ||
"go.uber.org/zap" | ||
|
||
restfulspec "github.com/emicklei/go-restful-openapi/v2" | ||
restful "github.com/emicklei/go-restful/v3" | ||
"github.com/metal-stack/metal-lib/httperrors" | ||
"github.com/metal-stack/metal-lib/zapup" | ||
) | ||
|
||
type tenantResource struct { | ||
mdc mdm.Client | ||
} | ||
|
||
// NewTenant returns a webservice for tenant specific endpoints. | ||
func NewTenant(mdc mdm.Client) *restful.WebService { | ||
r := tenantResource{ | ||
mdc: mdc, | ||
} | ||
return r.webService() | ||
} | ||
|
||
func (r tenantResource) webService() *restful.WebService { | ||
ws := new(restful.WebService) | ||
ws. | ||
Path(BasePath + "v1/tenant"). | ||
Consumes(restful.MIME_JSON). | ||
Produces(restful.MIME_JSON) | ||
|
||
tags := []string{"tenant"} | ||
|
||
ws.Route(ws.GET("/{id}"). | ||
To(viewer(r.getTenant)). | ||
Operation("getTenant"). | ||
Doc("get tenant by id"). | ||
Param(ws.PathParameter("id", "identifier of the tenant").DataType("string")). | ||
Metadata(restfulspec.KeyOpenAPITags, tags). | ||
Writes(v1.TenantResponse{}). | ||
Returns(http.StatusOK, "OK", v1.TenantResponse{}). | ||
DefaultReturns("Error", httperrors.HTTPErrorResponse{})) | ||
|
||
ws.Route(ws.GET("/"). | ||
To(viewer(r.listTenants)). | ||
Operation("listTenants"). | ||
Doc("get all tenants"). | ||
Metadata(restfulspec.KeyOpenAPITags, tags). | ||
Writes([]v1.TenantResponse{}). | ||
Returns(http.StatusOK, "OK", []v1.TenantResponse{}). | ||
DefaultReturns("Error", httperrors.HTTPErrorResponse{})) | ||
|
||
return ws | ||
} | ||
|
||
func (r tenantResource) getTenant(request *restful.Request, response *restful.Response) { | ||
id := request.PathParameter("id") | ||
|
||
tres, err := r.mdc.Tenant().Get(context.Background(), &mdmv1.TenantGetRequest{Id: id}) | ||
if checkError(request, response, utils.CurrentFuncName(), err) { | ||
return | ||
} | ||
|
||
v1t := mapper.ToV1Tenant(tres.Tenant) | ||
err = response.WriteHeaderAndEntity(http.StatusOK, &v1t) | ||
if err != nil { | ||
zapup.MustRootLogger().Error("Failed to send response", zap.Error(err)) | ||
return | ||
} | ||
} | ||
|
||
func (r tenantResource) listTenants(request *restful.Request, response *restful.Response) { | ||
tres, err := r.mdc.Tenant().Find(context.Background(), &mdmv1.TenantFindRequest{}) | ||
if checkError(request, response, utils.CurrentFuncName(), err) { | ||
return | ||
} | ||
|
||
var v1ts []*v1.Tenant | ||
for _, t := range tres.Tenants { | ||
v1t := mapper.ToV1Tenant(t) | ||
v1ts = append(v1ts, v1t) | ||
} | ||
|
||
err = response.WriteHeaderAndEntity(http.StatusOK, v1ts) | ||
if err != nil { | ||
zapup.MustRootLogger().Error("Failed to send response", zap.Error(err)) | ||
return | ||
} | ||
} |
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,170 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
restful "github.com/emicklei/go-restful/v3" | ||
v1 "github.com/metal-stack/masterdata-api/api/rest/v1" | ||
mdmv1 "github.com/metal-stack/masterdata-api/api/v1" | ||
mdmv1mock "github.com/metal-stack/masterdata-api/api/v1/mocks" | ||
mdm "github.com/metal-stack/masterdata-api/pkg/client" | ||
"github.com/metal-stack/metal-lib/httperrors" | ||
"github.com/metal-stack/security" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MockedTenantService struct { | ||
t *testing.T | ||
ws *restful.WebService | ||
} | ||
|
||
func NewMockedTenantService(t *testing.T, tenantServiceMock func(mock *mdmv1mock.TenantServiceClient)) *MockedTenantService { | ||
tsc := &mdmv1mock.TenantServiceClient{} | ||
if tenantServiceMock != nil { | ||
tenantServiceMock(tsc) | ||
} | ||
mdc := mdm.NewMock(&mdmv1mock.ProjectServiceClient{}, tsc) | ||
ws := NewTenant(mdc) | ||
return &MockedTenantService{ | ||
t: t, | ||
ws: ws, | ||
} | ||
} | ||
|
||
//nolint:golint,unused | ||
func (m *MockedTenantService) list(user *security.User, resp interface{}) int { | ||
return webRequestGet(m.t, m.ws, user, user, "/v1/tenant", resp) | ||
} | ||
|
||
func (m *MockedTenantService) get(id string, user *security.User, resp interface{}) int { | ||
return webRequestGet(m.t, m.ws, user, user, "/v1/tenant/"+id, resp) | ||
} | ||
|
||
func Test_tenantResource_getTenant(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
userScenarios []security.User | ||
tenantServiceMock func(mock *mdmv1mock.TenantServiceClient) | ||
id string | ||
wantStatus int | ||
want *v1.TenantResponse | ||
wantErr *httperrors.HTTPErrorResponse | ||
}{ | ||
{ | ||
name: "entity forbidden for user with no privileges", | ||
userScenarios: []security.User{*noUser}, | ||
id: "121", | ||
wantStatus: 403, | ||
wantErr: httperrors.Forbidden(fmt.Errorf("you are not member in one of [k8s_kaas-view maas-all-all-view k8s_kaas-edit maas-all-all-edit k8s_kaas-admin maas-all-all-admin]")), | ||
}, | ||
{ | ||
name: "entity allowed for user with view privileges", | ||
userScenarios: []security.User{*testViewUser}, | ||
id: "122", | ||
tenantServiceMock: func(mock *mdmv1mock.TenantServiceClient) { | ||
mock.On("Get", context.Background(), &mdmv1.TenantGetRequest{Id: "122"}).Return(&mdmv1.TenantResponse{Tenant: &mdmv1.Tenant{Name: "t122"}}, nil) | ||
}, | ||
want: &v1.TenantResponse{Tenant: v1.Tenant{Name: "t122"}}, | ||
wantStatus: 200, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "entity allowed for user with admin privileges", | ||
userScenarios: []security.User{*testAdminUser}, | ||
tenantServiceMock: func(mock *mdmv1mock.TenantServiceClient) { | ||
mock.On("Get", context.Background(), &mdmv1.TenantGetRequest{Id: "123"}).Return(&mdmv1.TenantResponse{Tenant: &mdmv1.Tenant{Name: "t123"}}, nil) | ||
}, | ||
id: "123", | ||
want: &v1.TenantResponse{Tenant: v1.Tenant{Name: "t123"}}, | ||
wantStatus: 200, | ||
wantErr: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
for _, user := range tt.userScenarios { | ||
user := user | ||
name := fmt.Sprintf("%s/%s", tt.name, user) | ||
t.Run(name, func(t *testing.T) { | ||
service := NewMockedTenantService(t, tt.tenantServiceMock) | ||
if tt.wantErr != nil { | ||
got := httperrors.HTTPErrorResponse{} | ||
status := service.get(tt.id, &user, &got) | ||
require.Equal(t, tt.wantStatus, status) | ||
require.Equal(t, *tt.wantErr, got) | ||
return | ||
} | ||
|
||
got := v1.TenantResponse{} | ||
status := service.get(tt.id, &user, &got) | ||
require.Equal(t, tt.wantStatus, status) | ||
require.Equal(t, *tt.want, got) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func Test_tenantResource_listTenants(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
userScenarios []security.User | ||
tenantServiceMock func(mock *mdmv1mock.TenantServiceClient) | ||
wantStatus int | ||
want []*v1.Tenant | ||
wantErr *httperrors.HTTPErrorResponse | ||
}{ | ||
{ | ||
name: "entity forbidden for user with no privileges", | ||
userScenarios: []security.User{*noUser}, | ||
wantStatus: 403, | ||
wantErr: httperrors.Forbidden(fmt.Errorf("you are not member in one of [k8s_kaas-view maas-all-all-view k8s_kaas-edit maas-all-all-edit k8s_kaas-admin maas-all-all-admin]")), | ||
}, | ||
{ | ||
name: "entity allowed for user with view privileges", | ||
userScenarios: []security.User{*testViewUser}, | ||
tenantServiceMock: func(mock *mdmv1mock.TenantServiceClient) { | ||
mock.On("Find", context.Background(), &mdmv1.TenantFindRequest{}).Return(&mdmv1.TenantListResponse{Tenants: []*mdmv1.Tenant{{Name: "t121"}, {Name: "t122"}}}, nil) | ||
}, | ||
want: []*v1.Tenant{{Name: "t121"}, {Name: "t122"}}, | ||
wantStatus: 200, | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "entity allowed for user with admin privileges", | ||
userScenarios: []security.User{*testAdminUser}, | ||
tenantServiceMock: func(mock *mdmv1mock.TenantServiceClient) { | ||
mock.On("Find", context.Background(), &mdmv1.TenantFindRequest{}).Return(&mdmv1.TenantListResponse{Tenants: []*mdmv1.Tenant{{Name: "t123"}}}, nil) | ||
}, | ||
want: []*v1.Tenant{{Name: "t123"}}, | ||
wantStatus: 200, | ||
wantErr: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
for _, user := range tt.userScenarios { | ||
user := user | ||
name := fmt.Sprintf("%s/%s", tt.name, user) | ||
t.Run(name, func(t *testing.T) { | ||
service := NewMockedTenantService(t, tt.tenantServiceMock) | ||
|
||
if tt.wantErr != nil { | ||
got := httperrors.HTTPErrorResponse{} | ||
status := service.list(&user, &got) | ||
require.Equal(t, tt.wantStatus, status) | ||
require.Equal(t, *tt.wantErr, got) | ||
return | ||
} | ||
|
||
var got []*v1.Tenant | ||
status := service.list(&user, &got) | ||
require.Equal(t, tt.wantStatus, status) | ||
require.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.