Skip to content

Commit

Permalink
working transformer parking sfhera, without pushing to bdp
Browse files Browse the repository at this point in the history
  • Loading branch information
clezag committed Jul 3, 2024
1 parent d445343 commit 048d076
Show file tree
Hide file tree
Showing 8 changed files with 604 additions and 49 deletions.
22 changes: 16 additions & 6 deletions transformers/parking-offstreet-sfhera/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
#
# SPDX-License-Identifier: CC0-1.0

LOG_LEVEL="INFO"
MQ_URI="amqp://localhost:5672"
MQ_QUEUE="municipality-bolzano.parking-macello"
MQ_EXCHANGE="routed"
MQ_KEY="municipality-bolzano.parking-macello"
MQ_CONSUMER="tr-parking-os-sfhera-dev"
LOG_LEVEL="DEBUG"
MQ_LISTEN_URI="amqp://localhost:5672"
MQ_LISTEN_QUEUE="municipality-bolzano.parking-macello"
MQ_LISTEN_EXCHANGE="routed"
MQ_LISTEN_KEY="municipality-bolzano.parking-macello"
MQ_LISTEN_CONSUMER="tr-parking-os-sfhera-dev"
MONGO_URI="mongodb://user:password@localhost:27017/?directConnection=true"

BDP_BASE_URL=https://share.opendatahub.testingmachine.eu
BDP_PROVENANCE_VERSION=0.1.0
BDP_PROVENANCE_NAME=odh-a22-traffic-quality
BDP_ORIGIN=a22

ODH_TOKEN_URL=https://auth.opendatahub.testingmachine.eu/auth/realms/noi/protocol/openid-connect/token
ODH_CLIENT_ID=odh-a22-dataprocessor
ODH_CLIENT_SECRET=
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ env:
LOG_LEVEL: info
LOG_STYLE: json

MQ_LISTEN_KEY: echarging.route220
MQ_LISTEN_QUEUE: echarging.route220
MQ_LISTEN_ACKTIMEOUT: 300000
MQ_LISTEN_LISTEN_KEY: echarging.route220
MQ_LISTEN_LISTEN_QUEUE: echarging.route220
MQ_LISTEN_LISTEN_ACKTIMEOUT: 300000

envSecretRef:
- name: MQ_LISTEN_URI
- name: MQ_LISTEN_LISTEN_URI
secret: rabbitmq-svcbind
key: uri
- name: MONGO_CONNECTIONSTRING
Expand Down
22 changes: 18 additions & 4 deletions transformers/parking-offstreet-sfhera/localdev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@
# Setup port forwards to services in cluster
# They keep running in the background as long as the shell is alive
# use 'killall kubectl' to terminate them
kubectl port-forward -n core svc/bdp-core 8080 --address 0.0.0.0 &
kubectl port-forward -n core svc/rabbitmq-headless 5672 --address 0.0.0.0 &
kubectl port-forward -n core svc/rabbitmq-headless 15672 &
kubectl port-forward -n core svc/mongodb-headless 27017 --address 0.0.0.0 &
# kubectl port-forward -n core svc/bdp-core 8080 --address 0.0.0.0 &
# kubectl port-forward -n core svc/rabbitmq-headless 5672 --address 0.0.0.0 &
# kubectl port-forward -n core svc/rabbitmq-headless 15672 &
# kubectl port-forward -n core svc/mongodb-headless 27017 --address 0.0.0.0 &

# Extract connection strings from secrets
RABBIT_URI=`kubectl get secret -n core rabbitmq-svcbind -o jsonpath='{.data.uri}' | base64 -d`
MONGO_URI=`kubectl get secret -n core mongodb-collector-svcbind -o jsonpath='{.data.uri}' | base64 -d`
# The +srv type connection string requires a TXT DNS record
# Since we don't have access to the cluster DNS here, use a regular direct connection string
MONGO_URI="${MONGO_URI/mongodb+srv/mongodb}"

# Write connection string to .env file
echo >> .env
sed -i '/MQ_LISTEN_URI=/d' .env
echo "MQ_LISTEN_URI=$RABBIT_URI" >> .env
sed -i '/MONGO_URI=/d' .env
echo "MONGO_URI=$MONGO_URI" >> .env
104 changes: 104 additions & 0 deletions transformers/parking-offstreet-sfhera/src/bdplib/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-FileCopyrightText: NOI Techpark <[email protected]>

// SPDX-License-Identifier: AGPL-3.0-or-later

package bdplib

import (
"encoding/json"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)

type Token struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
NotBeforePolicy int64 `json:"not-before-policy"`
RefreshExpiresIn int64 `json:"refresh_expires_in"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
Scope string
}

type Auth struct {
TokenUrl string
ClientId string
ClientSecret string
token Token
tokenExpiry int64
}

func AuthFromEnv() *Auth {
a := Auth{}
a.TokenUrl = os.Getenv("ODH_TOKEN_URL")
a.ClientId = os.Getenv("ODH_CLIENT_ID")
a.ClientSecret = os.Getenv("ODH_CLIENT_SECRET")
return &a
}

func (a *Auth) getToken() string {
ts := time.Now().Unix()

if len(a.token.AccessToken) == 0 || ts > a.tokenExpiry {
// if no token is available or refreshToken is expired, get new token
a.newToken()
}

return a.token.AccessToken
}

func (a *Auth) newToken() {
slog.Info("Getting new token...")
params := url.Values{}
params.Add("client_id", a.ClientId)
params.Add("client_secret", a.ClientSecret)
params.Add("grant_type", "client_credentials")

a.authRequest(params)

slog.Info("Getting new token done.")
}

func (a *Auth) authRequest(params url.Values) {
body := strings.NewReader(params.Encode())

req, err := http.NewRequest("POST", a.TokenUrl, body)
if err != nil {
slog.Error("error", err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
if err != nil {
slog.Error("error", err)
return
}
defer resp.Body.Close()

slog.Info("Auth response code is: " + strconv.Itoa(resp.StatusCode))
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
slog.Error("error", err)
return
}

err = json.Unmarshal(bodyBytes, &a.token)
if err != nil {
slog.Error("error", err)
return
}
}

// calculate token expiry timestamp with 600 seconds margin
a.tokenExpiry = time.Now().Unix() + a.token.ExpiresIn - 600

slog.Debug("auth token expires in " + strconv.FormatInt(a.tokenExpiry, 10))
}
Loading

0 comments on commit 048d076

Please sign in to comment.