Skip to content

Commit

Permalink
chore: change webhook password field to token
Browse files Browse the repository at this point in the history
This change also adds a HasToken field to the API since the API doesn't
return the hashed token.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Nov 18, 2024
1 parent 60d0440 commit 2f4340e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions apiclient/types/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Webhook struct {
WebhookManifest
AliasAssigned bool `json:"aliasAssigned,omitempty"`
LastSuccessfulRunCompleted *Time `json:"lastSuccessfulRunCompleted,omitempty"`
HasToken bool `json:"hasToken,omitempty"`
}

type WebhookManifest struct {
Expand Down
15 changes: 8 additions & 7 deletions pkg/api/handlers/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewWebhookHandler() *WebhookHandler {

type webhookRequest struct {
types.WebhookManifest `json:",inline"`
Password string `json:"password"`
Token string `json:"token"`
}

func (a *WebhookHandler) Update(req api.Context) error {
Expand All @@ -56,13 +56,13 @@ func (a *WebhookHandler) Update(req api.Context) error {
return err
}

if webhookReq.Password != "" {
hash, err := bcrypt.GenerateFromPassword([]byte(webhookReq.Password), bcrypt.DefaultCost)
if webhookReq.Token != "" {
hash, err := bcrypt.GenerateFromPassword([]byte(webhookReq.Token), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
}
wh.Spec.TokenHash = hash
webhookReq.Password = ""
webhookReq.Token = ""
}

wh.Spec.WebhookManifest = webhookReq.WebhookManifest
Expand Down Expand Up @@ -110,12 +110,12 @@ func (a *WebhookHandler) Create(req api.Context) error {
},
}

if webhookReq.Password != "" {
hash, err := bcrypt.GenerateFromPassword([]byte(webhookReq.Password), bcrypt.DefaultCost)
if webhookReq.Token != "" {
hash, err := bcrypt.GenerateFromPassword([]byte(webhookReq.Token), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
}
webhookReq.Password = ""
webhookReq.Token = ""
wh.Spec.TokenHash = hash
}

Expand Down Expand Up @@ -143,6 +143,7 @@ func convertWebhook(webhook v1.Webhook, urlPrefix string) *types.Webhook {
WebhookManifest: manifest,
AliasAssigned: webhook.Status.AliasAssigned,
LastSuccessfulRunCompleted: v1.NewTime(webhook.Status.LastSuccessfulRunCompleted),
HasToken: len(webhook.Spec.TokenHash) > 0,
}

wh.Secret = fmt.Sprintf("%x", sha256.Sum256([]byte(webhook.Spec.Secret)))
Expand Down

0 comments on commit 2f4340e

Please sign in to comment.