gateway: rename command(handler) to action(handler)

Since we're going to migrate all actions (also queries that now are implemented
in the api handlers) there
This commit is contained in:
Simone Gotti 2019-05-03 23:48:49 +02:00
parent ca5b5f3a7e
commit 3f7e554f04
15 changed files with 220 additions and 220 deletions

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package command package action
import ( import (
"net/http" "net/http"
@ -25,7 +25,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
type CommandHandler struct { type ActionHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
sd *common.TokenSigningData sd *common.TokenSigningData
configstoreClient *csapi.Client configstoreClient *csapi.Client
@ -34,8 +34,8 @@ type CommandHandler struct {
webExposedURL string webExposedURL string
} }
func NewCommandHandler(logger *zap.Logger, sd *common.TokenSigningData, configstoreClient *csapi.Client, agolaID, apiExposedURL, webExposedURL string) *CommandHandler { func NewActionHandler(logger *zap.Logger, sd *common.TokenSigningData, configstoreClient *csapi.Client, agolaID, apiExposedURL, webExposedURL string) *ActionHandler {
return &CommandHandler{ return &ActionHandler{
log: logger.Sugar(), log: logger.Sugar(),
sd: sd, sd: sd,
configstoreClient: configstoreClient, configstoreClient: configstoreClient,

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package command package action
import ( import (
"context" "context"
@ -29,7 +29,7 @@ type CreateOrgRequest struct {
CreatorUserID string CreatorUserID string
} }
func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) { func (h *ActionHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) {
if req.Name == "" { if req.Name == "" {
return nil, util.NewErrBadRequest(errors.Errorf("organization name required")) return nil, util.NewErrBadRequest(errors.Errorf("organization name required"))
} }
@ -44,12 +44,12 @@ func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (
org.CreatorUserID = req.CreatorUserID org.CreatorUserID = req.CreatorUserID
} }
c.log.Infof("creating organization") h.log.Infof("creating organization")
org, resp, err := c.configstoreClient.CreateOrg(ctx, org) org, resp, err := h.configstoreClient.CreateOrg(ctx, org)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create organization")) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create organization"))
} }
c.log.Infof("organization %s created, ID: %s", org.Name, org.ID) h.log.Infof("organization %s created, ID: %s", org.Name, org.ID)
return org, nil return org, nil
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package command package action
import ( import (
"context" "context"
@ -38,7 +38,7 @@ type CreateProjectRequest struct {
SkipSSHHostKeyCheck bool SkipSSHHostKeyCheck bool
} }
func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRequest) (*csapi.Project, error) { func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectRequest) (*csapi.Project, error) {
if !util.ValidateName(req.Name) { if !util.ValidateName(req.Name) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid project name %q", req.Name)) return nil, util.NewErrBadRequest(errors.Errorf("invalid project name %q", req.Name))
} }
@ -49,13 +49,13 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
return nil, util.NewErrBadRequest(errors.Errorf("empty remote repo path")) return nil, util.NewErrBadRequest(errors.Errorf("empty remote repo path"))
} }
pg, resp, err := c.configstoreClient.GetProjectGroup(ctx, req.ParentID) pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, req.ParentID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project group %q", req.Name)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project group %q", req.Name))
} }
projectPath := path.Join(pg.Path, req.Name) projectPath := path.Join(pg.Path, req.Name)
_, resp, err = c.configstoreClient.GetProject(ctx, projectPath) _, resp, err = h.configstoreClient.GetProject(ctx, projectPath)
if err != nil { if err != nil {
if resp != nil && resp.StatusCode != http.StatusNotFound { if resp != nil && resp.StatusCode != http.StatusNotFound {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", req.Name)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", req.Name))
@ -64,16 +64,16 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
return nil, util.NewErrBadRequest(errors.Errorf("project %q already exists", projectPath)) return nil, util.NewErrBadRequest(errors.Errorf("project %q already exists", projectPath))
} }
user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID) user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))
} }
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
} }
c.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
var la *types.LinkedAccount var la *types.LinkedAccount
for _, v := range user.LinkedAccounts { for _, v := range user.LinkedAccounts {
if v.RemoteSourceID == rs.ID { if v.RemoteSourceID == rs.ID {
@ -81,12 +81,12 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
break break
} }
} }
c.log.Infof("la: %s", util.Dump(la)) h.log.Infof("la: %s", util.Dump(la))
if la == nil { if la == nil {
return nil, errors.Errorf("user doesn't have a linked account for remote source %q", rs.Name) return nil, errors.Errorf("user doesn't have a linked account for remote source %q", rs.Name)
} }
gitsource, err := c.GetGitSource(ctx, rs, user.Name, la) gitsource, err := h.GetGitSource(ctx, rs, user.Name, la)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to create gitsource client") return nil, errors.Wrapf(err, "failed to create gitsource client")
} }
@ -96,7 +96,7 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
return nil, errors.Wrapf(err, "failed to get repository info from gitsource") return nil, errors.Wrapf(err, "failed to get repository info from gitsource")
} }
c.log.Infof("generating ssh key pairs") h.log.Infof("generating ssh key pairs")
privateKey, _, err := util.GenSSHKeyPair(4096) privateKey, _, err := util.GenSSHKeyPair(4096)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to generate ssh key pair") return nil, errors.Wrapf(err, "failed to generate ssh key pair")
@ -124,18 +124,18 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
SSHPrivateKey: string(privateKey), SSHPrivateKey: string(privateKey),
} }
c.log.Infof("creating project") h.log.Infof("creating project")
rp, resp, err := c.configstoreClient.CreateProject(ctx, p) rp, resp, err := h.configstoreClient.CreateProject(ctx, p)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create project")) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create project"))
} }
c.log.Infof("project %s created, ID: %s", p.Name, p.ID) h.log.Infof("project %s created, ID: %s", p.Name, p.ID)
return rp, c.SetupProject(ctx, rs, user, la, rp) return rp, h.SetupProject(ctx, rs, user, la, rp)
} }
func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, user *types.User, la *types.LinkedAccount, project *csapi.Project) error { func (h *ActionHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, user *types.User, la *types.LinkedAccount, project *csapi.Project) error {
gitsource, err := c.GetGitSource(ctx, rs, user.Name, la) gitsource, err := h.GetGitSource(ctx, rs, user.Name, la)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to create gitsource client") return errors.Wrapf(err, "failed to create gitsource client")
} }
@ -145,28 +145,28 @@ func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSourc
return errors.Wrapf(err, "failed to extract public key") return errors.Wrapf(err, "failed to extract public key")
} }
webhookURL, err := url.Parse(fmt.Sprintf("%s/webhooks", c.apiExposedURL)) webhookURL, err := url.Parse(fmt.Sprintf("%s/webhooks", h.apiExposedURL))
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to generate webhook url") return errors.Wrapf(err, "failed to generate webhook url")
} }
q := url.Values{} q := url.Values{}
q.Add("projectid", project.ID) q.Add("projectid", project.ID)
q.Add("agolaid", c.agolaID) q.Add("agolaid", h.agolaID)
webhookURL.RawQuery = q.Encode() webhookURL.RawQuery = q.Encode()
// generate deploy keys and webhooks containing the agola project id so we // generate deploy keys and webhooks containing the agola project id so we
// can have multiple projects referencing the same remote repository and this // can have multiple projects referencing the same remote repository and this
// will trigger multiple different runs // will trigger multiple different runs
deployKeyName := fmt.Sprintf("agola deploy key - %s", project.ID) deployKeyName := fmt.Sprintf("agola deploy key - %s", project.ID)
c.log.Infof("creating/updating deploy key: %s", string(pubKey)) h.log.Infof("creating/updating deploy key: %s", string(pubKey))
if err := gitsource.UpdateDeployKey(project.RepositoryPath, deployKeyName, string(pubKey), true); err != nil { if err := gitsource.UpdateDeployKey(project.RepositoryPath, deployKeyName, string(pubKey), true); err != nil {
return errors.Wrapf(err, "failed to create deploy key") return errors.Wrapf(err, "failed to create deploy key")
} }
c.log.Infof("deleting existing webhooks") h.log.Infof("deleting existing webhooks")
if err := gitsource.DeleteRepoWebhook(project.RepositoryPath, webhookURL.String()); err != nil { if err := gitsource.DeleteRepoWebhook(project.RepositoryPath, webhookURL.String()); err != nil {
return errors.Wrapf(err, "failed to delete repository webhook") return errors.Wrapf(err, "failed to delete repository webhook")
} }
c.log.Infof("creating webhook to url: %s", webhookURL) h.log.Infof("creating webhook to url: %s", webhookURL)
if err := gitsource.CreateRepoWebhook(project.RepositoryPath, webhookURL.String(), ""); err != nil { if err := gitsource.CreateRepoWebhook(project.RepositoryPath, webhookURL.String(), ""); err != nil {
return errors.Wrapf(err, "failed to create repository webhook") return errors.Wrapf(err, "failed to create repository webhook")
} }
@ -174,29 +174,29 @@ func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSourc
return nil return nil
} }
func (c *CommandHandler) ReconfigProject(ctx context.Context, projectRef string) error { func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string) error {
p, resp, err := c.configstoreClient.GetProject(ctx, projectRef) p, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", projectRef)) return ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", projectRef))
} }
user, resp, err := c.configstoreClient.GetUserByLinkedAccount(ctx, p.LinkedAccountID) user, resp, err := h.configstoreClient.GetUserByLinkedAccount(ctx, p.LinkedAccountID)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user with linked account id %q", p.LinkedAccountID)) return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user with linked account id %q", p.LinkedAccountID))
} }
la := user.LinkedAccounts[p.LinkedAccountID] la := user.LinkedAccounts[p.LinkedAccountID]
c.log.Infof("la: %s", util.Dump(la)) h.log.Infof("la: %s", util.Dump(la))
if la == nil { if la == nil {
return errors.Errorf("linked account %q in user %q doesn't exist", p.LinkedAccountID, user.Name) return errors.Errorf("linked account %q in user %q doesn't exist", p.LinkedAccountID, user.Name)
} }
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID)) return ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID))
} }
// TODO(sgotti) update project repo path if the remote let us query by repository id // TODO(sgotti) update project repo path if the remote let us query by repository id
return c.SetupProject(ctx, rs, user, la, p) return h.SetupProject(ctx, rs, user, la, p)
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package command package action
import ( import (
"context" "context"
@ -32,12 +32,12 @@ type CreateProjectGroupRequest struct {
Visibility types.Visibility Visibility types.Visibility
} }
func (c *CommandHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*csapi.ProjectGroup, error) { func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*csapi.ProjectGroup, error) {
if !util.ValidateName(req.Name) { if !util.ValidateName(req.Name) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid projectGroup name %q", req.Name)) return nil, util.NewErrBadRequest(errors.Errorf("invalid projectGroup name %q", req.Name))
} }
user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID) user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))
} }
@ -57,12 +57,12 @@ func (c *CommandHandler) CreateProjectGroup(ctx context.Context, req *CreateProj
Visibility: req.Visibility, Visibility: req.Visibility,
} }
c.log.Infof("creating projectGroup") h.log.Infof("creating projectGroup")
rp, resp, err := c.configstoreClient.CreateProjectGroup(ctx, p) rp, resp, err := h.configstoreClient.CreateProjectGroup(ctx, p)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create projectGroup")) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create projectGroup"))
} }
c.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID) h.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID)
return rp, nil return rp, nil
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package command package action
import ( import (
"context" "context"
@ -32,7 +32,7 @@ type CreateRemoteSourceRequest struct {
Oauth2ClientSecret string Oauth2ClientSecret string
} }
func (c *CommandHandler) CreateRemoteSource(ctx context.Context, req *CreateRemoteSourceRequest) (*types.RemoteSource, error) { func (h *ActionHandler) CreateRemoteSource(ctx context.Context, req *CreateRemoteSourceRequest) (*types.RemoteSource, error) {
if !util.ValidateName(req.Name) { if !util.ValidateName(req.Name) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid remotesource name %q", req.Name)) return nil, util.NewErrBadRequest(errors.Errorf("invalid remotesource name %q", req.Name))
} }
@ -73,12 +73,12 @@ func (c *CommandHandler) CreateRemoteSource(ctx context.Context, req *CreateRemo
Oauth2ClientSecret: req.Oauth2ClientSecret, Oauth2ClientSecret: req.Oauth2ClientSecret,
} }
c.log.Infof("creating remotesource") h.log.Infof("creating remotesource")
rs, resp, err := c.configstoreClient.CreateRemoteSource(ctx, rs) rs, resp, err := h.configstoreClient.CreateRemoteSource(ctx, rs)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create remotesource")) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create remotesource"))
} }
c.log.Infof("remotesource %s created, ID: %s", rs.Name, rs.ID) h.log.Infof("remotesource %s created, ID: %s", rs.Name, rs.ID)
return rs, nil return rs, nil
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package command package action
import ( import (
"context" "context"
@ -44,7 +44,7 @@ type CreateUserRequest struct {
UserName string UserName string
} }
func (c *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest) (*types.User, error) { func (h *ActionHandler) CreateUser(ctx context.Context, req *CreateUserRequest) (*types.User, error) {
if req.UserName == "" { if req.UserName == "" {
return nil, util.NewErrBadRequest(errors.Errorf("user name required")) return nil, util.NewErrBadRequest(errors.Errorf("user name required"))
} }
@ -56,12 +56,12 @@ func (c *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest)
UserName: req.UserName, UserName: req.UserName,
} }
c.log.Infof("creating user") h.log.Infof("creating user")
u, resp, err := c.configstoreClient.CreateUser(ctx, creq) u, resp, err := h.configstoreClient.CreateUser(ctx, creq)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create user")) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create user"))
} }
c.log.Infof("user %s created, ID: %s", u.Name, u.ID) h.log.Infof("user %s created, ID: %s", u.Name, u.ID)
return u, nil return u, nil
} }
@ -71,7 +71,7 @@ type CreateUserTokenRequest struct {
TokenName string TokenName string
} }
func (c *CommandHandler) CreateUserToken(ctx context.Context, req *CreateUserTokenRequest) (string, error) { func (h *ActionHandler) CreateUserToken(ctx context.Context, req *CreateUserTokenRequest) (string, error) {
var userID string var userID string
userIDVal := ctx.Value("userid") userIDVal := ctx.Value("userid")
if userIDVal != nil { if userIDVal != nil {
@ -85,7 +85,7 @@ func (c *CommandHandler) CreateUserToken(ctx context.Context, req *CreateUserTok
} }
userRef := req.UserRef userRef := req.UserRef
user, resp, err := c.configstoreClient.GetUser(ctx, userRef) user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil { if err != nil {
return "", ErrFromRemote(resp, errors.Wrapf(err, "failed to get user")) return "", ErrFromRemote(resp, errors.Wrapf(err, "failed to get user"))
} }
@ -98,15 +98,15 @@ func (c *CommandHandler) CreateUserToken(ctx context.Context, req *CreateUserTok
return "", util.NewErrBadRequest(errors.Errorf("user %q already have a token with name %q", userRef, req.TokenName)) return "", util.NewErrBadRequest(errors.Errorf("user %q already have a token with name %q", userRef, req.TokenName))
} }
c.log.Infof("creating user token") h.log.Infof("creating user token")
creq := &csapi.CreateUserTokenRequest{ creq := &csapi.CreateUserTokenRequest{
TokenName: req.TokenName, TokenName: req.TokenName,
} }
res, resp, err := c.configstoreClient.CreateUserToken(ctx, userRef, creq) res, resp, err := h.configstoreClient.CreateUserToken(ctx, userRef, creq)
if err != nil { if err != nil {
return "", ErrFromRemote(resp, errors.Wrapf(err, "failed to create user token")) return "", ErrFromRemote(resp, errors.Wrapf(err, "failed to create user token"))
} }
c.log.Infof("token %q for user %q created", req.TokenName, userRef) h.log.Infof("token %q for user %q created", req.TokenName, userRef)
return res.Token, nil return res.Token, nil
} }
@ -121,17 +121,17 @@ type CreateUserLARequest struct {
Oauth2AccessTokenExpiresAt time.Time Oauth2AccessTokenExpiresAt time.Time
} }
func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) { func (h *ActionHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) {
userRef := req.UserRef userRef := req.UserRef
user, resp, err := c.configstoreClient.GetUser(ctx, userRef) user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", userRef)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", userRef))
} }
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
} }
c.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
var la *types.LinkedAccount var la *types.LinkedAccount
for _, v := range user.LinkedAccounts { for _, v := range user.LinkedAccounts {
if v.RemoteSourceID == rs.ID { if v.RemoteSourceID == rs.ID {
@ -139,7 +139,7 @@ func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequ
break break
} }
} }
c.log.Infof("la: %s", util.Dump(la)) h.log.Infof("la: %s", util.Dump(la))
if la != nil { if la != nil {
return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", userRef, rs.Name)) return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", userRef, rs.Name))
} }
@ -171,18 +171,18 @@ func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequ
Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt, Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt,
} }
c.log.Infof("creating linked account") h.log.Infof("creating linked account")
la, resp, err = c.configstoreClient.CreateUserLA(ctx, userRef, creq) la, resp, err = h.configstoreClient.CreateUserLA(ctx, userRef, creq)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create linked account")) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create linked account"))
} }
c.log.Infof("linked account %q for user %q created", la.ID, userRef) h.log.Infof("linked account %q for user %q created", la.ID, userRef)
return la, nil return la, nil
} }
func (c *CommandHandler) UpdateUserLA(ctx context.Context, userRef string, la *types.LinkedAccount) error { func (h *ActionHandler) UpdateUserLA(ctx context.Context, userRef string, la *types.LinkedAccount) error {
user, resp, err := c.configstoreClient.GetUser(ctx, userRef) user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", userRef)) return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", userRef))
} }
@ -193,7 +193,7 @@ func (c *CommandHandler) UpdateUserLA(ctx context.Context, userRef string, la *t
break break
} }
} }
c.log.Infof("la: %s", util.Dump(la)) h.log.Infof("la: %s", util.Dump(la))
if !laFound { if !laFound {
return util.NewErrBadRequest(errors.Errorf("user %q doesn't have a linked account with id %q", userRef, la.ID)) return util.NewErrBadRequest(errors.Errorf("user %q doesn't have a linked account with id %q", userRef, la.ID))
} }
@ -207,18 +207,18 @@ func (c *CommandHandler) UpdateUserLA(ctx context.Context, userRef string, la *t
Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt, Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt,
} }
c.log.Infof("updating user %q linked account", userRef) h.log.Infof("updating user %q linked account", userRef)
la, resp, err = c.configstoreClient.UpdateUserLA(ctx, userRef, la.ID, creq) la, resp, err = h.configstoreClient.UpdateUserLA(ctx, userRef, la.ID, creq)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Wrapf(err, "failed to update user")) return ErrFromRemote(resp, errors.Wrapf(err, "failed to update user"))
} }
c.log.Infof("linked account %q for user %q updated", la.ID, userRef) h.log.Infof("linked account %q for user %q updated", la.ID, userRef)
return nil return nil
} }
// RefreshLinkedAccount refreshed the linked account oauth2 access token and update linked account in the configstore // RefreshLinkedAccount refreshed the linked account oauth2 access token and update linked account in the configstore
func (c *CommandHandler) RefreshLinkedAccount(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (*types.LinkedAccount, error) { func (h *ActionHandler) RefreshLinkedAccount(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (*types.LinkedAccount, error) {
switch rs.AuthType { switch rs.AuthType {
case types.RemoteSourceAuthTypeOauth2: case types.RemoteSourceAuthTypeOauth2:
// refresh access token if expired // refresh access token if expired
@ -237,7 +237,7 @@ func (c *CommandHandler) RefreshLinkedAccount(ctx context.Context, rs *types.Rem
la.Oauth2RefreshToken = token.RefreshToken la.Oauth2RefreshToken = token.RefreshToken
la.Oauth2AccessTokenExpiresAt = token.Expiry la.Oauth2AccessTokenExpiresAt = token.Expiry
if err := c.UpdateUserLA(ctx, userName, la); err != nil { if err := h.UpdateUserLA(ctx, userName, la); err != nil {
return nil, errors.Wrapf(err, "failed to update linked account") return nil, errors.Wrapf(err, "failed to update linked account")
} }
} }
@ -248,8 +248,8 @@ func (c *CommandHandler) RefreshLinkedAccount(ctx context.Context, rs *types.Rem
// GetGitSource is a wrapper around common.GetGitSource that will also refresh // GetGitSource is a wrapper around common.GetGitSource that will also refresh
// the oauth2 access token and update the linked account when needed // the oauth2 access token and update the linked account when needed
func (c *CommandHandler) GetGitSource(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (gitsource.GitSource, error) { func (h *ActionHandler) GetGitSource(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (gitsource.GitSource, error) {
la, err := c.RefreshLinkedAccount(ctx, rs, userName, la) la, err := h.RefreshLinkedAccount(ctx, rs, userName, la)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -265,7 +265,7 @@ type RegisterUserRequest struct {
Oauth2AccessTokenExpiresAt time.Time Oauth2AccessTokenExpiresAt time.Time
} }
func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequest) (*types.User, error) { func (h *ActionHandler) RegisterUser(ctx context.Context, req *RegisterUserRequest) (*types.User, error) {
if req.UserName == "" { if req.UserName == "" {
return nil, util.NewErrBadRequest(errors.Errorf("user name required")) return nil, util.NewErrBadRequest(errors.Errorf("user name required"))
} }
@ -273,11 +273,11 @@ func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequ
return nil, util.NewErrBadRequest(errors.Errorf("invalid user name %q", req.UserName)) return nil, util.NewErrBadRequest(errors.Errorf("invalid user name %q", req.UserName))
} }
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
} }
c.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken) accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken)
if err != nil { if err != nil {
@ -309,12 +309,12 @@ func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequ
}, },
} }
c.log.Infof("creating user account") h.log.Infof("creating user account")
u, resp, err := c.configstoreClient.CreateUser(ctx, creq) u, resp, err := h.configstoreClient.CreateUser(ctx, creq)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create linked account")) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create linked account"))
} }
c.log.Infof("user %q created", req.UserName) h.log.Infof("user %q created", req.UserName)
return u, nil return u, nil
} }
@ -332,12 +332,12 @@ type LoginUserResponse struct {
User *types.User User *types.User
} }
func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) { func (h *ActionHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) {
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
} }
c.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken) accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken)
if err != nil { if err != nil {
@ -356,7 +356,7 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (
return nil, errors.Errorf("empty remote user id for remote source %q", rs.ID) return nil, errors.Errorf("empty remote user id for remote source %q", rs.ID)
} }
user, resp, err := c.configstoreClient.GetUserByLinkedAccountRemoteUserAndSource(ctx, remoteUserInfo.ID, rs.ID) user, resp, err := h.configstoreClient.GetUserByLinkedAccountRemoteUserAndSource(ctx, remoteUserInfo.ID, rs.ID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user for remote user id %q and remote source %q", remoteUserInfo.ID, rs.ID)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user for remote user id %q and remote source %q", remoteUserInfo.ID, rs.ID))
} }
@ -368,7 +368,7 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (
break break
} }
} }
c.log.Infof("la: %s", util.Dump(la)) h.log.Infof("la: %s", util.Dump(la))
if la == nil { if la == nil {
return nil, errors.Errorf("linked account for user %q for remote source %q doesn't exist", user.Name, rs.Name) return nil, errors.Errorf("linked account for user %q for remote source %q doesn't exist", user.Name, rs.Name)
} }
@ -391,16 +391,16 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (
Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt, Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt,
} }
c.log.Infof("updating user %q linked account", user.Name) h.log.Infof("updating user %q linked account", user.Name)
la, resp, err = c.configstoreClient.UpdateUserLA(ctx, user.Name, la.ID, creq) la, resp, err = h.configstoreClient.UpdateUserLA(ctx, user.Name, la.ID, creq)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to update user")) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to update user"))
} }
c.log.Infof("linked account %q for user %q updated", la.ID, user.Name) h.log.Infof("linked account %q for user %q updated", la.ID, user.Name)
} }
// generate jwt token // generate jwt token
token, err := common.GenerateLoginJWTToken(c.sd, user.ID) token, err := common.GenerateLoginJWTToken(h.sd, user.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -423,12 +423,12 @@ type AuthorizeResponse struct {
RemoteSourceName string RemoteSourceName string
} }
func (c *CommandHandler) Authorize(ctx context.Context, req *AuthorizeRequest) (*AuthorizeResponse, error) { func (h *ActionHandler) Authorize(ctx context.Context, req *AuthorizeRequest) (*AuthorizeResponse, error) {
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
} }
c.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken) accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken)
if err != nil { if err != nil {
@ -458,17 +458,17 @@ type RemoteSourceAuthResponse struct {
Response interface{} Response interface{}
} }
func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourceName, loginName, loginPassword string, requestType RemoteSourceRequestType, req interface{}) (*RemoteSourceAuthResponse, error) { func (h *ActionHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourceName, loginName, loginPassword string, requestType RemoteSourceRequestType, req interface{}) (*RemoteSourceAuthResponse, error) {
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, remoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName))
} }
c.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
switch requestType { switch requestType {
case RemoteSourceRequestTypeCreateUserLA: case RemoteSourceRequestTypeCreateUserLA:
req := req.(*CreateUserLARequest) req := req.(*CreateUserLARequest)
user, resp, err := c.configstoreClient.GetUser(ctx, req.UserRef) user, resp, err := h.configstoreClient.GetUser(ctx, req.UserRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.UserRef)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.UserRef))
} }
@ -479,7 +479,7 @@ func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourc
break break
} }
} }
c.log.Infof("la: %s", util.Dump(la)) h.log.Infof("la: %s", util.Dump(la))
if la != nil { if la != nil {
return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", req.UserRef, rs.Name)) return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", req.UserRef, rs.Name))
} }
@ -500,15 +500,15 @@ func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourc
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to create git source") return nil, errors.Wrapf(err, "failed to create git source")
} }
token, err := common.GenerateJWTToken(c.sd, rs.Name, string(requestType), req) token, err := common.GenerateJWTToken(h.sd, rs.Name, string(requestType), req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
redirect, err := oauth2Source.GetOauth2AuthorizationURL(c.webExposedURL+"/oauth2/callback", token) redirect, err := oauth2Source.GetOauth2AuthorizationURL(h.webExposedURL+"/oauth2/callback", token)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c.log.Infof("oauth2 redirect: %s", redirect) h.log.Infof("oauth2 redirect: %s", redirect)
return &RemoteSourceAuthResponse{ return &RemoteSourceAuthResponse{
Oauth2Redirect: redirect, Oauth2Redirect: redirect,
@ -519,17 +519,17 @@ func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourc
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to create git source") return nil, errors.Wrapf(err, "failed to create git source")
} }
tokenName := "agola-" + c.agolaID tokenName := "agola-" + h.agolaID
accessToken, err := passwordSource.LoginPassword(loginName, loginPassword, tokenName) accessToken, err := passwordSource.LoginPassword(loginName, loginPassword, tokenName)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to login to remote source %q with login name %q", rs.Name, loginName) return nil, errors.Wrapf(err, "failed to login to remote source %q with login name %q", rs.Name, loginName)
} }
c.log.Infof("access token: %s", accessToken) h.log.Infof("access token: %s", accessToken)
requestj, err := json.Marshal(req) requestj, err := json.Marshal(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cres, err := c.HandleRemoteSourceAuthRequest(ctx, requestType, string(requestj), accessToken, "", "", time.Time{}) cres, err := h.HandleRemoteSourceAuthRequest(ctx, requestType, string(requestj), accessToken, "", "", time.Time{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -560,7 +560,7 @@ type CreateUserLAResponse struct {
LinkedAccount *types.LinkedAccount LinkedAccount *types.LinkedAccount
} }
func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requestType RemoteSourceRequestType, requestString string, userAccessToken, oauth2AccessToken, oauth2RefreshToken string, oauth2AccessTokenExpiresAt time.Time) (*RemoteSourceAuthResult, error) { func (h *ActionHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requestType RemoteSourceRequestType, requestString string, userAccessToken, oauth2AccessToken, oauth2RefreshToken string, oauth2AccessTokenExpiresAt time.Time) (*RemoteSourceAuthResult, error) {
switch requestType { switch requestType {
case RemoteSourceRequestTypeCreateUserLA: case RemoteSourceRequestTypeCreateUserLA:
var req *CreateUserLARequest var req *CreateUserLARequest
@ -576,7 +576,7 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
Oauth2RefreshToken: oauth2RefreshToken, Oauth2RefreshToken: oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt, Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
} }
la, err := c.CreateUserLA(ctx, creq) la, err := h.CreateUserLA(ctx, creq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -601,7 +601,7 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
Oauth2RefreshToken: oauth2RefreshToken, Oauth2RefreshToken: oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt, Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
} }
cresp, err := c.RegisterUser(ctx, creq) cresp, err := h.RegisterUser(ctx, creq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -623,7 +623,7 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
Oauth2RefreshToken: oauth2RefreshToken, Oauth2RefreshToken: oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt, Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
} }
cresp, err := c.LoginUser(ctx, creq) cresp, err := h.LoginUser(ctx, creq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -645,7 +645,7 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
Oauth2RefreshToken: oauth2RefreshToken, Oauth2RefreshToken: oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt, Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
} }
cresp, err := c.Authorize(ctx, creq) cresp, err := h.Authorize(ctx, creq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -659,19 +659,19 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
} }
} }
func (c *CommandHandler) HandleOauth2Callback(ctx context.Context, code, state string) (*RemoteSourceAuthResult, error) { func (h *ActionHandler) HandleOauth2Callback(ctx context.Context, code, state string) (*RemoteSourceAuthResult, error) {
token, err := jwt.Parse(state, func(token *jwt.Token) (interface{}, error) { token, err := jwt.Parse(state, func(token *jwt.Token) (interface{}, error) {
if token.Method != c.sd.Method { if token.Method != h.sd.Method {
return nil, errors.Errorf("unexpected signing method: %v", token.Header["alg"]) return nil, errors.Errorf("unexpected signing method: %v", token.Header["alg"])
} }
var key interface{} var key interface{}
switch c.sd.Method { switch h.sd.Method {
case jwt.SigningMethodRS256: case jwt.SigningMethodRS256:
key = c.sd.PrivateKey key = h.sd.PrivateKey
case jwt.SigningMethodHS256: case jwt.SigningMethodHS256:
key = c.sd.Key key = h.sd.Key
default: default:
return nil, errors.Errorf("unsupported signing method %q", c.sd.Method.Alg()) return nil, errors.Errorf("unsupported signing method %q", h.sd.Method.Alg())
} }
return key, nil return key, nil
}) })
@ -687,21 +687,21 @@ func (c *CommandHandler) HandleOauth2Callback(ctx context.Context, code, state s
requestType := RemoteSourceRequestType(claims["request_type"].(string)) requestType := RemoteSourceRequestType(claims["request_type"].(string))
requestString := claims["request"].(string) requestString := claims["request"].(string)
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, remoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName))
} }
c.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
oauth2Source, err := common.GetOauth2Source(rs, "") oauth2Source, err := common.GetOauth2Source(rs, "")
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to create gitlab source") return nil, errors.Wrapf(err, "failed to create gitlab source")
} }
oauth2Token, err := oauth2Source.RequestOauth2Token(c.webExposedURL+"/oauth2/callback", code) oauth2Token, err := oauth2Source.RequestOauth2Token(h.webExposedURL+"/oauth2/callback", code)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return c.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken, oauth2Token.Expiry) return h.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken, oauth2Token.Expiry)
} }

