1
mirror of https://git.burble.com/burble.dn42/dn42regsrv.git synced 2024-02-26 20:28:04 +01:00

Add API endpoint to force a registry refresh.

This commit is contained in:
Simon Marsh 2020-12-12 15:00:23 +00:00
parent f36e0ce71c
commit bd750fccb3
No known key found for this signature in database
GPG Key ID: 30B29A716A54DBB3
4 changed files with 62 additions and 5 deletions

14
API.md
View File

@ -296,6 +296,20 @@ wget -O - -q http://localhost:8042/api/dns/.meta | jq
} }
``` ```
It is also possible to force a refresh of the registry data.
To limit abuse, the refresh endpoint is protected by a secret token that must be passed using the `Authorization` header.
```
POST /api/registry/.meta/refresh
```
Example :
```
wget -O - -q --header='Authorization: secret' --post-data='' http://localhost:8042/api/registry/.meta/refresh
```
The token is set using the `--AuthToken` command line parameter.
## Route Origin Authorisation (ROA) API ## Route Origin Authorisation (ROA) API
Route Origin Authorisation (ROA) data can be obtained from the server in Route Origin Authorisation (ROA) data can be obtained from the server in

View File

@ -124,6 +124,7 @@ func main() {
gitPath = flag.StringP("GitPath", "g", "/usr/bin/git", "Path to git executable") gitPath = flag.StringP("GitPath", "g", "/usr/bin/git", "Path to git executable")
autoPull = flag.BoolP("AutoPull", "a", true, "Automatically pull the registry") autoPull = flag.BoolP("AutoPull", "a", true, "Automatically pull the registry")
branch = flag.StringP("Branch", "p", "master", "git branch to pull") branch = flag.StringP("Branch", "p", "master", "git branch to pull")
authToken = flag.StringP("AuthToken", "t", "secret", "Auth token for refresh endpoint")
) )
flag.Parse() flag.Parse()
@ -140,7 +141,7 @@ func main() {
} }
InitialiseRegistryData(*regDir, interval, InitialiseRegistryData(*regDir, interval,
*gitPath, *autoPull, *branch) *gitPath, *autoPull, *branch, *authToken)
// initialise router // initialise router
router := mux.NewRouter() router := mux.NewRouter()

View File

@ -51,6 +51,10 @@ func InitRegistryAPI(params ...interface{}) {
s.HandleFunc("/{type}/{object}/{key}", regKeyHandler) s.HandleFunc("/{type}/{object}/{key}", regKeyHandler)
s.HandleFunc("/{type}/{object}/{key}/{attribute}", regAttributeHandler) s.HandleFunc("/{type}/{object}/{key}/{attribute}", regAttributeHandler)
// add PUSH method to refresh registry
router.HandleFunc("/registry/.meta/refresh", regRefreshHandler).
Methods("POST")
log.Info("Registry API installed") log.Info("Registry API installed")
} }
@ -68,6 +72,28 @@ func regMetaHandler(w http.ResponseWriter, r *http.Request) {
ResponseJSON(w, rv) ResponseJSON(w, rv)
} }
//////////////////////////////////////////////////////////////////////////
// force a registry refresh
func regRefreshHandler(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == AuthorisationToken {
// trigger a refresh
RegistryRefresh <- true
// all good
response := struct{ status string }{"ok"}
ResponseJSON(w, response)
} else {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("403 - Forbidden"))
}
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// filter functions // filter functions

View File

@ -71,9 +71,13 @@ type Registry struct {
// and a variable for the actual data // and a variable for the actual data
var RegistryData *Registry var RegistryData *Registry
// store the current commit has // store the previous commit
var previousCommit string var previousCommit string
// also store the registry refresh ticker
var AuthorisationToken string
var RegistryRefresh chan bool
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// utility and manipulation functions // utility and manipulation functions
@ -636,7 +640,9 @@ func refreshRegistry(regDir string, gitPath string, branch string) {
// called from main to initialse the registry data and syncing // called from main to initialse the registry data and syncing
func InitialiseRegistryData(regDir string, refresh time.Duration, func InitialiseRegistryData(regDir string, refresh time.Duration,
gitPath string, autoPull bool, branch string) { gitPath string, autoPull bool, branch string, token string) {
AuthorisationToken = token
// validate that the regDir/data path exists // validate that the regDir/data path exists
dataPath := regDir + "/data" dataPath := regDir + "/data"
@ -680,10 +686,20 @@ func InitialiseRegistryData(regDir string, refresh time.Duration,
previousCommit = getCommitHash(regDir, gitPath) previousCommit = getCommitHash(regDir, gitPath)
reloadRegistry(dataPath, previousCommit) reloadRegistry(dataPath, previousCommit)
// create the refresh timer
RegistryRefresh = make(chan bool)
ticker := time.NewTicker(refresh)
go func() { go func() {
// every refresh interval for {
for range time.Tick(refresh) {
select {
// when the ticker ticks
case <-RegistryRefresh:
case <-ticker.C:
}
log.Debug("Refresh Timer") log.Debug("Refresh Timer")
// automatically try to refresh the registry ? // automatically try to refresh the registry ?