-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "Add webhook secret bootstrap and verification for Gitlab" (#…
…4506) * Reapply "Add webhook secret bootstrap and verification for Gitlab (#4492)" (#4504) This reverts commit a06e930. * Handle embedded structurs in our config parsing our config parsing sets the defaults and registers options in viper. It did not handle the case of embedded structures (or squashed tags). This adds that functionality. Signed-off-by: Juan Antonio Osorio <[email protected]> --------- Signed-off-by: Juan Antonio Osorio <[email protected]>
- Loading branch information
Showing
11 changed files
with
327 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright 2024 Stacklok, 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 webhooksecret provides a way to generate and verify secrets for GitLab webhooks. | ||
package webhooksecret | ||
|
||
import ( | ||
sum "crypto/sha512" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
// ErrEmptyBaseOrUniq is returned when the base or uniq strings are empty. | ||
ErrEmptyBaseOrUniq = errors.New("base or uniq strings are empty") | ||
) | ||
|
||
// New creates a new secret for usage in the gitlab webhook. | ||
// The secret is generated by combining the base and uniq strings | ||
// and then hashing the result. | ||
func New(base string, uniq string) (string, error) { | ||
if base == "" || uniq == "" { | ||
return "", ErrEmptyBaseOrUniq | ||
} | ||
|
||
hash := sum.New() | ||
_, err := hash.Write([]byte(base + uniq)) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to write secret: %w", err) | ||
} | ||
|
||
return hex.EncodeToString(hash.Sum(nil)), nil | ||
} | ||
|
||
// Verify checks if the given secret is valid for the given base and uniq strings. | ||
func Verify(base string, uniq string, secret string) bool { | ||
s, err := New(base, uniq) | ||
if err != nil { | ||
// If we can't generate the secret, we can't verify it. | ||
return false | ||
} | ||
|
||
return s == secret | ||
} |
Oops, something went wrong.