-
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.
Sync Source and Authenticate Listener
The source table was extended to hold a hashed password, listener_password_hash, which is now synchronized into a new Source type, being hold in the RuntimeConfig. This value is now being used in the Listener to enforce authenticated API requests.
- Loading branch information
Showing
9 changed files
with
204 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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"` | ||
|
||
ListenerPasswordHash string `db:"listener_password_hash"` | ||
} | ||
|
||
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 ID") | ||
} 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,4 @@ | ||
ALTER TABLE source ADD COLUMN listener_password_hash text; | ||
-- php > $listener_password_hash = password_hash("correct horse battery staple", PASSWORD_DEFAULT)); | ||
UPDATE source SET listener_password_hash = '$2y$10$QU8bJ7cpW1SmoVQ/RndX5O2J5L1PJF7NZ2dlIW7Rv3zUEcbUFg3z2'; | ||
ALTER TABLE source ALTER COLUMN listener_password_hash SET NOT NULL; |