gateway: use agola ID in gitsource tokenname

This commit is contained in:
Simone Gotti 2019-04-30 12:13:12 +02:00
parent e970e217e2
commit fefa2819c9
6 changed files with 13 additions and 9 deletions

View File

@ -129,7 +129,7 @@ func serve(cmd *cobra.Command, args []string) error {
return errors.Wrapf(err, "failed to start scheduler")
}
gateway, err := gateway.NewGateway(&c.Gateway)
gateway, err := gateway.NewGateway(c)
if err != nil {
return errors.Wrapf(err, "failed to start gateway")
}

View File

@ -94,13 +94,13 @@ func New(opts Opts) (*Client, error) {
}, nil
}
func (c *Client) LoginPassword(username, password string) (string, error) {
func (c *Client) LoginPassword(username, password, tokenName string) (string, error) {
// try to get agola access token if it already exists
var accessToken string
tokens, err := c.client.ListAccessTokens(username, password)
if err == nil {
for _, token := range tokens {
if token.Name == "agola" {
if token.Name == tokenName {
accessToken = token.Sha1
break
}
@ -112,7 +112,7 @@ func (c *Client) LoginPassword(username, password string) (string, error) {
token, terr := c.client.CreateAccessToken(
username,
password,
gitea.CreateAccessTokenOption{Name: "agola"},
gitea.CreateAccessTokenOption{Name: tokenName},
)
if terr != nil {
return "", terr

View File

@ -48,7 +48,7 @@ type UserSource interface {
type PasswordSource interface {
UserSource
LoginPassword(username, password string) (string, error)
LoginPassword(username, password, tokenName string) (string, error)
}
type Oauth2Source interface {

View File

@ -29,15 +29,17 @@ type CommandHandler struct {
log *zap.SugaredLogger
sd *common.TokenSigningData
configstoreClient *csapi.Client
agolaID string
apiExposedURL string
webExposedURL string
}
func NewCommandHandler(logger *zap.Logger, sd *common.TokenSigningData, configstoreClient *csapi.Client, apiExposedURL, webExposedURL string) *CommandHandler {
func NewCommandHandler(logger *zap.Logger, sd *common.TokenSigningData, configstoreClient *csapi.Client, agolaID, apiExposedURL, webExposedURL string) *CommandHandler {
return &CommandHandler{
log: logger.Sugar(),
sd: sd,
configstoreClient: configstoreClient,
agolaID: agolaID,
apiExposedURL: apiExposedURL,
webExposedURL: webExposedURL,
}

View File

@ -518,7 +518,8 @@ func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourc
if err != nil {
return nil, errors.Wrapf(err, "failed to create git source")
}
accessToken, err := passwordSource.LoginPassword(loginName, loginPassword)
tokenName := "agola-" + c.agolaID
accessToken, err := passwordSource.LoginPassword(loginName, loginPassword, tokenName)
if err != nil {
return nil, errors.Wrapf(err, "failed to login to remote source %q with login name %q", rs.Name, loginName)
}

View File

@ -58,7 +58,8 @@ type Gateway struct {
sd *common.TokenSigningData
}
func NewGateway(c *config.Gateway) (*Gateway, error) {
func NewGateway(gc *config.Config) (*Gateway, error) {
c := &gc.Gateway
if c.Debug {
level.SetLevel(zapcore.DebugLevel)
}
@ -122,7 +123,7 @@ func NewGateway(c *config.Gateway) (*Gateway, error) {
configstoreClient := csapi.NewClient(c.ConfigStoreURL)
ch := command.NewCommandHandler(logger, sd, configstoreClient, c.APIExposedURL, c.WebExposedURL)
ch := command.NewCommandHandler(logger, sd, configstoreClient, gc.ID, c.APIExposedURL, c.WebExposedURL)
return &Gateway{
c: c,