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/db"
|
|
|
|
"agola.io/agola/internal/services/configstore/action"
|
|
|
|
"agola.io/agola/internal/services/configstore/readdb"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
csapitypes "agola.io/agola/services/configstore/api/types"
|
|
|
|
"agola.io/agola/services/configstore/types"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SecretHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-12 22:22:23 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:22:23 +00:00
|
|
|
func NewSecretHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *SecretHandler {
|
|
|
|
return &SecretHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-05-12 22:22:23 +00:00
|
|
|
ctx := r.Context()
|
2019-03-14 13:36:18 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
secretID := vars["secretid"]
|
|
|
|
|
2019-05-12 22:22:23 +00:00
|
|
|
secret, err := h.ah.GetSecret(ctx, secretID)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, secret); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecretsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-12 22:22:23 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:22:23 +00:00
|
|
|
func NewSecretsHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *SecretsHandler {
|
|
|
|
return &SecretsHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SecretsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-05-12 22:22:23 +00:00
|
|
|
ctx := r.Context()
|
2019-03-14 13:36:18 +00:00
|
|
|
query := r.URL.Query()
|
|
|
|
_, tree := query["tree"]
|
|
|
|
|
|
|
|
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
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:22:23 +00:00
|
|
|
secrets, err := h.ah.GetSecrets(ctx, parentType, parentRef, tree)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2019-04-30 14:28:01 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
resSecrets := make([]*csapitypes.Secret, len(secrets))
|
2019-04-30 14:28:01 +00:00
|
|
|
for i, s := range secrets {
|
2019-07-31 13:39:07 +00:00
|
|
|
resSecrets[i] = &csapitypes.Secret{Secret: s}
|
2019-04-30 14:28:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
err = h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-03-14 13:36:18 +00:00
|
|
|
// populate parent path
|
2019-04-30 14:28:01 +00:00
|
|
|
for _, s := range resSecrets {
|
2019-04-30 15:06:44 +00:00
|
|
|
pp, err := h.readDB.GetPath(tx, s.Parent.Type, s.Parent.ID)
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-30 14:28:01 +00:00
|
|
|
s.ParentPath = pp
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, resSecrets); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateSecretHandler struct {
|
2019-07-02 12:46:00 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewCreateSecretHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateSecretHandler {
|
|
|
|
return &CreateSecretHandler{log: logger.Sugar(), 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
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var secret *types.Secret
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&secret); 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
|
|
|
|
}
|
|
|
|
|
|
|
|
secret.Parent.Type = parentType
|
|
|
|
secret.Parent.ID = parentRef
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
secret, err = h.ah.CreateSecret(ctx, secret)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, secret); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 08:32:32 +00:00
|
|
|
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)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2019-07-08 08:32:32 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var secret *types.Secret
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&secret); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-07-08 08:32:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secret.Parent.Type = parentType
|
|
|
|
secret.Parent.ID = parentRef
|
|
|
|
|
|
|
|
areq := &action.UpdateSecretRequest{
|
|
|
|
SecretName: secretName,
|
|
|
|
Secret: secret,
|
|
|
|
}
|
|
|
|
secret, err = h.ah.UpdateSecret(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2019-07-08 08:32:32 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, secret); err != nil {
|
2019-07-08 08:32:32 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
type DeleteSecretHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewDeleteSecretHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteSecretHandler {
|
|
|
|
return &DeleteSecretHandler{log: logger.Sugar(), 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
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
err = h.ah.DeleteSecret(ctx, parentType, parentRef, secretName)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2019-04-08 10:29:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|