Skip to content

Commit 0b395b6

Browse files
20250220-01 NGINX One bugfixes (#83)
1 parent 8acd9d5 commit 0b395b6

File tree

1 file changed

+79
-71
lines changed

1 file changed

+79
-71
lines changed

src/v5_2/NGINXOneOutput.py

+79-71
Original file line numberDiff line numberDiff line change
@@ -235,89 +235,97 @@ def NGINXOneOutput(d, declaration: ConfigDeclaration, apiversion: str, b64HttpCo
235235
#### / NGINX App Protect policies support
236236

237237
### Publish staged config to config sync group
238+
returnHttpCode = 422
239+
238240
r = requests.put(url=f'{nOneUrl}/api/nginx/one/namespaces/{nOneNamespace}/config-sync-groups/{igUid}/config',
239241
data=json.dumps(stagedConfig),
240242
headers={'Content-Type': 'application/json', "Authorization": f"Bearer APIToken {nOneToken}"},
241243
verify=False)
242244

243-
if r.status_code != 202:
244-
# Configuration push failed
245+
if r.status_code not in [200, 202]:
246+
# Configuration publish failed
245247
return {"status_code": r.status_code,
246248
"message": {"status_code": r.status_code, "message": r.text},
247249
"headers": {'Content-Type': 'application/json'}}
248250

249-
# Fetch the deployment status
250-
publishResponse = json.loads(r.text)
251-
publication_id = publishResponse['object_id']
251+
if r.status_code == 202:
252+
# Configuration has been submitted to NGINX One Console, fetch the deployment status - reply was HTTP/202
253+
publishResponse = json.loads(r.text)
254+
publication_id = publishResponse['object_id']
255+
256+
# Wait for either NGINX One Cloud Console success or failure after pushing a staged config
257+
isPending = True
258+
while isPending:
259+
time.sleep(NcgConfig.config['nms']['staged_config_publish_waittime'])
260+
deploymentCheck = requests.get(url=f'{nOneUrl}/api/nginx/one/namespaces/{nOneNamespace}/config-sync-groups/{igUid}/publications/{publication_id}',
261+
headers={"Authorization": f"Bearer APIToken {nOneToken}"},
262+
verify=False)
252263

253-
# Wait for either NGINX One Cloud Console success or failure after pushing a staged config
254-
isPending = True
255-
while isPending:
256-
time.sleep(NcgConfig.config['nms']['staged_config_publish_waittime'])
257-
deploymentCheck = requests.get(url=f'{nOneUrl}/api/nginx/one/namespaces/{nOneNamespace}/config-sync-groups/{igUid}/publications/{publication_id}',
258-
headers={"Authorization": f"Bearer APIToken {nOneToken}"},
259-
verify=False)
264+
checkJson = json.loads(deploymentCheck.text)
260265

261-
checkJson = json.loads(deploymentCheck.text)
266+
if not checkJson['status'] == 'pending':
267+
isPending = False
262268

263-
if not checkJson['status'] == 'pending':
264-
isPending = False
269+
if checkJson['status'] == "failed":
270+
# Staged config publish to NGINX One failed
271+
jsonResponse = checkJson['status_reasons'][0]
272+
returnHttpCode = 422
273+
elif checkJson['status'] == "succeeded":
274+
jsonResponse = { "message": "Config successfully applied", "status": checkJson['status'] }
275+
returnHttpCode = 200
265276

266-
if checkJson['status'] == "failed":
267-
# Staged config publish to NGINX One failed
268-
jsonResponse = checkJson['status_reasons'][0]
269-
deploymentCheck.status_code = 422
270277
else:
271-
# Staged config publish to NGINX One succeeded
272-
jsonResponse = json.loads(deploymentCheck.text)
273-
274-
# if nmsSynctime > 0 and runfromautosync == False:
275-
if runfromautosync == False:
276-
# No configuration is found, generate one
277-
configUid = str(v5_2.MiscUtils.getuniqueid())
278-
279-
# Stores the staged config to redis
280-
# Redis keys:
281-
# ncg.declaration.[configUid] = original config declaration
282-
# ncg.declarationrendered.[configUid] = original config declaration - rendered
283-
# ncg.basestagedconfig.[configUid] = base staged configuration
284-
# ncg.apiversion.[configUid] = ncg API version
285-
# ncg.status.[configUid] = latest status
286-
287-
NcgRedis.redis.set(f'ncg.declaration.{configUid}', pickle.dumps(declaration))
288-
NcgRedis.redis.set(f'ncg.declarationrendered.{configUid}', json.dumps(d))
289-
NcgRedis.redis.set(f'ncg.basestagedconfig.{configUid}', json.dumps(baseStagedConfig))
290-
NcgRedis.redis.set(f'ncg.apiversion.{configUid}', apiversion)
291-
292-
# TODO: NGINX App Protect not supported with NGINX One
293-
## Makes NGINX App Protect policies active
294-
#doWeHavePolicies = v5_2.NAPUtils.makePolicyActive(nmsUrl=nmsUrl, nmsUsername=nmsUsername,
295-
# nmsPassword=nmsPassword,
296-
# activePolicyUids=activePolicyUids,
297-
# instanceGroupUid=igUid)
298-
#
299-
#if doWeHavePolicies:
300-
# # Clean up NGINX App Protect WAF policies not used anymore
301-
# # and not defined in the declaration just pushed
302-
# time.sleep(NcgConfig.config['nms']['staged_config_publish_waittime'])
303-
# v5_2.NAPUtils.cleanPolicyLeftovers(nmsUrl=nmsUrl, nmsUsername=nmsUsername,
304-
# nmsPassword=nmsPassword,
305-
# currentPolicies=provisionedNapPolicies)
306-
307-
# If deploying a new configuration in GitOps mode start autosync
308-
if nOneSynctime == 0:
309-
NcgRedis.declarationsList[configUid] = "static"
310-
elif not runfromautosync:
311-
# GitOps autosync
312-
print(f'Starting autosync for configUid {configUid} every {nOneSynctime} seconds')
313-
314-
job = schedule.every(nOneSynctime).seconds.do(lambda: V5_2_CreateConfig.configautosync(configUid))
315-
# Keep track of GitOps configs, key is the threaded job
316-
NcgRedis.declarationsList[configUid] = job
317-
318-
NcgRedis.redis.set(f'ncg.apiversion.{configUid}', apiversion)
319-
320-
responseContent = {'code': deploymentCheck.status_code, 'content': jsonResponse, 'configUid': configUid}
278+
# Staged config publish to NGINX One succeeded - reply was HTTP/200
279+
jsonResponse = json.loads(r.text)
280+
returnHttpCode = 200
281+
282+
# if nmsSynctime > 0 and runfromautosync == False:
283+
if runfromautosync == False:
284+
# No configuration is found, generate one
285+
configUid = str(v5_2.MiscUtils.getuniqueid())
286+
287+
# Stores the staged config to redis
288+
# Redis keys:
289+
# ncg.declaration.[configUid] = original config declaration
290+
# ncg.declarationrendered.[configUid] = original config declaration - rendered
291+
# ncg.basestagedconfig.[configUid] = base staged configuration
292+
# ncg.apiversion.[configUid] = ncg API version
293+
# ncg.status.[configUid] = latest status
294+
295+
NcgRedis.redis.set(f'ncg.declaration.{configUid}', pickle.dumps(declaration))
296+
NcgRedis.redis.set(f'ncg.declarationrendered.{configUid}', json.dumps(d))
297+
NcgRedis.redis.set(f'ncg.basestagedconfig.{configUid}', json.dumps(baseStagedConfig))
298+
NcgRedis.redis.set(f'ncg.apiversion.{configUid}', apiversion)
299+
300+
# TODO: NGINX App Protect not supported with NGINX One
301+
## Makes NGINX App Protect policies active
302+
#doWeHavePolicies = v5_2.NAPUtils.makePolicyActive(nmsUrl=nmsUrl, nmsUsername=nmsUsername,
303+
# nmsPassword=nmsPassword,
304+
# activePolicyUids=activePolicyUids,
305+
# instanceGroupUid=igUid)
306+
#
307+
#if doWeHavePolicies:
308+
# # Clean up NGINX App Protect WAF policies not used anymore
309+
# # and not defined in the declaration just pushed
310+
# time.sleep(NcgConfig.config['nms']['staged_config_publish_waittime'])
311+
# v5_2.NAPUtils.cleanPolicyLeftovers(nmsUrl=nmsUrl, nmsUsername=nmsUsername,
312+
# nmsPassword=nmsPassword,
313+
# currentPolicies=provisionedNapPolicies)
314+
315+
# If deploying a new configuration in GitOps mode start autosync
316+
if nOneSynctime == 0:
317+
NcgRedis.declarationsList[configUid] = "static"
318+
elif not runfromautosync:
319+
# GitOps autosync
320+
print(f'Starting autosync for configUid {configUid} every {nOneSynctime} seconds')
321+
322+
job = schedule.every(nOneSynctime).seconds.do(lambda: V5_2_CreateConfig.configautosync(configUid))
323+
# Keep track of GitOps configs, key is the threaded job
324+
NcgRedis.declarationsList[configUid] = job
325+
326+
NcgRedis.redis.set(f'ncg.apiversion.{configUid}', apiversion)
327+
328+
responseContent = {' code': returnHttpCode, 'content': jsonResponse, 'configUid': configUid}
321329

322330
# Configuration push completed, update redis keys
323331
if configUid != "":
@@ -329,8 +337,8 @@ def NGINXOneOutput(d, declaration: ConfigDeclaration, apiversion: str, b64HttpCo
329337
NcgRedis.redis.set('ncg.declarationrendered.' + configUid, json.dumps(d))
330338
NcgRedis.redis.set('ncg.basestagedconfig.' + configUid, json.dumps(baseStagedConfig))
331339

332-
return {"status_code": deploymentCheck.status_code,
333-
"message": {"status_code": deploymentCheck.status_code,
340+
return {"status_code": returnHttpCode,
341+
"message": {"status_code": returnHttpCode,
334342
"message": responseContent},
335343
"headers": {'Content-Type': 'application/json'}
336344
}

0 commit comments

Comments
 (0)