2021-03-16 19:43:02 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
2023-06-28 14:06:32 +01:00
|
|
|
"context"
|
2023-06-12 13:42:49 +01:00
|
|
|
"encoding/json"
|
2021-03-16 19:43:02 +00:00
|
|
|
"net/http"
|
2023-08-04 15:45:13 +01:00
|
|
|
"strconv"
|
2021-03-16 19:43:02 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
2022-11-21 18:58:42 +00:00
|
|
|
"storj.io/storj/private/web"
|
2021-03-16 19:43:02 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrAPIKeysAPI - console api keys api error type.
|
2021-06-24 16:49:15 +01:00
|
|
|
ErrAPIKeysAPI = errs.Class("console api keys")
|
2021-03-16 19:43:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// APIKeys is an api controller that exposes all api keys related functionality.
|
|
|
|
type APIKeys struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *console.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPIKeys is a constructor for api api keys controller.
|
|
|
|
func NewAPIKeys(log *zap.Logger, service *console.Service) *APIKeys {
|
|
|
|
return &APIKeys{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 15:45:13 +01:00
|
|
|
// GetProjectAPIKeys returns paged API keys by project ID.
|
|
|
|
func (keys *APIKeys) GetProjectAPIKeys(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
projectIDParam := query.Get("projectID")
|
|
|
|
if projectIDParam == "" {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("parameter 'projectID' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(projectIDParam)
|
|
|
|
if err != nil {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
limitParam := query.Get("limit")
|
|
|
|
if limitParam == "" {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("parameter 'limit' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
limit, err := strconv.ParseUint(limitParam, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pageParam := query.Get("page")
|
|
|
|
if pageParam == "" {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("parameter 'page' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
page, err := strconv.ParseUint(pageParam, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
orderParam := query.Get("order")
|
|
|
|
if orderParam == "" {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("parameter 'order' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
order, err := strconv.ParseUint(orderParam, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
orderDirectionParam := query.Get("orderDirection")
|
|
|
|
if orderDirectionParam == "" {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("parameter 'orderDirection' can't be empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
orderDirection, err := strconv.ParseUint(orderDirectionParam, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
searchString := query.Get("search")
|
|
|
|
|
|
|
|
cursor := console.APIKeyCursor{
|
|
|
|
Search: searchString,
|
|
|
|
Limit: uint(limit),
|
|
|
|
Page: uint(page),
|
|
|
|
Order: console.APIKeyOrder(order),
|
|
|
|
OrderDirection: console.OrderDirection(orderDirection),
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeys, err := keys.service.GetAPIKeys(ctx, projectID, cursor)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keys.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(apiKeys)
|
|
|
|
if err != nil {
|
|
|
|
keys.log.Error("failed to write json all api keys response", zap.Error(ErrAPIKeysAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllAPIKeyNames returns all API key names by project ID.
|
2023-06-12 13:42:49 +01:00
|
|
|
func (keys *APIKeys) GetAllAPIKeyNames(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
projectIDString := r.URL.Query().Get("projectID")
|
|
|
|
if projectIDString == "" {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("Project ID was not provided."))
|
2023-06-12 13:42:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(projectIDString)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2023-06-12 13:42:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeyNames, err := keys.service.GetAllAPIKeyNamesByProjectID(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
2023-06-12 13:42:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2023-06-12 13:42:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(apiKeyNames)
|
|
|
|
if err != nil {
|
|
|
|
keys.log.Error("failed to write json all api key names response", zap.Error(ErrAPIKeysAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 15:45:13 +01:00
|
|
|
// DeleteByNameAndProjectID deletes specific API key by it's name and project ID.
|
2023-01-05 09:17:16 +00:00
|
|
|
// ID here may be project.publicID or project.ID.
|
2021-03-16 19:43:02 +00:00
|
|
|
func (keys *APIKeys) DeleteByNameAndProjectID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
name := r.URL.Query().Get("name")
|
|
|
|
projectIDString := r.URL.Query().Get("projectID")
|
2023-01-05 09:17:16 +00:00
|
|
|
publicIDString := r.URL.Query().Get("publicID")
|
2021-03-16 19:43:02 +00:00
|
|
|
|
|
|
|
if name == "" {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2021-03-16 19:43:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-05 09:17:16 +00:00
|
|
|
var projectID uuid.UUID
|
|
|
|
if projectIDString != "" {
|
|
|
|
projectID, err = uuid.FromString(projectIDString)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2023-01-05 09:17:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if publicIDString != "" {
|
|
|
|
projectID, err = uuid.FromString(publicIDString)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2023-01-05 09:17:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("Project ID was not provided."))
|
2021-03-16 19:43:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = keys.service.DeleteAPIKeyByNameAndProjectID(ctx, name, projectID)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
2021-03-16 19:43:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-23 20:23:27 +00:00
|
|
|
if console.ErrNoAPIKey.Has(err) {
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusNoContent, err)
|
2021-03-23 20:23:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-28 14:06:32 +01:00
|
|
|
keys.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2021-03-16 19:43:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveJSONError writes JSON error to response output stream.
|
2023-06-28 14:06:32 +01:00
|
|
|
func (keys *APIKeys) serveJSONError(ctx context.Context, w http.ResponseWriter, status int, err error) {
|
|
|
|
web.ServeJSONError(ctx, keys.log, w, status, err)
|
2021-03-16 19:43:02 +00:00
|
|
|
}
|