Skip to content

Commit

Permalink
#36 ANKLET_GLOBAL_RECEIVER_SECRET
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Oct 23, 2024
1 parent f32c834 commit 510f31a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1
0.8.2
2 changes: 2 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ services:
- ANKLET_GLOBAL_DATABASE_USER=
- ANKLET_GLOBAL_DATABASE_PASSWORD=
- ANKLET_GLOBAL_DATABASE_DATABASE=0
# ANKLET_GLOBAL_RECEIVER_SECRET allows you to set a global secret for all receivers
# - ANKLET_GLOBAL_RECEIVER_SECRET=12345
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Config struct {
GlobalDatabaseUser string `yaml:"global_database_user"`
GlobalDatabasePassword string `yaml:"global_database_password"`
GlobalDatabaseDatabase int `yaml:"global_database_database"`
GlobalReceiverSecret string `yaml:"global_receiver_secret"`
}

type Log struct {
Expand Down Expand Up @@ -174,6 +175,11 @@ func LoadInEnvs(config Config) (Config, error) {
config.GlobalDatabasePassword = envGlobalDatabasePassword
}

envGlobalReceiverSecret := os.Getenv("ANKLET_GLOBAL_RECEIVER_SECRET")
if envGlobalReceiverSecret != "" {
config.GlobalReceiverSecret = envGlobalReceiverSecret
}

// pidFileDir := os.Getenv("ANKLET_PID_FILE_DIR")
// if pidFileDir != "" {
// config.PidFileDir = pidFileDir
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ func main() {
loadedConfig.WorkDir = "./"
}

// handle global receiver secret
if loadedConfig.GlobalReceiverSecret != "" {
for index, plugin := range loadedConfig.Plugins {
if strings.Contains(plugin.Plugin, "_receiver") {
loadedConfig.Plugins[index].Secret = loadedConfig.GlobalReceiverSecret
}
}
}

logger.DebugContext(parentCtx, "loaded config", slog.Any("config", loadedConfig))
parentCtx = context.WithValue(parentCtx, config.ContextKey("config"), &loadedConfig)

Expand Down
3 changes: 2 additions & 1 deletion plugins/receivers/github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ The Github Receiver Plugin is used to receive webhook events from github and sto
In the `config.yml`, you can define the `github_receiver` plugin as follows:

```
global_receiver_secret: 12345 # this can be set using the ANKLET_GLOBAL_RECEIVER_SECRET env var too
plugins:
- name: GITHUB_RECEIVER
plugin: github_receiver
hook_id: 489747753
port: 54321
secret: 123412342
# secret: 12345
private_key: /Users/nathanpierce/veertuinc-anklet.2024-07-19.private-key.pem
app_id: 949431
installation_id: 52970581
Expand Down
7 changes: 5 additions & 2 deletions plugins/receivers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ func Run(

configFileName := config.GetConfigFileNameFromContext(pluginCtx)
if ctxPlugin.Token == "" && ctxPlugin.PrivateKey == "" {
logging.Panic(workerCtx, pluginCtx, "token or private_key are not set at global level or in "+configFileName+":plugins:"+ctxPlugin.Name+"<token/private_key>")
logging.Panic(workerCtx, pluginCtx, "token or private_key are not set at global level or in "+configFileName+":plugins:"+ctxPlugin.Name)
}
if ctxPlugin.Owner == "" {
logging.Panic(workerCtx, pluginCtx, "owner is not set in "+configFileName+":plugins:"+ctxPlugin.Name+"<owner>")
logging.Panic(workerCtx, pluginCtx, "owner is not set in "+configFileName+":plugins:"+ctxPlugin.Name)
}
if ctxPlugin.Secret == "" {
logging.Panic(workerCtx, pluginCtx, "secret is not set in "+configFileName+":plugins:"+ctxPlugin.Name)
}

databaseContainer, err := database.GetDatabaseFromContext(pluginCtx)
Expand Down

0 comments on commit 510f31a

Please sign in to comment.