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/gateway/common"
|
|
|
|
"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 VariableValue struct {
|
|
|
|
SecretName string `json:"secret_name"`
|
|
|
|
SecretVar string `json:"secret_var"`
|
|
|
|
MatchingSecretParentPath string `json:"matching_secret_parent_path"`
|
|
|
|
|
|
|
|
When *types.When `json:"when"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type VariableResponse struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Values []VariableValue `json:"values"`
|
|
|
|
ParentPath string `json:"parent_path"`
|
|
|
|
}
|
|
|
|
|
2019-04-30 14:28:01 +00:00
|
|
|
func createVariableResponse(v *csapi.Variable, secrets []*csapi.Secret) *VariableResponse {
|
2019-03-14 13:36:18 +00:00
|
|
|
nv := &VariableResponse{
|
|
|
|
ID: v.ID,
|
|
|
|
Name: v.Name,
|
|
|
|
Values: make([]VariableValue, len(v.Values)),
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: v.ParentPath,
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, varvalue := range v.Values {
|
|
|
|
nv.Values[i] = VariableValue{
|
|
|
|
SecretName: varvalue.SecretName,
|
|
|
|
SecretVar: varvalue.SecretVar,
|
|
|
|
When: varvalue.When,
|
|
|
|
}
|
|
|
|
// get matching secret for var value
|
2019-04-30 14:28:01 +00:00
|
|
|
secret := common.GetVarValueMatchingSecret(varvalue, v.ParentPath, secrets)
|
2019-03-14 13:36:18 +00:00
|
|
|
if secret != nil {
|
2019-04-30 14:28:01 +00:00
|
|
|
nv.Values[i].MatchingSecretParentPath = secret.ParentPath
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nv
|
|
|
|
}
|
|
|
|
|
|
|
|
type VariableHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVariableHandler(logger *zap.Logger, configstoreClient *csapi.Client) *VariableHandler {
|
|
|
|
return &VariableHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *VariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
query := r.URL.Query()
|
|
|
|
_, tree := query["tree"]
|
|
|
|
_, removeoverriden := query["removeoverriden"]
|
|
|
|
|
|
|
|
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 csvars []*csapi.Variable
|
|
|
|
var cssecrets []*csapi.Secret
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
switch parentType {
|
|
|
|
case types.ConfigTypeProjectGroup:
|
|
|
|
var err error
|
2019-04-09 12:53:00 +00:00
|
|
|
var resp *http.Response
|
|
|
|
csvars, resp, err = h.configstoreClient.GetProjectGroupVariables(ctx, parentRef, tree)
|
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, parentRef, true)
|
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case types.ConfigTypeProject:
|
|
|
|
var err error
|
2019-04-09 12:53:00 +00:00
|
|
|
var resp *http.Response
|
|
|
|
csvars, resp, err = h.configstoreClient.GetProjectVariables(ctx, parentRef, tree)
|
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, parentRef, true)
|
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if removeoverriden {
|
|
|
|
// remove overriden variables
|
|
|
|
csvars = common.FilterOverridenVariables(csvars)
|
|
|
|
}
|
|
|
|
|
|
|
|
variables := make([]*VariableResponse, len(csvars))
|
|
|
|
for i, v := range csvars {
|
|
|
|
variables[i] = createVariableResponse(v, cssecrets)
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, variables); 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 CreateVariableRequest struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
|
2019-04-02 09:07:39 +00:00
|
|
|
Values []types.VariableValue `json:"values,omitempty"`
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateVariableHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCreateVariableHandler(logger *zap.Logger, configstoreClient *csapi.Client) *CreateVariableHandler {
|
|
|
|
return &CreateVariableHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
parentType, parentRef, err := GetConfigTypeRef(r)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req CreateVariableRequest
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-02 09:07:39 +00:00
|
|
|
if len(req.Values) == 0 {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("empty variable values")))
|
|
|
|
return
|
2019-04-02 09:07:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
v := &types.Variable{
|
|
|
|
Name: req.Name,
|
|
|
|
Parent: types.Parent{
|
|
|
|
Type: parentType,
|
|
|
|
ID: parentRef,
|
|
|
|
},
|
2019-04-02 09:07:39 +00:00
|
|
|
Values: req.Values,
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:28:01 +00:00
|
|
|
var cssecrets []*csapi.Secret
|
|
|
|
var rv *csapi.Variable
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
switch parentType {
|
|
|
|
case types.ConfigTypeProjectGroup:
|
|
|
|
var err error
|
2019-04-09 12:53:00 +00:00
|
|
|
var resp *http.Response
|
|
|
|
cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, parentRef, true)
|
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Infof("creating project group variable")
|
2019-04-30 14:28:01 +00:00
|
|
|
rv, resp, err = h.configstoreClient.CreateProjectGroupVariable(ctx, parentRef, v)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-03-14 13:36:18 +00:00
|
|
|
case types.ConfigTypeProject:
|
2019-04-09 12:53:00 +00:00
|
|
|
var err error
|
|
|
|
var resp *http.Response
|
|
|
|
cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, parentRef, true)
|
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Infof("creating project variable")
|
2019-04-30 14:28:01 +00:00
|
|
|
rv, resp, err = h.configstoreClient.CreateProjectVariable(ctx, parentRef, v)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpErrorFromRemote(w, resp, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-04-30 14:28:01 +00:00
|
|
|
h.log.Infof("variable %s created, ID: %s", rv.Name, rv.ID)
|
2019-03-14 13:36:18 +00:00
|
|
|
|
2019-04-30 14:28:01 +00:00
|
|
|
res := createVariableResponse(rv, cssecrets)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusCreated, 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 DeleteVariableHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
configstoreClient *csapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDeleteVariableHandler(logger *zap.Logger, configstoreClient *csapi.Client) *DeleteVariableHandler {
|
|
|
|
return &DeleteVariableHandler{log: logger.Sugar(), configstoreClient: configstoreClient}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteVariableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
variableName := vars["variablename"]
|
|
|
|
|
|
|
|
parentType, parentRef, err := GetConfigTypeRef(r)
|
|
|
|
if 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp *http.Response
|
|
|
|
switch parentType {
|
|
|
|
case types.ConfigTypeProjectGroup:
|
|
|
|
h.log.Infof("deleting project group variable")
|
|
|
|
resp, err = h.configstoreClient.DeleteProjectGroupVariable(ctx, parentRef, variableName)
|
|
|
|
case types.ConfigTypeProject:
|
|
|
|
h.log.Infof("deleting project variable")
|
|
|
|
resp, err = h.configstoreClient.DeleteProjectVariable(ctx, parentRef, variableName)
|
|
|
|
}
|
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
|
|
|
}
|