-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load sources configuration from database
The source table was extended to hold additional data, auth_username and auth_password, which is now synchronized into a new Source type, being hold in the RuntimeConfig. Those two values are now being used in the Listener to enforce authenticated API requests.
- Loading branch information
Showing
7 changed files
with
159 additions
and
20 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,79 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"github.com/jmoiron/sqlx" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Source entry within the ConfigSet to describe a source. | ||
type Source struct { | ||
ID int64 `db:"id"` | ||
Type string `db:"type"` | ||
Name string `db:"name"` | ||
|
||
AuthUsername string `db:"auth_username"` | ||
AuthPassword string `db:"auth_password"` | ||
} | ||
|
||
func (r *RuntimeConfig) fetchSources(ctx context.Context, tx *sqlx.Tx) error { | ||
var sourcePtr *Source | ||
stmt := r.db.BuildSelectStmt(sourcePtr, sourcePtr) | ||
r.logger.Debugf("Executing query %q", stmt) | ||
|
||
var sources []*Source | ||
if err := tx.SelectContext(ctx, &sources, stmt); err != nil { | ||
r.logger.Errorln(err) | ||
return err | ||
} | ||
|
||
sourcesById := make(map[int64]*Source) | ||
for _, s := range sources { | ||
sourceLogger := r.logger.With( | ||
zap.Int64("id", s.ID), | ||
zap.String("name", s.Name), | ||
zap.String("type", s.Type), | ||
) | ||
if sourcesById[s.ID] != nil { | ||
sourceLogger.Warnw("ignoring duplicate config for source type") | ||
} else { | ||
sourcesById[s.ID] = s | ||
|
||
sourceLogger.Debugw("loaded source config") | ||
} | ||
} | ||
|
||
if r.Sources != nil { | ||
// mark no longer existing sources for deletion | ||
for id := range r.Sources { | ||
if _, ok := sourcesById[id]; !ok { | ||
sourcesById[id] = nil | ||
} | ||
} | ||
} | ||
|
||
r.pending.Sources = sourcesById | ||
|
||
return nil | ||
} | ||
|
||
func (r *RuntimeConfig) applyPendingSources() { | ||
if r.Sources == nil { | ||
r.Sources = make(map[int64]*Source) | ||
} | ||
|
||
for id, pendingSource := range r.pending.Sources { | ||
if pendingSource == nil { | ||
r.logger.Infow("Source has been removed", | ||
zap.Int64("id", r.Sources[id].ID), | ||
zap.String("name", r.Sources[id].Name), | ||
zap.String("type", r.Sources[id].Type)) | ||
|
||
delete(r.Sources, id) | ||
} else { | ||
r.Sources[id] = pendingSource | ||
} | ||
} | ||
|
||
r.pending.Sources = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ALTER TABLE source | ||
ADD COLUMN auth_username text, | ||
ADD COLUMN auth_password text; | ||
UPDATE source SET auth_username = 'icinga', auth_password = 'correct horse battery staple'; | ||
ALTER TABLE source | ||
ALTER COLUMN auth_username SET NOT NULL, | ||
ALTER COLUMN auth_password SET NOT NULL; |