View File

@ -18,7 +18,7 @@ import (
"net/http" "net/http"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
"go.uber.org/zap" "go.uber.org/zap"
@ -26,7 +26,7 @@ import (
type OAuth2CallbackHandler struct { type OAuth2CallbackHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
configstoreClient *csapi.Client configstoreClient *csapi.Client
} }
@ -35,8 +35,8 @@ type RemoteSourceAuthResult struct {
Response interface{} `json:"response,omitempty"` Response interface{} `json:"response,omitempty"`
} }
func NewOAuth2CallbackHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client) *OAuth2CallbackHandler { func NewOAuth2CallbackHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client) *OAuth2CallbackHandler {
return &OAuth2CallbackHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient} return &OAuth2CallbackHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient}
} }
func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -45,7 +45,7 @@ func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
code := query.Get("code") code := query.Get("code")
state := query.Get("state") state := query.Get("state")
cresp, err := h.ch.HandleOauth2Callback(ctx, code, state) cresp, err := h.ah.HandleOauth2Callback(ctx, code, state)
if err != nil { if err != nil {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
httpError(w, util.NewErrBadRequest(err)) httpError(w, util.NewErrBadRequest(err))
@ -54,27 +54,27 @@ func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
var response interface{} var response interface{}
switch cresp.RequestType { switch cresp.RequestType {
case command.RemoteSourceRequestTypeCreateUserLA: case action.RemoteSourceRequestTypeCreateUserLA:
authresp := cresp.Response.(*command.CreateUserLAResponse) authresp := cresp.Response.(*action.CreateUserLAResponse)
response = &CreateUserLAResponse{ response = &CreateUserLAResponse{
LinkedAccount: authresp.LinkedAccount, LinkedAccount: authresp.LinkedAccount,
} }
case command.RemoteSourceRequestTypeLoginUser: case action.RemoteSourceRequestTypeLoginUser:
authresp := cresp.Response.(*command.LoginUserResponse) authresp := cresp.Response.(*action.LoginUserResponse)
response = &LoginUserResponse{ response = &LoginUserResponse{
Token: authresp.Token, Token: authresp.Token,
User: createUserResponse(authresp.User), User: createUserResponse(authresp.User),
} }
case command.RemoteSourceRequestTypeAuthorize: case action.RemoteSourceRequestTypeAuthorize:
authresp := cresp.Response.(*command.AuthorizeResponse) authresp := cresp.Response.(*action.AuthorizeResponse)
response = &AuthorizeResponse{ response = &AuthorizeResponse{
RemoteUserInfo: authresp.RemoteUserInfo, RemoteUserInfo: authresp.RemoteUserInfo,
RemoteSourceName: authresp.RemoteSourceName, RemoteSourceName: authresp.RemoteSourceName,
} }
case command.RemoteSourceRequestTypeRegisterUser: case action.RemoteSourceRequestTypeRegisterUser:
response = &RegisterUserResponse{} response = &RegisterUserResponse{}
} }

View File

@ -21,7 +21,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
"go.uber.org/zap" "go.uber.org/zap"
@ -35,11 +35,11 @@ type CreateOrgRequest struct {
type CreateOrgHandler struct { type CreateOrgHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
} }
func NewCreateOrgHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateOrgHandler { func NewCreateOrgHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateOrgHandler {
return &CreateOrgHandler{log: logger.Sugar(), ch: ch} return &CreateOrgHandler{log: logger.Sugar(), ah: ah}
} }
func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -59,12 +59,12 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
creq := &command.CreateOrgRequest{ creq := &action.CreateOrgRequest{
Name: req.Name, Name: req.Name,
CreatorUserID: userID, CreatorUserID: userID,
} }
org, err := h.ch.CreateOrg(ctx, creq) org, err := h.ah.CreateOrg(ctx, creq)
if httpError(w, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return

View File

@ -21,7 +21,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
@ -40,13 +40,13 @@ type CreateProjectRequest struct {
type CreateProjectHandler struct { type CreateProjectHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
configstoreClient *csapi.Client configstoreClient *csapi.Client
exposedURL string exposedURL string
} }
func NewCreateProjectHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectHandler { func NewCreateProjectHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectHandler {
return &CreateProjectHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL} return &CreateProjectHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL}
} }
func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -67,7 +67,7 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
userID := userIDVal.(string) userID := userIDVal.(string)
h.log.Infof("userID: %q", userID) h.log.Infof("userID: %q", userID)
creq := &command.CreateProjectRequest{ areq := &action.CreateProjectRequest{
Name: req.Name, Name: req.Name,
ParentID: req.ParentID, ParentID: req.ParentID,
Visibility: req.Visibility, Visibility: req.Visibility,
@ -77,7 +77,7 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck, SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
} }
project, err := h.ch.CreateProject(ctx, creq) project, err := h.ah.CreateProject(ctx, areq)
if httpError(w, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
@ -91,13 +91,13 @@ func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type ProjectReconfigHandler struct { type ProjectReconfigHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
configstoreClient *csapi.Client configstoreClient *csapi.Client
exposedURL string exposedURL string
} }
func NewProjectReconfigHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *ProjectReconfigHandler { func NewProjectReconfigHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *ProjectReconfigHandler {
return &ProjectReconfigHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL} return &ProjectReconfigHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL}
} }
func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -109,7 +109,7 @@ func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
return return
} }
if err := h.ch.ReconfigProject(ctx, projectRef); err != nil { if err := h.ah.ReconfigProject(ctx, projectRef); err != nil {
httpError(w, err) httpError(w, err)
return return
} }

