2019-04-09 12:53:00 +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:48:49 +00:00
|
|
|
package action
|
2019-04-09 12:53:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2022-02-24 10:18:29 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/common"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
cstypes "agola.io/agola/services/configstore/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func (h *ActionHandler) GetRemoteSource(ctx context.Context, rsRef string) (*cstypes.RemoteSource, error) {
|
2022-02-21 11:19:55 +00:00
|
|
|
rs, _, err := h.configstoreClient.GetRemoteSource(ctx, rsRef)
|
2019-05-05 12:54:16 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-05 12:54:16 +00:00
|
|
|
}
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetRemoteSourcesRequest struct {
|
|
|
|
Start string
|
|
|
|
Limit int
|
|
|
|
Asc bool
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func (h *ActionHandler) GetRemoteSources(ctx context.Context, req *GetRemoteSourcesRequest) ([]*cstypes.RemoteSource, error) {
|
2022-02-21 11:19:55 +00:00
|
|
|
remoteSources, _, err := h.configstoreClient.GetRemoteSources(ctx, req.Start, req.Limit, req.Asc)
|
2019-05-05 12:54:16 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-05 12:54:16 +00:00
|
|
|
}
|
|
|
|
return remoteSources, nil
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
type CreateRemoteSourceRequest struct {
|
2019-05-07 13:59:08 +00:00
|
|
|
Name string
|
|
|
|
APIURL string
|
2019-05-22 14:28:42 +00:00
|
|
|
SkipVerify bool
|
2019-05-07 13:59:08 +00:00
|
|
|
Type string
|
|
|
|
AuthType string
|
|
|
|
Oauth2ClientID string
|
|
|
|
Oauth2ClientSecret string
|
|
|
|
SSHHostKey string
|
|
|
|
SkipSSHHostKeyCheck bool
|
2019-07-05 11:33:29 +00:00
|
|
|
RegistrationEnabled *bool
|
|
|
|
LoginEnabled *bool
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func (h *ActionHandler) CreateRemoteSource(ctx context.Context, req *CreateRemoteSourceRequest) (*cstypes.RemoteSource, error) {
|
2022-02-24 10:18:29 +00:00
|
|
|
if !common.IsUserAdmin(ctx) {
|
2019-05-03 21:19:23 +00:00
|
|
|
return nil, errors.Errorf("user not admin")
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
if !util.ValidateName(req.Name) {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("invalid remotesource name %q", req.Name))
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Name == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("remotesource name required"))
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
if req.APIURL == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("remotesource api url required"))
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
if req.Type == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("remotesource type required"))
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
if req.AuthType == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("remotesource auth type required"))
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate if the remote source type supports the required auth type
|
2019-07-31 13:17:54 +00:00
|
|
|
if !cstypes.SourceSupportsAuthType(cstypes.RemoteSourceType(req.Type), cstypes.RemoteSourceAuthType(req.AuthType)) {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("remotesource type %q doesn't support auth type %q", req.Type, req.AuthType))
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
if req.AuthType == string(cstypes.RemoteSourceAuthTypeOauth2) {
|
2019-04-09 12:53:00 +00:00
|
|
|
if req.Oauth2ClientID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("remotesource oauth2 clientid required"))
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
if req.Oauth2ClientSecret == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("remotesource oauth2 client secret required"))
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
rs := &cstypes.RemoteSource{
|
2019-05-07 13:59:08 +00:00
|
|
|
Name: req.Name,
|
2019-07-31 13:17:54 +00:00
|
|
|
Type: cstypes.RemoteSourceType(req.Type),
|
|
|
|
AuthType: cstypes.RemoteSourceAuthType(req.AuthType),
|
2019-05-07 13:59:08 +00:00
|
|
|
APIURL: req.APIURL,
|
2019-05-22 14:28:42 +00:00
|
|
|
SkipVerify: req.SkipVerify,
|
2019-05-07 13:59:08 +00:00
|
|
|
Oauth2ClientID: req.Oauth2ClientID,
|
|
|
|
Oauth2ClientSecret: req.Oauth2ClientSecret,
|
|
|
|
SSHHostKey: req.SSHHostKey,
|
|
|
|
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
|
2019-07-05 11:33:29 +00:00
|
|
|
RegistrationEnabled: req.RegistrationEnabled,
|
|
|
|
LoginEnabled: req.LoginEnabled,
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("creating remotesource")
|
2022-02-21 11:19:55 +00:00
|
|
|
rs, _, err := h.configstoreClient.CreateRemoteSource(ctx, rs)
|
2019-04-09 12:53:00 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to create remotesource")
|
2019-04-09 12:53:00 +00:00
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("remotesource %s created, ID: %s", rs.Name, rs.ID)
|
2019-04-09 12:53:00 +00:00
|
|
|
|
|
|
|
return rs, nil
|
|
|
|
}
|
2019-05-05 12:54:16 +00:00
|
|
|
|
2019-05-23 08:29:03 +00:00
|
|
|
type UpdateRemoteSourceRequest struct {
|
|
|
|
RemoteSourceRef string
|
|
|
|
|
|
|
|
Name *string
|
|
|
|
APIURL *string
|
|
|
|
SkipVerify *bool
|
|
|
|
Oauth2ClientID *string
|
|
|
|
Oauth2ClientSecret *string
|
|
|
|
SSHHostKey *string
|
|
|
|
SkipSSHHostKeyCheck *bool
|
2019-07-05 11:33:29 +00:00
|
|
|
RegistrationEnabled *bool
|
|
|
|
LoginEnabled *bool
|
2019-05-23 08:29:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:17:54 +00:00
|
|
|
func (h *ActionHandler) UpdateRemoteSource(ctx context.Context, req *UpdateRemoteSourceRequest) (*cstypes.RemoteSource, error) {
|
2022-02-24 10:18:29 +00:00
|
|
|
if !common.IsUserAdmin(ctx) {
|
2019-05-23 08:29:03 +00:00
|
|
|
return nil, errors.Errorf("user not admin")
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
rs, _, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceRef)
|
2019-05-23 08:29:03 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-23 08:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Name != nil {
|
|
|
|
rs.Name = *req.Name
|
|
|
|
}
|
|
|
|
if req.APIURL != nil {
|
|
|
|
rs.APIURL = *req.APIURL
|
|
|
|
}
|
|
|
|
if req.SkipVerify != nil {
|
|
|
|
rs.SkipVerify = *req.SkipVerify
|
|
|
|
}
|
|
|
|
if req.Oauth2ClientID != nil {
|
|
|
|
rs.Oauth2ClientID = *req.Oauth2ClientID
|
|
|
|
}
|
|
|
|
if req.Oauth2ClientSecret != nil {
|
|
|
|
rs.Oauth2ClientSecret = *req.Oauth2ClientSecret
|
|
|
|
}
|
|
|
|
if req.SSHHostKey != nil {
|
|
|
|
rs.SSHHostKey = *req.SSHHostKey
|
|
|
|
}
|
|
|
|
if req.SkipSSHHostKeyCheck != nil {
|
|
|
|
rs.SkipSSHHostKeyCheck = *req.SkipSSHHostKeyCheck
|
|
|
|
}
|
2019-07-05 11:33:29 +00:00
|
|
|
if req.RegistrationEnabled != nil {
|
|
|
|
rs.RegistrationEnabled = req.RegistrationEnabled
|
|
|
|
}
|
|
|
|
if req.LoginEnabled != nil {
|
|
|
|
rs.LoginEnabled = req.LoginEnabled
|
|
|
|
}
|
2019-05-23 08:29:03 +00:00
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("updating remotesource")
|
2022-02-21 11:19:55 +00:00
|
|
|
rs, _, err = h.configstoreClient.UpdateRemoteSource(ctx, req.RemoteSourceRef, rs)
|
2019-05-23 08:29:03 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to update remotesource")
|
2019-05-23 08:29:03 +00:00
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Info().Msgf("remotesource %s updated", rs.Name)
|
2019-05-23 08:29:03 +00:00
|
|
|
|
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:54:16 +00:00
|
|
|
func (h *ActionHandler) DeleteRemoteSource(ctx context.Context, rsRef string) error {
|
2022-02-24 10:18:29 +00:00
|
|
|
if !common.IsUserAdmin(ctx) {
|
2019-05-03 21:19:23 +00:00
|
|
|
return errors.Errorf("user not admin")
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if _, err := h.configstoreClient.DeleteRemoteSource(ctx, rsRef); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "failed to delete remote source")
|
2019-05-05 12:54:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|