1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import config as config
import requests
import json
# Configuration
PLAINTEXT_PW = "TODO"
INPUT_TEMPLATE = "template.json"
OUTPUT_FILE = "configured-flow.json"
# API Endpoints
BASE_URL = f"{config.NIFI_HOST}/nifi-api"
LOGIN_URL = f"{BASE_URL}/access/token"
ENCRYPT_URL = f"{BASE_URL}/flow/encrypt-text"
def get_access_token():
"""Authenticate with NiFi and get JWT token"""
try:
response = requests.post(
LOGIN_URL,
headers={"Content-Type": "application/x-www-form-urlencoded"},
data=f"username={config.NIFI_USER}&password={config.NIFI_PASS}",
verify=False # For self-signed certificates
)
response.raise_for_status()
return response.text
except requests.exceptions.RequestException as e:
print(f"Authentication failed: {str(e)}")
exit(1)
def encrypt_password(token, plaintext):
"""Encryptpassword using NiFi's API"""
try:
response = requests.post(
ENCRYPT_URL,
headers={
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": f"Bearer {token}"
},
data=f"value={plaintext}",
verify=False
)
response.raise_for_status()
return response.json()["value"]
except requests.exceptions.RequestException as e:
print(f"Encryption failed: {str(e)}")
exit(1)
def update_template(encrypted_pw):
"""Update JSON template with encrypted password"""
try:
with open(INPUT_TEMPLATE, "r") as f:
flow = json.load(f)
# Find and update InvokeHTTP processor
for processor in flow["flowContents"]["processors"]:
if processor["type"] == "org.apache.nifi.processors.standard.InvokeHTTP":
# Update password property
processor["properties"]["Request password"] = encrypted_pw
# Mark property as sensitive
processor["propertyDescriptors"]["Request password"]["sensitive"] = True
with open(OUTPUT_FILE, "w") as f:
json.dump(flow, f, indent=2)
print(f"Successfully generated: {OUTPUT_FILE}")
except Exception as e:
print(f"Template update failed: {str(e)}")
exit(1)
if __name__ == "__main__":
# 1. Authenticate
token = get_access_token()
print("token part DONE")
# 2. Encrypt config.NIFI_PASS
encrypt_password(token, PLAINTEXT_PW)
# 3. Update template
#update_template(encrypted_config.NIFI_PASS)
|