2019-05-03 10:47:22 +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.
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
package action
|
2019-05-03 10:47:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/datamanager"
|
|
|
|
"agola.io/agola/internal/db"
|
2019-07-31 13:17:54 +00:00
|
|
|
"agola.io/agola/internal/services/configstore/types"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2019-05-03 10:47:22 +00:00
|
|
|
|
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-03 10:47:22 +00:00
|
|
|
)
|
|
|
|
|
2019-05-23 08:29:03 +00:00
|
|
|
func (h *ActionHandler) ValidateRemoteSource(ctx context.Context, remoteSource *types.RemoteSource) error {
|
2019-05-03 10:47:22 +00:00
|
|
|
if remoteSource.Name == "" {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource name required"))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if !util.ValidateName(remoteSource.Name) {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("invalid remotesource name %q", remoteSource.Name))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if remoteSource.Name == "" {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource name required"))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if remoteSource.APIURL == "" {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource api url required"))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if remoteSource.Type == "" {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource type required"))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if remoteSource.AuthType == "" {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource auth type required"))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 08:29:03 +00:00
|
|
|
// validate if the remotesource type supports the required auth type
|
2019-05-03 10:47:22 +00:00
|
|
|
if !types.SourceSupportsAuthType(types.RemoteSourceType(remoteSource.Type), types.RemoteSourceAuthType(remoteSource.AuthType)) {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource type %q doesn't support auth type %q", remoteSource.Type, remoteSource.AuthType))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if remoteSource.AuthType == types.RemoteSourceAuthTypeOauth2 {
|
|
|
|
if remoteSource.Oauth2ClientID == "" {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource oauth2clientid required for auth type %q", types.RemoteSourceAuthTypeOauth2))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if remoteSource.Oauth2ClientSecret == "" {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource oauth2clientsecret required for auth type %q", types.RemoteSourceAuthTypeOauth2))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:29:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) CreateRemoteSource(ctx context.Context, remoteSource *types.RemoteSource) (*types.RemoteSource, error) {
|
|
|
|
if err := h.ValidateRemoteSource(ctx, remoteSource); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-03 10:47:22 +00:00
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
// changegroup is the remotesource name
|
|
|
|
cgNames := []string{util.EncodeSha256Hex("remotesourcename-" + remoteSource.Name)}
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-03 10:47:22 +00:00
|
|
|
var err error
|
2019-05-03 21:35:25 +00:00
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check duplicate remoteSource name
|
2019-05-03 21:35:25 +00:00
|
|
|
u, err := h.readDB.GetRemoteSourceByName(tx, remoteSource.Name)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if u != nil {
|
2019-05-23 08:29:03 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource %q already exists", u.Name))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteSource.ID = uuid.NewV4().String()
|
|
|
|
|
|
|
|
rsj, err := json.Marshal(remoteSource)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to marshal remotesource: %w", err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
actions := []*datamanager.Action{
|
|
|
|
{
|
|
|
|
ActionType: datamanager.ActionTypePut,
|
|
|
|
DataType: string(types.ConfigTypeRemoteSource),
|
|
|
|
ID: remoteSource.ID,
|
|
|
|
Data: rsj,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
2019-05-03 10:47:22 +00:00
|
|
|
return remoteSource, err
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:29:03 +00:00
|
|
|
type UpdateRemoteSourceRequest struct {
|
|
|
|
RemoteSourceRef string
|
|
|
|
|
|
|
|
RemoteSource *types.RemoteSource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) UpdateRemoteSource(ctx context.Context, req *UpdateRemoteSourceRequest) (*types.RemoteSource, error) {
|
|
|
|
if err := h.ValidateRemoteSource(ctx, req.RemoteSource); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-06 12:40:31 +00:00
|
|
|
var curRemoteSource *types.RemoteSource
|
2019-05-23 08:29:03 +00:00
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-23 08:29:03 +00:00
|
|
|
var err error
|
2019-07-06 12:40:31 +00:00
|
|
|
|
|
|
|
// check remotesource exists
|
|
|
|
curRemoteSource, err = h.readDB.GetRemoteSourceByName(tx, req.RemoteSourceRef)
|
2019-05-23 08:29:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if curRemoteSource == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource with ref %q doesn't exist", req.RemoteSourceRef))
|
|
|
|
}
|
|
|
|
|
2019-07-06 12:40:31 +00:00
|
|
|
if curRemoteSource.Name != req.RemoteSource.Name {
|
|
|
|
// check duplicate remoteSource name
|
|
|
|
u, err := h.readDB.GetRemoteSourceByName(tx, req.RemoteSource.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if u != nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource %q already exists", u.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set/override ID that must be kept from the current remote source
|
|
|
|
req.RemoteSource.ID = curRemoteSource.ID
|
|
|
|
|
2019-05-23 08:29:03 +00:00
|
|
|
// changegroup is the remotesource id and also name since we could change the
|
|
|
|
// name so concurrently updating on the new name
|
|
|
|
cgNames := []string{util.EncodeSha256Hex("remotesourcename-" + req.RemoteSource.Name), util.EncodeSha256Hex("remotesourceid-" + req.RemoteSource.ID)}
|
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rsj, err := json.Marshal(req.RemoteSource)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to marshal remotesource: %w", err)
|
2019-05-23 08:29:03 +00:00
|
|
|
}
|
|
|
|
actions := []*datamanager.Action{
|
|
|
|
{
|
|
|
|
ActionType: datamanager.ActionTypePut,
|
|
|
|
DataType: string(types.ConfigTypeRemoteSource),
|
|
|
|
ID: req.RemoteSource.ID,
|
|
|
|
Data: rsj,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
|
|
|
return req.RemoteSource, err
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func (h *ActionHandler) DeleteRemoteSource(ctx context.Context, remoteSourceName string) error {
|
2019-05-03 10:47:22 +00:00
|
|
|
var remoteSource *types.RemoteSource
|
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-03 10:47:22 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// check remoteSource existance
|
2019-05-03 21:35:25 +00:00
|
|
|
remoteSource, err = h.readDB.GetRemoteSourceByName(tx, remoteSourceName)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if remoteSource == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("remotesource %q doesn't exist", remoteSourceName))
|
|
|
|
}
|
2019-05-09 22:02:37 +00:00
|
|
|
|
|
|
|
// changegroup is the remotesource id
|
|
|
|
cgNames := []string{util.EncodeSha256Hex("remotesourceid-" + remoteSource.ID)}
|
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-03 10:47:22 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
actions := []*datamanager.Action{
|
|
|
|
{
|
|
|
|
ActionType: datamanager.ActionTypeDelete,
|
|
|
|
DataType: string(types.ConfigTypeRemoteSource),
|
|
|
|
ID: remoteSource.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:29:03 +00:00
|
|
|
// changegroup is all the remotesources
|
2019-05-03 21:35:25 +00:00
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
2019-05-03 10:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|