Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ func PatchRepo(c *gin.Context) {
if in.SecretExtensionNetrc != nil {
repo.SecretExtensionNetrc = *in.SecretExtensionNetrc
}
if in.ExposeAccessTokenEvents != nil {
repo.ExposeAccessTokenEvents = *in.ExposeAccessTokenEvents
}

err := _store.UpdateRepo(repo)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions server/model/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type Repo struct {
RegistryExtensionNetrc bool `json:"registry_extension_netrc" xorm:"DEFAULT FALSE 'registry_extension_netrc'"`
SecretExtensionEndpoint string `json:"secret_extension_endpoint" xorm:"varchar(500) 'secret_extension_endpoint'"`
SecretExtensionNetrc bool `json:"secret_extension_netrc" xorm:"DEFAULT FALSE 'secret_extension_netrc'"`
ExposeAccessTokenEvents []WebhookEvent `json:"expose_access_token_events" xorm:"json 'expose_access_token_events'"`

// Rest API Only

Expand Down Expand Up @@ -169,6 +170,7 @@ type RepoPatch struct {
RegistryExtensionNetrc *bool `json:"registry_extension_netrc"`
SecretExtensionEndpoint *string `json:"secret_extension_endpoint,omitempty"`
SecretExtensionNetrc *bool `json:"secret_extension_netrc,omitempty"`
ExposeAccessTokenEvents *[]WebhookEvent `json:"expose_access_token_events"`
} // @name RepoPatch

type ForgeRemoteID string
Expand Down
24 changes: 24 additions & 0 deletions server/pipeline/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"database/sql"
"errors"
"fmt"
"strconv"

"github.com/rs/zerolog/log"

Expand All @@ -32,6 +33,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v3/server/model"
"go.woodpecker-ci.org/woodpecker/v3/server/pipeline/metadata"
"go.woodpecker-ci.org/woodpecker/v3/server/store"
"go.woodpecker-ci.org/woodpecker/v3/shared/token"
)

func parsePipeline(ctx context.Context, forge forge.Forge, store store.Store, currentPipeline *model.Pipeline, user *model.User, repo *model.Repo, forgeYamls []*forge_types.FileMeta, envs map[string]string) ([]*builder.Item, error) {
Expand Down Expand Up @@ -68,6 +70,28 @@ func parsePipeline(ctx context.Context, forge forge.Forge, store store.Store, cu
})
}

if len(repo.ExposeAccessTokenEvents) > 0 {
t := token.New(token.PipelineToken)
t.Set("user-id", strconv.FormatInt(user.ID, 10))
t.Set("pipeline-id", strconv.FormatInt(currentPipeline.ID, 10))
// TODO: If a pipeline is restarted, Timestamp is equal to the timestamp of start of the last pipeline.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only issue that I'm facing that wasn't able to solve. I could just get the current timestamp, but I don't really like it

tokenString, err := t.SignExpires(user.Hash, currentPipeline.Timestamp+repo.Timeout*60)
if err != nil {
return nil, fmt.Errorf("failed to generate pipeline token: %w", err)
}

var events []pipeline_metadata.Event
for _, event := range repo.ExposeAccessTokenEvents {
events = append(events, pipeline_metadata.Event(event))
}

secrets = append(secrets, compiler.Secret{
Name: "woodpecker_token",
Value: tokenString,
Events: events,
})
}

