Skip to content

Commit

Permalink
Bugfix authkey processing for api
Browse files Browse the repository at this point in the history
  • Loading branch information
senseless committed Jul 18, 2024
1 parent 59a7b90 commit 1bc0faa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
4 changes: 3 additions & 1 deletion docs/memberOverride.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash
# This requires JQ and Curl installed on the local server

# Configuration
AUTH_KEY="your_auth_key_here"
DETAILS="Stakeplus"
# Membername must match member name (stored in key) here: https://github.com/ibp-network/config/blob/main/members_professional.json
DETAILS="Membername"
SERVERS=("http://dns-01.dotters.network:8080/api" "http://dns-02.dotters.network:8080/api" "http://dns-03.dotters.network:8080/api")

# Function to send the request
Expand Down
20 changes: 9 additions & 11 deletions powerdns/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
return
}

//log.Printf("Sending response: %+v\n", res)
// log.Printf("Sending response: %+v\n", res)

w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(res)
Expand All @@ -41,11 +41,10 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
}

func enableMember(req ApiRequest) Response {
if authValue, ok := configData.AuthKey[req.Details]; !ok || req.AuthKey != authValue {
return Response{
Result: "Unauthorized access",
}
} else if authValue, ok := configData.AuthKey["root"]; !ok || req.AuthKey != authValue {
authValue := configData.AuthKey[req.Details]
rootAuthValue := configData.AuthKey["root"]

if (req.AuthKey != authValue) && (req.AuthKey != rootAuthValue) {
return Response{
Result: "Unauthorized access",
}
Expand All @@ -72,11 +71,10 @@ func enableMember(req ApiRequest) Response {
}

func disableMember(req ApiRequest) Response {
if authValue, ok := configData.AuthKey[req.Details]; !ok || req.AuthKey != authValue {
return Response{
Result: "Unauthorized access",
}
} else if authValue, ok := configData.AuthKey["root"]; !ok || req.AuthKey != authValue {
authValue := configData.AuthKey[req.Details]
rootAuthValue := configData.AuthKey["root"]

if (req.AuthKey != authValue) && (req.AuthKey != rootAuthValue) {
return Response{
Result: "Unauthorized access",
}
Expand Down
30 changes: 21 additions & 9 deletions powerdns/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ func updateSiteStatus(status config.SiteResults) {
if !member.Results[checkName].OfflineTS.IsZero() && time.Since(member.Results[checkName].OfflineTS).Seconds() >= float64(configData.MinimumOfflineTime) {
member.Results[checkName] = Result{Success: true}
previousStatus["site"][memberName][checkName] = result.Success
sendMatrixMessage(fmt.Sprintf("<b>Adding member</b> <i>%s</i> <b>to all rotations</b><br><i><b>Server:</b> %s</i><br><i><b>Check %s:</b> false -> true</i><BR><b>Result Data:</b> %v", memberName, configData.ServerName, checkName, result.CheckData))
logStatusChange("Site Status Change", memberName, checkName, false, true, result.CheckData)

if !member.Override {
sendMatrixMessage(fmt.Sprintf("<b>Adding member</b> <i>%s</i> <b>to all rotations</b><br><i><b>Server:</b> %s</i><br><i><b>Check %s:</b> false -> true</i><BR><b>Result Data:</b> %v", memberName, configData.ServerName, checkName, result.CheckData))
logStatusChange("Site Status Change", memberName, checkName, false, true, result.CheckData)
}
}
} else {
member.Results[checkName] = Result{
Expand All @@ -82,8 +85,10 @@ func updateSiteStatus(status config.SiteResults) {
}

previousStatus["site"][memberName][checkName] = result.Success
sendMatrixMessage(fmt.Sprintf("<b>Removing member</b> <i>%s</i> <b>from all rotations</b><br><i><b>Server:</b> %s</i><br><i><b>Check %s:</b> true -> false</i><BR><b>Result Data:</b> %v", memberName, configData.ServerName, checkName, result.CheckData))
logStatusChange("Site Status Change", memberName, checkName, true, false, result.CheckData)
if !member.Override {
sendMatrixMessage(fmt.Sprintf("<b>Removing member</b> <i>%s</i> <b>from all rotations</b><br><i><b>Server:</b> %s</i><br><i><b>Check %s:</b> true -> false</i><BR><b>Result Data:</b> %v", memberName, configData.ServerName, checkName, result.CheckData))
logStatusChange("Site Status Change", memberName, checkName, true, false, result.CheckData)
}
}

for i := range powerDNSConfigs {
Expand Down Expand Up @@ -144,18 +149,25 @@ func updateEndpointStatus(status config.EndpointResults) {
if !member.Results[checkName].OfflineTS.IsZero() && time.Since(member.Results[checkName].OfflineTS).Seconds() >= float64(configData.MinimumOfflineTime) {
member.Results[checkName] = Result{Success: true}
previousStatus[endpointURL][memberName][checkName] = result.Success
sendMatrixMessage(fmt.Sprintf("<b>Adding member</b> <i>%s</i> <b>to endpoint</b> <i>%s</i><br><i><b>Server:</b> %s</i><br><i><b>Check %s:</b> false -> true</i><BR><b>Result Data:</b> %v", memberName, endpointURL, configData.ServerName, checkName, result.CheckData))
logStatusChange("EndPoint Status Change", memberName, checkName, false, true, result.CheckData)

if !member.Override {
sendMatrixMessage(fmt.Sprintf("<b>Adding member</b> <i>%s</i> <b>to endpoint</b> <i>%s</i><br><i><b>Server:</b> %s</i><br><i><b>Check %s:</b> false -> true</i><BR><b>Result Data:</b> %v", memberName, endpointURL, configData.ServerName, checkName, result.CheckData))
logStatusChange("EndPoint Status Change", memberName, checkName, false, true, result.CheckData)
}
}

} else {
member.Results[checkName] = Result{
Success: false,
OfflineTS: time.Now(),
}

previousStatus[endpointURL][memberName][checkName] = result.Success
sendMatrixMessage(fmt.Sprintf("<b>Removing member</b> <i>%s</i> <b>from endpoint</b> <i>%s</i><br><i><b>Server:</b> %s</i><br><i><b>Check %s:</b> true -> false</i><BR><b>Result Data:</b> %v", memberName, endpointURL, configData.ServerName, checkName, result.CheckData))
logStatusChange("EndPoint Status Change", memberName, checkName, true, false, result.CheckData)

if !member.Override {
sendMatrixMessage(fmt.Sprintf("<b>Removing member</b> <i>%s</i> <b>from endpoint</b> <i>%s</i><br><i><b>Server:</b> %s</i><br><i><b>Check %s:</b> true -> false</i><BR><b>Result Data:</b> %v", memberName, endpointURL, configData.ServerName, checkName, result.CheckData))
logStatusChange("EndPoint Status Change", memberName, checkName, true, false, result.CheckData)
}
}

for j := range powerDNSConfigs {
Expand Down Expand Up @@ -264,7 +276,7 @@ func statusOutput(w http.ResponseWriter, r *http.Request) {
for _, memberName := range memberNames {
member := config.Members[memberName]
sb.WriteString("<tr>")
sb.WriteString(fmt.Sprintf("<td>%s</td><td> %t </td><td>%s</td><td>%s</td><td>%.2f</td><td>%.2f</td><td><ul>", memberName, member.Override, member.IPv4, member.IPv6, member.Latitude, member.Longitude))
sb.WriteString(fmt.Sprintf("<td>%s</td><td>%t</td><td>%s</td><td>%s</td><td>%.2f</td><td>%.2f</td><td><ul>", memberName, member.Override, member.IPv4, member.IPv6, member.Latitude, member.Longitude))
for checkName, result := range member.Results {
sb.WriteString(fmt.Sprintf("<li>%s: %v", checkName, result.Success))
if !result.OfflineTS.IsZero() {
Expand Down

0 comments on commit 1bc0faa

Please sign in to comment.