2019-03-14 13:36:18 +00:00
|
|
|
// 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"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/action"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
csapitypes "agola.io/agola/services/configstore/api/types"
|
|
|
|
cstypes "agola.io/agola/services/configstore/types"
|
|
|
|
gwapitypes "agola.io/agola/services/gateway/api/types"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-03-14 13:36:18 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createSecretResponse(s *csapitypes.Secret) *gwapitypes.SecretResponse {
|
|
|
|
return &gwapitypes.SecretResponse{
|
2019-03-14 13:36:18 +00:00
|
|
|
ID: s.ID,
|
|
|
|
Name: s.Name,
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: s.ParentPath,
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecretHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:11:29 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewSecretHandler(log zerolog.Logger, ah *action.ActionHandler) *SecretHandler {
|
|
|
|
return &SecretHandler{log: log, ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
query := r.URL.Query()
|
|
|
|
_, tree := query["tree"]
|
2019-09-20 13:31:22 +00:00
|
|
|
_, removeoverridden := query["removeoverridden"]
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
parentType, parentRef, err := GetConfigTypeRef(r)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:11:29 +00:00
|
|
|
areq := &action.GetSecretsRequest{
|
2019-09-20 13:31:22 +00:00
|
|
|
ParentType: parentType,
|
|
|
|
ParentRef: parentRef,
|
|
|
|
Tree: tree,
|
|
|
|
RemoveOverridden: removeoverridden,
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-05-05 12:11:29 +00:00
|
|
|
cssecrets, err := h.ah.GetSecrets(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
secrets := make([]*gwapitypes.SecretResponse, len(cssecrets))
|
2019-03-14 13:36:18 +00:00
|
|
|
for i, s := range cssecrets {
|
|
|
|
secrets[i] = createSecretResponse(s)
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, secrets); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateSecretHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:11:29 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCreateSecretHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateSecretHandler {
|
|
|
|
return &CreateSecretHandler{log: log, ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateSecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
parentType, parentRef, err := GetConfigTypeRef(r)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.CreateSecretRequest
|
2019-03-14 13:36:18 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:11:29 +00:00
|
|
|
areq := &action.CreateSecretRequest{
|
|
|
|
Name: req.Name,
|
|
|
|
ParentType: parentType,
|
|
|
|
ParentRef: parentRef,
|
2019-07-31 13:39:07 +00:00
|
|
|
Type: cstypes.SecretType(req.Type),
|
2019-05-05 12:11:29 +00:00
|
|
|
Data: req.Data,
|
|
|
|
SecretProviderID: req.SecretProviderID,
|
|
|
|
Path: req.Path,
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-05-05 12:11:29 +00:00
|
|
|
cssecret, err := h.ah.CreateSecret(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-08 08:32:45 +00:00
|
|
|
res := createSecretResponse(cssecret)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-07-08 08:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateSecretHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-07-08 08:32:45 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewUpdateSecretHandler(log zerolog.Logger, ah *action.ActionHandler) *UpdateSecretHandler {
|
|
|
|
return &UpdateSecretHandler{log: log, ah: ah}
|
2019-07-08 08:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-07-08 08:32:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.UpdateSecretRequest
|
2019-07-08 08:32:45 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-07-08 08:32:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
areq := &action.UpdateSecretRequest{
|
|
|
|
SecretName: secretName,
|
|
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
ParentType: parentType,
|
|
|
|
ParentRef: parentRef,
|
2019-07-31 13:39:07 +00:00
|
|
|
Type: cstypes.SecretType(req.Type),
|
2019-07-08 08:32:45 +00:00
|
|
|
Data: req.Data,
|
|
|
|
SecretProviderID: req.SecretProviderID,
|
|
|
|
Path: req.Path,
|
|
|
|
}
|
|
|
|
cssecret, err := h.ah.UpdateSecret(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-07-08 08:32:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:11:29 +00:00
|
|
|
res := createSecretResponse(cssecret)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteSecretHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:11:29 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewDeleteSecretHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteSecretHandler {
|
|
|
|
return &DeleteSecretHandler{log: log, ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:11:29 +00:00
|
|
|
err = h.ah.DeleteSecret(ctx, parentType, parentRef, secretName)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|