agola/internal/services/gateway/api/secret.go
Simone Gotti c1da3ab566 *: Improve error handling
* Create an APIError that should only be used for api returned errors.
  It'll wrap an error and can have different Kinds and optional code and
  message.
* The http handlers will use the first APIError available in the
  error chain and generate a json response body containing the code and
  the user message. The wrapped error is internal and is not sent in the
  response.
  If no api error is available in the chain a generic internal
  server error will be returned.
* Add a RemoteError type that will be created from remote services calls
  (runservice, configstore). It's similar to the APIError but a
  different type to not propagate to the caller response and it'll not
  contain any wrapped error.
* Gateway: when we call a remote service, by default, we'll create a
  APIError using the RemoteError Kind (omitting the code and the
  message that usually must not be propagated).
  This is done for all the remote service calls as a starting point, in
  future, if this default behavior is not the right one for a specific
  remote service call, a new api error with a different kind and/or
  augmented with the calling service error codes and user messages could
  be created.
* datamanager: Use a dedicated ErrNotExist (and converting objectstorage
  ErrNotExist).
2022-02-25 16:11:19 +01:00

204 lines
5.3 KiB
Go

// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"encoding/json"
"net/http"
"agola.io/agola/internal/services/gateway/action"
"agola.io/agola/internal/util"
csapitypes "agola.io/agola/services/configstore/api/types"
cstypes "agola.io/agola/services/configstore/types"
gwapitypes "agola.io/agola/services/gateway/api/types"
"github.com/gorilla/mux"
"go.uber.org/zap"
)
func createSecretResponse(s *csapitypes.Secret) *gwapitypes.SecretResponse {
return &gwapitypes.SecretResponse{
ID: s.ID,
Name: s.Name,
ParentPath: s.ParentPath,
}
}
type SecretHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewSecretHandler(logger *zap.Logger, ah *action.ActionHandler) *SecretHandler {
return &SecretHandler{log: logger.Sugar(), ah: ah}
}
func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
query := r.URL.Query()
_, tree := query["tree"]
_, removeoverridden := query["removeoverridden"]
parentType, parentRef, err := GetConfigTypeRef(r)
if util.HTTPError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
areq := &action.GetSecretsRequest{
ParentType: parentType,
ParentRef: parentRef,
Tree: tree,
RemoveOverridden: removeoverridden,
}
cssecrets, err := h.ah.GetSecrets(ctx, areq)
if util.HTTPError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
secrets := make([]*gwapitypes.SecretResponse, len(cssecrets))
for i, s := range cssecrets {
secrets[i] = createSecretResponse(s)
}
if err := util.HTTPResponse(w, http.StatusOK, secrets); err != nil {
h.log.Errorf("err: %+v", err)
}
}
type CreateSecretHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewCreateSecretHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateSecretHandler {
return &CreateSecretHandler{log: logger.Sugar(), ah: ah}
}
func (h *CreateSecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
parentType, parentRef, err := GetConfigTypeRef(r)
if util.HTTPError(w, err) {
return
}
var req gwapitypes.CreateSecretRequest
d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
return
}
areq := &action.CreateSecretRequest{
Name: req.Name,
ParentType: parentType,
ParentRef: parentRef,
Type: cstypes.SecretType(req.Type),
Data: req.Data,
SecretProviderID: req.SecretProviderID,
Path: req.Path,
}
cssecret, err := h.ah.CreateSecret(ctx, areq)
if util.HTTPError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
res := createSecretResponse(cssecret)
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
h.log.Errorf("err: %+v", err)
}
}
type UpdateSecretHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewUpdateSecretHandler(logger *zap.Logger, ah *action.ActionHandler) *UpdateSecretHandler {
return &UpdateSecretHandler{log: logger.Sugar(), ah: ah}
}
func (h *UpdateSecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
secretName := vars["secretname"]
parentType, parentRef, err := GetConfigTypeRef(r)
if util.HTTPError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
var req gwapitypes.UpdateSecretRequest
d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil {
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
return
}
areq := &action.UpdateSecretRequest{
SecretName: secretName,
Name: req.Name,
ParentType: parentType,
ParentRef: parentRef,
Type: cstypes.SecretType(req.Type),
Data: req.Data,
SecretProviderID: req.SecretProviderID,
Path: req.Path,
}
cssecret, err := h.ah.UpdateSecret(ctx, areq)
if util.HTTPError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
res := createSecretResponse(cssecret)
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
h.log.Errorf("err: %+v", err)
}
}
type DeleteSecretHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
}
func NewDeleteSecretHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteSecretHandler {
return &DeleteSecretHandler{log: logger.Sugar(), ah: ah}
}
func (h *DeleteSecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
secretName := vars["secretname"]
parentType, parentRef, err := GetConfigTypeRef(r)
if util.HTTPError(w, err) {
return
}
err = h.ah.DeleteSecret(ctx, parentType, parentRef, secretName)
if util.HTTPError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
h.log.Errorf("err: %+v", err)
}
}