2019-02-21 16:58:25 +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-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-05-03 12:24:18 +00:00
|
|
|
"net/http"
|
2019-04-30 10:13:51 +00:00
|
|
|
"net/url"
|
2019-02-21 16:58:25 +00:00
|
|
|
"path"
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
|
2019-02-21 16:58:25 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
|
|
|
"github.com/sorintlab/agola/internal/util"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
func (h *ActionHandler) GetProject(ctx context.Context, projectRef string) (*csapi.Project, error) {
|
|
|
|
project, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
2019-05-03 21:19:23 +00:00
|
|
|
|
|
|
|
isProjectMember, err := h.IsProjectMember(ctx, project.OwnerType, project.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to determine ownership")
|
|
|
|
}
|
|
|
|
if project.GlobalVisibility == types.VisibilityPublic {
|
|
|
|
return project, nil
|
|
|
|
}
|
|
|
|
if !isProjectMember {
|
|
|
|
return nil, util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
return project, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type CreateProjectRequest struct {
|
2019-04-30 15:09:26 +00:00
|
|
|
CurrentUserID string
|
2019-02-21 16:58:25 +00:00
|
|
|
Name string
|
2019-05-05 15:19:23 +00:00
|
|
|
ParentRef string
|
2019-04-30 15:09:26 +00:00
|
|
|
Visibility types.Visibility
|
2019-02-21 16:58:25 +00:00
|
|
|
RemoteSourceName string
|
2019-04-03 09:07:54 +00:00
|
|
|
RepoPath string
|
2019-02-21 16:58:25 +00:00
|
|
|
SkipSSHHostKeyCheck bool
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectRequest) (*csapi.Project, error) {
|
2019-05-03 21:19:23 +00:00
|
|
|
user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))
|
|
|
|
}
|
|
|
|
parentRef := req.ParentRef
|
|
|
|
if parentRef == "" {
|
|
|
|
// create project in current user namespace
|
|
|
|
parentRef = path.Join("user", user.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, parentRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project group %q", parentRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to determine ownership")
|
|
|
|
}
|
|
|
|
if !isProjectOwner {
|
|
|
|
return nil, util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
if !util.ValidateName(req.Name) {
|
2019-04-08 10:08:31 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid project name %q", req.Name))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-05-03 07:55:37 +00:00
|
|
|
if req.RemoteSourceName == "" {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("empty remote source name"))
|
|
|
|
}
|
|
|
|
if req.RepoPath == "" {
|
2019-05-03 10:41:49 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("empty remote repo path"))
|
2019-05-03 07:55:37 +00:00
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-05-03 12:24:18 +00:00
|
|
|
projectPath := path.Join(pg.Path, req.Name)
|
2019-05-03 21:48:49 +00:00
|
|
|
_, resp, err = h.configstoreClient.GetProject(ctx, projectPath)
|
2019-05-03 12:24:18 +00:00
|
|
|
if err != nil {
|
|
|
|
if resp != nil && resp.StatusCode != http.StatusNotFound {
|
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", req.Name))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("project %q already exists", projectPath))
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("rs: %s", util.Dump(rs))
|
2019-02-21 16:58:25 +00:00
|
|
|
var la *types.LinkedAccount
|
|
|
|
for _, v := range user.LinkedAccounts {
|
|
|
|
if v.RemoteSourceID == rs.ID {
|
|
|
|
la = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("la: %s", util.Dump(la))
|
2019-02-21 16:58:25 +00:00
|
|
|
if la == nil {
|
|
|
|
return nil, errors.Errorf("user doesn't have a linked account for remote source %q", rs.Name)
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
gitsource, err := h.GetGitSource(ctx, rs, user.Name, la)
|
2019-04-03 09:07:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to create gitsource client")
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
repo, err := gitsource.GetRepoInfo(req.RepoPath)
|
2019-04-03 09:07:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to get repository info from gitsource")
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("generating ssh key pairs")
|
2019-04-03 09:07:54 +00:00
|
|
|
privateKey, _, err := util.GenSSHKeyPair(4096)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to generate ssh key pair")
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
p := &types.Project{
|
2019-03-14 13:36:18 +00:00
|
|
|
Name: req.Name,
|
|
|
|
Parent: types.Parent{
|
|
|
|
Type: types.ConfigTypeProjectGroup,
|
2019-05-05 15:19:23 +00:00
|
|
|
ID: parentRef,
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-05-03 10:21:44 +00:00
|
|
|
Visibility: req.Visibility,
|
|
|
|
RemoteRepositoryConfigType: types.RemoteRepositoryConfigTypeRemoteSource,
|
|
|
|
RemoteSourceID: rs.ID,
|
|
|
|
LinkedAccountID: la.ID,
|
|
|
|
RepositoryID: repo.ID,
|
|
|
|
RepositoryPath: req.RepoPath,
|
|
|
|
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
|
|
|
|
SSHPrivateKey: string(privateKey),
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("creating project")
|
|
|
|
rp, resp, err := h.configstoreClient.CreateProject(ctx, p)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create project"))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("project %s created, ID: %s", p.Name, p.ID)
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
return rp, h.SetupProject(ctx, rs, user, la, rp)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func (h *ActionHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, user *types.User, la *types.LinkedAccount, project *csapi.Project) error {
|
|
|
|
gitsource, err := h.GetGitSource(ctx, rs, user.Name, la)
|
2019-04-03 09:07:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create gitsource client")
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-04-11 15:11:17 +00:00
|
|
|
pubKey, err := util.ExtractPublicKey([]byte(project.SSHPrivateKey))
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-03 09:07:54 +00:00
|
|
|
return errors.Wrapf(err, "failed to extract public key")
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
webhookURL, err := url.Parse(fmt.Sprintf("%s/webhooks", h.apiExposedURL))
|
2019-04-30 10:13:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to generate webhook url")
|
|
|
|
}
|
|
|
|
q := url.Values{}
|
|
|
|
q.Add("projectid", project.ID)
|
2019-05-03 21:48:49 +00:00
|
|
|
q.Add("agolaid", h.agolaID)
|
2019-04-30 10:13:51 +00:00
|
|
|
webhookURL.RawQuery = q.Encode()
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-02-28 16:19:53 +00:00
|
|
|
// generate deploy keys and webhooks containing the agola project id so we
|
|
|
|
// can have multiple projects referencing the same remote repository and this
|
|
|
|
// will trigger multiple different runs
|
2019-04-11 15:11:17 +00:00
|
|
|
deployKeyName := fmt.Sprintf("agola deploy key - %s", project.ID)
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("creating/updating deploy key: %s", string(pubKey))
|
2019-04-11 15:11:17 +00:00
|
|
|
if err := gitsource.UpdateDeployKey(project.RepositoryPath, deployKeyName, string(pubKey), true); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
return errors.Wrapf(err, "failed to create deploy key")
|
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("deleting existing webhooks")
|
2019-04-30 10:13:51 +00:00
|
|
|
if err := gitsource.DeleteRepoWebhook(project.RepositoryPath, webhookURL.String()); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
return errors.Wrapf(err, "failed to delete repository webhook")
|
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("creating webhook to url: %s", webhookURL)
|
2019-04-30 10:13:51 +00:00
|
|
|
if err := gitsource.CreateRepoWebhook(project.RepositoryPath, webhookURL.String(), ""); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
return errors.Wrapf(err, "failed to create repository webhook")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string) error {
|
|
|
|
p, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", projectRef))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:19:23 +00:00
|
|
|
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to determine ownership")
|
|
|
|
}
|
|
|
|
if !isProjectOwner {
|
|
|
|
return util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
user, resp, err := h.configstoreClient.GetUserByLinkedAccount(ctx, p.LinkedAccountID)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user with linked account id %q", p.LinkedAccountID))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
la := user.LinkedAccounts[p.LinkedAccountID]
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("la: %s", util.Dump(la))
|
2019-02-21 16:58:25 +00:00
|
|
|
if la == nil {
|
2019-04-30 10:56:43 +00:00
|
|
|
return errors.Errorf("linked account %q in user %q doesn't exist", p.LinkedAccountID, user.Name)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 15:11:17 +00:00
|
|
|
// TODO(sgotti) update project repo path if the remote let us query by repository id
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
return h.SetupProject(ctx, rs, user, la, p)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-05-05 12:27:22 +00:00
|
|
|
|
|
|
|
func (h *ActionHandler) DeleteProject(ctx context.Context, projectRef string) error {
|
2019-05-03 21:19:23 +00:00
|
|
|
p, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
|
|
|
|
if err != nil {
|
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", projectRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to determine ownership")
|
|
|
|
}
|
|
|
|
if !isProjectOwner {
|
|
|
|
return util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = h.configstoreClient.DeleteProject(ctx, projectRef)
|
2019-05-05 12:27:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|