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"
|
|
|
|
|
|
|
|
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
|
|
|
"github.com/sorintlab/agola/internal/util"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SecretResponse struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ParentPath string `json:"parent_path"`
|
|
|
|
}
|
|
|
|
|
2019-04-30 14:28:01 +00:00
|
|
|
func createSecretResponse(s *csapi.Secret) *SecretResponse {
|
2019-03-14 13:36:18 +00:00
|
|
|
return &SecretResponse{
|
|
|
|
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 {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSecretHandler(logger *zap.Logger, configstoreClient *csapi.Client) *SecretHandler {
|
|
|
|
return &SecretHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
query := r.URL.Query()
|
|
|
|
_, tree := query["tree"]
|
|
|
|
|
|
|
|
parentType, parentRef, err := GetConfigTypeRef(r)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-30 14:28:01 +00:00
|
|
|
var cssecrets []*csapi.Secret
|
2019-04-09 12:53:00 +00:00
|
|
|
var resp *http.Response
|
2019-03-14 13:36:18 +00:00
|
|
|
switch parentType {
|
|
|
|
case types.ConfigTypeProjectGroup:
|
2019-04-09 12:53:00 +00:00
|
|
|
cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, parentRef, tree)
|
2019-03-14 13:36:18 +00:00
|
|
|
case types.ConfigTypeProject:
|
2019-04-09 12:53:00 +00:00
|
|
|
cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, parentRef, tree)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secrets := make([]*SecretResponse, len(cssecrets))
|
|
|
|
for i, s := range cssecrets {
|
|
|
|
secrets[i] = createSecretResponse(s)
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, secrets); 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 CreateSecretRequest struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
|
|
|
|
Type types.SecretType `json:"type,omitempty"`
|
|
|
|
|
|
|
|
// internal secret
|
|
|
|
Data map[string]string `json:"data,omitempty"`
|
|
|
|
|
|
|
|
// external secret
|
|
|
|
SecretProviderID string `json:"secret_provider_id,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateSecretHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCreateSecretHandler(logger *zap.Logger, configstoreClient *csapi.Client) *CreateSecretHandler {
|
|
|
|
return &CreateSecretHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateSecretHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
parentType, parentRef, err := GetConfigTypeRef(r)
|
|
|
|
if httpError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req CreateSecretRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !util.ValidateName(req.Name) {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("invalid secret name %q", req.Name)))
|
|
|
|
return
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := &types.Secret{
|
|
|
|
Name: req.Name,
|
2019-04-05 08:32:32 +00:00
|
|
|
Type: req.Type,
|
|
|
|
Data: req.Data,
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
var resp *http.Response
|
2019-04-30 14:28:01 +00:00
|
|
|
var rs *csapi.Secret
|
2019-03-14 13:36:18 +00:00
|
|
|
switch parentType {
|
|
|
|
case types.ConfigTypeProjectGroup:
|
|
|
|
h.log.Infof("creating project group secret")
|
2019-04-30 14:28:01 +00:00
|
|
|
rs, resp, err = h.configstoreClient.CreateProjectGroupSecret(ctx, parentRef, s)
|
2019-03-14 13:36:18 +00:00
|
|
|
case types.ConfigTypeProject:
|
|
|
|
h.log.Infof("creating project secret")
|
2019-04-30 14:28:01 +00:00
|
|
|
rs, resp, err = h.configstoreClient.CreateProjectSecret(ctx, parentRef, s)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-30 14:28:01 +00:00
|
|
|
h.log.Infof("secret %s created, ID: %s", rs.Name, rs.ID)
|
2019-03-14 13:36:18 +00:00
|
|
|
|
2019-04-30 14:28:01 +00:00
|
|
|
res := createSecretResponse(rs)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); 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 DeleteSecretHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDeleteSecretHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteSecretHandler {
|
|
|
|
return &DeleteSecretHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 httpError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp *http.Response
|
|
|
|
switch parentType {
|
|
|
|
case types.ConfigTypeProjectGroup:
|
|
|
|
h.log.Infof("deleting project group secret")
|
|
|
|
resp, err = h.configstoreClient.DeleteProjectGroupSecret(ctx, parentRef, secretName)
|
|
|
|
case types.ConfigTypeProject:
|
|
|
|
h.log.Infof("deleting project secret")
|
|
|
|
resp, err = h.configstoreClient.DeleteProjectSecret(ctx, parentRef, secretName)
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|