View File

@ -21,7 +21,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
@ -37,13 +37,13 @@ type CreateProjectGroupRequest struct {
type CreateProjectGroupHandler struct { type CreateProjectGroupHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
configstoreClient *csapi.Client configstoreClient *csapi.Client
exposedURL string exposedURL string
} }
func NewCreateProjectGroupHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectGroupHandler { func NewCreateProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client, exposedURL string) *CreateProjectGroupHandler {
return &CreateProjectGroupHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient, exposedURL: exposedURL} return &CreateProjectGroupHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient, exposedURL: exposedURL}
} }
func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -64,14 +64,14 @@ func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
userID := userIDVal.(string) userID := userIDVal.(string)
h.log.Infof("userID: %q", userID) h.log.Infof("userID: %q", userID)
creq := &command.CreateProjectGroupRequest{ creq := &action.CreateProjectGroupRequest{
Name: req.Name, Name: req.Name,
ParentID: req.ParentID, ParentID: req.ParentID,
Visibility: req.Visibility, Visibility: req.Visibility,
CurrentUserID: userID, CurrentUserID: userID,
} }
projectGroup, err := h.ch.CreateProjectGroup(ctx, creq) projectGroup, err := h.ah.CreateProjectGroup(ctx, creq)
if httpError(w, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return

View File

@ -19,7 +19,7 @@ import (
gitsource "github.com/sorintlab/agola/internal/gitsources" gitsource "github.com/sorintlab/agola/internal/gitsources"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
@ -44,12 +44,12 @@ func createRemoteRepoResponse(r *gitsource.RepoInfo) *RemoteRepoResponse {
type UserRemoteReposHandler struct { type UserRemoteReposHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
configstoreClient *csapi.Client configstoreClient *csapi.Client
} }
func NewUserRemoteReposHandler(logger *zap.Logger, ch *command.CommandHandler, configstoreClient *csapi.Client) *UserRemoteReposHandler { func NewUserRemoteReposHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csapi.Client) *UserRemoteReposHandler {
return &UserRemoteReposHandler{log: logger.Sugar(), ch: ch, configstoreClient: configstoreClient} return &UserRemoteReposHandler{log: logger.Sugar(), ah: ah, configstoreClient: configstoreClient}
} }
func (h *UserRemoteReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *UserRemoteReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -91,7 +91,7 @@ func (h *UserRemoteReposHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
return return
} }
gitsource, err := h.ch.GetGitSource(ctx, rs, user.Name, la) gitsource, err := h.ah.GetGitSource(ctx, rs, user.Name, la)
if err != nil { if err != nil {
httpError(w, util.NewErrBadRequest(errors.Wrapf(err, "failed to create gitsource client"))) httpError(w, util.NewErrBadRequest(errors.Wrapf(err, "failed to create gitsource client")))
return return

View File

@ -21,7 +21,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
"go.uber.org/zap" "go.uber.org/zap"
@ -40,11 +40,11 @@ type CreateRemoteSourceRequest struct {
type CreateRemoteSourceHandler struct { type CreateRemoteSourceHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
} }
func NewCreateRemoteSourceHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateRemoteSourceHandler { func NewCreateRemoteSourceHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateRemoteSourceHandler {
return &CreateRemoteSourceHandler{log: logger.Sugar(), ch: ch} return &CreateRemoteSourceHandler{log: logger.Sugar(), ah: ah}
} }
func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -57,7 +57,7 @@ func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
return return
} }
creq := &command.CreateRemoteSourceRequest{ creq := &action.CreateRemoteSourceRequest{
Name: req.Name, Name: req.Name,
APIURL: req.APIURL, APIURL: req.APIURL,
Type: req.Type, Type: req.Type,
@ -65,7 +65,7 @@ func (h *CreateRemoteSourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
Oauth2ClientID: req.Oauth2ClientID, Oauth2ClientID: req.Oauth2ClientID,
Oauth2ClientSecret: req.Oauth2ClientSecret, Oauth2ClientSecret: req.Oauth2ClientSecret,
} }
rs, err := h.ch.CreateRemoteSource(ctx, creq) rs, err := h.ah.CreateRemoteSource(ctx, creq)
if httpError(w, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return

View File

@ -24,7 +24,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
gitsource "github.com/sorintlab/agola/internal/gitsources" gitsource "github.com/sorintlab/agola/internal/gitsources"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/types" "github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util" "github.com/sorintlab/agola/internal/util"
"go.uber.org/zap" "go.uber.org/zap"
@ -38,11 +38,11 @@ type CreateUserRequest struct {
type CreateUserHandler struct { type CreateUserHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
} }
func NewCreateUserHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateUserHandler { func NewCreateUserHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserHandler {
return &CreateUserHandler{log: logger.Sugar(), ch: ch} return &CreateUserHandler{log: logger.Sugar(), ah: ah}
} }
func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -55,11 +55,11 @@ func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
creq := &command.CreateUserRequest{ creq := &action.CreateUserRequest{
UserName: req.UserName, UserName: req.UserName,
} }
u, err := h.ch.CreateUser(ctx, creq) u, err := h.ah.CreateUser(ctx, creq)
if httpError(w, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
@ -252,11 +252,11 @@ type CreateUserLAResponse struct {
type CreateUserLAHandler struct { type CreateUserLAHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
} }
func NewCreateUserLAHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateUserLAHandler { func NewCreateUserLAHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserLAHandler {
return &CreateUserLAHandler{log: logger.Sugar(), ch: ch} return &CreateUserLAHandler{log: logger.Sugar(), ah: ah}
} }
func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -283,13 +283,13 @@ func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
} }
func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userRef string, req *CreateUserLARequest) (*CreateUserLAResponse, error) { func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userRef string, req *CreateUserLARequest) (*CreateUserLAResponse, error) {
creq := &command.CreateUserLARequest{ creq := &action.CreateUserLARequest{
UserRef: userRef, UserRef: userRef,
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
} }
h.log.Infof("creating linked account") h.log.Infof("creating linked account")
cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.RemoteSourceLoginName, req.RemoteSourceLoginPassword, command.RemoteSourceRequestTypeCreateUserLA, creq) cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.RemoteSourceLoginName, req.RemoteSourceLoginPassword, action.RemoteSourceRequestTypeCreateUserLA, creq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -298,7 +298,7 @@ func (h *CreateUserLAHandler) createUserLA(ctx context.Context, userRef string,
Oauth2Redirect: cresp.Oauth2Redirect, Oauth2Redirect: cresp.Oauth2Redirect,
}, nil }, nil
} }
authresp := cresp.Response.(*command.CreateUserLAResponse) authresp := cresp.Response.(*action.CreateUserLAResponse)
resp := &CreateUserLAResponse{ resp := &CreateUserLAResponse{
LinkedAccount: authresp.LinkedAccount, LinkedAccount: authresp.LinkedAccount,
@ -343,11 +343,11 @@ type CreateUserTokenResponse struct {
type CreateUserTokenHandler struct { type CreateUserTokenHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
} }
func NewCreateUserTokenHandler(logger *zap.Logger, ch *command.CommandHandler) *CreateUserTokenHandler { func NewCreateUserTokenHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateUserTokenHandler {
return &CreateUserTokenHandler{log: logger.Sugar(), ch: ch} return &CreateUserTokenHandler{log: logger.Sugar(), ah: ah}
} }
func (h *CreateUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -362,12 +362,12 @@ func (h *CreateUserTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
return return
} }
creq := &command.CreateUserTokenRequest{ creq := &action.CreateUserTokenRequest{
UserRef: userRef, UserRef: userRef,
TokenName: req.TokenName, TokenName: req.TokenName,
} }
h.log.Infof("creating user %q token", userRef) h.log.Infof("creating user %q token", userRef)
token, err := h.ch.CreateUserToken(ctx, creq) token, err := h.ah.CreateUserToken(ctx, creq)
if httpError(w, err) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return
@ -416,15 +416,15 @@ type RegisterUserRequest struct {
type RegisterUserHandler struct { type RegisterUserHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
} }
type RegisterUserResponse struct { type RegisterUserResponse struct {
Oauth2Redirect string `json:"oauth2_redirect"` Oauth2Redirect string `json:"oauth2_redirect"`
} }
func NewRegisterUserHandler(logger *zap.Logger, ch *command.CommandHandler) *RegisterUserHandler { func NewRegisterUserHandler(logger *zap.Logger, ah *action.ActionHandler) *RegisterUserHandler {
return &RegisterUserHandler{log: logger.Sugar(), ch: ch} return &RegisterUserHandler{log: logger.Sugar(), ah: ah}
} }
func (h *RegisterUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *RegisterUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -449,12 +449,12 @@ func (h *RegisterUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
} }
func (h *RegisterUserHandler) registerUser(ctx context.Context, req *RegisterUserRequest) (*RegisterUserResponse, error) { func (h *RegisterUserHandler) registerUser(ctx context.Context, req *RegisterUserRequest) (*RegisterUserResponse, error) {
creq := &command.RegisterUserRequest{ creq := &action.RegisterUserRequest{
UserName: req.CreateUserRequest.UserName, UserName: req.CreateUserRequest.UserName,
RemoteSourceName: req.CreateUserLARequest.RemoteSourceName, RemoteSourceName: req.CreateUserLARequest.RemoteSourceName,
} }
cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.CreateUserLARequest.RemoteSourceName, req.CreateUserLARequest.RemoteSourceLoginName, req.CreateUserLARequest.RemoteSourceLoginPassword, command.RemoteSourceRequestTypeRegisterUser, creq) cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.CreateUserLARequest.RemoteSourceName, req.CreateUserLARequest.RemoteSourceLoginName, req.CreateUserLARequest.RemoteSourceLoginPassword, action.RemoteSourceRequestTypeRegisterUser, creq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -463,7 +463,7 @@ func (h *RegisterUserHandler) registerUser(ctx context.Context, req *RegisterUse
Oauth2Redirect: cresp.Oauth2Redirect, Oauth2Redirect: cresp.Oauth2Redirect,
}, nil }, nil
} }
//authresp := cresp.Response.(*command.RegisterUserResponse) //authresp := cresp.Response.(*action.RegisterUserResponse)
resp := &RegisterUserResponse{} resp := &RegisterUserResponse{}
return resp, nil return resp, nil
@ -471,7 +471,7 @@ func (h *RegisterUserHandler) registerUser(ctx context.Context, req *RegisterUse
type AuthorizeHandler struct { type AuthorizeHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
} }
type AuthorizeResponse struct { type AuthorizeResponse struct {
@ -480,8 +480,8 @@ type AuthorizeResponse struct {
RemoteSourceName string `json:"remote_source_name"` RemoteSourceName string `json:"remote_source_name"`
} }
func NewAuthorizeHandler(logger *zap.Logger, ch *command.CommandHandler) *AuthorizeHandler { func NewAuthorizeHandler(logger *zap.Logger, ah *action.ActionHandler) *AuthorizeHandler {
return &AuthorizeHandler{log: logger.Sugar(), ch: ch} return &AuthorizeHandler{log: logger.Sugar(), ah: ah}
} }
func (h *AuthorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *AuthorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -506,11 +506,11 @@ func (h *AuthorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func (h *AuthorizeHandler) authorize(ctx context.Context, req *LoginUserRequest) (*AuthorizeResponse, error) { func (h *AuthorizeHandler) authorize(ctx context.Context, req *LoginUserRequest) (*AuthorizeResponse, error) {
creq := &command.LoginUserRequest{ creq := &action.LoginUserRequest{
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
} }
cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, command.RemoteSourceRequestTypeAuthorize, creq) cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, action.RemoteSourceRequestTypeAuthorize, creq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -519,7 +519,7 @@ func (h *AuthorizeHandler) authorize(ctx context.Context, req *LoginUserRequest)
Oauth2Redirect: cresp.Oauth2Redirect, Oauth2Redirect: cresp.Oauth2Redirect,
}, nil }, nil
} }
authresp := cresp.Response.(*command.AuthorizeResponse) authresp := cresp.Response.(*action.AuthorizeResponse)
resp := &AuthorizeResponse{ resp := &AuthorizeResponse{
RemoteUserInfo: authresp.RemoteUserInfo, RemoteUserInfo: authresp.RemoteUserInfo,
@ -542,11 +542,11 @@ type LoginUserResponse struct {
type LoginUserHandler struct { type LoginUserHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
} }
func NewLoginUserHandler(logger *zap.Logger, ch *command.CommandHandler) *LoginUserHandler { func NewLoginUserHandler(logger *zap.Logger, ah *action.ActionHandler) *LoginUserHandler {
return &LoginUserHandler{log: logger.Sugar(), ch: ch} return &LoginUserHandler{log: logger.Sugar(), ah: ah}
} }
func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -571,12 +571,12 @@ func (h *LoginUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) { func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) {
creq := &command.LoginUserRequest{ creq := &action.LoginUserRequest{
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
} }
h.log.Infof("logging in user") h.log.Infof("logging in user")
cresp, err := h.ch.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, command.RemoteSourceRequestTypeLoginUser, creq) cresp, err := h.ah.HandleRemoteSourceAuth(ctx, req.RemoteSourceName, req.LoginName, req.LoginPassword, action.RemoteSourceRequestTypeLoginUser, creq)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -585,7 +585,7 @@ func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest)
Oauth2Redirect: cresp.Oauth2Redirect, Oauth2Redirect: cresp.Oauth2Redirect,
}, nil }, nil
} }
authresp := cresp.Response.(*command.LoginUserResponse) authresp := cresp.Response.(*action.LoginUserResponse)
resp := &LoginUserResponse{ resp := &LoginUserResponse{
Token: authresp.Token, Token: authresp.Token,

View File

@ -25,8 +25,8 @@ import (
"github.com/sorintlab/agola/internal/objectstorage" "github.com/sorintlab/agola/internal/objectstorage"
"github.com/sorintlab/agola/internal/services/config" "github.com/sorintlab/agola/internal/services/config"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/gateway/api" "github.com/sorintlab/agola/internal/services/gateway/api"
"github.com/sorintlab/agola/internal/services/gateway/command"
"github.com/sorintlab/agola/internal/services/gateway/common" "github.com/sorintlab/agola/internal/services/gateway/common"
"github.com/sorintlab/agola/internal/services/gateway/handlers" "github.com/sorintlab/agola/internal/services/gateway/handlers"
rsapi "github.com/sorintlab/agola/internal/services/runservice/scheduler/api" rsapi "github.com/sorintlab/agola/internal/services/runservice/scheduler/api"
@ -54,7 +54,7 @@ type Gateway struct {
ost *objectstorage.ObjStorage ost *objectstorage.ObjStorage
runserviceClient *rsapi.Client runserviceClient *rsapi.Client
configstoreClient *csapi.Client configstoreClient *csapi.Client
ch *command.CommandHandler ah *action.ActionHandler
sd *common.TokenSigningData sd *common.TokenSigningData
} }
@ -123,14 +123,14 @@ func NewGateway(gc *config.Config) (*Gateway, error) {
configstoreClient := csapi.NewClient(c.ConfigStoreURL) configstoreClient := csapi.NewClient(c.ConfigStoreURL)
ch := command.NewCommandHandler(logger, sd, configstoreClient, gc.ID, c.APIExposedURL, c.WebExposedURL) ah := action.NewActionHandler(logger, sd, configstoreClient, gc.ID, c.APIExposedURL, c.WebExposedURL)
return &Gateway{ return &Gateway{
c: c, c: c,
ost: ost, ost: ost,
runserviceClient: rsapi.NewClient(c.RunServiceURL), runserviceClient: rsapi.NewClient(c.RunServiceURL),
configstoreClient: configstoreClient, configstoreClient: configstoreClient,
ch: ch, ah: ah,
sd: sd, sd: sd,
}, nil }, nil
} }
@ -146,17 +146,17 @@ func (g *Gateway) Run(ctx context.Context) error {
corsAllowedOriginsOptions := ghandlers.AllowedOrigins([]string{"*"}) corsAllowedOriginsOptions := ghandlers.AllowedOrigins([]string{"*"})
corsHandler = ghandlers.CORS(corsAllowedMethodsOptions, corsAllowedHeadersOptions, corsAllowedOriginsOptions) corsHandler = ghandlers.CORS(corsAllowedMethodsOptions, corsAllowedHeadersOptions, corsAllowedOriginsOptions)
webhooksHandler := &webhooksHandler{log: log, ch: g.ch, configstoreClient: g.configstoreClient, runserviceClient: g.runserviceClient, apiExposedURL: g.c.APIExposedURL} webhooksHandler := &webhooksHandler{log: log, ah: g.ah, configstoreClient: g.configstoreClient, runserviceClient: g.runserviceClient, apiExposedURL: g.c.APIExposedURL}
projectGroupHandler := api.NewProjectGroupHandler(logger, g.configstoreClient) projectGroupHandler := api.NewProjectGroupHandler(logger, g.configstoreClient)
projectGroupSubgroupsHandler := api.NewProjectGroupSubgroupsHandler(logger, g.configstoreClient) projectGroupSubgroupsHandler := api.NewProjectGroupSubgroupsHandler(logger, g.configstoreClient)
projectGroupProjectsHandler := api.NewProjectGroupProjectsHandler(logger, g.configstoreClient) projectGroupProjectsHandler := api.NewProjectGroupProjectsHandler(logger, g.configstoreClient)
createProjectGroupHandler := api.NewCreateProjectGroupHandler(logger, g.ch, g.configstoreClient, g.c.APIExposedURL) createProjectGroupHandler := api.NewCreateProjectGroupHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL)
projectHandler := api.NewProjectHandler(logger, g.configstoreClient) projectHandler := api.NewProjectHandler(logger, g.configstoreClient)
createProjectHandler := api.NewCreateProjectHandler(logger, g.ch, g.configstoreClient, g.c.APIExposedURL) createProjectHandler := api.NewCreateProjectHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL)
deleteProjectHandler := api.NewDeleteProjectHandler(logger, g.configstoreClient) deleteProjectHandler := api.NewDeleteProjectHandler(logger, g.configstoreClient)
projectReconfigHandler := api.NewProjectReconfigHandler(logger, g.ch, g.configstoreClient, g.c.APIExposedURL) projectReconfigHandler := api.NewProjectReconfigHandler(logger, g.ah, g.configstoreClient, g.c.APIExposedURL)
secretHandler := api.NewSecretHandler(logger, g.configstoreClient) secretHandler := api.NewSecretHandler(logger, g.configstoreClient)
createSecretHandler := api.NewCreateSecretHandler(logger, g.configstoreClient) createSecretHandler := api.NewCreateSecretHandler(logger, g.configstoreClient)
@ -169,21 +169,21 @@ func (g *Gateway) Run(ctx context.Context) error {
currentUserHandler := api.NewCurrentUserHandler(logger, g.configstoreClient) currentUserHandler := api.NewCurrentUserHandler(logger, g.configstoreClient)
userHandler := api.NewUserHandler(logger, g.configstoreClient) userHandler := api.NewUserHandler(logger, g.configstoreClient)
usersHandler := api.NewUsersHandler(logger, g.configstoreClient) usersHandler := api.NewUsersHandler(logger, g.configstoreClient)
createUserHandler := api.NewCreateUserHandler(logger, g.ch) createUserHandler := api.NewCreateUserHandler(logger, g.ah)
deleteUserHandler := api.NewDeleteUserHandler(logger, g.configstoreClient) deleteUserHandler := api.NewDeleteUserHandler(logger, g.configstoreClient)
createUserLAHandler := api.NewCreateUserLAHandler(logger, g.ch) createUserLAHandler := api.NewCreateUserLAHandler(logger, g.ah)
deleteUserLAHandler := api.NewDeleteUserLAHandler(logger, g.configstoreClient) deleteUserLAHandler := api.NewDeleteUserLAHandler(logger, g.configstoreClient)
createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.ch) createUserTokenHandler := api.NewCreateUserTokenHandler(logger, g.ah)
deleteUserTokenHandler := api.NewDeleteUserTokenHandler(logger, g.configstoreClient) deleteUserTokenHandler := api.NewDeleteUserTokenHandler(logger, g.configstoreClient)
remoteSourceHandler := api.NewRemoteSourceHandler(logger, g.configstoreClient) remoteSourceHandler := api.NewRemoteSourceHandler(logger, g.configstoreClient)
createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ch) createRemoteSourceHandler := api.NewCreateRemoteSourceHandler(logger, g.ah)
remoteSourcesHandler := api.NewRemoteSourcesHandler(logger, g.configstoreClient) remoteSourcesHandler := api.NewRemoteSourcesHandler(logger, g.configstoreClient)
orgHandler := api.NewOrgHandler(logger, g.configstoreClient) orgHandler := api.NewOrgHandler(logger, g.configstoreClient)
orgsHandler := api.NewOrgsHandler(logger, g.configstoreClient) orgsHandler := api.NewOrgsHandler(logger, g.configstoreClient)
createOrgHandler := api.NewCreateOrgHandler(logger, g.ch) createOrgHandler := api.NewCreateOrgHandler(logger, g.ah)
deleteOrgHandler := api.NewDeleteOrgHandler(logger, g.configstoreClient) deleteOrgHandler := api.NewDeleteOrgHandler(logger, g.configstoreClient)
runHandler := api.NewRunHandler(logger, g.runserviceClient) runHandler := api.NewRunHandler(logger, g.runserviceClient)
@ -195,12 +195,12 @@ func (g *Gateway) Run(ctx context.Context) error {
logsHandler := api.NewLogsHandler(logger, g.runserviceClient) logsHandler := api.NewLogsHandler(logger, g.runserviceClient)
reposHandler := api.NewReposHandler(logger, g.c.GitServerURL) reposHandler := api.NewReposHandler(logger, g.c.GitServerURL)
userRemoteReposHandler := api.NewUserRemoteReposHandler(logger, g.ch, g.configstoreClient) userRemoteReposHandler := api.NewUserRemoteReposHandler(logger, g.ah, g.configstoreClient)
loginUserHandler := api.NewLoginUserHandler(logger, g.ch) loginUserHandler := api.NewLoginUserHandler(logger, g.ah)
authorizeHandler := api.NewAuthorizeHandler(logger, g.ch) authorizeHandler := api.NewAuthorizeHandler(logger, g.ah)
registerHandler := api.NewRegisterUserHandler(logger, g.ch) registerHandler := api.NewRegisterUserHandler(logger, g.ah)
oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ch, g.configstoreClient) oauth2callbackHandler := api.NewOAuth2CallbackHandler(logger, g.ah, g.configstoreClient)
router := mux.NewRouter() router := mux.NewRouter()

