mirror of
https://github.com/harness/drone.git
synced 2025-05-17 09:30:00 +08:00
fix: [CODE-2728]: fix webhook internal url to not read from db (#2953)
This commit is contained in:
parent
123969c7e1
commit
ea9f3af325
@ -101,6 +101,7 @@ type Service struct {
|
|||||||
insecureHTTPClientInternal *http.Client
|
insecureHTTPClientInternal *http.Client
|
||||||
|
|
||||||
config Config
|
config Config
|
||||||
|
webhookURLProvider URLProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(
|
func NewService(
|
||||||
@ -120,7 +121,7 @@ func NewService(
|
|||||||
git git.Interface,
|
git git.Interface,
|
||||||
encrypter encrypt.Encrypter,
|
encrypter encrypt.Encrypter,
|
||||||
labelStore store.LabelStore,
|
labelStore store.LabelStore,
|
||||||
|
webhookURLProvider URLProvider,
|
||||||
) (*Service, error) {
|
) (*Service, error) {
|
||||||
if err := config.Prepare(); err != nil {
|
if err := config.Prepare(); err != nil {
|
||||||
return nil, fmt.Errorf("provided webhook service config is invalid: %w", err)
|
return nil, fmt.Errorf("provided webhook service config is invalid: %w", err)
|
||||||
@ -147,6 +148,7 @@ func NewService(
|
|||||||
config: config,
|
config: config,
|
||||||
|
|
||||||
labelStore: labelStore,
|
labelStore: labelStore,
|
||||||
|
webhookURLProvider: webhookURLProvider,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := gitReaderFactory.Launch(ctx, eventsReaderGroupName, config.EventReaderName,
|
_, err := gitReaderFactory.Launch(ctx, eventsReaderGroupName, config.EventReaderName,
|
||||||
|
@ -287,7 +287,11 @@ func (s *Service) executeWebhook(ctx context.Context, webhook *types.Webhook, tr
|
|||||||
// NOTE: if the body is an io.Reader, the value is used as response body as is, otherwise it'll be JSON serialized.
|
// NOTE: if the body is an io.Reader, the value is used as response body as is, otherwise it'll be JSON serialized.
|
||||||
func (s *Service) prepareHTTPRequest(ctx context.Context, execution *types.WebhookExecution,
|
func (s *Service) prepareHTTPRequest(ctx context.Context, execution *types.WebhookExecution,
|
||||||
triggerType enum.WebhookTrigger, webhook *types.Webhook, body any) (*http.Request, error) {
|
triggerType enum.WebhookTrigger, webhook *types.Webhook, body any) (*http.Request, error) {
|
||||||
execution.Request.URL = s.getWebhookURL(ctx, webhook)
|
url, err := s.webhookURLProvider.GetWebhookURL(ctx, webhook)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("webhook url is not resolvable: %w", err)
|
||||||
|
}
|
||||||
|
execution.Request.URL = url
|
||||||
|
|
||||||
// Serialize body before anything else.
|
// Serialize body before anything else.
|
||||||
// This allows the user to retrigger the execution even in case of bad URL.
|
// This allows the user to retrigger the execution even in case of bad URL.
|
||||||
@ -370,19 +374,6 @@ func (s *Service) prepareHTTPRequest(ctx context.Context, execution *types.Webho
|
|||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) getWebhookURL(ctx context.Context, webhook *types.Webhook) string {
|
|
||||||
// in case a webhook is internal use the specified URL if not empty instead of using one saved in db.
|
|
||||||
if webhook.Internal && s.config.InternalWebhooksURL != "" {
|
|
||||||
return s.config.InternalWebhooksURL
|
|
||||||
}
|
|
||||||
|
|
||||||
if webhook.Internal && s.config.InternalWebhooksURL == "" {
|
|
||||||
log.Ctx(ctx).Error().Msg("internal webhook URL is empty, falling back to one in db")
|
|
||||||
}
|
|
||||||
// set URL as is (already has been validated, any other error will be caught in request creation)
|
|
||||||
return webhook.URL
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) toXHeader(name string) string {
|
func (s *Service) toXHeader(name string) string {
|
||||||
return fmt.Sprintf("X-%s-%s", s.config.HeaderIdentity, name)
|
return fmt.Sprintf("X-%s-%s", s.config.HeaderIdentity, name)
|
||||||
}
|
}
|
||||||
|
34
app/services/webhook/url_provider.go
Normal file
34
app/services/webhook/url_provider.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Copyright 2023 Harness, Inc.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package webhook
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/harness/gitness/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ URLProvider = (*GitnessURLProvider)(nil)
|
||||||
|
|
||||||
|
type GitnessURLProvider struct{}
|
||||||
|
|
||||||
|
func NewURLProvider(_ context.Context) *GitnessURLProvider {
|
||||||
|
return &GitnessURLProvider{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *GitnessURLProvider) GetWebhookURL(_ context.Context, webhook *types.Webhook) (string, error) {
|
||||||
|
// set URL as is (already has been validated, any other error will be caught in request creation)
|
||||||
|
return webhook.URL, nil
|
||||||
|
}
|
25
app/services/webhook/url_provider_interface.go
Normal file
25
app/services/webhook/url_provider_interface.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Copyright 2023 Harness, Inc.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package webhook
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/harness/gitness/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
type URLProvider interface {
|
||||||
|
GetWebhookURL(ctx context.Context, webhook *types.Webhook) (string, error)
|
||||||
|
}
|
@ -32,6 +32,7 @@ import (
|
|||||||
// WireSet provides a wire set for this package.
|
// WireSet provides a wire set for this package.
|
||||||
var WireSet = wire.NewSet(
|
var WireSet = wire.NewSet(
|
||||||
ProvideService,
|
ProvideService,
|
||||||
|
ProvideURLProvider,
|
||||||
)
|
)
|
||||||
|
|
||||||
func ProvideService(
|
func ProvideService(
|
||||||
@ -51,6 +52,7 @@ func ProvideService(
|
|||||||
git git.Interface,
|
git git.Interface,
|
||||||
encrypter encrypt.Encrypter,
|
encrypter encrypt.Encrypter,
|
||||||
labelStore store.LabelStore,
|
labelStore store.LabelStore,
|
||||||
|
webhookURLProvider URLProvider,
|
||||||
) (*Service, error) {
|
) (*Service, error) {
|
||||||
return NewService(
|
return NewService(
|
||||||
ctx,
|
ctx,
|
||||||
@ -68,5 +70,10 @@ func ProvideService(
|
|||||||
git,
|
git,
|
||||||
encrypter,
|
encrypter,
|
||||||
labelStore,
|
labelStore,
|
||||||
|
webhookURLProvider,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ProvideURLProvider(ctx context.Context) URLProvider {
|
||||||
|
return NewURLProvider(ctx)
|
||||||
|
}
|
||||||
|
@ -365,7 +365,8 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
|
|||||||
webhookConfig := server.ProvideWebhookConfig(config)
|
webhookConfig := server.ProvideWebhookConfig(config)
|
||||||
webhookStore := database.ProvideWebhookStore(db)
|
webhookStore := database.ProvideWebhookStore(db)
|
||||||
webhookExecutionStore := database.ProvideWebhookExecutionStore(db)
|
webhookExecutionStore := database.ProvideWebhookExecutionStore(db)
|
||||||
webhookService, err := webhook.ProvideService(ctx, webhookConfig, transactor, readerFactory, eventsReaderFactory, webhookStore, webhookExecutionStore, spaceStore, repoStore, pullReqStore, pullReqActivityStore, provider, principalStore, gitInterface, encrypter, labelStore)
|
urlProvider := webhook.ProvideURLProvider(ctx)
|
||||||
|
webhookService, err := webhook.ProvideService(ctx, webhookConfig, transactor, readerFactory, eventsReaderFactory, webhookStore, webhookExecutionStore, spaceStore, repoStore, pullReqStore, pullReqActivityStore, provider, principalStore, gitInterface, encrypter, labelStore, urlProvider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user