registryService := server.Config.Services.Manager.RegistryServiceFromRepo(repo)
regs, err := registryService.RegistryListPipeline(ctx, repo, currentPipeline, netrc)
if err != nil {
Expand Down
28 changes: 23 additions & 5 deletions server/router/middleware/session/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func SetUser() gin.HandlerFunc {
return func(c *gin.Context) {
var user *model.User

t, err := token.ParseRequest([]token.Type{token.UserToken, token.SessToken}, c.Request, func(t *token.Token) (string, error) {
t, err := token.ParseRequest([]token.Type{token.UserToken, token.SessToken, token.PipelineToken}, c.Request, func(t *token.Token) (string, error) {
var err error
userID, err := strconv.ParseInt(t.Get("user-id"), 10, 64)
if err != nil {
Expand All @@ -55,10 +55,11 @@ func SetUser() gin.HandlerFunc {
if err == nil {
c.Set("user", user)

// if this is a session token (ie not the API token)
// this means the user is accessing with a web browser,
// so we should implement CSRF protection measures.
if t.Type == token.SessToken {
switch t.Type {
case token.SessToken:
// if this is a session token (ie not the API token)
// this means the user is accessing with a web browser,
// so we should implement CSRF protection measures.
err = token.CheckCsrf(c.Request, func(_ *token.Token) (string, error) {
return user.Hash, nil
})
Expand All @@ -68,6 +69,23 @@ func SetUser() gin.HandlerFunc {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
case token.PipelineToken:
/// If this is a token generated for a pipeline,
// check if the pipeline is still running.
pipelineID, err := strconv.ParseInt(t.Get("pipeline-id"), 10, 64)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
pipeline, err := store.FromContext(c).GetPipeline(pipelineID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if pipeline.Status != model.StatusRunning {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
}
}
c.Next()
Expand Down
1 change: 1 addition & 0 deletions shared/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
CsrfToken Type = "csrf"
AgentToken Type = "agent"
OAuthStateToken Type = "oauth-state"
PipelineToken Type = "pipeline"
)

// SignerAlgo id default algorithm used to sign JWT tokens.
Expand Down
3 changes: 3 additions & 0 deletions web/src/lib/api/types/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export interface Repo {

// True if repo only exist in the woodpecker store and not at the forge anymore
has_no_forge_repo?: boolean;

expose_access_token_events: string[];
}

/* eslint-disable no-unused-vars */
Expand Down Expand Up @@ -134,6 +136,7 @@ export type RepoSettings = Pick<
| 'allow_deploy'
| 'cancel_previous_pipeline_events'
| 'netrc_trusted'
| 'expose_access_token_events'
>;

export type ExtensionSettings = Pick<
Expand Down
23 changes: 23 additions & 0 deletions web/src/views/repo/settings/General.vue
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@
</template>
</InputField>

<InputField
label="Expose Woodpecker token"
>
<CheckboxesField
v-model="repoSettings.expose_access_token_events"
:options="exposeAccessTokenEventsOptions"
/>
<template #description>
Selected event triggers will expose the <span class="code-box-inline">woodpecker_token</span> secret to the pipeline.
</template>
</InputField>

<Button
type="submit"
class="mr-auto"
Expand Down Expand Up @@ -219,6 +231,7 @@ function loadRepoSettings() {
allow_deploy: repo.value.allow_deploy,
cancel_previous_pipeline_events: repo.value.cancel_previous_pipeline_events || [],
netrc_trusted: repo.value.netrc_trusted || [],
expose_access_token_events: repo.value.expose_access_token_events || [],
};
}

Expand Down Expand Up @@ -273,5 +286,15 @@ const cancelPreviousPipelineEventsOptions: CheckboxOption[] = [
{ value: WebhookEvents.Deploy, text: i18n.t('repo.pipeline.event.deploy') },
];

const exposeAccessTokenEventsOptions: CheckboxOption[] = [
{ value: WebhookEvents.Push, text: i18n.t('repo.pipeline.event.push') },
{ value: WebhookEvents.Tag, text: i18n.t('repo.pipeline.event.tag') },
{ value: WebhookEvents.Release, text: i18n.t('repo.pipeline.event.release') },
{ value: WebhookEvents.PullRequest, text: i18n.t('repo.pipeline.event.pr') },
{ value: WebhookEvents.Deploy, text: i18n.t('repo.pipeline.event.deploy') },
{ value: WebhookEvents.Cron, text: i18n.t('repo.pipeline.event.cron') },
{ value: WebhookEvents.Manual, text: i18n.t('repo.pipeline.event.manual') },
];

useWPTitle(computed(() => [i18n.t('repo.settings.general.project'), repo.value.full_name]));
</script>