-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to configure notications credentials
- Loading branch information
Showing
7 changed files
with
162 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package notifications | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"strings" | ||
) | ||
|
||
type Config struct { | ||
Url string `yaml:"url"` | ||
Username string `yaml:"username"` | ||
Password string `yaml:"password"` | ||
KubernetesWebUrl string `yaml:"kubernetes_web_url" default:"http://localhost/icingaweb2/kubernetes"` | ||
} | ||
|
||
// Validate implements the config.Validator interface. | ||
func (c *Config) Validate() error { | ||
if c.Username == "" && c.Password != "" { | ||
return errors.New("'username' must be set, if password is provided") | ||
} | ||
if c.Username != "" { | ||
// Since Icinga Notifications does not yet support basic HTTP authentication with a simple user and password, | ||
// we have to use a static “username” consisting of `source-` and the actual source ID for the time being. | ||
// See https://github.com/Icinga/icinga-notifications/issues/227 | ||
parts := strings.Split(c.Username, "-") | ||
if len(parts) != 2 || parts[0] != "source" { | ||
return errors.New("'username' must be of the form '<source>-<SourceID>'") | ||
} | ||
} | ||
if c.Url == "" && (c.Password != "" || c.Username != "") { | ||
return errors.New("Icinga Notifications base 'url' must be provided, if username and password are set") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IsNotificationsEnabled we are allowed to send events the Icinga Notifications daemon. | ||
func IsNotificationsEnabled(c *Config) bool { | ||
return c.Url != "" && c.Username == "" | ||
} |
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,59 @@ | ||
package notifications | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/google/uuid" | ||
"github.com/icinga/icinga-kubernetes/pkg/database" | ||
schemav1 "github.com/icinga/icinga-kubernetes/pkg/schema/v1" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// SyncSourceConfig synchronises the Icinga Notifications credentials from the YAML config with the database. | ||
func SyncSourceConfig(ctx context.Context, db *database.Database, config *Config) error { | ||
var typPtr *schemav1.Config | ||
|
||
if IsNotificationsEnabled(config) { | ||
var dbConfig []*schemav1.Config | ||
if err := db.SelectContext(ctx, &dbConfig, db.BuildSelectStmt(typPtr, typPtr)); err != nil { | ||
return errors.Wrap(err, "cannot fetch Icinga Notifications credentials from DB") | ||
} | ||
|
||
configPairs := []*schemav1.Config{ | ||
{schemav1.ConfigKeyNotificationsUsername, "Icinga for Kubernetes"}, | ||
{schemav1.ConfigKeyNotificationsPassword, uuid.NewString()}, | ||
} | ||
|
||
for _, pair := range configPairs { | ||
for _, dbPair := range dbConfig { | ||
if dbPair.Key == pair.Key { | ||
// If we already have a username and password in the DB, leave them unchanged. | ||
pair.Value = dbPair.Value | ||
break | ||
} | ||
} | ||
} | ||
|
||
stmt, _ := db.BuildUpsertStmt(typPtr) | ||
if _, err := db.NamedExecContext(ctx, stmt, configPairs); err != nil { | ||
return errors.Wrap(err, "cannot upsert Icinga Notifications credentials") | ||
} | ||
} else { | ||
stmt := fmt.Sprintf( | ||
"DELETE FROM %s WHERE %[2]s = :password OR %[2]s = :username", | ||
db.QuoteIdentifier(database.TableName(typPtr)), | ||
db.QuoteIdentifier("key")) | ||
|
||
// We purposefully do not delete the schemav1.ConfigKeyNotificationsSourceID key as it is used by | ||
// Icinga Notifications Web to delete the actual notification source and afterwards it'll delete it as well. | ||
args := map[string]any{ | ||
"password": schemav1.ConfigKeyNotificationsPassword, | ||
"username": schemav1.ConfigKeyNotificationsUsername, | ||
} | ||
if _, err := db.NamedExecContext(ctx, stmt, args); err != nil { | ||
return errors.Wrap(err, "cannot delete Icinga Notifications credentials") | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package v1 | ||
|
||
// Config represents a single key => value pair database config entry. | ||
type Config struct { | ||
Key ConfigKey | ||
Value string | ||
} | ||
|
||
// ConfigKey represents the database config.Key enums. | ||
type ConfigKey string | ||
|
||
const ( | ||
ConfigKeyNotificationsSourceID ConfigKey = "notifications.source_id" | ||
ConfigKeyNotificationsUsername ConfigKey = "notifications.username" | ||
ConfigKeyNotificationsPassword ConfigKey = "notifications.password" | ||
) |
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