View File

@ -26,7 +26,7 @@ import (
"github.com/sorintlab/agola/internal/gitsources/agolagit" "github.com/sorintlab/agola/internal/gitsources/agolagit"
"github.com/sorintlab/agola/internal/runconfig" "github.com/sorintlab/agola/internal/runconfig"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/command" "github.com/sorintlab/agola/internal/services/gateway/action"
"github.com/sorintlab/agola/internal/services/gateway/common" "github.com/sorintlab/agola/internal/services/gateway/common"
rsapi "github.com/sorintlab/agola/internal/services/runservice/scheduler/api" rsapi "github.com/sorintlab/agola/internal/services/runservice/scheduler/api"
rstypes "github.com/sorintlab/agola/internal/services/runservice/types" rstypes "github.com/sorintlab/agola/internal/services/runservice/types"
@ -108,7 +108,7 @@ func genGroup(baseGroupType GroupType, baseGroupID string, webhookData *types.We
type webhooksHandler struct { type webhooksHandler struct {
log *zap.SugaredLogger log *zap.SugaredLogger
ch *command.CommandHandler ah *action.ActionHandler
configstoreClient *csapi.Client configstoreClient *csapi.Client
runserviceClient *rsapi.Client runserviceClient *rsapi.Client
apiExposedURL string apiExposedURL string
@ -167,7 +167,7 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
return http.StatusInternalServerError, "", errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID) return http.StatusInternalServerError, "", errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID)
} }
gitSource, err = h.ch.GetGitSource(ctx, rs, user.Name, la) gitSource, err = h.ah.GetGitSource(ctx, rs, user.Name, la)
if err != nil { if err != nil {
return http.StatusInternalServerError, "", errors.Wrapf(err, "failed to create gitea client") return http.StatusInternalServerError, "", errors.Wrapf(err, "failed to create gitea client")
} }