Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
SendWakeUpSignal: disable authentication and run 'sendwakeupsignal-wo…
Browse files Browse the repository at this point in the history
…rker' every day 9 am
  • Loading branch information
jendakol committed Jan 13, 2021
1 parent de6c165 commit ffc8644
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 51 deletions.
31 changes: 4 additions & 27 deletions internal/functions/wakeup/wake-up.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/covid19cz/erouska-backend/internal/logging"
"github.com/covid19cz/erouska-backend/internal/messaging"
"github.com/covid19cz/erouska-backend/internal/secrets"
"net/http"
"time"
)
Expand All @@ -16,40 +15,18 @@ const topicName = "budicek"
//SendWakeUpSignal Sends wake-up signal to devices
func SendWakeUpSignal(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := logging.FromContext(ctx).Named("wake-up.SendWakeUpSignal")

secretClient := secrets.Client{}
pushSender := messaging.Client{}

httpStatus, httpBody := sendWakeUpSignalAuthenticated(ctx, r, secretClient, pushSender)

http.Error(w, httpBody, httpStatus)
}

func sendWakeUpSignalAuthenticated(ctx context.Context, r *http.Request, secretClient secrets.Client, pushSender messaging.Client) (int, string) {
logger := logging.FromContext(ctx).Named("wake-up.sendWakeUpSignalAuthenticated")

// authentication

apikey, err := secretClient.Get("manual-wakeup-apikey")
if err != nil {
logger.Warnf("Could not obtain api key: %v", err)
return 500, "Could not obtain api key"
}

providedAPIKeys := r.URL.Query()["apikey"]
if len(providedAPIKeys) != 1 || providedAPIKeys[0] != string(apikey) {
return 401, "Bad api key"
}

// authenticated, go ahead

if err := sendWakeUpSignal(ctx, pushSender); err != nil {
msg := fmt.Sprintf("Could not send wake-up signal: %v", err)
logger.Error(msg)
return 500, msg
http.Error(w, msg, 500)
return
}

return 200, "ok"
http.Error(w, "ok", 200)
}

func sendWakeUpSignal(ctx context.Context, msgClient messaging.PushSender) error {
Expand Down
48 changes: 24 additions & 24 deletions terraform/modules/erouska/coviddata.tf
Original file line number Diff line number Diff line change
Expand Up @@ -376,27 +376,27 @@ resource "google_project_iam_member" "sendwakeupsignal-invoker" {
member = "serviceAccount:${google_service_account.sendwakeupsignal-invoker.email}"
}

//resource "google_cloud_scheduler_job" "sendwakeupsignal-worker" {
// name = "sendwakeupsignal-worker"
// region = var.cloudscheduler_location
// schedule = "0 */4 * * *"
// time_zone = "Europe/Prague"
// attempt_deadline = "600s"
//
// retry_config {
// retry_count = 1
// }
//
// http_target {
// http_method = "GET"
// uri = data.google_cloudfunctions_function.sendwakeupsignal.https_trigger_url
// oidc_token {
// audience = data.google_cloudfunctions_function.sendwakeupsignal.https_trigger_url
// service_account_email = google_service_account.sendwakeupsignal-invoker.email
// }
// }
//
// depends_on = [
// google_project_service.services["cloudscheduler.googleapis.com"],
// ]
//}
resource "google_cloud_scheduler_job" "sendwakeupsignal-worker" {
name = "sendwakeupsignal-worker"
region = var.cloudscheduler_location
schedule = "0 9 * * *"
time_zone = "Europe/Prague"
attempt_deadline = "600s"

retry_config {
retry_count = 1
}

http_target {
http_method = "GET"
uri = data.google_cloudfunctions_function.sendwakeupsignal.https_trigger_url
oidc_token {
audience = data.google_cloudfunctions_function.sendwakeupsignal.https_trigger_url
service_account_email = google_service_account.sendwakeupsignal-invoker.email
}
}

depends_on = [
google_project_service.services["cloudscheduler.googleapis.com"],
]
}

0 comments on commit ffc8644

Please sign in to comment.