2019-05-04 13:16:49 +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 action
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/common"
|
|
|
|
"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"
|
2019-05-04 13:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GetVariablesRequest struct {
|
2019-07-31 13:17:54 +00:00
|
|
|
ParentType cstypes.ConfigType
|
2019-05-04 13:16:49 +00:00
|
|
|
ParentRef string
|
|
|
|
|
|
|
|
Tree bool
|
|
|
|
RemoveOverridden bool
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *ActionHandler) GetVariables(ctx context.Context, req *GetVariablesRequest) ([]*csapitypes.Variable, []*csapitypes.Secret, error) {
|
|
|
|
var csvars []*csapitypes.Variable
|
|
|
|
var cssecrets []*csapitypes.Secret
|
2019-05-04 13:16:49 +00:00
|
|
|
|
|
|
|
switch req.ParentType {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.ConfigTypeProjectGroup:
|
2019-05-04 13:16:49 +00:00
|
|
|
var err error
|
2022-02-21 11:19:55 +00:00
|
|
|
csvars, _, err = h.configstoreClient.GetProjectGroupVariables(ctx, req.ParentRef, req.Tree)
|
2019-05-04 13:16:49 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), err)
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
2022-02-21 11:19:55 +00:00
|
|
|
cssecrets, _, err = h.configstoreClient.GetProjectGroupSecrets(ctx, req.ParentRef, true)
|
2019-05-04 13:16:49 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), err)
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.ConfigTypeProject:
|
2019-05-04 13:16:49 +00:00
|
|
|
var err error
|
2022-02-21 11:19:55 +00:00
|
|
|
csvars, _, err = h.configstoreClient.GetProjectVariables(ctx, req.ParentRef, req.Tree)
|
2019-05-04 13:16:49 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), err)
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
2022-02-21 11:19:55 +00:00
|
|
|
cssecrets, _, err = h.configstoreClient.GetProjectSecrets(ctx, req.ParentRef, true)
|
2019-05-04 13:16:49 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), err)
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.RemoveOverridden {
|
|
|
|
// remove overriden variables
|
|
|
|
csvars = common.FilterOverriddenVariables(csvars)
|
|
|
|
}
|
|
|
|
|
|
|
|
return csvars, cssecrets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateVariableRequest struct {
|
|
|
|
Name string
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
ParentType cstypes.ConfigType
|
2019-05-04 13:16:49 +00:00
|
|
|
ParentRef string
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
Values []cstypes.VariableValue
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *ActionHandler) CreateVariable(ctx context.Context, req *CreateVariableRequest) (*csapitypes.Variable, []*csapitypes.Secret, error) {
|
2019-05-03 21:19:23 +00:00
|
|
|
isVariableOwner, err := h.IsVariableOwner(ctx, req.ParentType, req.ParentRef)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, errors.Wrapf(err, "failed to determine ownership")
|
2019-05-03 21:19:23 +00:00
|
|
|
}
|
|
|
|
if !isVariableOwner {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized"))
|
2019-05-03 21:19:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 13:16:49 +00:00
|
|
|
if !util.ValidateName(req.Name) {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("invalid variable name %q", req.Name))
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Values) == 0 {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("empty variable values"))
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
v := &cstypes.Variable{
|
2019-05-04 13:16:49 +00:00
|
|
|
Name: req.Name,
|
2019-07-31 13:17:54 +00:00
|
|
|
Parent: cstypes.Parent{
|
2019-05-04 13:16:49 +00:00
|
|
|
Type: req.ParentType,
|
|
|
|
ID: req.ParentRef,
|
|
|
|
},
|
|
|
|
Values: req.Values,
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var cssecrets []*csapitypes.Secret
|
|
|
|
var rv *csapitypes.Variable
|
2019-05-04 13:16:49 +00:00
|
|
|
|
|
|
|
switch req.ParentType {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.ConfigTypeProjectGroup:
|
2019-05-04 13:16:49 +00:00
|
|
|
var err error
|
2022-02-21 11:19:55 +00:00
|
|
|
cssecrets, _, err = h.configstoreClient.GetProjectGroupSecrets(ctx, req.ParentRef, true)
|
2019-05-04 13:16:49 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get project group %q secrets", req.ParentRef))
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("creating project group variable")
|
2022-02-21 11:19:55 +00:00
|
|
|
rv, _, err = h.configstoreClient.CreateProjectGroupVariable(ctx, req.ParentRef, v)
|
2019-05-04 13:16:49 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to create variable"))
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.ConfigTypeProject:
|
2019-05-04 13:16:49 +00:00
|
|
|
var err error
|
2022-02-21 11:19:55 +00:00
|
|
|
cssecrets, _, err = h.configstoreClient.GetProjectSecrets(ctx, req.ParentRef, true)
|
2019-05-04 13:16:49 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get project %q secrets", req.ParentRef))
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("creating project variable")
|
2022-02-21 11:19:55 +00:00
|
|
|
rv, _, err = h.configstoreClient.CreateProjectVariable(ctx, req.ParentRef, v)
|
2019-05-04 13:16:49 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to create variable"))
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("variable %s created, ID: %s", rv.Name, rv.ID)
|
2019-07-06 13:25:45 +00:00
|
|
|
|
|
|
|
return rv, cssecrets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateVariableRequest struct {
|
|
|
|
VariableName string
|
|
|
|
|
|
|
|
Name string
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
ParentType cstypes.ConfigType
|
2019-07-06 13:25:45 +00:00
|
|
|
ParentRef string
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
Values []cstypes.VariableValue
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func (h *ActionHandler) UpdateVariable(ctx context.Context, req *UpdateVariableRequest) (*csapitypes.Variable, []*csapitypes.Secret, error) {
|
2019-07-06 13:25:45 +00:00
|
|
|
isVariableOwner, err := h.IsVariableOwner(ctx, req.ParentType, req.ParentRef)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, errors.Wrapf(err, "failed to determine ownership")
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
if !isVariableOwner {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized"))
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !util.ValidateName(req.Name) {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("invalid variable name %q", req.Name))
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Values) == 0 {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("empty variable values"))
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
v := &cstypes.Variable{
|
2019-07-06 13:25:45 +00:00
|
|
|
Name: req.Name,
|
2019-07-31 13:17:54 +00:00
|
|
|
Parent: cstypes.Parent{
|
2019-07-06 13:25:45 +00:00
|
|
|
Type: req.ParentType,
|
|
|
|
ID: req.ParentRef,
|
|
|
|
},
|
|
|
|
Values: req.Values,
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var cssecrets []*csapitypes.Secret
|
|
|
|
var rv *csapitypes.Variable
|
2019-07-06 13:25:45 +00:00
|
|
|
|
|
|
|
switch req.ParentType {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.ConfigTypeProjectGroup:
|
2019-07-06 13:25:45 +00:00
|
|
|
var err error
|
2022-02-21 11:19:55 +00:00
|
|
|
cssecrets, _, err = h.configstoreClient.GetProjectGroupSecrets(ctx, req.ParentRef, true)
|
2019-07-06 13:25:45 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get project group %q secrets", req.ParentRef))
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("creating project group variable")
|
2022-02-21 11:19:55 +00:00
|
|
|
rv, _, err = h.configstoreClient.UpdateProjectGroupVariable(ctx, req.ParentRef, req.VariableName, v)
|
2019-07-06 13:25:45 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to create variable"))
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.ConfigTypeProject:
|
2019-07-06 13:25:45 +00:00
|
|
|
var err error
|
2022-02-21 11:19:55 +00:00
|
|
|
cssecrets, _, err = h.configstoreClient.GetProjectSecrets(ctx, req.ParentRef, true)
|
2019-07-06 13:25:45 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to get project %q secrets", req.ParentRef))
|
2019-07-06 13:25:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("creating project variable")
|
2022-02-21 11:19:55 +00:00
|
|
|
rv, _, err = h.configstoreClient.UpdateProjectVariable(ctx, req.ParentRef, req.VariableName, v)
|
2019-07-06 13:25:45 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to create variable"))
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("variable %s created, ID: %s", rv.Name, rv.ID)
|
2019-05-04 13:16:49 +00:00
|
|
|
|
|
|
|
return rv, cssecrets, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func (h *ActionHandler) DeleteVariable(ctx context.Context, parentType cstypes.ConfigType, parentRef, name string) error {
|
2019-05-03 21:19:23 +00:00
|
|
|
isVariableOwner, err := h.IsVariableOwner(ctx, parentType, parentRef)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "failed to determine ownership")
|
2019-05-03 21:19:23 +00:00
|
|
|
}
|
|
|
|
if !isVariableOwner {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized"))
|
2019-05-03 21:19:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 13:16:49 +00:00
|
|
|
switch parentType {
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.ConfigTypeProjectGroup:
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("deleting project group variable")
|
2022-02-21 11:19:55 +00:00
|
|
|
_, err = h.configstoreClient.DeleteProjectGroupVariable(ctx, parentRef, name)
|
2019-07-31 13:17:54 +00:00
|
|
|
case cstypes.ConfigTypeProject:
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("deleting project variable")
|
2022-02-21 11:19:55 +00:00
|
|
|
_, err = h.configstoreClient.DeleteProjectVariable(ctx, parentRef, name)
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to delete variable"))
|
2019-05-04 13:16:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|