9
9
import re
10
10
import requests
11
11
from threading import Thread
12
- import redis
13
12
14
13
# Load configuration from environment variables
15
14
PROVISIONING_SERVICE_URL = os .environ .get ("PROVISIONING_SERVICE_URL" )
20
19
MQTT_BROKER_PASSWORD = os .environ .get ("MQTT_BROKER_PASSWORD" )
21
20
MQTT_REAUTH_TOPIC = os .environ .get ("MQTT_REAUTH_TOPIC" , "request-auth" )
22
21
AUTH_SERVICE_URL = os .environ .get ("AUTH_SERVICE_URL" )
23
- VAULT_ADDRESS = os .environ .get ("VAULT_ADDRESS" , "http://vault:8200" )
24
- REDIS_HOST = os .environ .get ("REDIS_HOST" , "redis" )
25
- REDIS_PORT = int (os .environ .get ("REDIS_PORT" , 6379 ))
26
22
FRAMES_OUTPUT_DIRECTORY = "frames_captured"
27
23
28
- # Init redis connection
29
- redis_client = redis .Redis (host = REDIS_HOST , port = REDIS_PORT , db = 0 )
30
24
mac_address = ""
31
25
32
26
# Function to calculate the SHA-256 hash of a file
@@ -87,22 +81,6 @@ def get_challenge(mac_address):
87
81
print ("Error getting challenge:" , e )
88
82
return None
89
83
90
- # Function to retrieve the Vault token from Redis
91
- def get_vault_token ():
92
- """
93
- Retrieve the Vault token from Redis.
94
-
95
- Returns:
96
- str: Vault token.
97
- """
98
- try :
99
- token = redis_client .get ("vault_root_token" )
100
- if token :
101
- return token .decode ("utf-8" )
102
- else :
103
- raise Exception ("Vault token not found in Redis" )
104
- except Exception as e :
105
- raise Exception ("Error retrieving Vault token from Redis" )
106
84
107
85
# Function to authenticate using CHAP (Challenge-Handshake Authentication Protocol)
108
86
def authenticate_chap (mac_address , client_response ):
@@ -125,29 +103,6 @@ def authenticate_chap(mac_address, client_response):
125
103
print ("Error during authentication:" , e )
126
104
return False
127
105
128
- # Function to retrieve the node password from Vault
129
- def get_node_password (mac_address ):
130
- """
131
- Retrieve the node password from Vault.
132
-
133
- Args:
134
- mac_address (str): MAC address of the device.
135
-
136
- Returns:
137
- str: Node password.
138
- """
139
- try :
140
- response = requests .get (
141
- f"{ VAULT_ADDRESS } /v1/secret/data/users/{ mac_address } " ,
142
- headers = {"X-Vault-Token" : get_vault_token ()}
143
- )
144
- response_json = response .json ()
145
- node_password = response_json ["data" ]["password" ]
146
- return node_password
147
- except Exception as e :
148
- print ("Error retrieving node password from Vault:" , e )
149
- return None
150
-
151
106
# Callback function when the MQTT client connects to the broker
152
107
def on_connect (client , userdata , flags , rc ):
153
108
"""
@@ -252,18 +207,23 @@ def authenticate(mac_address):
252
207
print ("Hash value of this code:" , code_hash )
253
208
challenge = get_challenge (mac_address )
254
209
if challenge :
255
- node_password = get_node_password (mac_address )
256
- if node_password :
257
- client_response = hashlib .sha256 ((node_password + challenge + code_hash ).encode ()).hexdigest ()
258
- return authenticate_chap (mac_address , client_response )
210
+ password_response = requests .get (f"{ PROVISIONING_SERVICE_URL } /get-fog-password?mac_address={ mac_address } " )
211
+ if password_response .status_code == 200 :
212
+ password_data = password_response .json ()
213
+ node_password = password_data .get ("fog_password" )
214
+ if node_password :
215
+ client_response = hashlib .sha256 ((node_password + challenge + code_hash ).encode ()).hexdigest ()
216
+ return authenticate_chap (mac_address , client_response )
217
+ else :
218
+ print ("Error retrieving node password from provisioning service" )
259
219
else :
260
- print ("Error retrieving node password from Vault " )
220
+ print ("Error getting node password from provisioning service " )
261
221
else :
262
222
print ("Error getting challenge" )
263
223
except Exception as e :
264
224
print ("Error during reauthentication:" , e )
265
225
return False
266
-
226
+
267
227
# Initialize the MQTT client and set up callbacks
268
228
client = mqtt .Client ()
269
229
client .username_pw_set (username = MQTT_BROKER_USERNAME , password = MQTT_BROKER_PASSWORD )
0 commit comments