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

Project API key create and list #114

Merged
merged 5 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 24 additions & 15 deletions pkg/quarterdeck/mock/quarterdeck.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type handlerOptions struct {
handler http.HandlerFunc
status int
fixture interface{}
auth bool
}

// Helper to apply the supplied options, panics if there is an error
Expand All @@ -107,21 +108,19 @@ func handler(opts ...HandlerOption) http.HandlerFunc {
return conf.handler
}

// Encode the fixture data
var data []byte
if conf.fixture != nil {
var err error
if data, err = json.Marshal(conf.fixture); err != nil {
panic(err)
}
}

return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(conf.status)

if data != nil {
w.Header().Set("Content-Type", "application/json")
w.Write(data)
switch {
case conf.auth && r.Header.Get("Authorization") == "":
// TODO: Validate the auth token
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("missing authorization header"))
Copy link
Contributor Author

@pdeziel pdeziel Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the quarterdeck mock so we can verify that the client is actually sending the authorization token to quarterdeck in tests.

case conf.fixture != nil:
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(conf.status)
json.NewEncoder(w).Encode(conf.fixture)
default:
w.WriteHeader(conf.status)
}
}
}
Expand All @@ -147,8 +146,18 @@ func UseHandler(f http.HandlerFunc) HandlerOption {
}
}

// Return a 401 response if the request is not authenticated
func RequireAuth() HandlerOption {
return func(o *handlerOptions) {
o.auth = true
}
}

func fullPath(path, param string) string {
return path + "/" + param
if param != "" {
param = "/" + param
}
return path + param
}

// Endpoint handlers
Expand Down
3 changes: 2 additions & 1 deletion pkg/tenant/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type TenantClient interface {
ProjectAPIKeyList(ctx context.Context, id string, in *PageQuery) (*ProjectAPIKeyPage, error)
ProjectAPIKeyCreate(ctx context.Context, id string, in *APIKey) (*APIKey, error)

APIKeyCreate(context.Context, *APIKey) (*APIKey, error)
APIKeyList(context.Context, *PageQuery) (*APIKeyPage, error)
APIKeyDetail(ctx context.Context, id string) (*APIKey, error)
APIKeyUpdate(context.Context, *APIKey) (*APIKey, error)
Expand Down Expand Up @@ -154,7 +155,7 @@ type ProjectAPIKeyPage struct {
}

type APIKey struct {
ID int `json:"id,omitempty"`
ID string `json:"id,omitempty"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret,omitempty"`
Name string `json:"name"`
Expand Down
27 changes: 22 additions & 5 deletions pkg/tenant/api/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,26 @@ func (s *APIv1) ProjectAPIKeyCreate(ctx context.Context, id string, in *APIKey)
return out, nil
}

func (s *APIv1) APIKeyCreate(ctx context.Context, in *APIKey) (out *APIKey, err error) {
// Make the HTTP Request
var req *http.Request
if req, err = s.NewRequest(ctx, http.MethodPost, "/v1/apikeys", in, nil); err != nil {
return nil, err
}

// Make the HTTP response
out = &APIKey{}
var rep *http.Response
if rep, err = s.Do(req, out, true); err != nil {
return nil, err
}

if rep.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("expected status created, received %s", rep.Status)
}
return out, nil
}

func (s *APIv1) APIKeyList(ctx context.Context, in *PageQuery) (out *APIKeyPage, err error) {
var params url.Values
if params, err = query.Values(in); err != nil {
Expand Down Expand Up @@ -716,14 +736,11 @@ func (s *APIv1) APIKeyDetail(ctx context.Context, id string) (out *APIKey, err e
}

func (s *APIv1) APIKeyUpdate(ctx context.Context, in *APIKey) (out *APIKey, err error) {
// Convert ID from integer to string
sid := fmt.Sprintf("%d", in.ID)

if sid == "" {
if in.ID == "" {
return nil, ErrAPIKeyIDRequired
}

path := fmt.Sprintf("/v1/apikey/%s", sid)
path := fmt.Sprintf("/v1/apikey/%s", in.ID)

// Make the HTTP request
var req *http.Request
Expand Down
48 changes: 42 additions & 6 deletions pkg/tenant/api/v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ func TestProjectAPIKeyList(t *testing.T) {
ProjectID: "001",
APIKeys: []*api.APIKey{
{
ID: 001,
ID: "001",
ClientID: "client001",
ClientSecret: "segredo",
Name: "myapikey",
Expand Down Expand Up @@ -990,7 +990,7 @@ func TestProjectAPIKeyList(t *testing.T) {

func TestProjectAPIKeyCreate(t *testing.T) {
fixture := &api.APIKey{
ID: 001,
ID: "001",
ClientID: "client001",
ClientSecret: "segredo",
Name: "myapikey",
Expand Down Expand Up @@ -1024,11 +1024,47 @@ func TestProjectAPIKeyCreate(t *testing.T) {
require.Equal(t, fixture, out, "unexpected response error")
}

func TestAPIKeyCreate(t *testing.T) {
fixture := &api.APIKey{
ID: "001",
ClientID: "client001",
ClientSecret: "segredo",
Name: "myapikey",
Owner: "Ryan Moore",
Permissions: []string{"Read", "Write", "Delete"},
Created: time.Now().Format(time.RFC3339Nano),
Modified: time.Now().Format(time.RFC3339Nano),
}

// Create a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodPost, r.Method)
require.Equal(t, "/v1/apikeys", r.URL.Path)

in := &api.APIKey{}
err := json.NewDecoder(r.Body).Decode(in)
require.NoError(t, err, "could not decode request")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(fixture)
}))
defer ts.Close()

// Creates a client to execute tests against the test server
client, err := api.New(ts.URL)
require.NoError(t, err, "could not create api client")

out, err := client.APIKeyCreate(context.Background(), &api.APIKey{})
require.NoError(t, err, "could not execute api request")
require.Equal(t, fixture, out, "response did not match")
}

func TestAPIKeyList(t *testing.T) {
fixture := &api.APIKeyPage{
APIKeys: []*api.APIKey{
{
ID: 001,
ID: "001",
ClientID: "client001",
ClientSecret: "segredo",
Name: "myapikey",
Expand Down Expand Up @@ -1073,7 +1109,7 @@ func TestAPIKeyList(t *testing.T) {

func TestAPIKeyDetail(t *testing.T) {
fixture := &api.APIKey{
ID: 001,
ID: "001",
ClientID: "client001",
ClientSecret: "segredo",
Name: "myapikey",
Expand Down Expand Up @@ -1105,7 +1141,7 @@ func TestAPIKeyDetail(t *testing.T) {

func TestAPIKeyUpdate(t *testing.T) {
fixture := &api.APIKey{
ID: 101,
ID: "101",
Name: "apikey01",
}

Expand All @@ -1125,7 +1161,7 @@ func TestAPIKeyUpdate(t *testing.T) {
require.NoError(t, err, "could not execute api request")

req := &api.APIKey{
ID: 101,
ID: "101",
Name: "apikey02",
}

Expand Down