-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ba4833
commit a2a6472
Showing
22 changed files
with
380 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package config | ||
|
||
type Config struct { | ||
ProjectID string | ||
DripEnv string | ||
SlackRegistryChannelWebhook string | ||
JWTSecret string | ||
ProjectID string | ||
DripEnv string | ||
SlackRegistryChannelWebhook string | ||
DiscordSecurityChannelWebhook string | ||
JWTSecret string | ||
SecretScannerURL string | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,71 @@ | ||
package discord | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"registry-backend/config" | ||
) | ||
|
||
type DiscordService interface { | ||
SendSecurityCouncilMessage(msg string) error | ||
} | ||
|
||
type DripDiscordService struct { | ||
securityDiscordChannelWebhook string | ||
config *config.Config | ||
} | ||
|
||
func NewDiscordService(config *config.Config) *DripDiscordService { | ||
return &DripDiscordService{ | ||
config: config, | ||
securityDiscordChannelWebhook: config.DiscordSecurityChannelWebhook, | ||
} | ||
} | ||
|
||
type discordRequestBody struct { | ||
Content string `json:"content"` | ||
} | ||
|
||
func (s *DripDiscordService) SendSecurityCouncilMessage(msg string) error { | ||
if s.config.DripEnv == "prod" { | ||
return sendDiscordNotification(msg, s.securityDiscordChannelWebhook) | ||
} else { | ||
println("Skipping sending message to Discord in non-prod environment. " + msg) | ||
} | ||
return nil | ||
} | ||
|
||
func sendDiscordNotification(msg string, discordWebhookURL string) error { | ||
if discordWebhookURL == "" { | ||
return fmt.Errorf("no Discord webhook URL provided, skipping sending message to Discord") | ||
} | ||
|
||
body, err := json.Marshal(discordRequestBody{Content: msg}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodPost, discordWebhookURL, bytes.NewBuffer(body)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode >= 400 { | ||
// You can handle or log the HTTP error status code here | ||
return fmt.Errorf("request to Discord returned error status: %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} |
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,14 @@ | ||
package gateways | ||
|
||
import ( | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockDiscordService struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockDiscordService) SendSecurityCouncilMessage(msg string) error { | ||
args := m.Called(msg) | ||
return args.Error(0) | ||
} |
Oops, something went wrong.